Skip to content

Commit b752949

Browse files
committed
Fixed "Get Post in group" method
1 parent 8d730bd commit b752949

File tree

1 file changed

+24
-14
lines changed

1 file changed

+24
-14
lines changed

FacebookWebBot.py

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from selenium import webdriver
33
from selenium.common.exceptions import NoSuchElementException
44
from selenium.webdriver.common.keys import Keys
5+
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
56
import time
67
import os
78
import json
@@ -46,7 +47,7 @@ class Post():
4647

4748
def __init__(self):
4849
self.posterName = ""
49-
self.description = ""
50+
self.text = ""
5051
self.numLikes = 0
5152
self.time = ""
5253
self.privacy = ""
@@ -77,7 +78,7 @@ def to_json(self):
7778

7879
def __str__(self):
7980
s = "\nPost by " + self.posterName + ": "
80-
s += self.description + "\n"
81+
s += self.text + "\n"
8182
s += "Likes: " + str(self.numLikes) + " - "
8283
s += "Comments: " + str(self.numComents) + " - "
8384
s += self.time + " "
@@ -89,6 +90,13 @@ def __repr__(self):
8990
return self.__str__()
9091

9192

93+
dcap = dict(DesiredCapabilities.PHANTOMJS)
94+
dcap["phantomjs.page.settings.userAgent"] = (
95+
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/53 "
96+
"(KHTML, like Gecko) Chrome/15.0.87"
97+
)
98+
99+
92100
class FacebookBot(webdriver.PhantomJS):
93101
"""Main class for browsing facebook"""
94102

@@ -104,7 +112,7 @@ def __init__(self):
104112
path = pathToPhantomJs
105113
webdriver.PhantomJS.__init__(self, path, service_args=service_args)
106114
"""
107-
webdriver.PhantomJS.__init__(self)
115+
webdriver.PhantomJS.__init__(self, desired_capabilities=dcap)
108116

109117
def get(self, url):
110118
"""The make the driver go to the url but reformat the url if is for facebook page"""
@@ -193,13 +201,15 @@ def newMessageToFriend(
193201
send.send_keys(Keys.ENTER)
194202
return True
195203

196-
def getPostInGroup(self, url, deep=2):
197-
"""Get a list of posts (list:Post) in group url(str) iterating deep(int) times in the group"""
204+
def getPostInGroup(self, url, deep=2, moreText="Ver más publicaciones"):
205+
"""Get a list of posts (list:Post) in group url(str) iterating deep(int) times in the group
206+
pass moreText depending of your language, i couldn't find a elegant solution for this"""
198207

199208
self.get(url)
200209
ids = [4, 5, 6, 7, 9]
201210
posts = []
202211
for n in range(deep):
212+
#print("Searching, deep ",n)
203213
for i in ids:
204214
# print(i)
205215
post = Post()
@@ -212,7 +222,7 @@ def getPostInGroup(self, url, deep=2):
212222
post.numLikes = int(a[3].text.split(" ")[0])
213223
except ValueError:
214224
post.numLikes = 0
215-
# post.description = p.find_element_by_tag_name("p").text
225+
post.text = p.find_element_by_tag_name("p").text
216226
post.time = p.find_element_by_tag_name("abbr").text
217227
# p.text.split("· ")[1].split("\n")[0]
218228
post.privacy = self.title
@@ -227,18 +237,18 @@ def getPostInGroup(self, url, deep=2):
227237
# post.linkToShare = a[5].get_attribute('href')
228238
post.linkToLikers = a[1].get_attribute('href')
229239
post.linkToMore = a[6].get_attribute('href')
230-
posts.append(post)
240+
if post not in posts:
241+
posts.append(post)
231242
except Exception:
232243
continue
233244
try:
234-
more = self.find_element_by_class_name("dm").find_elements_by_tag_name("a")[
235-
0].get_attribute('href')
245+
more = self.find_element_by_partial_link_text(
246+
moreText).get_attribute('href')
236247
self.get(more)
237-
except Exception:
238-
pass
239-
#print("can't get more :(")
240-
# return posts, self.getScrenshotName("PostsIn" + self.title,
241-
# screenshot, screenshotPath)
248+
# self.find_element_by_partial_link_text(moreText)
249+
except Exception as e:
250+
print(e)
251+
print(" Can't get more posts")
242252
return posts
243253

244254
def postInGroup(self, groupURL, text):

0 commit comments

Comments
 (0)