-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
153 lines (115 loc) · 4.53 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
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
from flask import Flask, request, redirect, send_from_directory
import os
import spotipy
from spotipy.oauth2 import SpotifyOAuth
def bp():
return os.path.abspath(os.path.dirname(__file__))
def getCreds():
with open(f"{bp()}/creds.txt", "r") as f:
id, secret = [i.lstrip().rstrip().split()[1] for i in f.readlines()]
return id, secret
clientID, clientSecret = getCreds()
scope = 'user-library-read, user-read-private, user-read-playback-state, user-modify-playback-state'
app = Flask(__name__)
@app.route("/favicon.ico")
def favicon():
return send_from_directory(bp(), 'favicon.ico', mimetype='image/vnd.microsoft.icon')
@app.route("/index")
@app.route("/")
def index():
with open(f"{bp()}/index.html", "r") as f:
html = f.read()
return html
@app.route("/select", methods=["POST"])
def select():
query = request.form.get("query", "")
if query == "":
return redirect("/")
if query[0] == "~":
command = query[1:].strip()
if command == "purge-used":
os.system(f"echo '' > {bp()}/used.dat")
elif command == "lock":
with open(f"{bp()}/lock.dat", "w") as lockF:
lockF.write("1")
elif command == "unlock":
with open(f"{bp()}/lock.dat", "w") as lockF:
lockF.write("0")
elif command == "used":
with open(f"{bp()}/used.dat", "r") as usedF:
message = usedF.read()
with open(f"{bp()}/add.html", "r") as f:
html = f.read()
html = html.replace("@@message@@", message)
return html
sp = spotipy.Spotify( \
auth_manager=SpotifyOAuth(clientID, clientSecret, "http://www.google.co.uk", \
scope=scope, cache_path=f"{bp()}/.cache", open_browser=False))
devices = sp.devices()
trackIDs = []
trackLines = []
results = sp.search(query)
for item in results['tracks']['items']:
trackIDs.append("spotify:track:" + item['id'])
trackLines.append(item["name"] + " by " + ", ".join([a["name"] for a in item["artists"]]))
with open(f"{bp()}/select.html", "r") as f:
html = f.read()
html = html.replace("@@trackoptions@@", makeTrackOptions(trackLines, trackIDs))
return html
@app.route("/add", methods=["POST"])
def add():
trackID = request.form.get("trackID")
sp = spotipy.Spotify( \
auth_manager=SpotifyOAuth(clientID, clientSecret, "http://www.google.co.uk",\
scope=scope, cache_path=f"{bp()}/.cache", open_browser=False))
devices = sp.devices()
track = sp.track(trackID)
trackName = track["name"]
artist = ", ".join([a["name"] for a in track["artists"]])
usedF = open(f"{bp()}/used.dat", "r")
used = usedF.readlines()
used = [s.strip("\n").strip("\r") for s in used]
usedF.close()
if trackID not in used:
message = "Successfully added to queue! Thanks!"
try:
deviceID = devices["devices"][0]["id"]
try: ### likewise
sp.add_to_queue(trackID, device_id=deviceID)
usedF = open(f"{bp()}/used.dat", "a")
usedF.write(trackID + "\n\r" + " --> " + trackName + " - " + artist + "<br>\n\r")
usedF.close()
except: # debugging
message = "Adding unsuccessful: Spotify Error."
except:
message = "Adding unsuccessful: No devices."
else:
message = "Track already recently queued!"
with open(f"{bp()}/add.html", "r") as f:
html = f.read()
html = html.replace("@@message@@", message)
return html
@app.route("/list")
def list():
sp = spotipy.Spotify( \
auth_manager=SpotifyOAuth(clientID, clientSecret, "http://www.google.co.uk",\
scope=scope, cache_path=f"{bp()}/.cache", open_browser=False))
return "Not Implemented"
def makeDeviceOptions(devices):
out = ""
for dev in devices["devices"]:
out += f" <option>{dev['name']}</option>\n"
return out
def makeTrackOptions(trackLines, uris):
with open(f"{bp()}/lock.dat", "r") as lockF:
locked = int(lockF.read())
if locked:
return "<div class='container'> Chune has been locked by the Admin, try again later!</div>"
out = ""
for name, uri in zip(trackLines, uris):
out += f" <button type='Submit' name='trackID' class='btn btn-primary' value='{uri}'>{name}</button>\n"
return out
def devName2ID(devices, name):
for dev in devices["devices"]:
if dev["name"] is name:
return dev["id"]