-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathfbPost.py
182 lines (126 loc) · 5.72 KB
/
fbPost.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
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
import time
from selenium.webdriver.common.keys import Keys
import json
import os
LOGIN_URL = 'https://www.facebook.com/login.php'
GROUP_URL='https://www.facebook.com/groups/'
options = webdriver.ChromeOptions()
#^disable push notifications
options.add_argument("--disable-notifications")
#^disale usb device error
options.add_experimental_option('excludeSwitches', ['enable-logging'])
driver = webdriver.Chrome('./chromedriver.exe', options=options)
class FacebookBot():
def __init__(self,data,text,groups,mode):
self.email= data['Email Address']
self.password= data['Password']
self.mode = mode
self.textContents = text
self.groupdata=groups
self.fb_posting = ["//div[@class='m9osqain a5q79mjw jm1wdb64 k4urcfbm']",
"/html/body/div[1]/div/div[1]/div/div[4]/div/div/div[1]/div/div[2]/div/div/div/div/div[1]/form/div/div[1]/div/div/div[1]/div/div[2]/div[1]/div[1]/div[1]/div/div/div/div/div[2]/div/div/div/div",
"/html/body/div[1]/div/div[1]/div/div[4]/div/div/div[1]/div/div[2]/div/div/div/div/div[1]/form/div/div[1]/div/div/div[1]/div/div[3]/div[2]/div/div",
"/html/body/div[1]/div/div[1]/div/div[4]/div/div/div[1]/div/div[2]/div/div/div/div/div[1]/form/div/div[1]/div/div/div[1]/div/div[3]/div[1]/div[2]/div[3]/input"]
#^Group ids
def ids(self):
id = self.groupdata
split_links = id.split(',')
global group
group=[]
for p in split_links:
group.append(p.replace('https://www.facebook.com/groups/','').replace("\n",'').split('/')[0])
#^Login part
def login(self):
print("Process Initiating")
driver.get(LOGIN_URL)
time.sleep(2) # Wait for some time to load
email_element = driver.find_element_by_id('email')
email_element.send_keys(self.email) # Give email as i/p
password_element = driver.find_element_by_id('pass')
password_element.send_keys(self.password) # Give password as i/p
login_button = driver.find_element_by_id('loginbutton')
login_button.click() # Login click
time.sleep(2) # Wait for 2 seconds for the page to show up
# def get_imagePath(self):
# global image_paths
# image_paths= []
#^Posting part
def page_posting(self):
try:
i=0
for p in group:
#^Get homepage :
driver.get(GROUP_URL+p)
time.sleep(5)
#^Get new post :
driver.find_element_by_xpath(self.fb_posting[0]).click()
time.sleep(2)
#^Get images :
if (self.mode):
try :
x = driver.find_element_by_xpath(self.fb_posting[3])
time.sleep(2)
#^get current directory
cwd = os.getcwd()
#^get images from img folder
path =r"\img"
#^change directory to img folder
os.chdir(cwd+path )
#^all images
print("files are :",os.listdir())
#^all paths
for p in os.listdir():
#image_paths.append(cwd+path+"\{0}".format(p))
#print("images retrieved", image_paths)
x.send_keys(cwd+path+"\{0}".format(p))
#^clear dir
os.chdir(cwd)
except Exception as e:
print("Error is: \n ",e)
driver.close()
else :
pass
time.sleep(2)
#^ enter text :
driver.find_element_by_xpath(self.fb_posting[1]).send_keys(Keys.ENTER, self.textContents)
time.sleep(2)
#^ Post :
driver.find_element_by_xpath(self.fb_posting[2]).click()
time.sleep(5)
#^ Counter :
i+=1
print("posts completed",i)
except Exception as e:
print("Error is: \n ",e)
driver.close()
def src_bot():
#^Passing the file details and fb credentials
f = open('./fb_credentials.json','r')
t= open ('./PostingContents.txt','r')
g = open('./group_links.txt','r')
groups = g.read()
data=json.loads(f.read())
text= t.read()
#^Boolean To handle images
mode = False
i = input("Do you want to upload with images? Reply with Y or N")
if (i =="N"):
#^Init the object
bot=FacebookBot(data,text,groups,mode)
bot.ids()
bot.login()
bot.page_posting()
driver.close()
print("Task Completed Successfully!")
else :
mode = True
#^Init the object
bot=FacebookBot(data,text,groups,mode)
bot.ids()
bot.login()
bot.page_posting()
driver.close()
print("Task Completed Successfully!")
src_bot()