-
Notifications
You must be signed in to change notification settings - Fork 5
/
fb_scrap.py
347 lines (302 loc) · 15.5 KB
/
fb_scrap.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
# -*- coding: utf-8 -*-
# Facebook Insights (OI:OPEN INSIGHTS)
# Data Science Team
# John Mohd Bhat
from datetime import datetime,timedelta
import os
import csv
import datetime
import shutil
import os.path
import itertools
import datetime
import ConfigParser
import csv, codecs
import re
import logging
import json
import urllib2
import urllib
import urlparse
import BaseHTTPServer
import webbrowser
import pandas as pd
from pandas.io.common import EmptyDataError
import sys
reload(sys)
sys.setdefaultencoding('utf8')
#from __future__ import unicode_literals
# commented metrics was not required in the project. Uncomment if you need any of below metric
insights_groups = {
"page-impressions": [
'page_impressions',
'page_impressions_unique',
#'page_impressions_paid',
#'page_impressions_paid_unique',
#'page_impressions_organic',
#'page_impressions_organic_unique',
#'page_impressions_viral',
#'page_impressions_viral_unique',
#'page_impressions_by_story_type',
#'page_impressions_by_story_type_unique',
'page_impressions_by_city_unique',
#'page_impressions_by_country_unique',
'page_impressions_by_age_gender_unique',
#'page_impressions_frequency_distribution',
#'page_impressions_viral_frequency_distribution',
#'page_impressions_by_paid_non_paid',
#'page_impressions_by_paid_non_paid_unique',
],
"page-engagement": [
'page_engaged_users',
'page_consumptions',
#'page_consumptions_unique',
#'page_consumptions_by_consumption_type',
#'page_consumptions_by_consumption_type_unique',
#'page_places_checkin_total',
#'page_places_checkin_total_unique',
#'page_places_checkin_mobile',
#'page_places_checkin_mobile_unique',
#'page_places_checkins_by_age_gender',
#'page_places_checkins_by_locale',
#'page_places_checkins_by_country',
'page_negative_feedback',
#'page_negative_feedback_unique',
'page_negative_feedback_by_type',
# 'page_negative_feedback_by_type_unique',
#'page_positive_feedback_by_type',
#'page_positive_feedback_by_type_unique',
'page_fans_online',
'page_fans_online_per_day',
],
"page-user-demographics": [
'page_fans',
#'page_fans_locale',
'page_fans_city',
#'page_fans_country',
'page_fans_gender_age',
'page_fan_adds',
# 'page_fan_adds_unique',
#'page_fans_by_like_source',
#'page_fans_by_like_source_unique',
'page_fan_removes',
#'page_fan_removes_unique',
#'page_fans_by_unlike_source_unique',
],
"page-views": [
'page_views_total',
#'page_views_unique',
# 'page_views_login',
#'page_views_login_unique',
#'page_views_logout',
'page_views_external_referrals',
],
"page-posts": [
'page_posts_impressions',
'page_posts_impressions_unique',
#'page_posts_impressions_paid',
#'page_posts_impressions_paid_unique',
'page_posts_impressions_organic',
#'page_posts_impressions_organic_unique',
#'page_posts_impressions_viral',
#'page_posts_impressions_viral_unique',
#'page_posts_impressions_frequency_distribution',
#'page_posts_impressions_by_paid_non_paid',
#'page_posts_impressions_by_paid_non_paid_unique',
],
"page-video-views": [
'page_video_views',
#Total number of times page's videos have been viewed for more than 3 seconds
'page_video_views_unique',
#Total number of times page's videos have been played for unique people for more than 3 seconds.
'page_video_complete_views_30s',
#Total number of times page's videos have been viewed for more than 30 seconds.
],
}
try:
ROOT = os.path.dirname(os.path.abspath(__file__))
except NameError: # We are the main py2exe script, not a module
import sys
ROOT = os.path.dirname(os.path.abspath(sys.argv[0]))
path = lambda x: os.path.join(
os.path.dirname(os.path.realpath(__file__)), x)
settings = ConfigParser.ConfigParser()
settings.read(path('insights.conf'))
APP_ID = '1960277150920761'
APP_SECRET = '0331e3b6f735c453e57e61a9ad7e210b'
ENDPOINT = 'graph.facebook.com'
REDIRECT_URI = 'http://localhost:8080/'
LOCAL_FILE = '.fb_access_token'
class FacebookGraphAPI(object):
def __init__(self):
self.page = settings.get('facebook', 'app_or_page')
self.token = settings.get('facebook', 'access_token')
self.api_url = "https://graph.facebook.com"
self.last_url = ''
def __getattr__(self, name):
if name.startswith('_'):
raise AttributeError(name)
else:
def caller(**params):
url = self.construct_api_url(name, params)
return self.api_request(url)
return caller
def construct_api_url(self, method_name, params):
"""
since parameter is taken as today because API gives data of today-1 day
so we get the data of yesterday
'since' should be a string in %Y-%m-%d format.
"""
#since = params.get('since', None)
today = datetime.date.today()
#dif=timedelta(days=4)
params['since'] = today
api_method = "/".join(method_name.split('__'))
params['access_token'] =self.token
url = "%s/%s/%s?%s" % (self.api_url,
self.page,
api_method,
urllib.urlencode(params))
print('Making request to "%s"' % (url))
return url
def api_request(self, url):
handle = urllib2.urlopen(url)
self.last_url = handle.geturl()
return json.load(handle)
def get_url(path, args=None):
args = args or {}
if ACCESS_TOKEN:
args['access_token'] = ACCESS_TOKEN
if 'access_token' in args or 'client_secret' in args:
endpoint = "https://"+ENDPOINT
else:
endpoint = "http://"+ENDPOINT
return endpoint+path+'?'+urllib.urlencode(args)
def get(path, args=None):
return urllib2.urlopen(get_url(path, args=args)).read()
try:
INTRO_MESSAGE = '''\
__
| | |
|__| |
'''
print INTRO_MESSAGE
try:
api = FacebookGraphAPI()
print('Connectd to facebook page or app.')
except:
api = None
print('Cant connect to facebook page or app')
print("ready")
insight_path = 'insights'
fullpath = os.path.join(settings.get('facebook', 'output_destination'), insight_path)
#delete all files from directory
try:
map( os.unlink, (os.path.join( fullpath,f) for f in os.listdir(fullpath)) )
except OSError :
pass
for name, value in settings.items('insights'):
for k, v in insights_groups.iteritems():
#print(k)
if (k == name) :
for metric in v:
filepath = os.path.join(fullpath, metric)
#create the paths
if not os.path.exists(fullpath):
print('Directory "%s" not exists. Creating it...' % (fullpath))
try:
os.makedirs(fullpath)
except OSError:
print('Error creating directory: "%s".' % (fullpath))
metric_name = 'insights__%s' % metric
#get insights data
insights = getattr(api, metric_name)()
#initialize the csv writer
csvname = '%s.csv' % (metric)
if not os.path.exists(os.path.join(fullpath,csvname)):
#print(csvname)
tsvh = csv.writer(open(os.path.join(fullpath, csvname), 'wb'))
header = ['date', 'period', 'metric_values']
tsvh.writerow(header)
#else:
#tsvh=open(os.path.join(filepath, csvname),'a')
for metri in insights['data']:
for row in metri['values']:
date = datetime.datetime.strptime(row['end_time'], '%Y-%m-%dT%H:%M:%S+0000').date()
try:
if type(row['value']) is dict and metri['period']=='day':
values=list(row['value'].values())
#values.append(date.strftime("%Y-%m-%d"))
headers=list(row['value'].keys())
#remove bad data in headers
for i in range (0,len(headers)-1):
headers[i]=re.sub(', ','_',headers[i])
headers[i]=re.sub(' ','',headers[i])
headers[i]= headers[i].encode('ascii','ignore')
headers[i]=re.sub('Ã','',headers[i])
headers[i]=re.sub('´','',headers[i])
headers[i]=re.sub("'",'',headers[i])
headers[i]=re.sub("-",'_',headers[i])
#print(headers)
Date=[date.strftime("%Y-%m-%d") for i in xrange(len(values)+1)]
#columns=['date','name_type','value']
with open(os.path.join(fullpath, csvname),'a') as f:
writer = csv.writer(f)
writer.writerow(header)
p=zip(Date,headers,values)
writer.writerows(p)
f.close()
out = None
##########################################################################################
# To get the metics with period lifetime #
##########################################################################################
elif type(row['value']) is dict and metri['period']=='lifetime':
values=list(row['value'].values())
#print(values)
headers=list(row['value'].keys())
#headers.append('date')
for i in range (0,len(headers)-1):
headers[i]=re.sub(', ','_',headers[i])
headers[i]=re.sub(' ','',headers[i])
headers[i]= headers[i].encode('ascii','ignore')
headers[i]=re.sub('Ã','',headers[i])
headers[i]=re.sub('´','',headers[i])
headers[i]=re.sub("'",'',headers[i])
headers[i]=re.sub("-",'_',headers[i])
Date=[date.strftime("%Y-%m-%d") for i in xrange(len(headers))]
#columns=['date','name','value']
with open(os.path.join(fullpath, csvname),'a') as f:
writer = csv.writer(f)
writer.writerow(header)
p=zip(Date,headers,values)
writer.writerows(p)
f.close()
out = None
else:
if metri['period']=='day' or metric in ['page_fans','post_negative_feedback','page_views_total','page_fans_city','page_fans_gender_age','post_fan_reach','post_engaged_users','post_impressions','post_impressions_unique','post_consumptions_by_type','post_engaged_fan','page_impressions_by_city'] :
out = [date, metri['period'], row['value']]
else :
continue
except KeyError :
if metric=='page_views_total':
out = [date, metri['period'], 0]
tsvh=csv.writer(open(os.path.join(fullpath,csvname),'a'))
tsvh.writerow(out)
out = None
else:
print(metric)
out = [date, metri['period'], 0]
tsvh=csv.writer(open(os.path.join(fullpath,csvname),'a'))
tsvh.writerow(out)
tsvh.writerow(out)
tsvh.writerow(out)
tsvh.writerow(out)
out = None
#print (out)
if out!=None:
tsvh=csv.writer(open(os.path.join(fullpath,csvname),'a'))
tsvh.writerow(out)
out=None
print('Process Finished Correctly!')
except Exception, e:
print e