-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
272 lines (217 loc) · 7.58 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
from bottle import Bottle, request, response, run, static_file
from bottle_log import LoggingPlugin
from dotenv import load_dotenv
from firebase_admin import auth, credentials
from firebase_admin._auth_utils import InvalidIdTokenError
import firebase_admin
import db
import json
import models
import os
import utils
HEADER_AUTHORIZATION = "Authorization"
ENV_HOST = "HOST"
ENV_PORT = "PORT"
ENV_GOOGLE_APPLICATION_CREDENTIALS = "GOOGLE_APPLICATION_CREDENTIALS"
DIR_MEDIA = "media"
DIR_AVATARS = DIR_MEDIA + "/avatars"
DIR_COVERS = DIR_MEDIA + "/covers"
DIR_GENRES = DIR_MEDIA + "/genres"
DIR_MUSIC = DIR_MEDIA + "/music"
load_dotenv()
app = Bottle()
app.install(LoggingPlugin(app.config))
@app.get("/api/profile")
def get_profile():
token = request.headers.get(HEADER_AUTHORIZATION)
if token is None:
response.status = 401
return "Unauthorized"
profile = db.get_user_by_token(token)
if profile is None:
profile = db.create_user(token)
response.status = 201
return profile
@app.post("/api/profile")
def update_profile():
data = request.json
try:
profile = models.User(**data)
db.update_user(profile)
response.status = 200
return "Success"
except:
response.status = 400
return "Invalid user data"
@app.post("/api/authorization")
def authorize():
data = request.json
try:
decoded_token = auth.verify_id_token(data["access_token"])
uid = decoded_token["uid"]
return {
"token": uid
}
except InvalidIdTokenError:
response.status = 510
return "Invalid auth token"
@app.post("/api/media/avatar")
def upload_avatar():
token = request.headers.get(HEADER_AUTHORIZATION)
if token is None:
response.status = 401
return "Unauthorized"
file = request.files.get("image")
extension = file.filename.split('.')[-1]
if extension not in ("png", "jpg", "jpeg"):
response.status = 400
return "Invalid file format"
save_path = os.path.join(os.getcwd(), DIR_AVATARS)
file.filename = utils.generate_random_string(32) + "." + extension
file.save(save_path)
user = db.get_user_by_token(token)
old_avatar_path = os.path.join(os.getcwd(), user["avatar"][1:])
db.update_avatar(token, "/" + DIR_AVATARS + "/" + file.filename)
if os.path.exists(old_avatar_path):
os.remove(old_avatar_path)
response.status = 200
return "Success"
@app.get("/media/avatars/<filename>")
def get_avatar(filename):
return static_file(filename, DIR_AVATARS)
@app.get("/media/covers/<filename>")
def get_cover(filename):
return static_file(filename, DIR_COVERS)
@app.get("/media/genres/<filename>")
def get_cover(filename):
return static_file(filename, DIR_GENRES)
@app.get("/media/music/<filename>")
def get_track(filename):
return static_file(filename, DIR_MUSIC)
@app.get("/api/catalog/albums")
def get_albums():
token = request.headers.get(HEADER_AUTHORIZATION)
if token is None:
response.status = 401
return "Unauthorized"
offset = request.query.offset or '0'
limit = request.query.limit or "20"
albums = db.get_albums(limit, offset)
return json.dumps(albums, ensure_ascii=False)
@app.get("/api/catalog/genres")
def get_genres():
token = request.headers.get(HEADER_AUTHORIZATION)
if token is None:
response.status = 401
return "Unauthorized"
offset = request.query.offset or '0'
limit = request.query.limit or "20"
genres = db.get_genres(limit, offset)
return json.dumps(genres, ensure_ascii=False)
@app.get("/api/catalog/tracks/<track_id>")
def get_track(track_id):
token = request.headers.get(HEADER_AUTHORIZATION)
if token is None:
response.status = 401
return "Unauthorized"
if track_id is None:
response.status = 400
return "Bad request"
user = db.get_user_by_token(token)
track = db.get_track(track_id, user["id"])
if track is None:
response.status = 404
return "Not found"
return json.dumps(track, ensure_ascii=False)
@app.get("/api/catalog/tracks")
def get_tracks():
token = request.headers.get(HEADER_AUTHORIZATION)
if token is None:
response.status = 401
return "Unauthorized"
album_id = request.query.albumId or None
if album_id is None:
response.status = 400
return "Bad request"
user = db.get_user_by_token(token)
tracks = db.get_tracks_by_album_id(album_id, user["id"])
return json.dumps(tracks, ensure_ascii=False)
@app.get("/api/catalog/tracks/info")
def get_track_infos():
token = request.headers.get(HEADER_AUTHORIZATION)
if token is None:
response.status = 401
return "Unauthorized"
album_id = request.query.albumId or None
if album_id is None:
response.status = 400
return "Bad request"
track_infos = db.get_track_infos_by_album_id(album_id)
return json.dumps(track_infos, ensure_ascii=False)
@app.post("/api/favorites")
def add_to_favorites():
data = request.json
token = request.headers.get(HEADER_AUTHORIZATION)
try:
favorites_request = models.AddToFavoritesRequest(**data)
user = db.get_user_by_token(token)
db.create_favorite(user["id"], favorites_request.trackId)
response.status = 200
return "Success"
except:
response.status = 500
return "Internal server error"
@app.delete("/api/favorites/<track_id>")
def remove_from_favorites(track_id):
token = request.headers.get(HEADER_AUTHORIZATION)
try:
user = db.get_user_by_token(token)
db.delete_favorite(user["id"], track_id)
response.status = 200
return "Success"
except:
response.status = 500
return "Internal server error"
@app.get("/api/catalog/favorites")
def get_favorite_tracks():
token = request.headers.get(HEADER_AUTHORIZATION)
if token is None:
response.status = 401
return "Unauthorized"
offset = request.query.offset or None
limit = request.query.limit or None
user = db.get_user_by_token(token)
tracks = db.get_favorite_tracks(user["id"], limit, offset)
return json.dumps(tracks, ensure_ascii=False)
@app.get("/api/catalog/favorites/info")
def get_favorite_track_infos():
token = request.headers.get(HEADER_AUTHORIZATION)
if token is None:
response.status = 401
return "Unauthorized"
user = db.get_user_by_token(token)
tracks = db.get_favorite_track_infos(user["id"])
return json.dumps(tracks, ensure_ascii=False)
@app.get("/api/search")
def search():
token = request.headers.get(HEADER_AUTHORIZATION)
if token is None:
response.status = 401
return "Unauthorized"
query = request.query.query or None
if query is None or len(str(query)) < 1:
response.status = 400
return "Bad request"
offset = request.query.offset or '0'
limit = request.query.limit or "20"
albums = db.get_search_results(query, limit, offset)
return json.dumps(albums, ensure_ascii=False)
if __name__ == "__main__":
os.makedirs(os.path.join(os.getcwd(), DIR_MEDIA), exist_ok=True)
os.makedirs(os.path.join(os.getcwd(), DIR_AVATARS), exist_ok=True)
os.makedirs(os.path.join(os.getcwd(), DIR_COVERS), exist_ok=True)
os.makedirs(os.path.join(os.getcwd(), DIR_GENRES), exist_ok=True)
os.makedirs(os.path.join(os.getcwd(), DIR_MUSIC), exist_ok=True)
cred = credentials.Certificate(os.getenv(ENV_GOOGLE_APPLICATION_CREDENTIALS))
firebase_admin.initialize_app(cred)
run(app, host=os.getenv(ENV_HOST), port=os.getenv(ENV_PORT), debug=True)