-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.py
67 lines (56 loc) · 1.76 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
from flask import Flask, jsonify, render_template, request
from spotify import (
get_album,
get_all_trackids,
get_track,
get_play,
check_regex,
query_spotify,
format_duration,
)
app = Flask(__name__)
@app.route("/")
def index():
return render_template("index.html")
@app.route("/spotify", methods=["POST"])
def download():
if not request.form:
return "No arguments provided"
url_type, id = check_regex(request.form.get("url"))
if url_type == "album":
return render_template("spotify.html", data=get_album(id), types="album")
elif url_type == "track":
return render_template("spotify.html", data=get_track(id), types="track")
elif url_type == "playlist":
return render_template("spotify.html", data=get_play(id), types="playlist")
else:
return (
render_template(
"index.html", error="Invalid URL...Please check the URL and try again"
),
400,
)
@app.route("/api/search")
def api():
q = request.args.get("q")
return query_spotify(q) if q else "No arguments provided"
@app.get("/api/getalltracks")
def get_all_tracks():
album_id = request.args.get("id")
album = bool(request.args.get("album"))
if album_id:
return jsonify(get_all_trackids(album_id, album))
else:
return "No arguments provided", 400
@app.get("/api/tracks/<string:track_id>")
def track_details(track_id: str):
if track_id:
try:
return jsonify(get_track(track_id))
except Exception as e:
return "Invalid Track ID", 400
else:
return "No arguments provided", 400
app.add_template_filter(format_duration)
if __name__ == "__main__":
app.run(debug=True, host="0.0.0.0", port=5000)