-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
81 lines (75 loc) · 2.89 KB
/
app.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
from chalice import Chalice
# from requests import async
import requests
import grequests
import os
import json
import boto3
from base64 import b64decode
FB_BASE_URL = 'https://graph.facebook.com/v2.8'
app = Chalice(app_name='social-media-scraper')
USER_ID = 'AnthonyBourdain'
@app.route('/fb/photos', cors=True)
def get_facebook_photos():
try:
encrypted_fb_id = os.environ['APP_ID']
decrypted_fb_id = boto3.client('kms').decrypt(CiphertextBlob=b64decode(encrypted_fb_id))['Plaintext']
encrypted_fb_secret = os.environ['APP_SECRET']
decrypted_fb_secret = boto3.client('kms').decrypt(CiphertextBlob=b64decode(encrypted_fb_secret))['Plaintext']
except KeyError as e:
import ConfigParser
config_parser = ConfigParser.ConfigParser()
config_parser.readfp(open('.config'))
decrypted_fb_id = config_parser.get('fb', 'id')
decrypted_fb_secret = config_parser.get('fb', 'secret')
payload = {
'client_id': decrypted_fb_id,
'client_secret': decrypted_fb_secret,
'grant_type': 'client_credentials'
}
response = requests.get(
'https://graph.facebook.com/oauth/access_token',
params=payload
)
response_dict = response.json()
fb_access_token = response_dict['access_token']
try:
recent_fb_photos = get_recent_fb_photos(fb_access_token)
except Exception as e:
raise Exception('Error retrieving photos')
return {'photos': recent_fb_photos}
def get_recent_fb_photos(fb_access_token):
photos = []
payload = {
'type': 'uploaded',
'access_token': fb_access_token,
'fields': 'place',
'limit': 1000
}
try:
response = requests.get(
'{}/{}/photos'.format(FB_BASE_URL, USER_ID),
params=payload
)
response_dict = response.json()
while True:
for photo in response_dict['data']:
if 'place' in photo.keys():
if 'location' in photo['place'].keys():
photos.append({
'caption': photo['place']['name'],
'location': photo['place']['location'],
'image_url': '{}/{}?access_token={}&fields=images'.format(FB_BASE_URL, photo['id'], fb_access_token)
})
if not (len(photos) < 20 and 'next' in response_dict['paging']):
break
response = requests.get(response_dict['paging']['next'])
response_dict = response.json()
requests_to_send = (grequests.get(photo['image_url']) for photo in photos)
results_array = grequests.map(requests_to_send)
for i in range(len(results_array)):
response_dict = results_array[i].json()
photos[i]['image_url'] = response_dict['images'][0]['source']
except Exception as e:
raise
return photos