-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
212 lines (183 loc) · 6.46 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
import os
import binascii
from libspot.proto import StorageResolve_pb2 as StorageResolve
from libspot.core import Session
from libspot.metadata import TrackId
from libspot.util import convert_milliseconds
from aiohttp import web
import requests
import logging
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
)
LOGGER = logging.getLogger("spot-dl-server")
if os.path.isfile("credentials.json"):
session = Session.Builder().stored_file().create()
else:
print("No credentials.json file found.")
exit()
def get_lyrics(track_id: str):
token = session.tokens().get("user-read-playback-state")
resp = requests.get(
"https://spclient.wg.spotify.com/color-lyrics/v2/track/{}".format(track_id),
headers={
"Authorization": "Bearer %s" % token,
"User-Agent": "Spotify/8.9.96.476 Android/34 (22101316I)",
"Accept": "application/json",
},
params={
"vocalRemoval": "false",
"syllableSync": "false",
"clientLanguage": "en_IN",
},
)
try:
synced_lyric = ""
lines = resp.json()["lyrics"]["lines"]
for line in lines:
startms = int(line["startTimeMs"])
words = line["words"]
timeStamp = convert_milliseconds(startms)
synced_lyric += f"[{timeStamp}]{words}\n"
except KeyError:
synced_lyric = "You'd have to guess this one"
return synced_lyric
def search_track_solo(query: str):
token = session.tokens().get("user-read-email")
resp = requests.get(
"https://api.spotify.com/v1/search",
{"limit": "5", "offset": "0", "q": query, "type": "track"},
headers={"Authorization": "Bearer %s" % token},
)
i = 1
tracks = resp.json()["tracks"]["items"]
return tracks[0]["id"]
def search_track(query: str, lim: int = 5):
token = session.tokens().get("user-read-email")
resp = requests.get(
"https://api.spotify.com/v1/search",
{"limit": "{}".format(lim), "offset": "0", "q": query, "type": "track"},
headers={"Authorization": "Bearer %s" % token},
)
results = []
for i in range(lim):
try:
results.append(
{
"name": resp.json()["tracks"]["items"][i]["name"],
"artist": resp.json()["tracks"]["items"][i]["artists"][0]["name"],
"id": resp.json()["tracks"]["items"][i]["id"],
"year": resp.json()["tracks"]["items"][i]["album"]["release_date"][
:4
],
"cover": resp.json()["tracks"]["items"][i]["album"]["images"][0][
"url"
],
"cover_small": resp.json()["tracks"]["items"][i]["album"]["images"][2][
"url"
],
}
)
except:
pass
return results
def get_playlist(playlist_id: str):
token = session.tokens().get("user-read-email")
resp = requests.get(
"https://api.spotify.com/v1/playlists/{}".format(playlist_id),
headers={"Authorization": "Bearer %s" % token},
)
tracks = resp.json()["tracks"]["items"]
playlist = []
for track in tracks:
if track["track"]["name"] == "":
continue
try:
cover = track["track"]["album"]["images"][0]["url"]
except:
cover = ""
playlist.append(
{
"name": track["track"]["name"],
"artist": track["track"]["artists"][0]["name"],
"id": track["track"]["id"],
"year": track["track"]["album"]["release_date"][:4],
"cover": cover,
}
)
return playlist
def get_track(track_id: str):
if len(track_id) != 22:
track_id = search_track_solo(track_id)
track_id_str = track_id
track_id: TrackId = TrackId.from_base62(track_id)
song = session.api().get_metadata_4_track(track_id)
cover_id = ""
if song.album.cover_group.image and len(song.album.cover_group.image) > 2:
cover_id = binascii.hexlify(song.album.cover_group.image[2].file_id).decode()
# lr = get_lyrics(tc)
key = session.audio_key().get_audio_key(song.gid, song.file[0].file_id, True)
resp = session.api().send(
"GET",
"/storage-resolve/files/audio/interactive/{}".format(
binascii.hexlify(song.file[0].file_id).decode()
),
None,
None,
)
storage_resolve_response = StorageResolve.StorageResolveResponse()
storage_resolve_response.ParseFromString(resp.content)
try:
lyr = get_lyrics(track_id_str)
except:
lyr = "You'd have to guess this one"
return (
str(storage_resolve_response.cdnurl[0]),
key,
song.name,
song.artist[0].name,
track_id_str,
"https://i.scdn.co/image/" + cover_id,
lyr,
)
async def get_track_handler(request):
track_id = request.match_info.get("id")
LOGGER.info(f"new-track-request: {track_id}")
try:
cdnurl, key, name, artist, tc, cover, lyrics = get_track(track_id)
except Exception as e:
return web.json_response({"error": str(e)}, status=500)
return web.json_response(
{
"cdnurl": cdnurl,
"key": key.hex(),
"name": name,
"artist": artist,
"tc": tc,
"cover": cover,
"lyrics": lyrics,
}
)
async def search_track_handler(request):
query = request.match_info.get("query")
lim = request.query.get("lim", 5)
LOGGER.info(f"new-search-request: {query}")
try:
results = search_track(query, int(lim))
except Exception as e:
return web.json_response({"error": str(e)}, status=500)
return web.json_response({"results": results})
async def get_playlist_handler(request):
playlist_id = request.match_info.get("id")
LOGGER.info(f"new-playlist-request: {playlist_id}")
try:
results = get_playlist(playlist_id)
except Exception as e:
return web.json_response({"error": str(e)}, status=500)
return web.json_response({"results": results})
app = web.Application()
app.router.add_get("/get_track/{id}", get_track_handler)
app.router.add_get("/search_track/{query}", search_track_handler)
app.router.add_get("/get_playlist/{id}", get_playlist_handler)
web.run_app(app, host="0.0.0.0", port=5000)