-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfetch_pictures_from_facebook.py
93 lines (63 loc) · 2.55 KB
/
fetch_pictures_from_facebook.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
import json
import os
import urllib.request
################################################################################
def fetch_data(url, api_key):
request = urllib.request.Request(url)
# standard authorization for FB
request.add_header('Authorization', 'Bearer {}'.format(api_key))
response = urllib.request.urlopen(request)
encoding = response.headers.get_content_charset()
data = json.loads(response.read().decode(encoding))
return data
################################################################################
def get_next_from_data(data):
# check for paging key in the data
if 'paging' in data:
paging = data['paging']
if 'next' in paging:
return paging['next']
else:
return None
else:
return None
################################################################################
def iterate_over_images(data, pic_number):
for data_object in data:
pic_url = data_object['source']
f = open('{}.jpg'.format(pic_number), 'wb')
f.write(urllib.request.urlopen(pic_url).read())
f.close()
pic_number = pic_number + 1
################################################################################
if __name__ == '__main__':
# TODO for now support for 2.3 but upgrade to 2.9
# TODO require extra efforts as parsing will be on the basis of ID only
# TODO direct image url is not available in v2.9 api set
base_url = "https://graph.facebook.com/v2.3/"
facebook_access_token = 'EAACEdEose0cBADBgjcN1sD3zZCfx36J7ZCtjL2EYQrboaPc2RTQOr53kpkTee2DTu6RdLxThgdPG9i0g4lVgnp8AlS1afF9gip8Hsxrlx1peqDZBNnq7rRd5xDYr59VjinWVoUR6g3M8ueSZADRJfdb1vUcQZCCCjywgiSR1vJwhERQzjm5ZCrdUNXxAxYTOwZD'
if not os.path.exists("data/user_pics"):
os.makedirs("data/user_pics")
os.chdir("data/user_pics")
data = fetch_data(base_url+"me", facebook_access_token)
data = fetch_data(base_url+"me/photos", facebook_access_token)
next_ = get_next_from_data(data)
data = data['data']
pic_num = 0
iterate_over_images(data, pic_num)
pic_num += len(data)
print(len(data))
if next_ is None:
is_more_photos = False
else:
is_more_photos = True
while(is_more_photos):
data = fetch_data(next_, facebook_access_token)
next_ = get_next_from_data(data)
data = data['data']
iterate_over_images(data, pic_num)
pic_num += len(data)
print(len(data))
if next_ is None:
is_more_photos=False
print("Completed")