-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfb_main.py
More file actions
326 lines (274 loc) · 11.2 KB
/
fb_main.py
File metadata and controls
326 lines (274 loc) · 11.2 KB
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
#-*- encoding: utf-8 -*-
__author__ = 'Sid'
from HTMLParser import HTMLParser
import json
from selenium import webdriver
from datetime import datetime
from parsers import fb_constants as constants
from utils.ParseLogger import logger
class FBNode(object):
def __init__(self, fid=None):
self.fid = fid
def __repr__(self):
return self.__unicode__().encode('utf-8')
def __unicode__(self):
return u'FID: {0}'.format(unicode(self.fid))
def __hash__(self):
return hash(self.fid)
def __eq__(self, other):
if isinstance(other, FBNode) and self.fid == other.fid:
return True
return False
class FBUser(FBNode):
def __init__(self, fid=None, full_name=None, user_name=None, friends=None, current_city=None, home_town=None, phones=None,
address=None, emails=None, birth_date=None, birth_year=None, gender=None, interested_in=None,
languages=None, family_members=None):
super(FBUser, self).__init__(fid)
self.full_name = full_name # String
self.user_name = user_name # String
self.friends = friends
self.current_city = current_city # String
self.home_town = home_town # String
self.phones = phones # String
self.address = address # String
self.emails = emails # String
self.birth_date = birth_date # String
self.birth_year = birth_year # String
self.gender = gender # String
self.interested_in = interested_in # String
self.languages = languages # List of string
self.family_members = family_members # List of FBUsers
def import_to_db(self, cursor):
"""
:param cursor: cursor to DB
Imports some fields to a DB
"""
USER_INSERT = r"INSERT INTO USERS(ID, USER_NAME, FULL_NAME) " \
r"VALUES(%(id)s, %(username)s, %(fullname)s) " \
r"ON DUPLICATE KEY UPDATE USER_NAME=%(username)s, FULL_NAME=%(fullname)s"
cursor.execute(USER_INSERT, {
'id': self.fid, 'username': self.user_name, 'fullname': self.full_name
})
def __repr__(self):
return self.__unicode__().encode('utf-8')
def __unicode__(self):
return u'FID: {0}, username: {1}, full name: {2}'.format(self.fid, self.user_name, self.full_name)
class FBPicture(FBNode):
def __init__(self, fid=None, author=None, taggees=None, likers=None, published=None,
caption=None, commenters=None, comments=None, sharers=None, privacy=None):
super(FBPicture, self).__init__(fid)
self.author = author # FBUser
self.taggees = taggees # List of FBUsers
self.caption = caption
self.commenters = commenters # List of FBUsers
self.likers = likers # List of FBUsers
self.sharers = sharers # List of FBUsers
self.privacy = privacy # String
self.comments = comments # List of FBComments
self.published = published # datetime object
class FBPage(FBNode):
def __init__(self, fid=None, page_user=None, page_title=None, likers=None, likers_amount=None,
short_description=None, long_description=None):
super(FBPage, self).__init__(fid)
self.page_user = page_user
self.page_title = page_title
self.likers = likers
self.likers_amount = likers_amount
self.short_description= short_description
self.long_description = long_description
class FBGroup(FBNode):
def __init__(self, fbid, username=None, title=None, privacy=None, description=None,
category=None, members_amount=None, members=None):
super(FBGroup, self).__init__(fbid)
self.username = username # username (string)
self.title = title # full name (string)
self.privacy = privacy # string
self.description = description # string
self.members_amount = members_amount # int
self.members = members # list of FBUsers
self.category = category # Category (string)
def import_to_db(self, parse_time, cursor):
"""
:param parse_time: The time the metadata has been parsed
:param cursor: cursor to DB
imports some fields to DB
"""
GROUP_INSERT = r"INSERT INTO GROUPS(ID, NAME_R, USERNAME, DESCRIPTION, CATEGORY, PRIVACY, MEMBERS_AMOUNT, " \
r"LAST_META_PARSE) " \
r"VALUES(%(id)s, %(name)s, %(user)s, %(desc)s, %(cat)s, %(priv)s, %(members)s, %(time)s )" \
r"ON DUPLICATE KEY " \
r"UPDATE NAME_R=%(name)s, USERNAME=%(user)s, DESCRIPTION=%(desc)s, CATEGORY=%(cat)s," \
r"PRIVACY=%(priv)s, MEMBERS_AMOUNT=%(members)s, LAST_META_PARSE=%(time)s"
cursor.execute(GROUP_INSERT, {
'id': self.fid, 'name': self.title, 'user': self.username, 'desc': self.description,
'cat': self.category, 'priv': self.privacy, 'members': self.members_amount, 'time': parse_time
})
class FBPost(FBNode):
"""
class to contain post about a post
"""
def __init__(self, id='', group=None, author=None, date_time='', content='', commenters=None):
super(FBPost, self).__init__(id)
self.group = group
self.author = author
if commenters:
self.comments = commenters
else:
self.comments = []
self.content = content
if type(date_time) == int:
# timestamp unix
post_datetime = datetime.fromtimestamp(date_time)
elif type(date_time) == datetime:
post_datetime = date_time
else:
post_datetime = None
self.date_time = post_datetime
class FBParser(object):
"""
General FB Parser
"""
def __init__(self, log_name="FBParser", log_dir='.', debug=False):
self.driver = None # Will be initialized later
self._html_parser = HTMLParser()
self._user_id = None # Will be initialized later
self.debug = debug
self._logger = logger
def set_driver(self, driver):
self.driver = driver
def init_connect(self, email, password):
"""
Connect to facebook
return user fid if successfull, None otherwise
"""
self.driver.get('https://facebook.com')
# set email
email_element = self.driver.find_element_by_id('email')
email_element.send_keys(email)
# set password
password_element = self.driver.find_element_by_id('pass')
password_element.send_keys(password)
# press login button
self.driver.find_element_by_id('loginbutton').click()
if 'attempt' in self.driver.current_url:
# Failed to log in
return None
my_id_match = constants.FBRegexes.my_fid.search(self.driver.page_source)
if my_id_match:
return my_id_match.group('result')
return None
def _fix_payload(self, payload):
"""
:param payload: html payload
:return: unescaped html
"""
payload_html = payload.replace(r'\u003C', '<')
return self._html_parser.unescape(payload_html)
def _info_from_url(self, regex_name, src_string):
"""
:param regex: regex's name in dict. will be used to extract the info
:param src_string: source string to extract regex from
:return: extracted info, None if not found
"""
info_match = getattr(constants.FBRegexes, regex_name).search(src_string)
if info_match is None:
return None
return info_match.group('result')
def _parse_user_from_link(self, user_link):
"""
:param xpath_element: XPath element
:return: FBUser instance of current element
"""
username = full_name = fid = None
fid_url = user_link.xpath(constants.FBXpaths.user_fid_url)
if len(fid_url) > 0:
fid = unicode(self._info_from_url('liker_fid_from_url', fid_url[0]))
username_url = user_link.xpath(constants.FBXpaths.user_username_url)
if len(username_url) > 0:
username = unicode(self._info_from_url('liker_username_from_url', username_url[0]))
if username in [u'profile.php', u'people']:
username = None
full_name_result = user_link.xpath(constants.FBXpaths.user_full_name)
if len(full_name_result) > 0:
full_name = unicode(full_name_result[0])
return FBUser(fid, full_name, username)
def quit(self):
"""
:return: Close browser
"""
self.driver.quit()
self.driver = None
def _parse_payload_from_ajax_response(self, ajax_response, source=None):
"""
:param ajax_response: full response
:param source: what do we parse (page/friends etc)
:return: string of actual html response
"""
if self.debug:
print 'full response:', ajax_response
full_json_match = constants.FBRegexes.json_from_html.search(ajax_response) # Keep only json string
if not full_json_match:
return None
full_json = full_json_match.group('json')
if self.debug:
print 'json:', full_json
try:
json_dict = json.loads(full_json)
except Exception, e:
print 'Could not load json'
return None
try:
if source in ['friends', 'group_posts']:
return json_dict['payload']
elif source in ['group', 'mutual_friends']:
return json_dict['domops'][0][3]['__html']
elif source == 'group':
return json_dict
return json_dict['jsmods']['markup'][0][1]['__html']
except Exception:
try:
error = json['jsmods']['require'][1][3][0]['__html']
except Exception:
error = 'Couldnt parse from picture'
raise JSONParseError(error)
return None
def run(self):
raise NotImplementedError("You must implement this method")
def run_already_connected(self, *args, **kwargs):
return self.run(*args, **kwargs)
@staticmethod
def browser_needed(func):
def func_wrapper(self, *args, **kwargs):
if self.driver is None:
self.driver = webdriver.Chrome()
return func(self, *args, **kwargs)
return func_wrapper
class JSONParseError(Exception):
def __init__(self, message):
super(JSONParseError, self).__init__(message)
def _default_vs_new(default_val, new_val):
"""
:param default_val: Default value
:param new_val: New Value
:return: new_val if not null, default otherwise
"""
if new_val is not None:
return new_val
return default_val
def _stronger_value(original_value, new_value):
"""
:param original_value: original value
:param new_value: value to replace the original value
:return: new_value if it isnt null, original_value otherwise
"""
if new_value:
return new_value
return original_value
def blankify(str_txt, wanted_type=unicode):
"""
:param str_txt: Original text
:return: Blank string if str_txt is None
"""
if str_txt is None:
return wanted_type('')
return str_txt