-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
130 lines (96 loc) · 3.65 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# coding: utf8
from flask import Flask, render_template, request, session, redirect
from instagram import client
import os
app = Flask('consumer-instagram')
app.secret_key = '\xcb\xf0\x83"\xbe\xeb\x85\xa6rm\x95\xfe\x10\x920\xef\xaf&rs\xf1\x11}P'
CONFIG = {
'client_id': 'CLIENT_ID',
'client_secret': 'CLIENT_SECRET',
'redirect_uri': 'http://localhost:5000/auth_callback'
}
auth = client.InstagramAPI(**CONFIG)
@app.route('/')
@app.route('/<username>')
def index(username=None):
if is_authorized():
try:
api = client.InstagramAPI(
access_token=session['access_token'],
client_secret=CONFIG['client_secret']
)
if username is None:
user_id = api.user().id
else:
user_id = api.user_search(username)[0].id
media = request_media(
api.user_recent_media, {'user_id': user_id})
except Exception as e:
return render_template('msg.html', msg=e)
text = request.values['text'] if 'text' in request.values else None
return render_template('index.html', media=media, text=text)
try:
url = auth.get_authorize_url(scope=["likes", "comments"])
return render_template('auth.html', url=url)
except Exception as e:
return render_template('msg.html', msg=e)
@app.route('/tag/<tag_name>')
def tag(tag_name):
if not is_authorized():
return redirect('/')
try:
api = client.InstagramAPI(
access_token=session['access_token'],
client_secret=CONFIG['client_secret']
)
media = request_media(api.tag_recent_media, {'tag_name': tag_name})
except Exception as e:
return render_template('msg.html', msg=e)
text = request.values['text'] if 'text' in request.values else None
return render_template('index.html', media=media, tag='#%s' % tag_name, text=text)
@app.route('/popular')
def popular():
if not is_authorized():
return redirect('/')
try:
api = client.InstagramAPI(
access_token=session['access_token'],
client_secret=CONFIG['client_secret']
)
media = api.media_popular(count=100)
except Exception as e:
return render_template('msg.html', msg=e)
text = request.values['text'] if 'text' in request.values else None
return render_template('index.html', media=media, text=text)
@app.route('/auth_callback', methods=['GET'])
def callback():
code = request.values['code']
if not code:
return render_template('msg.html', msg='Missing code')
try:
access_token, user_info = auth.exchange_code_for_access_token(code)
if not access_token:
return render_template('msg.html',
msg='Could not get access token')
api = client.InstagramAPI(
access_token, client_secret=CONFIG['client_secret'])
session['access_token'] = access_token
session['user_info'] = user_info
except Exception as e:
return render_template('msg.html', msg=e)
return redirect('/')
def request_media(method, params={}):
try:
media, next_ = method(count=100, **params)
while next_ and len(media) < 100:
more_medias, next_ = method(
with_next_url=next_, count=100, **params)
media.extend(more_medias)
except Exception as e:
return render_template('msg.html', msg=e)
return media
def is_authorized():
return False if 'access_token' not in session else True
if __name__ == '__main__':
port = int(os.environ.get("PORT", 5000))
app.run(host='0.0.0.0', port=port)