-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.py
79 lines (69 loc) · 2.2 KB
/
index.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
import os
import threading
from contextlib import suppress
from dotenv import load_dotenv
from quart import Quart, send_file
from quart_cors import cors
from util.download import CACHE_DIR, ZIP_DIR, DOWNLOAD_DIR, start, start_playlist
from util.spotify import start_token_thread
from util.statistics import totalCaches, totalSongs, totalPlaylists, totalSongData, totalStorage, songStorage, playlistStorage, cacheStorage
app = Quart(__name__)
app = cors(app, allow_origin="*")
load_dotenv()
port = os.environ.get("port")
ip = os.environ.get("ip")
@app.route('/track/<string:id>')
async def serve_audio(id):
try:
filename = await start(id)
return await send_file(filename, mimetype='audio/mpeg'), 200
except:
return {
"failed": True,
"message": "Song not found"
}, 404
@app.route('/playlist/<string:id>')
async def serve_playlist(id):
try:
filename = await start_playlist(id)
return await send_file(filename, as_attachment=True, attachment_filename=f'{id}.zip', mimetype='application/zip'), 200
except:
return {
"failed": True,
"message": "Playlist not found"
}, 404
@app.route("/stats")
async def stats():
return {
"failed": False,
"data": {
"total": await totalCaches(),
"songs": await totalSongs(),
"caches": await totalSongData(),
"playlists": await totalPlaylists()
},
"storage": {
"total": await totalStorage(),
"songs": await songStorage(),
"playlists": await playlistStorage(),
"caches": await cacheStorage()
}
}
@app.route('/')
async def serve_index():
return {
"message": "Online",
"github": "https://github.com/g3vv/yank",
"routes": {
"track": "/track/{song_id}",
"playlist": "/playlist/{playlist_id}",
"stats": "/stats"
}
}
token_thread = threading.Thread(target=start_token_thread)
token_thread.start()
if __name__ == '__main__':
for directory in {CACHE_DIR, ZIP_DIR, DOWNLOAD_DIR}:
with suppress(FileExistsError):
os.mkdir(directory)
app.run(ip, port=port)