-
Notifications
You must be signed in to change notification settings - Fork 0
/
flask_app.py
112 lines (91 loc) · 3.18 KB
/
flask_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
import json
import requests
from config import config
from flask import Flask, render_template, request, redirect
from wtforms import Form, IntegerField, validators
app = Flask(__name__)
app.config.from_object(config['default'])
# Base addreses
base_api_url = 'https://api.stackexchange.com'
base_oauth_url = 'https://stackexchange.com/oauth'
base_oauth_token_url = base_oauth_url + '/access_token'
# Additional urls
get_users_info_url = '/2.2/users/{}?site=stackoverflow'
get_users_posts_url = '/2.2/users/{}/posts?site=stackoverflow'
get_personal_users_posts_url = '/2.2/me/posts?site=stackoverflow'
# List of Stackexchange application credentials
client_id = '11193'
redirect_uri = "http://localhost:5000"
client_secret = '35qF*sMB36Ju1JfNiL9VLw(('
key = 'vX7iutxvD0eY94F2ufmTMw(('
# Form for entering user id
class SearchPostsForm(Form):
user_id = IntegerField(
'User ID',
[validators.NumberRange(min=1, max=10000000)]
)
@app.route('/', methods=['GET', 'POST'])
def index():
"""
View that contains search posts form and form that redirect user to
"get_personal_posts" view.
Search Posts Form:
requires: user_id,
returns: list of user's posts
If Application Login Page on stackexchage returns code parameter
that this view will get list of logged user.
"""
form = SearchPostsForm(request.form)
posts = None
if request.method == 'GET':
code = request.args.get('code')
# If code parameter exists than application
# can get access token to get personal user's posts
if code:
# Get access_token
access_token = requests.post(
base_oauth_token_url,
data={
"client_id": client_id,
"client_secret": client_secret,
"code": code,
"redirect_uri": redirect_uri,
}
).content.decode('utf-8')
# Get personal posts
posts = requests.get(
''.join([
base_api_url,
get_personal_users_posts_url,
"&{}".format(access_token),
"&key={}".format(key),
])
).content
if request.method == 'POST' and form.validate():
# Get posts by user_id
posts = requests.get(
''.join([
base_api_url,
get_users_posts_url.format(form.user_id.data)
])
).content
if posts:
posts = json.loads(posts.decode('utf8'))['items']
return render_template('index.html', form=form, posts=posts)
@app.route('/get-personal-posts/', methods=['POST'])
def get_personal_posts():
"""
View that used for redirecting user to Application Login page
on stackexchange
"""
return redirect(
''.join([
base_oauth_url,
"?",
"client_id={}".format(client_id),
"&scope=no_expiry",
"&redirect_uri={}".format(redirect_uri),
])
)
if __name__ == '__main__':
app.run()