-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
225 lines (161 loc) · 7.5 KB
/
main.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
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
from pyautogui import typewrite, press
from random import randint
import base64
from time import sleep
from sys import argv
class Publisher():
def __init__(self, product_name):
if (product_name == 'add_element'):
self.add_product_info_to_database()
return
# product info got by the terminal argument
product = self.get_product_info(product_name)
# get product info raises an exception if no product is found, and ends the program
self.publish(product)
def publish(self, product_info):
# few options to make it prettier
chrome_options = webdriver.ChromeOptions()
# chrome_options.add_argument('--headless')
chrome_options.add_argument('--start-maximized')
# chrome_options.add_argument('--log-level=OFF')
# initialize the webdriver
self.webdriver = webdriver.Chrome(options=chrome_options)
# go to facebook.com
self.webdriver.get('https://www.facebook.com')
# email and password credentials ( already decrypted )
credentials = self.get_login_info()
# typing the email and password and logging in
email = self.webdriver.find_element_by_id('email')
email.send_keys(credentials.get('email'))
password = self.webdriver.find_element_by_id('pass')
password.send_keys(credentials.get('password') + Keys.ENTER)
# wait for the page to load
self.wait_for_selector('//div[@aria-label="Facebook"]', False)
# choosing a random group
groups = [
'https://www.facebook.com/groups/424204217729696',
'https://www.facebook.com/groups/mercadonegromontesclaros/',
'https://www.facebook.com/groups/310280522400702/',
'https://www.facebook.com/groups/432417496866854/'
]
random_group = groups[randint(0, len(groups) - 1)]
self.webdriver.get(random_group)
# wait for the 'vender algo' selector, and click it
self.wait_for_selector('//div[@aria-label="Vender algo"]', True)
# wait for the 'Item a venda' selector
self.wait_for_selector('//input[@aria-invalid="false"]', False)
# typing the product info ( title, price, ... ), the product info is an object
self.type_product_info(product_info)
# wait for everything to finish uploading
sleep(3)
# clicking next ( go to group selection page )
next_group_selector = '//div[@aria-label="Avançar"]'
self.wait_for_selector(next_group_selector, True)
# wait for the group animation
sleep(1.25)
# clicking on every group
groups = self.webdriver.find_elements_by_xpath('//img[@width="24"]')
# 1 until 13 are the groups i want to post things about
# if you want to remove a group ( in some cases your groups aren't for selling, you can black-mail the group-index value )
for i in range(1, 14):
groups[i].click()
# publishing it
self.webdriver.find_element_by_xpath('//*[text()="Publicar"]').click()
sleep(10)
print('Done!')
# also responsable for encryption and decryption
def get_login_info(self):
# email and password are encrypted saved into a hidden 'login_info.txt' file. also hidden through '.gitignore'
file = open('.login_info.txt', 'r')
lines = file.readlines()
encrypted_email = lines[0]
encrypted_password = lines[1]
encrypted_email_bytes = encrypted_email.encode('ascii')
encrypted_password_bytes = encrypted_password.encode('ascii')
decrypted_email_bytes = base64.b64decode(encrypted_email_bytes)
decrypted_password_bytes = base64.b64decode(encrypted_password_bytes)
email = decrypted_email_bytes.decode('ascii')
password = decrypted_password_bytes.decode('ascii')
return {
'email': email,
'password': password
}
def wait_for_selector(self, selector='', should_click=False):
while (True):
try:
if (should_click):
self.webdriver.find_element_by_xpath(selector).click()
else:
self.webdriver.find_element_by_xpath(selector)
break
except:
sleep(3)
def type_product_info(self, product_info: {}):
# elements = [title, price]
elements_xpath = '//input[@type="text"]'
elements = self.webdriver.find_elements_by_xpath(elements_xpath)
elements[0].send_keys(product_info.get('title'))
elements[1].send_keys(product_info.get('price'))
description = self.webdriver.find_element_by_xpath('//textarea')
description.send_keys(product_info.get('description'))
self.send_images(product_info.get('images'))
# perform all the clicks and operations, once the image_selector is open
def send_images(self, images):
text_selector = '//*[text()="Adicionar fotos"]'
for i, image in enumerate(images):
self.webdriver.find_element_by_xpath(text_selector).click()
sleep(2)
# first occurrence has to be setted up
if (i == 0):
press('left')
for i in range(7):
press('down')
press('enter')
press('right')
press('enter')
sleep(1)
typewrite(image)
press('enter')
sleep(2)
text_selector = '//*[text()="Adicionar foto"]'
def get_product_info(self, product_name=''):
if (product_name == ''):
raise 'Invalid Name'
with open('products_info.txt', 'r') as file:
lines = file.readlines()
for i in range(len(lines)):
lines[i] = lines[i].replace('{', '').replace('}', '')
lines[i] = lines[i].replace("'", '')
lines[i] = lines[i].split(', ')
title = lines[i][0].split(': ')[1]
# title equals the name of the product we want to publish
if (product_name in title):
# proceed to get the rest of the information
price = lines[i][1].split(': ')[1]
description = lines[i][2].split(
': ')[1].replace('\\n', '\n')
images = lines[i][3:]
for i in range(len(images)):
images[i] = images[i].replace('images: [', '')
images[i] = images[i].replace(']', '').strip()
return {
'title': title,
'price': price,
'description': description,
'images': images
}
raise 'Name not found!'
def add_product_info_to_database(self):
# ask the user input, and format's it in only one line
title = f"'title': '{input('Title: ').replace(':', '=')}'"
price = f"'price': '{input('Price: ')}'"
description = f"'description': '{input('Description: ').replace(':', '=')}'"
images = f"'images': ['{input('Images: ')}']"
with open('products_info.txt', 'a') as file:
query = '{' + f'{title}, {price}, {description}, {images}' + '}'
file.write(query + '\n')
if __name__ == '__main__':
Publisher(' '.join(argv[1:]))