-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfb.py
433 lines (352 loc) · 15.3 KB
/
fb.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.alert import Alert
from selenium.common.exceptions import *
from io import StringIO
import time
import re
import requests
import sys
import os
from datetime import datetime, date, timedelta
from time import strftime
from bs4 import BeautifulSoup
from random import shuffle, randint
import glob
import pyperclip
FACEBOOK_URL = 'https://www.facebook.com'
def current_time():
return '[' + (datetime.now()).strftime('%Y/%m/%d %H:%M:%S') + ']'
def sleep_time(seconds, count_down_msg = 'YES'):
pause_sec = seconds
while seconds >= 0:
if count_down_msg == 'YES':
print('pause %s seconds.... Count down = %04s' %(pause_sec, seconds), end = '\r')
time.sleep(1)
seconds = seconds - 1
if count_down_msg == 'YES':
print(end = '\n')
def rand_sleep_time(rand_start, rand_end, count_down_msg = 'YES'):
rand_num = randint(rand_start, rand_end)
sleep_time(rand_num, count_down_msg)
def facebook_get_user_info():
user_name_elem = driver.find_element_by_xpath("//div[@class='linkWrap noCount']")
user_name = user_name_elem.text
return user_name
def facebook_login(username, password):
chrome_intialization()
print ("%s %s Login to Facebook...." %(current_time(), username)),
sys.stdout.flush()
url = "http://www.facebook.com"
driver.get(url)
elem = driver.find_element_by_id("email")
elem.send_keys(username)
elem = driver.find_element_by_id("pass")
elem.send_keys(password)
elem.send_keys(Keys.RETURN)
sleep_time(1, "N")
html_source = driver.page_source
if "Forgot password?" in html_source or "忘記密碼?" in html_source:
print ("%s Incorrect Username or Password" %(current_time()))
driver.quit()
return "failed"
else:
print ("%s %s Login to Facebook success" %(current_time(), username))
return driver.get_cookies()
def facebook_logout(username):
print ("%s %s Logout to Facebook...." %(current_time(), username))
url = "http://www.facebook.com/logout.php"
driver.get(url)
try:
logoutMenu = driver.find_element_by_id("logoutMenu")
except:
print('%s %s logout memu can not find' %(current_time(), username))
return "NoLogoutMenu"
logoutMenu.click()
sleep_time(3, "N")
# logoutBtn = driver.find_element_by_xpath("//*[@action='https://www.facebook.com/logout.php']")
logoutBtn = driver.find_element_by_xpath("//*[text()='登出']")
logoutBtn.click()
print ("%s %s Logout Success\n" %(current_time(), username))
return "Success"
def write_line_to_file(filename, line):
f = open (filename, 'a', encoding = 'utf-8')
f.write(line+'\n')
f.close()
def facebook_collect_groups_id(max_group_count = 200):
GROUPS_FLIE_NAME = './groups.cfg'
GROUPS_ID_LIST = list()
if (os.path.isfile(GROUPS_FLIE_NAME) and os.access(GROUPS_FLIE_NAME, os.R_OK)):
os.remove(GROUPS_FLIE_NAME)
print("%s Collecting groups... wait a minutes..." %(current_time()))
url = "https://www.facebook.com/groups/?category=membership"
driver.get(url)
GroupsElemsList = driver.find_elements_by_xpath("//*[@class='groupsRecommendedTitle']")
lenOfPage = facebook_scroll_end_of_page()
while(True):
if len(GroupsElemsList) > max_group_count:
break
lastCount = lenOfPage
lenOfPage = facebook_scroll_end_of_page()
GroupsElemsList = driver.find_elements_by_xpath("//*[@class='groupsRecommendedTitle']")
# Default maxime groups counts is 200
if lastCount == lenOfPage:
break
print("%s total %s groups" %(current_time(), len(GroupsElemsList)))
for group in GroupsElemsList:
group_name = group.text
group_link = group.get_attribute('href')
group_id = group.get_attribute('data-hovercard').split('=')[1]
GROUPS_ID_LIST.append({'g_id':group_id, 'g_name':group_name})
line = group_id + '=' + group_name
write_line_to_file(GROUPS_FLIE_NAME, line)
return GROUPS_ID_LIST
#
# Using BS4 to parser group html source
#
# html_source = driver.page_source
# soup = BeautifulSoup(html_source, 'lxml')
# for groups in soup.find_all('a', {'class':'groupsRecommendedTitle'}):
# group_name = groups.string
# group_link = groups.get('href')
def facebook_post_to_groups(GroupId, GroupName, TextMessage, number_idx):
print('%s Entering into %s (%s)' %(current_time(), GroupName, GroupId))
post_status = "Successed"
url = FACEBOOK_URL + '/' + GroupId
driver.get(url)
sleep_time(5, "NO")
try:
TextAreaElem = driver.find_element_by_xpath("//*[@name='xhpc_message_text']")
except:
print("%s %s on %s (%s) NoTextAreaElem" %(current_time(), TextMessage.strip(" \r\n"), GroupName, GroupId))
post_status = "NoTextAreaElem"
if (post_status == "Successed"):
sleep_time(3, count_down_msg = 'NO')
try:
# TextAreaElem.click()
TextAreaElem.send_keys("")
pyperclip.copy(TextMessage)
sleep_time(2, "N")
ActionChains(driver).key_down(Keys.CONTROL).send_keys('v').key_up(Keys.CONTROL).perform()
print("%s" %(TextMessage))
sleep_time(1, "N")
# ActionChains(driver).key_down(Keys.ENTER).perform()
except:
print("%s %s on %s (%s) send key failed" %(current_time(), TextMessage.strip(" \r\n"), GroupName, GroupId))
post_status = "SendKeyFailed"
if (post_status == "Successed"):
sleep_time(5, count_down_msg = 'NO')
retry_count = 0
while True:
try:
# PostBtnElem = driver.find_element_by_xpath("//button/span[.='Post']").click()
PostBtnElem = driver.find_element_by_xpath("//button/span[text()='發佈']").click()
sleep_time(3, count_down_msg = 'NO')
print("%s Post button pressed success" %(current_time()))
break
except:
print("%s Post Button is not exist" %(current_time()))
retry_count = retry_count + 1
sleep_time(1, 'NO')
if retry_count == 5:
post_status = "NoPostBtnElem"
return post_status
def GetChromeOptions_Notification(Value):
#
# 1 = Allow notification , 2 = Block notification
#
chrome_options = webdriver.ChromeOptions()
prefs = {"profile.default_content_setting_values.notifications" : Value}
chrome_options.add_experimental_option("prefs",prefs)
# driver = webdriver.Chrome(chrome_options=chrome_options)
return chrome_options
def facebook_get_graphic_token():
url = 'https://www.facebook.com/v2.3/dialog/oauth?response_type=token&display=popup&client_id=145634995501895&redirect_uri=https%3A%2F%2Fdevelopers.facebook.com%2Ftools%2Fexplorer%2Fcallback&scope=user_about_me%2Cuser_actions.books%2Cuser_actions.fitness%2Cuser_actions.music%2Cuser_actions.news%2Cuser_actions.video%2Cuser_birthday%2Cuser_education_history%2Cuser_events%2Cuser_friends%2Cuser_games_activity%2Cuser_hometown%2Cuser_likes%2Cuser_location%2Cuser_photos%2Cuser_posts%2Cuser_relationship_details%2Cuser_relationships%2Cuser_religion_politics%2Cuser_status%2Cuser_tagged_places%2Cuser_videos%2Cuser_website%2Cuser_work_history%2Cads_management%2Cads_read%2Cemail%2Cmanage_notifications%2Cmanage_pages%2Cpages_manage_leads%2Cpublish_actions%2Cpublish_pages%2Cread_custom_friendlists%2Cread_insights%2Cread_mailbox%2Cread_page_mailboxes%2Cread_stream%2Crsvp_event'
driver.get(url)
while True:
try:
OkayBtnElem = driver.find_element_by_name('__CONFIRM__').click()
sleep_time(3)
except:
break
html_source = driver.page_source
access_token_start = html_source.find('"accessToken"')
access_token = html_source[access_token_start:-1].split(',')[0].split(':')[1]
return access_token
def facebook_scroll_end_of_page():
lenOfPage = driver.execute_script("window.scrollTo(0, document.body.scrollHeight);var lenOfPage=document.body.scrollHeight;return lenOfPage;")
sleep_time(2, "N")
return lenOfPage
# match=False
# while(match==False):
# lastCount = lenOfPage
# time.sleep(1)
# lenOfPage = driver.execute_script("window.scrollTo(0, document.body.scrollHeight);var lenOfPage=document.body.scrollHeight;return lenOfPage;")
# if lastCount == lenOfPage:
# match=True
def facebook_search_by_type(search_type, key_word):
facebook_search_url = 'https://www.facebook.com/search/'
url = facebook_search_url + search_type + '/?q=' + key_word
driver.get(url)
lenOfPage = facebook_scroll_end_of_page()
while True:
lastCount = lenOfPage
lenOfPage = facebook_scroll_end_of_page()
driver.find_element_by_xpath
def facebook_prsss_like_button(user_name):
user_content_wrapper_elems = driver.find_elements_by_xpath("//div[@class='userContentWrapper _5pcr']")
for content_area in user_content_wrapper_elems:
try:
user_article_xpath = ('//a[text()="%s"]' %user_name)
user_article = content_area.find_element_by_xpath(user_article_xpath)
except:
# print("%s %s post article can not found, press like button failed." %(current_time(), user_name))
continue
like_btn_elem = content_area.find_element_by_xpath("//a[@data-testid='fb-ufi-likelink']")
like_btn_elem.click()
print('%s Press like button' %current_time())
break
# try:
# user_article_xpath = ('//a[text()="%s"]' %user_name)
# user_article = driver.find_element_by_xpath(user_article_xpath)
# except:
# print("%s %s post article can not found" %(current_time(), user_name))
# return "NotFound"
# like_btn_elem = driver.find_elements_by_xpath("//a[@data-testid='fb-ufi-likelink']")[0]
# like_btn_elem.click()
# print('%s Press like button' %current_time())
def get_message_from_file(message_file):
if not (os.path.isfile(message_file) and os.access(message_file, os.R_OK)):
print('%s is not exist' %message_file)
exit();
f = open(message_file, "r", encoding = 'utf-8')
data = f.read()
if len(data) == 0:
print('%s is empty, please check it again' %message_file)
exit();
f.close()
return data
def get_account_info_from_file(account_file_path):
if not (os.path.isfile(account_file_path) and os.access(account_file_path, os.R_OK)):
print('%s is not exist' %account_file_path)
exit();
content_list = list()
f = open(account_file_path, "r", encoding = 'utf-8')
for line in f.readlines():
if not len(line) or line.startswith('#') or line.startswith('\n'):
continue
line = line.strip(" \r\n")
line = line.replace(" ","")
content_list.append(line)
f.close()
return content_list
def get_article_number_list(fpath):
filelist = []
filelist = glob.glob(fpath)
return filelist
def chrome_intialization():
global driver
notifications_block = 2
ChromPrefs = GetChromeOptions_Notification(notifications_block)
if sys.platform == 'win32':
driver = webdriver.Chrome('./chromedriver.exe', chrome_options=ChromPrefs)
if sys.platform == 'darwin':
driver = webdriver.Chrome('./chromedriver', chrome_options=ChromPrefs)
driver.set_window_size(480, 320)
# if sys.platform != 'win32' and sys.platform != 'darwin' :
# display = Display(visible=0, size=(1600, 900))
# display.start()
def main():
EACH_ACCOUNT_INTERVALS_DELAY_MIN = 50 * 60
EACH_ACCOUNT_INTERVALS_DELAY_MAX = 80 * 60
EACH_ARTICLE_INTERVALS_DELAY_MIN = 3 * 60
EACH_ARTICLE_INTERVALS_DELAY_MAX = 5 * 60
HOW_MANY_COUNT_ARTICLE_SHOULD_PAUSE = 12
COLLET_MAX_GROUPS_COUNT = 200
account_info_file_path_name = './account.cfg'
account_info_list = get_account_info_from_file(account_info_file_path_name)
account_info_list_len = len(account_info_list)
account_list_idx = 0
if (account_info_list_len == 0):
print('Account file is not exist : please create "account.cfg" file')
exit()
article_file_path = "./article/*"
article_path_list = get_article_number_list(article_file_path)
article_path_list_len = len(article_path_list)
if (article_path_list_len == 0):
print('Article file is not exist : please create "./article/*"')
exit()
post_count = 1
# while True:
# process_start_time = datetime.now()
# next_start_time = process_start_time + timedelta(seconds = EACH_ACCOUNT_INTERVALS_DELAY)
account_info_list_len = len(account_info_list)
if (account_info_list_len == 0):
print('%s No avaialbe account' %(current_time()))
exit()
account_info = account_info_list[account_list_idx].split(",")
username = account_info[0]
password = account_info[1]
account_list_idx = account_list_idx + 1
if (account_list_idx >= account_info_list_len):
account_list_idx = 0
cookies = dict()
cookies = facebook_login(username,password)
try:
fb_user_name = facebook_get_user_info()
except NoSuchElementException:
del account_info_list[account_list_idx - 1]
print('%s %s has been locked by facebook' %(current_time(), username))
driver.close()
# continue
fb_groups_list = facebook_collect_groups_id(COLLET_MAX_GROUPS_COUNT)
# while process_start_time <= next_start_time:
while True:
for fb_group_dict in fb_groups_list:
for fb_group_id, fb_group_name in fb_group_dict.items():
shuffle(article_path_list)
rand_num = randint(0, article_path_list_len - 1)
article_path = article_path_list[rand_num]
msg = get_message_from_file(article_path)
print('\n%s ============== %s Start post NO.%s ===============' %(current_time(), fb_user_name, post_count))
try:
post_status = facebook_post_to_groups(fb_group_id, fb_group_name, msg, post_count)
except UnexpectedAlertPresentException:
sleep_time(2, "NO")
Alert(driver).accept()
continue
# if post status failed, remove the gruoup from list.
if (post_status == "SendKeyFailed") or (post_status == "NoTextAreaElem"):
fb_groups_list.remove(fb_group_dict)
print("%s Remove %s %s" %(current_time(), fb_group_id, fb_group_name))
print('%s ============== %s End post NO.%s ===============\n' %(current_time(), fb_user_name, post_count))
break
# else:
# If post successed, press like button.
# facebook_prsss_like_button(fb_user_name)
# print('%s ============== %s End post NO.%s ===============\n' %(current_time(), fb_user_name, post_count))
print('%s ============== %s End post NO.%s ===============\n' %(current_time(), fb_user_name, post_count))
# if process_start_time >= next_start_time:
if (post_count % HOW_MANY_COUNT_ARTICLE_SHOULD_PAUSE) == 0:
process_start_time = datetime.now()
account_intervals_delay = randint(EACH_ACCOUNT_INTERVALS_DELAY_MIN, EACH_ACCOUNT_INTERVALS_DELAY_MAX)
next_time = process_start_time + timedelta(seconds = account_intervals_delay)
print("%s Pausing.... Next start time on %s" %(current_time(), next_time.strftime('%Y/%m/%d %H:%M:%S')))
# facebook_logout(username)
# driver.close()
sleep_time(account_intervals_delay)
post_count += 1
break
else:
# if post successed then random sleep 2 ~ 4 mins
rand_sleep_time(EACH_ARTICLE_INTERVALS_DELAY_MIN, EACH_ARTICLE_INTERVALS_DELAY_MAX)
post_count += 1
# if process_start_time >= next_start_time:
# if (post_count % 10) == 0:
# break
# driver.close()
exit()