This repository was archived by the owner on Mar 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfb_pull.py
More file actions
66 lines (58 loc) · 2.27 KB
/
Copy pathfb_pull.py
File metadata and controls
66 lines (58 loc) · 2.27 KB
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
import requests, json, piexif, os, logging
from configparser import ConfigParser
from datetime import datetime
logging.basicConfig(filename='fb_pull.log',level=logging.DEBUG)
config = ConfigParser()
config.read('config.ini')
BASE_URI = "https://graph.facebook.com/v10.0/" # Graph API version 10.0
ACCESS_TOKEN = config['DEFAULT']['access_token']
def get_request_uri(api_call):
return BASE_URI + api_call + "&access_token=" + ACCESS_TOKEN
def setup_dir():
r = requests.get(get_request_uri("me?fields=name"))
j = json.loads(r.text)
fb_name = j['name']
dirname = "photos of " + fb_name
try:
os.mkdir(dirname)
os.chdir(dirname)
except FileExistsError:
pass
except OSError:
print ("Creation of the directory %s failed" % fb_name)
return 1
return 0
def get_photos():
r = requests.get(get_request_uri("me/photos?type=tagged&fields=images,backdated_time,created_time,from,name,name_tags,place")) #adding album eliminates photos?
j = json.loads(r.text)
photos = j['data']
nphotos = 0
while True:
logging.info(photos)
for photo in photos:
photo_uri = photo['images'][0]['source'] # assumes first image in 'images' is the highest res
time = photo['created_time'] if 'backdated_time' not in photo else photo['backdated_time']
time = datetime.strptime(time, '%Y-%m-%dT%H:%M:%S%z').strftime('%Y%m%d%H%M%S')
photographer = photo['from']['name']
description = False if 'name' not in photo else photo['name']
try:
os.mkdir(photographer)
except FileExistsError:
pass
except OSError:
print ("Creation of the directory {} failed".format(album_dir))
return -1
with open(photographer + '/' + time, 'wb') as f:
r = requests.get(photo_uri)
f.write(r.content)
nphotos += 1
try:
request_uri = j['paging']['next']
except KeyError:
return nphotos
r = requests.get(request_uri)
logging.info("getting 'next': " + str(r))
j = json.loads(r.text)
photos = j['data']
if __name__ == "__main__":
print("dumped {} photos".format(get_photos()))