-
Notifications
You must be signed in to change notification settings - Fork 1
/
fbget.py
executable file
·105 lines (85 loc) · 2.75 KB
/
fbget.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
#!/usr/bin/env python3
import requests
import json
import datetime
import sys
def get_profile_picture(fbkey, userid):
r = requests.get('https://graph.facebook.com/v2.6/'+str(userid)+'/picture',params={'access_token':fbkey,'redirect':'false'})
data = json.loads(r.text)
if 'data' in data:
if 'url' in data['data']:
return data['data']['url']
def get_topage(fbkey,needed_fb_fields=None):
if needed_fb_fields is []:
needed_fb_fields = None
request_url = 'https://graph.facebook.com/v2.6/587747891351295/tagged'
if needed_fb_fields is not None:
request_url += '?fields='+ ','.join(needed_fb_fields)
r = requests.get(request_url,params={'access_token':fbkey})
posts = json.loads(r.text)
if 'error' in posts:
print ('Error in getting posts to page')
return []
else:
return posts['data']
def get_frompage(fbkey,needed_fb_fields=None):
if needed_fb_fields is []:
needed_fb_fields = None
request_url = 'https://graph.facebook.com/v2.6/587747891351295/posts'
if needed_fb_fields is not None:
request_url += '?fields='+ ','.join(needed_fb_fields)
r = requests.get(request_url,params={'access_token':fbkey})
posts = json.loads(r.text)
if 'error' in posts:
print ('Error in getting posts from page')
return []
else:
return posts['data']
def elab(fbkey,post):
out = {}
if 'created_time' in post:
out['time'] = datetime.datetime.strptime(post['created_time'],'%Y-%m-%dT%H:%M:%S+0000')
else:
return None
if 'message' in post:
out['text'] = post['message']
else:
out['text'] = ""
if 'from' in post:
out['author_name'] = post['from']['name']
try:
out['author_pic'] = get_profile_picture(fbkey,post['from']['id'])
except:
out['author_pic'] = None
else:
return None
if 'full_picture' in post:
out['image'] = post['full_picture']
else:
out['image'] = None
if out['text'] is '' and out['image'] is None:
return None
return out
def get(fbkey):
posts = []
needed_fb_fields = ["created_time","message","from","full_picture"]
toposts = get_topage(fbkey,needed_fb_fields)
fromposts = get_frompage(fbkey,needed_fb_fields)
toposts = toposts[:7]
fromposts = fromposts[:7]
posts += toposts
posts += fromposts
out = []
for post in posts:
temp = elab(fbkey,post)
if temp is not None:
out.append(temp)
return out
def main():
fbkey = sys.argv[1]
posts = get(fbkey)
sorted_posts = sorted(posts,key = lambda post: post['time'])
sorted_posts.reverse()
print(sorted_posts)
if __name__ == "__main__":
main()