forked from alchem9st/scrap-preprocessor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfbGraph.py
520 lines (461 loc) · 18.3 KB
/
fbGraph.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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
# -*- coding: utf-8 -*-
from facepy import GraphAPI
import logging
import sys
import glob,csv,json
from random import randint
import parseFBWorkHours
KEYS_FB=['1812886018965623|a43d798be9fba8e94d3f4f98d0333b62', #Anon
'1327435673986191|8bfc29c57479473a77257f75377d88f3', #Anon
'1040517959329660|c7e458dd968fca33d05a18fddbcd86ab', #Rohit
'1697721727215546|a372f9c8b412e8b053094042fc4b42e6', #Shantanu
]# format is AppID|AppSecret, API version: 2.7
def UTF8(data):
try:
return data.encode('utf-8','ignore')
except:
return data
# SAFE DECODING ENCODING
def safe_dec_enc(data,basic=False):
if data:
if isinstance(data, unicode):
if basic:
return data.encode('ascii','ignore')
return data.encode('utf-8','ignore')
else:
if basic:
# REMOVE NON-STANDARD UNICODE-POINTS FROM BYTE-STRING
return data.decode('ascii','ignore').encode('ascii')
return data.decode('utf-8','ignore').encode('utf-8')
return ''
class processGraph:
def __init__(self, key=None):
self.key_index = 0
self.viewFactor=0
"""
>>> Graph = processGraph()
You May Initialise Facebook GraphAPI with your own AppID and AppSecret
>>> Graph = processGraph("<<App_ID>>|<<App_Secret>>")
"""
if not key:
while True:
self.graph = GraphAPI(KEYS_FB[self.key_index])
try:
self.graph.search("test","place")
break
except:
self.key_index = (self.key_index+1)%len(KEYS_FB)
else:
self.graph = GraphAPI(key)
# FOR STATE DATA TO BE USED BY Graph API
self.state_data_rows = []
file_name = glob.glob('./state_data/city_state.csv')
state_file = open(file_name[0],'r')
state_reader = csv.DictReader(state_file, dialect=csv.excel)
self.state_data_rows.extend(state_reader)
state_file.close()
def graceful_request(self, url):
while True:
try:
result = self.graph.get(url)
return result
except:
print 'ERROR. CHANGING KEY from index', self.key_index,
self.key_index = (self.key_index+1)%len(KEYS_FB)
print 'to', self.key_index
self.graph = GraphAPI(KEYS_FB[self.key_index])
def number_parser(self,x):
flag_add = False
numerals = ['0','1','2','3','4','5','6','7','8','9']
allowed_start_symbols = numerals + ['+']
############
#INITIAL CLEANUP
x = x.strip()
idx=0
for _ in x:
if _ in allowed_start_symbols:
break
idx += 1
x = x[idx:]
#############
if x.find('+91') == 0 or x.find('91 ') == 0:
flag_add = True
word = ''
phone_number = []
if flag_add:
word = list(x[3:])
else:
word = list(x)
non_zero_encountered = False
for letter in word:
# REMOVES 0 FROM START OF NUMBERS
if not non_zero_encountered:
if letter in numerals[1:]:
non_zero_encountered = True
if non_zero_encountered:
if letter in numerals:
phone_number.append(letter)
return ''.join(phone_number)
def website_parser(self,x):
if not x:
return ''
############
#INITIAL CLEANUP
x = x.strip()
x = x.replace('//www.','//')
#############
filler_flag = False
fillers = ['/','#']
for _ in fillers:
if _ in x[-1]:
filler_flag = True
if filler_flag:
x = x[:-1]
return x
def match_website(self,website,resp):
if website == self.website_parser(resp):
return True
return False
def match_phone_nos(self,phones,resp):
# DECREASING SEPARATOR PRIORITY ORDER
separators = [',', '/', ';', '&']
resp_nos = []
sep_found = False
for separator in separators:
if resp.find(separator) != -1:
resp_nos.extend(resp.split(separator))
sep_found = True
break
if not sep_found:
resp_nos.append(resp)
for i in range(len(resp_nos)):
resp_nos[i] = safe_dec_enc(resp_nos[i])
for x in resp_nos:
if self.number_parser(x) in phones:
return True
return False
def analyze_prediction(self,row,query,allow_website_match):
pin= row['Pincode']
phones = []
websites = []
emails = []
for i in range(1,6):
if row['Phone'+str(i)]:
phones.append(self.number_parser(row['Phone'+str(i)]))
if row['Website']:
websites.append(row['Website'].strip())
if row['Website2']:
websites.append(row['Website2'].strip())
if row['Mail']:
emails.append(row['Mail'].strip())
if row['Mail2']:
emails.append(row['Mail2'].strip())
search_result = self.graceful_request("search?q=%s&fields=location,phone,emails,website&type=place&limit=50"%(query))
for place in search_result['data']:
if 'location' in place:
if 'zip' in place['location'] :
if unicode(pin) == unicode(place['location']['zip']) and unicode(pin):
node = self.graceful_request(place['id']+"?fields=name,location,is_verified,description,phone,link,cover,website,emails")
return node
if 'phone' in place and phones:
if self.match_phone_nos(phones, safe_dec_enc(place['phone'])):
node = self.graceful_request(place['id']+"?fields=name,location,is_verified,description,phone,link,cover,website,emails")
return node
for email in emails:
if 'emails' in place and email:
for x in place['emails']:
if email == safe_dec_enc(x):
node = self.graceful_request(place['id']+"?fields=name,location,is_verified,description,phone,link,cover,website,emails")
return node
# WEBSITE MATCH IS NOT SAFE. HENCE SHOULD BE DONE ONLY IF ABOVE MEASURES FAILS.
if allow_website_match:
for website in websites:
website = self.website_parser(website)
match=False
multiple_match=False
correct_place_id=''
for place in search_result['data']:
if 'website' in place and website:
if self.match_website(website, safe_dec_enc(place['website'])):
if not match:
correct_place_id=place['id']
match=True
else:
multiple_match=True
break
if match and not multiple_match:
node = self.graceful_request(correct_place_id+"?fields=name,location,is_verified,description,phone,link,cover,website,emails")
return node
return dict()
def searchPlace(self,row,state):
################
row['Name'] = safe_dec_enc(row['Name'],True)
row['Locality'] = safe_dec_enc(row['Locality'],True)
row['City'] = safe_dec_enc(row['City'],True)
################
self.viewFactor=0
node = None
if row['Locality']:
query = row['Name'] + ', ' + row['Locality']
node=self.analyze_prediction(row,query,True)
if not node and row['City']:
query = row['Name'] + ', ' + row['City']
node=self.analyze_prediction(row,query,True)
if not node:
query = row['Name'] + ', ' + state
node=self.analyze_prediction(row,query,True)
if not node:
query = row['Name']
node = self.analyze_prediction(row,query,False)
return node
def _repairDetails(self,row,node):
if 'description' in node and not row['Details']:
row['Details'] = node['description']
#print "Added description "+node['description'][:40]+" to "+row["Name"]+" from facebook"
self.viewFactor+=1
return 1
return 0
def _repairWebsite(self,row,node):
if not row['Website']:
if 'website' in node:
row['Website'] = node['website']
#print "Added website "+node['website']+" to "+row["Name"]+" from facebook"
return 1
return 0
def _repairPin(self,row,node):
if 'location' in node:
if not row['Pincode'] and 'zip' in node['location'] :
row['Pincode'] = node['location']['zip']
#print "Added pin "+node['location']['zip']+" to "+row["Name"]+" from facebook"
return 1
return 0
def _repairStreet(self,row,node):
if 'location' in node:
if not row['Street Address'] and 'street' in node['location']:
row['Street Address'] = node['location']['street']
#print "Added address "+node['location']['street']+" to "+row["Name"]+" from facebook"
return 1
return 0
def _addPage(self,row,node):
if 'link' in node:
row['fb_page']= node['link']
#print "Added page "+node['link']+" to "+row["Name"]+" from facebook"
self.viewFactor+=1
return 1
return 0
def _isVerified(self,row,node):
if 'is_verified' in node:
if node['is_verified']:
row['fb_verified']= 'True'
self.viewFactor+=2
return 1
row['fb_verified']= 'False'
return 0
def _addCover(self,row,node):
if 'cover' in node:
row['Images URL'] = node['cover']['source']+","+row['Images URL']
#print "Added cover "+node['cover']['source']+" to "+row["Name"]+" from facebook"
self.viewFactor+=1
return 1
return 0
def _addEmails(self,row,node):
check = 0
if 'emails' in node:
for i in node['emails']:
if row['Mail'] and i.encode('utf-8','ignore').strip() not in row['Mail'].strip():
row['Mail2'] = i
return check+1
row['Mail'] = i
check = 1
return check
def _addPhone(self,row,node):
if 'phone' in node:
ph = map(UTF8,node['phone'].split(','))
for i in range(1,6):
if not row['Phone'+str(i)]:
break
for j,p in zip(range(i+1,6),ph):
row['Phone'+str(j)] = p.strip()
#print "Added phone "+p.strip()+" in "+'Phone'+str(j)+" from facebook"+str(node['location'])
return 1
return 0
def _addPicture(self,row,node):
if not 'id' in node:
return 0
profile_pic = self.graceful_request(node['id']+"/picture?height=500&width=500&redirect")
if 'data' in profile_pic:
if 'url' in profile_pic['data'] and 'is_silhouette' in profile_pic['data']:
if not profile_pic['data']['is_silhouette']:
row['Images URL'] = profile_pic['data']['url'] + "," + row['Images URL']
self.viewFactor+=2
return 1
return 0
def processSelective(self,rows,selection):
"""
Available Selections are:
_repairDetails
_repairWebsite
_repairPin
_repairStreet
_addPage
_addCover
_addPicture
_addPhone
_addEmail
e.g.
>>> Graph = processGraph()
>>> Graph.processSelective(CSV_Dictionary,'_repairDetails')
"""
stat=0
if selection in dir(self):
method = getattr(self,selection)
for row in rows:
try:
node = self.searchPlace(row)
stat+=method(row,node)
except:
logging.exception("Error loading %s from facebook for %s"%(selection,row['Name']))
print("New Info Added from Facebook\n%s:%d"%(stat));
def _addViews(self,row):
if row['Total Views']:
row['Total Views']+=self.viewFactor*randint(100,200)
else:
row['Total Views']=self.viewFactor*randint(100,200)
def _nodePhotos(self,row,node):
if 'id' not in node:
return
photos = []
after = ''
while True:
resp = self.graceful_request(node['id']+'/photos?type=uploaded&fields=source&limit=10&after=%s'%after)
if 'data' in resp:
for i in resp['data']:
photos.append(i['source'])
if 'paging' in resp:
after = resp['paging']['cursors']['after']
if 'next' not in resp['paging']:
break
else:
break
# TO GUARANTEE QUICK TERMINATION
if len(photos) >= 10:
break
row_data = ''
for photo in photos:
if row_data:
row_data += ','+ photo
else:
row_data = photo
row['fb_photos'] = row_data
def _nodeVideos(self,row,node):
if 'id' not in node:
return
videos = []
after = ''
while True:
resp = self.graceful_request(node['id']+'/videos?type=uploaded&fields=source,title,description&limit=10&after=%s'%after)
if 'data' in resp:
for i in resp['data']:
videos.append(i)
if 'paging' in resp:
after = resp['paging']['cursors']['after']
if 'next' not in resp['paging']:
break
else:
break
# TO GUARANTEE QUICK TERMINATION
if len(videos) >= 10:
break
row_data = ''
for video in videos:
x = ''
if 'title' in video:
x = '{"title":"%s","link":"%s"}'%(video['title'].encode('ascii','ignore').replace('"',''),video['source'])
elif 'description' in video:
x = '{"title":"%s","link":"%s"}'%(video['description'].encode('ascii','ignore').replace('"',''),video['source'])
else:
x = '{"title":"%s","link":"%s"}'%('',video['source'])
if row_data:
row_data += ','+ x
else:
row_data = x
row['fb_videos'] = row_data
def _nodeWorkingHours(self,row,node):
if 'id' not in node:
return
resp = self.graceful_request(node['id']+'?fields=hours')
row_data = ''
if 'hours' in resp:
try:
row_data = parseFBWorkHours.parse(resp['hours'])
except:
logging.exception("Error parsing Working Hours for" + row['Name'])
row['fb_workingHours'] = row_data
def _nodePosts(self,row,node):
if 'id' not in node:
return
posts = []
after = ''
while True:
resp = self.graceful_request(node['id']+'/posts?fields=message,type,created_time&limit=90&next=%s'%after)
if 'data' in resp:
for i in resp['data']:
if i['type'] == 'status' and 'message' in i:
posts.append(i)
if 'paging' in resp:
after = resp['paging']['next']
else:
break
# TO GUARANTEE QUICK TERMINATION
if len(posts) >= 10:
break
row_data = ''
for post in posts:
x = '{"created_time":"%s","message":"%s"}'%(post['created_time'],post['message'].encode('ascii','ignore').replace('"',''))
if row_data:
row_data += ','+ x
else:
row_data = x
row['fb_posts'] = row_data
def _mergeData(self,row):
# Merge Photos
if row['fb_photos']:
if row['Images URL']:
row['Images URL'] = row['fb_photos'] + ',' + row['Images URL']
else:
row['Images URL'] = row['fb_photos']
if not row['Working Hours'] and row['fb_workingHours']:
row['Working Hours'] = row['fb_workingHours']
def processAll(self,rows,state):
details,link,cover,website,pincode,street,dp,verified,phone,email=0,0,0,0,0,0,0,0,0,0 #stats
total = len(rows)
print("\nFetching info from FB Graph")
print 'STATE : ',state
for progress,row in enumerate(rows):
try:
node = self.searchPlace(row,state)
details += self._repairDetails(row,node)
website += self._repairWebsite(row,node)
pincode += self._repairPin(row,node)
street += self._repairStreet(row,node)
link += self._addPage(row,node)
phone += self._addPhone(row,node)
email += self._addEmails(row,node)
verified += self._isVerified(row,node)
self._addViews(row)
#self._nodePosts(row,node)
self._nodeVideos(row,node)
self._nodePhotos(row,node)
self._nodeWorkingHours(row,node)
self._mergeData(row)
# ENSURES COVER/DP AS THE FIRST PICTURE
cover += self._addCover(row,node)
dp += self._addPicture(row,node)
## pro=int((float(progress)/total)*100) # Comment out to avoid Bad characters in logs
## sys.stdout.write("\r%d%%"%pro)
## sys.stdout.flush()
except:
logging.exception("Error loading information from facebook for " + row['Name'])
## sys.stdout.write("\r100%")
## sys.stdout.flush()
print("\nNew Info Added from Facebook\nDetails:%d Facebook Link:%d Cover:%d \nWebsite:%d Pincode:%d Address:%d Images:%d Verified %d/%d Phone:%d Emails:%d"%(details,link,cover,website,pincode,street,dp,verified,link,phone,email));