-
Notifications
You must be signed in to change notification settings - Fork 0
/
utilities.py
221 lines (168 loc) · 9.39 KB
/
utilities.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
from config import *
from db import *
import json
from pathlib import Path
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
import pymongo
import os
import random
from datetime import datetime
from time import sleep
client = pymongo.MongoClient(f"mongodb+srv://{mongo_user}:{mongo_password}@{mongo_url}")
db = client.walmart
try:
current_path = os.path.dirname(os.path.abspath(__file__))
except:
current_path = '.'
def init_driver(gecko_driver, load_images = True, user_agent = '', is_headless = False):
firefox_profile = webdriver.FirefoxProfile()
firefox_profile.set_preference('dom.ipc.plugins.enabled.libflashplayer.so', False)
firefox_profile.set_preference('media.volume_scale', "0.0")
firefox_profile.set_preference("dom.webnotifications.enabled", False)
if not load_images:
firefox_profile.set_preference('permissions.default.image', 2)
if user_agent != '':
firefox_profile.set_preference("general.useragent.override", user_agent)
options = Options()
options.headless = is_headless
driver = webdriver.Firefox(executable_path = f"{current_path}/{gecko_driver}",
firefox_profile = firefox_profile,
options = options)
return driver
def get_url(page_url, driver):
driver.get(page_url)
sleep(page_load)
close_popup = driver.find_elements_by_css_selector('.acsFocusFirst')
if len(close_popup) > 0:
close_popup[0].click()
def get_products(driver):
products = driver.find_elements_by_css_selector('div.shelf-thumbs .standard-thumb')
products_info = []
for product in products:
# Get product title
product_title = ''
if len(product.find_elements_by_css_selector('div.product-details-container .details .title .thumb-header')) > 0:
product_title = product.find_elements_by_css_selector('div.product-details-container .details .title .thumb-header')[0].text
# Get product url
product_url = ''
if len(product.find_elements_by_css_selector('a.product-link')) > 0:
product_url = product.find_elements_by_css_selector('a.product-link')[0].get_attribute('href')
# Get product current price
current_price = 0
if len(product.find_elements_by_css_selector('div.product-details-container .all-price-sections .price-current')) > 0:
current_price = product.find_elements_by_css_selector('div.product-details-container .all-price-sections .price-current')[0].text
current_price = current_price.replace('\n', '')
current_price = current_price.replace('$', '')
current_price = current_price.replace(',', '')
if "to" in current_price:
current_price = current_price.split(' ')
current_price = float(current_price[0])
current_price = float(current_price)
# Get product old price
old_price = 0
if len(product.find_elements_by_css_selector('div.product-details-container .all-price-sections .pricing-spacer .price-was')) > 0:
old_price = product.find_elements_by_css_selector('div.product-details-container .all-price-sections .pricing-spacer .price-was')[0].text
old_price = old_price.replace('$', '')
old_price = old_price.replace("Was ", "")
old_price = old_price.replace('\n', '')
old_price = old_price.replace(',', '')
if "to" in old_price:
old_price = old_price.split(' ')
old_price = float(old_price[0])
if not isinstance(old_price,float):
if len(old_price) > 0:
old_price = float(old_price)
else:
old_price = 0
discount_number = 0
discount_percent = 0
if current_price != 0 and old_price != 0 and old_price > current_price:
discount_number = round(old_price - current_price)
discount_percent = round(100 - (current_price/old_price) * 100)
if current_price !=0 and len(product_url) > 0 and len(product_title) > 0:
product_info = {
'product_title': product_title,
'product_url': product_url,
'current_price': current_price,
'old_price': old_price,
'discount_number': discount_number,
'discount_percent': discount_percent,
'inserted_at': datetime.now(),
'updated_at': datetime.now(),
'published_at': False
}
if db.products.count_documents({'$or': [ {'product_title': product_title}, {'product_url': product_url}]}) == 0:
_ = db.products.insert_one(product_info)
else:
pd = db.products.find_one({'$or': [ {'product_title': product_title}, {'product_url': product_url}]})
if pd['current_price'] != current_price or pd['old_price'] != old_price:
db.products.update_one({'_id': pd['_id']}, {'$set': {
'current_price': current_price,
'old_price': old_price,
'discount_number': discount_number,
'discount_percent': discount_percent,
'updated_at': datetime.now()
}} )
products_info.append(product_info)
return products_info
def load_cookies(driver):
driver.get(twitter_url)
cookies = ''
cookie_file = f"{current_path}/{twitter_cookies_path}"
if Path(cookie_file).is_file():
with open(cookie_file, 'r', encoding = 'utf8') as ck_file:
cookies = ck_file.read()
if cookies != '':
cookies = json.loads(cookies)
if len(cookies) > 0:
for cookie in cookies:
driver.add_cookie(cookie)
sleep(5)
driver.get(f"{twitter_url}/settings/account")
if len(driver.find_elements_by_name("session[username_or_email]")) > 0:
_ = open(cookie_file, 'w').truncate()
return False
else:
return True
return False
def twitter_login(driver):
driver.get(twitter_login_page)
if driver.find_elements_by_name("session[username_or_email]") and driver.find_elements_by_name("session[password]"):
email = driver.find_element_by_name("session[username_or_email]")
email.clear()
password = driver.find_element_by_name("session[password]")
password.clear()
email.send_keys(twitter_email)
password.send_keys(twitter_password)
sleep(3)
password.submit()
sleep(5)
cookies_list = driver.get_cookies()
ck_file = open(f"{current_path}/{twitter_cookies_path}", 'w', encoding = 'utf8')
ck_file.write(json.dumps(cookies_list))
ck_file.close()
return True
def publish_product(driver, product):
driver.get(twitter_url)
messages = [
f"Hurry and grab a {product['discount_percent']}% discount on a {product['product_title']}! {product['product_url']}",
f"Take ${product['discount_number']} off a {product['product_title']}! {product['product_url']}",
f"If you need a {product['product_title']} use this {product['discount_percent']}% discount! {product['product_url']}",
f"Buy a {product['product_title']} for ${product['current_price']} instead of ${product['old_price']}! {product['product_url']}",
f"Dont overthink it and take a {product['discount_percent']}% discount on a {product['product_title']}! {product['product_url']}",
f"Save ${product['discount_number']} on a {product['product_title']} before it's too late! {product['product_url']}",
f"You won't have buyer's remorse with this {product['discount_percent']}% discount on a {product['product_title']}! {product['product_url']}",
f"Quickly grab a {product['product_title']} for ${product['discount_number']} off! {product['product_url']}"
]
message = random.choice(messages)
if len(driver.find_elements_by_xpath('/html/body/div/div/div/div[2]/main/div/div/div/div[1]/div/div[2]/div/div[2]/div[1]/div/div/div/div[2]/div[1]/div/div/div/div/div/div/div/div/div/div[1]/div/div/div/div[2]/div')) > 0:
tweet_box = driver.find_element_by_xpath('/html/body/div/div/div/div[2]/main/div/div/div/div[1]/div/div[2]/div/div[2]/div[1]/div/div/div/div[2]/div[1]/div/div/div/div/div/div/div/div/div/div[1]/div/div/div/div[2]/div')
tweet_box.send_keys(message)
if len(driver.find_elements_by_xpath('/html/body/div/div/div/div[2]/main/div/div/div/div[1]/div/div[2]/div/div[2]/div[1]/div/div/div/div[2]/div[4]/div/div/div[2]/div[3]')) > 0:
tweet_button = driver.find_element_by_xpath('/html/body/div/div/div/div[2]/main/div/div/div/div[1]/div/div[2]/div/div[2]/div[1]/div/div/div/div[2]/div[4]/div/div/div[2]/div[3]')
sleep(1)
tweet_button.click()
db.products.update_one({'_id': product['_id']}, {'$set': {'published_at': datetime.now()}})
return True
return False