-
Notifications
You must be signed in to change notification settings - Fork 7
/
app.py
88 lines (61 loc) · 2.38 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
82
83
84
85
86
87
88
import os
import time
from instagram.client import InstagramAPI
from flask import Flask, request, render_template, session, redirect, abort, flash, jsonify
app = Flask(__name__) # create our flask app
app.secret_key = os.environ.get('SECRET_KEY')
# configure Instagram API
instaConfig = {
'client_id':os.environ.get('CLIENT_ID'),
'client_secret':os.environ.get('CLIENT_SECRET'),
'redirect_uri' : os.environ.get('REDIRECT_URI')
}
api = InstagramAPI(**instaConfig)
@app.route('/')
def user_photos():
# if instagram info is in session variables, then display user photos
if 'instagram_access_token' in session and 'instagram_user' in session:
userAPI = InstagramAPI(access_token=session['instagram_access_token'])
recent_media, next = userAPI.user_recent_media(user_id=session['instagram_user'].get('id'),count=25)
templateData = {
'size' : request.args.get('size','thumb'),
'media' : recent_media
}
return render_template('display.html', **templateData)
else:
return redirect('/connect')
# Redirect users to Instagram for login
@app.route('/connect')
def main():
url = api.get_authorize_url(scope=["likes","comments"])
return redirect(url)
# Instagram will redirect users back to this route after successfully logging in
@app.route('/instagram_callback')
def instagram_callback():
code = request.args.get('code')
if code:
access_token, user = api.exchange_code_for_access_token(code)
if not access_token:
return 'Could not get access token'
app.logger.debug('got an access token')
app.logger.debug(access_token)
# Sessions are used to keep this data
session['instagram_access_token'] = access_token
session['instagram_user'] = user
return redirect('/') # redirect back to main page
else:
return "Uhoh no code provided"
@app.errorhandler(404)
def page_not_found(error):
return render_template('404.html'), 404
# This is a jinja custom filter
@app.template_filter('strftime')
def _jinja2_filter_datetime(date, fmt=None):
pyDate = time.strptime(date,'%a %b %d %H:%M:%S +0000 %Y') # convert instagram date string into python date/time
return time.strftime('%Y-%m-%d %h:%M:%S', pyDate) # return the formatted date.
# --------- Server On ----------
# start the webserver
if __name__ == "__main__":
app.debug = True
port = int(os.environ.get('PORT', 5000)) # locally PORT 5000, Heroku will assign its own port
app.run(host='0.0.0.0', port=port)