-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyfacebook.py
66 lines (57 loc) · 1.79 KB
/
myfacebook.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
__author__ = 'jpatdalton'
import urllib2
import json
import facebook
import requests
import columns
import db_setup
APP_SECRET = ''
APP_ID = ''
client_token = ''
access_token = ''
#access_token = ''
def get_page_data(page_id):
api_endpoint = "https://graph.facebook.com"
fb_graph_url = api_endpoint+"/"+page_id + '?&access_token='+ access_token
try:
api_request = urllib2.Request(fb_graph_url)
api_response = urllib2.urlopen(api_request)
try:
return json.loads(api_response.read())
except Exception, e:
print 'JSON error ', e
return "JSON error"
except Exception, e:
print 'not JSON error ', e
if hasattr(e, 'code'):
return e.code
elif hasattr(e, 'reason'):
return e.reason
def get_page_id(name):
the_id = 'None'
try:
api_endpoint = "https://graph.facebook.com/search?q=" + name + "&type=page" + '&access_token='+ access_token
data = requests.get(api_endpoint).json()["data"]
the_id = data[0]["id"]
except Exception, e:
print 'Cant get Facebook Id for artist: ' + name
return the_id
def get_fb_likes(page_id):
the_likes=0
try:
data = get_page_data(page_id)
the_likes = str(data['likes'])
except Exception, e:
print e, 'Error in get_fb_likes'
return the_likes
def get_likes(artists, worksheet, end):
col = columns.fb_likes
cell_list_likes = worksheet.range(col+'2:'+col+end)
for n in xrange(len(artists)):
try:
page_id = get_page_id(artists[n])
data = get_page_data(page_id)
cell_list_likes[n].value = str(data['likes'])
except Exception, e:
print e, ' Facebook error ', artists[n]
worksheet.update_cells(cell_list_likes)