-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
178 lines (148 loc) · 5.84 KB
/
main.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
from flask import Flask, request
from flask_cors import CORS
from bread.twitter_data_fetch import get_profile_tweets
from bread.user_fetch import get_verify_user, get_user_from_username, get_annotation_request
from bread.sticks_fetch import get_loaf_names, get_sticks_of_loaf, get_stick, get_like_stick
from bread.trending_fetch import get_trending
from bread.sticks_update import update_stick_with_annotation
from multiprocessing import Process
import json
import random
app = Flask(__name__)
cors = CORS(app)
app.config['CORS_HEADERS'] = 'Content-Type'
GET = 'GET'
POST = 'POST'
json_mime = 'application/json'
@app.route('/')
def hello():
return {'data': 'Welcome to Bread'}
@app.route('/trending', methods=[GET])
def get_trending_sticks():
if request.method == GET:
sticks = get_trending()
resp = {'sticks': sticks}
return app.response_class(response=json.dumps(resp), status=200, mimetype=json_mime)
@app.route('/annotation-request/<username>/<loaf>', methods=[GET])
def perform_annotation(username, loaf):
if request.method == GET:
anno = get_annotation_request(username, loaf)
if anno['annotation']:
return app.response_class(response=json.dumps(anno), status=200, mimetype=json_mime)
else:
response = {'annotation': False}
return app.response_class(response=json.dumps(response), status=200, mimetype=json_mime)
@app.route('/user-annotation/<username>/<stick_id>', methods=[GET])
def user_annotation(username, stick_id):
if request.method == GET:
stick = get_stick(stick_id)
user = get_user_from_username(username)
myid = user['id']
for i in stick['annotation']['annotations']:
if list(i.keys())[0] == str(myid):
return app.response_class(response=json.dumps({'score': float(i[str(myid)])}), status=200,
mimetype=json_mime)
return app.response_class(response=json.dumps({'score': None}), status=200, mimetype=json_mime)
@app.route('/verify-user/<username>', methods=[GET])
def verify_user(username):
if request.method == GET:
print('here')
username = get_verify_user(username)
if username is None:
resp = {'exists': False}
else:
user = get_user_from_username(username)
resp = {'exists': True, 'profile_image_url_https': user['profile_image_url_https']}
return app.response_class(response=json.dumps(resp), status=200, mimetype=json_mime)
@app.route('/user/<username>', methods=[GET])
def get_user(username):
if request.method == GET:
user = get_user_from_username(username)
resp = {'user': user}
return app.response_class(response=json.dumps(resp), status=200, mimetype=json_mime)
@app.route('/stick/<stick_id>', methods=[GET])
def get_my_stick(stick_id):
if request.method == GET:
stick = get_stick(stick_id)
resp = {'stick': stick}
return app.response_class(response=json.dumps(resp), status=200, mimetype=json_mime)
@app.route('/get-loafs', methods=[GET])
def get_loafs():
if request.method == GET:
loaf_names = get_loaf_names()
resp = {'loafs': loaf_names}
return app.response_class(response=json.dumps(resp), status=200, mimetype=json_mime)
@app.route('/loafs/<loaf>', methods=[GET])
def get_sticks_of_a_loaf(loaf):
if request.method == GET:
sticks = get_sticks_of_loaf(loaf)
random.shuffle(sticks)
sticks = sticks[:100]
resp = {'sticks': sticks,
'loaf': loaf}
return app.response_class(response=json.dumps(resp), status=200, mimetype=json_mime)
@app.route('/annotate', methods=[POST])
def annotate_stick():
if request.method == POST:
data = request.args
username = data['username']
stick_id = int(data['stick_id'])
score = float(data['score'])
process = Process(
target=update_stick_with_annotation,
args=(username, stick_id, score),
daemon=True
)
process.start()
resp = {'update': True,
'username': username,
'stick_id': stick_id}
print(resp)
return app.response_class(response=json.dumps(resp), status=200, mimetype=json_mime)
@app.route('/stick/like', methods=[POST])
def like_stick():
if request.method == POST:
data = request.args
stick_id = data['stick_id']
process = Process(
target=get_like_stick,
args=(stick_id, True),
daemon=True
)
process.start()
resp = {'update': True,
'stick_id': stick_id}
return app.response_class(response=json.dumps(resp), status=200, mimetype=json_mime)
@app.route('/stick/dislike', methods=[POST])
def dislike_stick():
if request.method == POST:
data = request.args
stick_id = data['stick_id']
process = Process(
target=get_like_stick,
args=(stick_id, False),
daemon=True
)
process.start()
resp = {'update': True,
'stick_id': stick_id}
return app.response_class(response=json.dumps(resp), status=200, mimetype=json_mime)
@app.route('/profile-tweets', methods=[POST])
def profile_tweets():
if request.method == POST:
data = request.args
username = data['username']
if 'offset' in data:
offset = data['offset']
else:
offset = 0
if 'limit' in data:
limit = data['limit']
else:
limit = 5
tweets = get_profile_tweets(username, limit, offset)
resp = {'tweets': tweets,
'username': username}
return app.response_class(response=json.dumps(resp), status=200, mimetype=json_mime)
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True, threaded=True)