forked from facebookarchive/python-instagram
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample_app.py
64 lines (54 loc) · 1.9 KB
/
sample_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
import bottle
from bottle import route, post, run, request
from instagram import client, subscriptions
bottle.debug(True)
CONFIG = {
'client_id': '',
'client_secret': '',
'redirect_uri': 'http://localhost:8515/oauth_callback'
}
unauthenticated_api = client.InstagramAPI(**CONFIG)
def process_tag_update(update):
print update
reactor = subscriptions.SubscriptionsReactor()
reactor.register_callback(subscriptions.SubscriptionType.TAG, process_tag_update)
@route('/')
def home():
try:
url = unauthenticated_api.get_authorize_url(scope=["likes","comments"])
return '<a href="%s">Connect with Instagram</a>' % url
except Exception, e:
print e
@route('/oauth_callback')
def on_callback():
code = request.GET.get("code")
if not code:
return 'Missing code'
try:
access_token = unauthenticated_api.exchange_code_for_access_token(code)
if not access_token:
return 'Could not get access token'
api = client.InstagramAPI(access_token=access_token)
recent_media, next = api.user_recent_media()
photos = []
for media in recent_media:
photos.append('<img src="%s"/>' % media.images['thumbnail'].url)
return ''.join(photos)
except Exception, e:
print e
@route('/realtime_callback')
@post('/realtime_callback')
def on_realtime_callback():
mode = request.GET.get("hub.mode")
challenge = request.GET.get("hub.challenge")
verify_token = request.GET.get("hub.verify_token")
if challenge:
return challenge
else:
x_hub_signature = request.header.get('X-Hub-Signature')
raw_response = request.body.read()
try:
reactor.process(CONFIG['client_secret'], raw_response, x_hub_signature)
except subscriptions.SubscriptionVerifyError:
print "Signature mismatch"
run(host='localhost', port=8515, reloader=True)