-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
252 lines (182 loc) · 7.61 KB
/
utils.py
File metadata and controls
252 lines (182 loc) · 7.61 KB
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
240
241
242
243
244
245
246
247
248
249
250
251
252
import os
import sys
import json
import time
import typing
import requests
import config
import spotify
from gcloud import detect_language_from_audio, detect_language_from_text
class Utils:
client: spotify.SpotifyClient = None
logger_func: typing.Callable = print
class TerminalColors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKCYAN = '\033[96m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
RED = '\033[91m'
ERROR = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
@classmethod
def print(cls, text, color=None, colors=None, *args, **kwargs):
colors = colors or []
if color and colors:
raise ValueError("Cannot use both color and colors")
color = color or ''.join(colors)
if not color:
print(text, *args, **kwargs)
else:
print(f"{color}{text}{cls.ENDC}", *args, **kwargs)
@classmethod
def log(cls, msg, *args, error=False, raw=False, color_on_error=True, color_on_non_error=False, **kwargs):
if not raw:
if error:
msg = f"[-] {msg}"
else:
msg = f"[+] {msg}"
col = cls.TerminalColors.ERROR if error else cls.TerminalColors.OKGREEN
if error and not color_on_error:
col = None
elif not error and not color_on_non_error:
col = None
cls.TerminalColors.print(msg, col, *args, **kwargs)
@staticmethod
def confirm(prompt: str = "Are you sure? (y/n)", ignore_y: bool = False) -> bool:
if "-y" in sys.argv and not ignore_y:
return True # for every prompt, return True
prompt += "\n> "
while True:
ans = input(prompt).lower()
if ans == "y":
return True
elif ans == "n":
return False
else:
print("Invalid input.")
@staticmethod
def start_webserver(filename="webserver.py") -> int:
path = os.path.dirname(os.path.abspath(__file__)) + f"/{filename}"
return os.system(f"{sys.executable} {path}")
@staticmethod
def read_request_file(filename="request.json") -> dict:
with open(filename, "r") as f:
return json.load(f)
@classmethod
def initialize_spotify_client(cls, code) -> spotify.SpotifyClient:
cls.client = spotify.SpotifyClient(config.SPOTIFY_CLIENT_ID, config.SPOTIFY_CLIENT_SECRET,
config.SPOTIFY_REDIRECT_URI)
cls.client.get_token(code, force=True)
return cls.client
@classmethod
def check_is_me(cls):
me = cls.client.get_current_user()
confirm = cls.confirm(f"Is this you? \"{me['display_name']}\" - \"{me['external_urls']['spotify']}\" (y/n)")
if not confirm:
raise RuntimeError("User does not match.")
@classmethod
def get_possible_languages(cls, track):
title = track["name"]
preview_url = track.get("preview_url")
resp_title = detect_language_from_text(title)
if preview_url:
preview = requests.get(preview_url).content
resp_preview = detect_language_from_audio(preview)
res = [] # code: ..., confidences: [..., ...], sum-confidence: ..., confidence: ...
for lang in resp_title:
res.append({
"code": lang["code"],
"confidences": [lang["confidence"]],
"sum-confidence": lang["confidence"],
"confidence": lang["confidence"]
})
if preview_url:
for lang in resp_preview: # type: ignore
if lang["code"] in [r["code"] for r in res]:
for r in res:
if r["code"] == lang["code"]:
r["confidences"].append(lang["confidence"])
r["sum-confidence"] += lang["confidence"]
r["confidence"] = r["sum-confidence"] / len(r["confidences"])
else:
res.append({
"code": lang["code"],
"confidences": [lang["confidence"]],
"sum-confidence": lang["confidence"],
"confidence": lang["confidence"]
})
res.sort(key=lambda x: x["confidence"], reverse=True)
return res, preview_url is not None
@staticmethod
def generate_track_uri(track_id):
return f'spotify:track:{track_id}'
@classmethod
def get_tracks_on_playlist(cls, playlist_id):
tracks = []
offset = 0
while True:
resp = cls.client.get_playlist_items(playlist_id, offset=offset)
if not resp["items"]:
break
for item in resp["items"]:
if track := item["track"]:
tracks.append(track)
else:
break
offset += 100
return tracks
@classmethod
def handle_removed_tracks(cls, tracks):
d = {}
track_ids = [x["id"] for x in tracks]
playlist_ids = config.SORTED_PLAYLIST_IDS.values()
for playlist_id in playlist_ids:
tracks = cls.get_tracks_on_playlist(playlist_id)
for track in tracks:
if track["id"] not in track_ids:
d[track["id"]].append(playlist_id)
for track_id, playlist_ids in d.items():
for playlist_id in playlist_ids:
cls.client.delete_playlist_items(playlist_id, cls.generate_track_uri(track_id))
cls.log(f"Removed track {track_id} from playlist {playlist_id}.")
time.sleep(.5)
@classmethod
def check_track_presence(cls, track_id, playlist_id):
tracks = cls.get_tracks_on_playlist(playlist_id)
for track in tracks:
if track["id"] == track_id:
return True
return False
@classmethod
def run(cls):
while True:
cls.log("Getting tracks on playlist...")
tracks = cls.get_tracks_on_playlist(config.PLAYLIST_ID)
cls.log("Removing tracks (if needed)...")
cls.handle_removed_tracks(tracks)
cls.log("Running language sorter...")
for track in tracks:
cls.log("Now checking track: " + track["name"] + " - " + track["external_urls"]["spotify"])
possible_languages, preview_available = cls.get_possible_languages(track)
if preview_available is False:
cls.log("WARNING: No preview available. Only checking for title...", error=True)
continue
flag = True
for lang in possible_languages:
if lang["code"] not in list(config.SORTED_PLAYLIST_IDS.keys()):
continue
if playlist_id := config.SORTED_PLAYLIST_IDS.get(lang["code"]):
if not cls.check_track_presence(track["id"], playlist_id):
cls.client.add_playlist_items(playlist_id, cls.generate_track_uri(track["id"]))
cls.log(f"Added track {track['id']} to playlist {playlist_id}.")
time.sleep(.25)
flag = False
if flag:
cls.log(f"No playlist found for this track. Possible languages returned {possible_languages}",
error=True)
time.sleep(.5)
time.sleep(5)