-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
104 lines (74 loc) · 2.09 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
from flask import Flask, jsonify, request
from flask import render_template, redirect, url_for
from flask_wtf.csrf import CSRFProtect
from flask_bootstrap import Bootstrap
from Crawler.api import API
import os
def create_app():
csrf = CSRFProtect()
app = Flask(__name__)
csrf.init_app(app)
SECRET_KEY = os.urandom(32)
app.config['SECRET_KEY'] = SECRET_KEY
Bootstrap(app)
return app
app = create_app()
@app.route('/welcome', methods=('GET', 'POST'))
def welcome_page():
'''
Simple GUI for API
'''
from Forms.SearchForm import SearchForm
# Using meta to disable the appearing of csrf in url
search = SearchForm()
# Once form has been submitted
if search.validate_on_submit():
# TODO
# We need to parse search.data in order to create a proper and meaningful query
return search.data
return render_template('welcome.html', form=search)
@app.route('/')
def index():
'''
API Module
:return: json object
'''
# Query string filled with parameters
args = request.args
# If query string has been omitted we render welcome template
if len(args) == 0:
return redirect(url_for('welcome_page'))
# Initialize API
api = API()
respond = api.parse(args)
if respond:
return jsonify({
'response': {
'error': respond
}
}), 404
result, respond = api.fetch()
if respond:
return jsonify({
'response': {
'error': respond
}
}), 404
# Return json format
return jsonify({
'response': {
'object': {
'username': result['username'],
'posts': result['posts'],
'followers': result['followers'],
'following': result['following'],
'biography': result['biography'],
'post': result['post']
},
'settings': {
'method': result['method']
}
}
}), 200
if __name__ == "__main__":
app.run()