-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspotlyrics.py
239 lines (201 loc) · 7.98 KB
/
spotlyrics.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
import time
import json
import os
import subprocess
import requests
import spotipy
from dotenv import load_dotenv
from spotipy.oauth2 import SpotifyOAuth
import pickle
import base64
import sys
# load .env credentials
load_dotenv()
# after the bearer token expires, retrieve a new one valid for 1hr
def REFRESH_BEARER_TOKEN():
return subprocess.getoutput('python get_cookie.py')
# return true if a song is currently playing, false otherwise
def IS_PLAYING():
return sp.current_playback()['is_playing']
# converting to b64 to avoid storing user tokens in plaintext
def ENCODE_TOKEN(bearer_token):
token_bytes = base64.b64encode(bearer_token.encode('ascii'))
b64_token = token_bytes.decode('ascii')
return b64_token
def DECODE_TOKEN(bearer_token):
return base64.b64decode(bearer_token).decode('UTF-8')
def GET_BEARER_TOKEN():
file = open('bearer_token.dat', 'rb')
b64_token = pickle.load(file)
file.close()
return DECODE_TOKEN(b64_token)
def DUMP_TOKEN(bearer_token):
file = open('bearer_token.dat', 'wb')
encoded = ENCODE_TOKEN(bearer_token)
pickle.dump(encoded, file)
file.close()
# get the current playback information (artist, song title)
def NOW_PLAYING(sp):
print("Now Playing: ", sp.current_playback()[
'item']['artists'][0]['name'], "-", sp.current_playback()['item']['name'])
print()
# retrieves the lyrics for the songs, timestamps for each line, and the current bar of the song playing
def GET_LYRIC_DATA():
# create spotipy instance to connect with user account and get information about current playback
sp = spotipy.Spotify(auth_manager=SpotifyOAuth(client_id=os.getenv('CLIENT_ID'),
client_secret=os.getenv(
'CLIENT_SECRET'),
redirect_uri="http://localhost:8888/",
scope="user-read-playback-state"))
try:
track_name = sp.current_playback()['item']['name']
except (TypeError):
print("No song is playing. Play a song and retry.")
exit()
track_name = sp.current_playback()['item']['name']
track_artist = sp.current_playback()['item']['artists'][0]['name']
result = []
track_id = sp.current_playback()['item']['id']
art_image_url = sp.current_playback()['item']['album']['images'][0]['url']
art_image_directory = art_image_url.split("image/")
substr = art_image_directory[1]
base_url = "https://spclient.wg.spotify.com/color-lyrics/v2/track/" + \
str(track_id) + "/image/https%3A%2F%2Fi.scdn.co%2Fimage%2F" + \
str(substr) + "?format=json&vocalRemoval=false&market=from_token"
BEARER_TOKEN = GET_BEARER_TOKEN()
headers = {
"Host": "spclient.wg.spotify.com",
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:97.0) Gecko/20100101 Firefox/97.0",
"Accept": "application/json",
"Accept-Language": "en",
"Accept-Encoding": "gzip, deflate, br",
"Referer": "https://open.spotify.com/",
"authorization": "Bearer " + BEARER_TOKEN,
"app-platform": "WebPlayer",
"spotify-app-version": "1.1.81.4.gf0a51a16",
"Origin": "https://open.spotify.com",
"Connection": "keep-alive",
"Sec-Fetch-Dest": "empty",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Site": "same-site",
"Pragma": "no-cache",
"Cache-Control": "no-cache",
"TE": "trailers"
}
try:
response = requests.get(base_url, headers=headers)
RESP_CODE = response.status_code
if(RESP_CODE != 200):
raise PermissionError("401 unauthorized/token has expired")
LYRICS_JSON = response.json()
NUM_LINES = len(LYRICS_JSON['lyrics']['lines'])
DUMP_TOKEN(BEARER_TOKEN)
except (PermissionError):
updated_token = REFRESH_BEARER_TOKEN()
headers = {
"Host": "spclient.wg.spotify.com",
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:97.0) Gecko/20100101 Firefox/97.0",
"Accept": "application/json",
"Accept-Language": "en",
"Accept-Encoding": "gzip, deflate, br",
"Referer": "https://open.spotify.com/",
"authorization": "Bearer " + updated_token,
"app-platform": "WebPlayer",
"spotify-app-version": "1.1.81.4.gf0a51a16",
"Origin": "https://open.spotify.com",
"Connection": "keep-alive",
"Sec-Fetch-Dest": "empty",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Site": "same-site",
"Pragma": "no-cache",
"Cache-Control": "no-cache",
"TE": "trailers"
}
try:
response = requests.get(base_url, headers=headers)
LYRICS_JSON = response.json()
NUM_LINES = len(LYRICS_JSON['lyrics']['lines'])
DUMP_TOKEN(updated_token)
except:
NOW_PLAYING(sp)
print("No lyrics found for this track.")
exit()
NOW_PLAYING(sp)
lyrics = []
ms_timestamp = []
for line in range(0, NUM_LINES):
line_one_based = line+1
lyrics.append(LYRICS_JSON['lyrics']['lines'][line]['words'])
ms_timestamp.append(
LYRICS_JSON['lyrics']['lines'][line]['startTimeMs'])
current_progress = sp.current_playback()['progress_ms']
current_line = 0
for i in range(0, len(ms_timestamp)-1):
if((current_progress >= int(ms_timestamp[i])) and (current_progress <= int(ms_timestamp[i+1]))
):
current_line = i
break
result.append(ms_timestamp)
result.append(current_line)
result.append(lyrics)
result.append(track_name)
result.append(track_artist)
return result
# when called, start the interactive cli session printing lyrics line by line and checking if a song gets changed
def PRINT_INTERACTIVE_LYRICS(data):
track_id = sp.current_playback()['item']['id']
ms_timestamp = data[0]
current_line = data[1]
lyrics = data[2]
starttime = time.time()
while True:
track_id2 = sp.current_playback()['item']['id']
if(track_id != track_id2):
print("song has changed")
os.system('cls' if os.name == 'nt' else 'clear')
song_changed = True
new_track_id = sp.current_playback()['item']['id']
track_id = new_track_id
break
current_progress = sp.current_playback()['progress_ms']
if(current_progress >= int(ms_timestamp[current_line])):
printed = False
print(lyrics[current_line])
current_line += 1
is_playing = sp.current_playback()['is_playing']
if(not(is_playing) and not printed):
print("Playback Paused")
printed = True
time.sleep(0.5 - ((time.time() - starttime) % 0.5))
# main function call to handle song changes, rewinds/forwards, and user arguments
if len(sys.argv) > 1:
arg = sys.argv[1]
if arg == "-l" or arg == "--lyrics":
data = GET_LYRIC_DATA()
if(data):
os.system('cls' if os.name == 'nt' else 'clear')
print("Lyrics for", data[3], "by", data[4], "\n")
for line in data[2]:
print(line)
exit()
else:
exit()
else:
print("usage: python spotlyrics.py [-h] [-l]\n")
print("spotlyrics - Line by line spotify lyrics\n")
print("options:\n",
" -h, --help show this help message and exit\n",
" -l, --lyrics print the current playback song's lyrics and exit\n"
)
exit()
else:
while(True):
sp = spotipy.Spotify(auth_manager=SpotifyOAuth(client_id=os.getenv('CLIENT_ID'),
client_secret=os.getenv(
'CLIENT_SECRET'),
redirect_uri="http://localhost:8888/",
scope="user-read-playback-state"))
# clear the terminal when the program starts
os.system('cls' if os.name == 'nt' else 'clear')
data = GET_LYRIC_DATA()
PRINT_INTERACTIVE_LYRICS(data)