-
Notifications
You must be signed in to change notification settings - Fork 11
/
Lister.py
290 lines (241 loc) · 10.7 KB
/
Lister.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
from selenium import webdriver
import time
import os
from Element import Element
from Helpers import read_json
from datetime import datetime
from colorama import Fore, Style
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
class Lister:
def __init__(self):
self.driver_file = 'chromedriver.exe'
self.sleep_time = 1
chrome_options = webdriver.ChromeOptions()
prefs = {"profile.default_content_setting_values.notifications" : 2}
chrome_options.add_experimental_option("prefs",prefs)
chrome_options.add_experimental_option("detach", True)
chrome_options.add_argument("--start-maximized")
self.driver = webdriver.Chrome('drivers/%s' % self.driver_file, chrome_options=chrome_options)
self.driver.implicitly_wait(30)
def read_accounts(self):
return read_json('accounts')['accounts']
def login(self, account_id):
registered_accounts = self.read_accounts()
account_info = list(filter(lambda acc: acc['id'] == account_id, registered_accounts))[0]
log('Logging in as "%s" ..' % account_info['name'], 'main')
self.driver.get('https://www.facebook.com/login')
# entering email
email_input = Element(self.driver, 'login_email').element
email_input.clear()
email_input.send_keys(account_info['email'])
# entering password
password_input = Element(self.driver, 'login_password').element
password_input.clear()
password_input.send_keys(account_info['password'])
# Submitting
password_button = Element(self.driver, 'login_button').element
password_button.click()
# Confirm Logged In
logged = WebDriverWait(self.driver, 60).until(
lambda driver: "login" not in driver.current_url
)
if logged : log('Logged in Successfully.', 'success')
else : log('Failed To Login.', 'failure')
return logged
def list(self, item):
self.driver.get('https://www.facebook.com/marketplace/create/item')
listing_item = Item(self.driver, item)
listing_item.upload_images()
time.sleep(self.sleep_time)
listing_item.enter_title()
time.sleep(self.sleep_time)
listing_item.enter_price()
time.sleep(self.sleep_time)
listing_item.choose_category()
time.sleep(self.sleep_time)
listing_item.choose_condition()
time.sleep(self.sleep_time)
if 'description' in item.keys() and item['description'] :
listing_item.enter_description()
time.sleep(self.sleep_time)
if 'sku' in item.keys() and item['sku'] :
listing_item.enter_sku()
time.sleep(self.sleep_time)
listing_item.choose_location()
time.sleep(self.sleep_time)
if 'hide_from_friends' in item.keys() and item['hide_from_friends'] :
listing_item.hide_from_friends()
time.sleep(self.sleep_time)
listing_item.click_next()
time.sleep(self.sleep_time)
listing_item.click_publish()
time.sleep(self.sleep_time)
# Check if Posted
posted = WebDriverWait(self.driver, 60).until(
lambda driver: "you" in driver.current_url
)
return posted
class Item :
def __init__(self, driver, item):
self.driver = driver
self.item = item
def upload_images(self):
try:
log('Uploading Images', 'main')
image_upload = Element(self.driver, 'post_image').element
self.driver.execute_script("document.querySelector('%s').classList = []" % Element(self.driver, 'post_image_css').xpath)
log('Showing image input ..', 'main')
joined_images_path = ' \n '.join([os.path.abspath('images/%s' % image['file']) for image in self.item['images']][:10])
log('sending images ..', 'main')
image_upload.send_keys(joined_images_path)
log('Uploaded Images Successfully .', 'success')
return True
except :
log('FAILED TO UPLOAD IMAGE', 'failure')
return False
def enter_title(self):
try:
log('Entering The Title', 'main')
title_input = Element(self.driver, 'post_title').element
title_input.clear()
title_input.send_keys(self.item['title'])
log('Entered Title Successfully .', 'success')
return True
except :
log('FAILED TO ENTER THE TITLE', 'failure')
return False
def enter_price(self):
try:
log('Entering The Price', 'main')
price_input = Element(self.driver, 'post_price').element
price_input.clear()
price_input.send_keys(self.item['price'])
log('Entered Price Successfully .', 'success')
return True
except :
log('FAILED TO ENTER THE PRICE', 'failure')
return False
def choose_category(self):
try:
log('Choosing The Category', 'main')
category_dropdown = Element(self.driver, 'post_category').element
category_dropdown.click()
values = self.item['category'] if 'category' in self.item.keys() and self.item['category'] else None
category_dropdown_option = Element(self.driver, 'post_category_option', values).element
print(Element(self.driver, 'post_category_option', values).xpath)
print(Element(self.driver, 'post_category_option', values).xpath)
print(Element(self.driver, 'post_category_option', values).xpath)
print(Element(self.driver, 'post_category_option', values).xpath)
print(Element(self.driver, 'post_category_option', values).xpath)
log('clicking The Category Dropdown ..', 'sub')
category_dropdown_option.click()
log('Category Chosen Successfully .', 'success')
return True
except :
log('FAILED TO CHOOSE THE CATEGORY', 'failure')
return False
def choose_condition(self):
try:
log('Choosing The Condition', 'main')
condition_dropdown = Element(self.driver, 'post_condition').element
condition_dropdown.click()
log('clicking The Condition Dropdown ..', 'sub')
values = self.item['condition'] if 'condition'in self.item.keys() and self.item['condition'] else None
condition_dropdown_option = Element(self.driver, 'post_condition_option', values).element
condition_dropdown_option.click()
log('Condition Chosen Successfully .', 'success')
return True
except :
log('FAILED TO CHOOSE THE CATEGORY', 'failure')
return False
def enter_description(self):
try:
log('Entering The Description', 'main')
description_input = Element(self.driver, 'post_description').element
description_input.clear()
description_input.send_keys(self.item['description'])
log('Entered Description Successfully .', 'success')
return True
except :
log('FAILED TO ENTER THE Description', 'failure')
return False
def enter_sku(self):
try:
log('Entering The SKU', 'main')
sku_input = Element(self.driver, 'post_sku').element
sku_input.clear()
sku_input.send_keys(self.item['sku'])
log('Entered SKU Successfully .', 'success')
return True
except :
log('FAILED TO ENTER THE SKU', 'failure')
return False
def choose_location(self):
try:
location = self.item['location'] if self.item['location'] else Element(self.driver, 'post_location_option').defaults
log('Choosing The Location', 'main')
location_input = Element(self.driver, 'post_location').element
location_input.click()
log('Searching Locations ..', 'sub')
location_input.send_keys(Keys.DELETE)
location_input.send_keys(location)
log('Choosing Location ..', 'sub')
values = self.item['location'] if 'location'in self.item.keys() and self.item['location'] else None
location_input_option = Element(self.driver, 'post_location_option', values).element
location_input_option.click()
log('Location Chosen Successfully .', 'success')
return True
except :
log('FAILED TO CHOOSE THE Location', 'failure')
return False
def hide_from_friends(self):
try:
log('Checking Hide From Friends', 'main')
self.click_button('post_hide_from_friends')
log('Checked Hide From Friends Successfully .', 'success')
return True
except :
log('FAILED TO Check Hide From Friends', 'failure')
return False
def click_next(self):
try:
log('Clicking Next', 'main')
self.click_button('post_next_button')
log('Clicked Next Successfully', 'success')
return True
except :
log('FAILED TO click Next', 'failure')
return False
def click_publish(self):
try:
log('Clicking Publish', 'main')
self.click_button('post_publish_button')
log('Clicked Publish Successfully', 'success')
return True
except :
log('FAILED TO click Publish', 'failure')
return False
def click_button(self, button):
element = Element(self.driver, button).element
element.click()
def log(msg, type=None):
now = datetime.now()
current_time = now.strftime("%H:%M:%S")
msg = "[%s] : %s" % (current_time, msg)
if type is not None:
if type == 'failure':
msg = Fore.RED + "\t- " + msg + Style.RESET_ALL
elif type == 'success':
msg = Fore.GREEN + "\t+ " + msg + Style.RESET_ALL
elif type == 'sub':
msg = Fore.WHITE + "\t> " + msg + Style.RESET_ALL
elif type == 'main':
msg = Fore.WHITE + ">> " + msg + Style.RESET_ALL
else:
msg = msg + Style.RESET_ALL
else:
msg = msg + Style.RESET_ALL
print (msg)