-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
services.py
492 lines (406 loc) · 20.4 KB
/
services.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
import codecs
import functools
import json
import os
import re
from urllib import request, parse
import pathvalidate
import requests
import unidecode # to remove accents
from azapi import azapi
from bs4 import BeautifulSoup
from sentry_sdk import capture_exception
try:
import spotify_lyric.crawlers.QQCrawler as QQCrawler
import spotify_lyric.model_traditional_conversion.langconv as langconv
except ModuleNotFoundError:
pass
# With Sync.
SERVICES_LIST1 = []
# Without Sync.
SERVICES_LIST2 = []
class Config:
PROXY = request.getproxies()
if os.name == "nt":
SETTINGS_DIR = f"{os.getenv('APPDATA')}\\SpotifyLyrics\\"
else:
SETTINGS_DIR = f"{os.path.expanduser('~')}/.SpotifyLyrics/"
DEFAULT_LYRICS_DIR = os.path.join(SETTINGS_DIR, "lyrics")
LYRICS_DIR = DEFAULT_LYRICS_DIR
UA = "Mozilla/5.0 (Maemo; Linux armv7l; rv:10.0.1) Gecko/20100101 Firefox/10.0.1 Fennec/10.0.1"
def lyrics_service(_func=None, *, synced=False, enabled=True):
def _decorator_lyrics_service(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
try:
return func(*args, **kwargs)
except requests.exceptions.RequestException as error:
print("%s: %s" % (func.__name__, error))
except Exception as e:
capture_exception(e)
if enabled:
if synced:
SERVICES_LIST1.append(wrapper)
else:
SERVICES_LIST2.append(wrapper)
return wrapper
if _func is None:
return _decorator_lyrics_service
else:
return _decorator_lyrics_service(_func)
@lyrics_service(synced=True)
def _local(song):
service_name = "Local"
if os.path.isdir(Config.LYRICS_DIR):
path_song_name = pathvalidate.sanitize_filename(song.name.lower())
path_artist_name = pathvalidate.sanitize_filename(song.artist.lower())
for file in os.listdir(Config.LYRICS_DIR):
file = os.path.join(Config.LYRICS_DIR, file)
if os.path.isfile(file):
file_parts = os.path.splitext(file)
file_extension = file_parts[1].lower()
if file_extension in (".txt", ".lrc"):
file_name = file_parts[0].lower()
if path_song_name in file_name and path_artist_name in file_name:
with open(file, "r", encoding="UTF-8") as lyrics_file:
lyrics = lyrics_file.read()
timed = file_extension == ".lrc"
url = f"file:///{os.path.abspath(file)}"
return lyrics, url, service_name, timed
@lyrics_service(synced=True)
def _rentanadviser(song):
service_name = "RentAnAdviser"
search_url = "https://www.rentanadviser.com/en/subtitles/subtitles4songs.aspx?%s" % parse.urlencode({
"src": f"{song.artist} {song.name}"
})
search_results = requests.get(search_url, proxies=Config.PROXY)
soup = BeautifulSoup(search_results.text, 'html.parser')
result_links = soup.find(id="tablecontainer").find_all("a")
for result_link in result_links:
if result_link["href"] != "subtitles4songs.aspx":
lower_title = result_link.get_text().lower()
if song.artist.lower() in lower_title and song.name.lower() in lower_title:
url = f'https://www.rentanadviser.com/en/subtitles/{result_link["href"]}&type=lrc'
possible_text = requests.get(url, proxies=Config.PROXY)
soup = BeautifulSoup(possible_text.text, 'html.parser')
event_validation = soup.find(id="__EVENTVALIDATION")["value"]
view_state = soup.find(id="__VIEWSTATE")["value"]
lrc = requests.post(url, {"__EVENTTARGET": "ctl00$ContentPlaceHolder1$btnlyrics",
"__EVENTVALIDATION": event_validation,
"__VIEWSTATE": view_state}, proxies=Config.PROXY).text
return lrc, possible_text.url, service_name, True
@lyrics_service(synced=True)
def _megalobiz(song):
service_name = "Megalobiz"
search_url = "https://www.megalobiz.com/search/all?%s" % parse.urlencode({
"qry": f"{song.artist} {song.name}",
"display": "more"
})
search_results = requests.get(search_url, proxies=Config.PROXY)
soup = BeautifulSoup(search_results.text, 'html.parser')
result_links = soup.find(id="list_entity_container").find_all("a", class_="entity_name")
for result_link in result_links:
lower_title = result_link.get_text().lower()
if song.artist.lower() in lower_title and song.name.lower() in lower_title:
url = f"https://www.megalobiz.com{result_link['href']}"
possible_text = requests.get(url, proxies=Config.PROXY)
soup = BeautifulSoup(possible_text.text, 'html.parser')
lrc = soup.find("div", class_="lyrics_details").span.get_text()
return lrc, possible_text.url, service_name, True
@lyrics_service(synced=True, enabled=False)
def _qq(song):
qq = QQCrawler.QQCrawler()
sid = qq.getSongId(artist=song.artist, song=song.name)
url = qq.getLyticURI(sid)
lrc_string = ""
for line in requests.get(url, proxies=Config.PROXY).text.splitlines():
line_text = line.split(']')
lrc_string += "]".join(line_text[:-1]) + langconv.Converter('zh-hant').convert(line_text)
return lrc_string, url, qq.name, True
@lyrics_service(synced=True)
def _lyricsify(song):
service_name = "Lyricsify"
search_url = "https://www.lyricsify.com/search?%s" % parse.urlencode({
"q": f"{song.artist} {song.name}"
})
search_results = requests.get(search_url, proxies=Config.PROXY, headers={"User-Agent": UA})
soup = BeautifulSoup(search_results.text, 'html.parser')
result_container = soup.find("div", class_="sub")
if result_container:
result_list = result_container.find_all("div", class_="li")
if result_list:
for result in result_list:
result_link = result.find("a")
name = result_link.get_text().lower()
if song.artist.lower() in name and song.name.lower() in name:
url = f"https://www.lyricsify.com{result_link['href']}?download"
lyrics_page = requests.get(url, proxies=Config.PROXY, headers={"User-Agent": UA})
soup = BeautifulSoup(lyrics_page.text, 'html.parser')
download_link = soup.find(id="iframe_download")["src"]
lrc = requests.get(download_link, proxies=Config.PROXY,
cookies=lyrics_page.cookies, headers={"User-Agent": UA}).text
return lrc, lyrics_page.url, service_name, True
@lyrics_service(synced=True)
def _rclyricsband(song):
service_name = "RC Lyrics Band"
search_results = requests.get("https://rclyricsband.com/", params={"s": "%s %s" % (song.artist, song.name)},
proxies=Config.PROXY)
search_soup = BeautifulSoup(search_results.text, 'html.parser')
for result in search_soup.find(id="content").find_all("article"):
title_link = result.find(rel="bookmark")
lower_title = title_link.get_text().lower()
if song.artist.lower() in lower_title and song.name.lower() in lower_title:
song_page = requests.get(title_link["href"])
song_page_soup = BeautifulSoup(song_page.text, 'html.parser')
lyrics = requests.get(song_page_soup.find_all(class_="su-button")[2]["href"]).text
return lyrics, song_page.url, service_name, True
@lyrics_service
def _musixmatch(song):
service_name = "Musixmatch"
def extract_mxm_props(soup_page):
scripts = soup_page.find_all("script")
for script in scripts:
if script and script.contents and "__mxmProps" in script.contents[0]:
return script.contents[0]
search_url = "https://www.musixmatch.com/search/%s-%s" % (
song.artist.replace(' ', '-'), song.name.replace(' ', '-'))
header = {"User-Agent": "curl/7.9.8 (i686-pc-linux-gnu) libcurl 7.9.8 (OpenSSL 0.9.6b) (ipv6 enabled)"}
search_results = requests.get(search_url, headers=header, proxies=Config.PROXY)
soup = BeautifulSoup(search_results.text, 'html.parser')
props = extract_mxm_props(soup)
if props:
page = re.findall('"track_share_url":"([^"]*)', props)
if page:
url = codecs.decode(page[0], 'unicode-escape')
lyrics_page = requests.get(url, headers=header, proxies=Config.PROXY)
soup = BeautifulSoup(lyrics_page.text, 'html.parser')
props = extract_mxm_props(soup)
if '"body":"' in props:
lyrics = props.split('"body":"')[1].split('","language"')[0]
lyrics = lyrics.replace("\\n", "\n")
lyrics = lyrics.replace("\\", "")
album = soup.find(class_="mxm-track-footer__album")
if album:
song.album = album.find(class_="mui-cell__title").getText()
if lyrics.strip():
return lyrics, lyrics_page.url, service_name
@lyrics_service
def _songmeanings(song):
service_name = "Songmeanings"
search_url = "http://songmeanings.com/m/query/?q=%s %s" % (song.artist, song.name)
search_results = requests.get(search_url, proxies=Config.PROXY)
soup = BeautifulSoup(search_results.text, 'html.parser')
url = ""
for link in soup.find_all('a', href=True):
if "songmeanings.com/m/songs/view/" in link['href']:
url = f"https:{link['href']}"
break
elif "/m/songs/view/" in link['href']:
result = f"https://songmeanings.com{link['href']}"
lyrics_page = requests.get(result, proxies=Config.PROXY)
soup = BeautifulSoup(lyrics_page.text, 'html.parser')
url = lyrics_page.url
break
lis = soup.find_all('ul', attrs={'data-inset': True})
if len(lis) > 1:
lyrics = lis[1].find_all('li')[1].getText()
# lyrics = lyrics.encode('cp437', errors='replace').decode('utf-8', errors='replace')
if "We are currently missing these lyrics." not in lyrics:
return lyrics, url, service_name
@lyrics_service
def _songlyrics(song):
service_name = "Songlyrics"
artistm = song.artist.replace(" ", "-")
songm = song.name.replace(" ", "-")
url = f"https://www.songlyrics.com/{artistm}/{songm}-lyrics"
lyrics_page = requests.get(url, proxies=Config.PROXY)
soup = BeautifulSoup(lyrics_page.text, 'html.parser')
lyrics_container = soup.find(id="songLyricsDiv")
if lyrics_container:
lyrics = lyrics_container.get_text()
if "Sorry, we have no" not in lyrics and "We do not have" not in lyrics:
title = soup.find("div", class_="pagetitle")
if title:
for info in title.find_all("p"):
if "Album:" in info.get_text():
song.album = info.find("a").get_text()
break
return lyrics, lyrics_page.url, service_name
@lyrics_service
def _genius(song):
service_name = "Genius"
url = "https://genius.com/%s-%s-lyrics" % (song.artist.replace(' ', '-'), song.name.replace(' ', '-'))
lyrics_page = requests.get(url, proxies=Config.PROXY)
soup = BeautifulSoup(lyrics_page.text, 'html.parser')
lyrics_container = soup.find("div", {"class": "lyrics"})
if lyrics_container:
lyrics = lyrics_container.get_text()
if song.artist.lower().replace(" ", "") in soup.text.lower().replace(" ", ""):
return lyrics, lyrics_page.url, service_name
@lyrics_service
def _versuri(song):
service_name = "Versuri"
search_url = "https://www.versuri.ro/q/%s+%s/" % \
(song.artist.replace(" ", "+").lower(), song.name.replace(" ", "+").lower())
search_results = requests.get(search_url, proxies=Config.PROXY)
soup = BeautifulSoup(search_results.text, 'html.parser')
for search_results in soup.findAll('a'):
if "/versuri/" in search_results['href']:
link_text = search_results.getText().lower()
if song.artist.lower() in link_text and song.name.lower() in link_text:
url = "https://www.versuri.ro" + search_results['href']
lyrics_page = requests.get(url, proxies=Config.PROXY)
soup = BeautifulSoup(lyrics_page.text, 'html.parser')
content = soup.find_all('div', {'id': 'pagecontent'})[0]
lyrics = str(content)[str(content).find("</script><br/>") + 14:str(content).find("<br/><br/><center>")]
lyrics = lyrics.replace("<br/>", "")
if "nu există" not in lyrics:
return lyrics, lyrics_page.url, service_name
@lyrics_service
def _azapi(song):
service = "Azapi"
try:
api = azapi.AZlyrics('duckduckgo', accuracy=0.5, proxies=Config.PROXY)
except requests.exceptions.RequestException:
api = azapi.AZlyrics('google', accuracy=0.5, proxies=Config.PROXY)
if song.artist:
api.artist = song.artist
api.title = song.name
songs = api.getSongs()
if song.name in songs:
result_song = songs[song.name]
song.album = result_song["album"]
if result_song["year"]:
song.year = int(result_song["year"])
lyrics = api.getLyrics(url=result_song["url"])
if isinstance(lyrics, str):
return lyrics, result_song["url"], service
# tab/chord services
def _ultimateguitar(song):
artist = unidecode.unidecode(song.artist)
title = unidecode.unidecode(song.name)
url_pt1 = 'https://www.ultimate-guitar.com/search.php?view_state=advanced&band_name='
url_pt2 = '&song_name='
url_pt3 = '&type%5B%5D=300&type%5B%5D=200&rating%5B%5D=5&version_la='
# song = song.replace('-', '+')
# artist = artist.replace('-', '+')
url = url_pt1 + artist + url_pt2 + title + url_pt3
page = requests.get(url)
if page.status_code == 200:
soup = BeautifulSoup(page.content, 'html.parser')
search_results_element = soup.find_all('div', {'class': 'js-store'})[0]
search_results_data = json.loads(search_results_element["data-content"])
urls = []
data = search_results_data["store"]["page"]["data"]
if "results" in data.keys():
for result in data["results"]:
urls.append(result["tab_url"])
return urls
return []
def _cifraclub(song):
artist = unidecode.unidecode(song.artist)
title = unidecode.unidecode(song.name)
url = 'https://www.cifraclub.com.br/{}/{}'.format(artist.replace(" ", "-").lower(), title.replace(" ", "-").lower())
try:
result = requests.get(url, proxies=Config.PROXY)
except requests.exceptions.RequestException as error:
print(f"cifraclub: {error}")
return []
if result.status_code == 200:
return [result.url]
else:
return []
# don't even get to this point, but it's an option for source
# just got to change services_list3 list order
def _songsterr(song):
artist = unidecode.unidecode(song.artist)
title = unidecode.unidecode(song.name)
url = 'https://www.songsterr.com/a/wa/bestMatchForQueryString?s={}&a={}'.format(title, artist)
return [url]
def _tanzmusikonline(song):
try:
token_request = requests.get('https://www.tanzmusik-online.de/search', timeout=30)
search = BeautifulSoup(token_request.content, 'html.parser').find(id="page-wrapper")
if search:
token = ""
for input_field in search.find("form").find_all("input"):
if input_field.get("name") == "_token":
token = input_field.get("value")
break
page = 1
highest_page = 2
song_urls = []
base_result_url = 'https://www.tanzmusik-online.de/search/result'
while page < highest_page:
search_results = requests.post(base_result_url + "?page=" + str(page), proxies=Config.PROXY,
cookies=token_request.cookies,
data={"artist": song.artist, "song": song.name, "_token": token,
"searchMode": "extended", "genre": 0, "submit": "Suchen"},
timeout=30)
search_soup = BeautifulSoup(search_results.content, 'html.parser')
for song_result in search_soup.find_all(class_="song"):
song_urls.append(song_result.find(class_="songTitle").a.get("href"))
if page == 1:
pagination = search_soup.find(class_="pagination")
if pagination:
for page_number_element in pagination.find_all("a"):
page_number = page_number_element.getText()
if page_number.isdigit():
highest_page = int(page_number) + 1
page += 1
language = requests.get("https://www.tanzmusik-online.de/locale/en", proxies=Config.PROXY, timeout=30)
for song_url in song_urls:
page = requests.get(song_url, proxies=Config.PROXY, cookies=language.cookies, timeout=30)
soup = BeautifulSoup(page.content, 'html.parser')
for dance in soup.find(class_="dances").find_all("div"):
dance_name = dance.a.getText().strip().replace("Disco Fox", "Discofox")
if dance_name not in song.dances:
song.dances.append(dance_name)
details = soup.find(class_="songDetails")
if details:
for detail in details.find_all(class_="line"):
classes = detail.i.get("class")
typ, text = detail.div.getText().split(":", 1)
if "fa-dot-circle-o" in classes:
if typ.strip().lower() == "album":
song.album = text.strip()
elif "fa-calendar-o" in classes:
song.year = int(text)
elif "fa-flag" in classes:
song.genre = text.strip()
elif "fa-music" in classes:
song.cycles_per_minute = int(text)
elif "fa-tachometer" in classes:
song.beats_per_minute = int(text)
except requests.exceptions.RequestException as error:
print("%s: %s" % ("Tanzmusik Online", error))
except Exception as e:
capture_exception(e)
def _welchertanz(song):
try:
interpreter_request = requests.get("https://tanzschule-woelbing.de/charts/interpreten/", proxies=Config.PROXY)
interpreter_soup = BeautifulSoup(interpreter_request.content, 'html.parser')
interpreter_links = []
for interpreter in interpreter_soup.find_all("a", class_="btn-dfeault"):
if "/charts/interpreten/?artist-hash=" in interpreter.get("href") \
and song.artist.lower() in interpreter.getText().lower():
interpreter_links.append(interpreter.get("href"))
for interpreter_link in interpreter_links:
interpreter_songs = requests.get("https://tanzschule-woelbing.de" + interpreter_link, proxies=Config.PROXY)
interpreter_songs_soup = BeautifulSoup(interpreter_songs.content, 'html.parser')
for interpreter_song in interpreter_songs_soup.find("table", class_="table").find_all("tr"):
infos = interpreter_song.find_all("td")
if infos and song.name.lower() in infos[1].getText().strip().lower():
dances = infos[2].find_all("a")
for dance in dances:
dance_name = dance.getText().strip() \
.replace("Cha-Cha-Cha", "Cha Cha Cha") \
.replace("Wiener", "Viennese") \
.replace("Walzer", "Waltz") \
.replace("Foxtrott", "Foxtrot")
if dance_name != "---" and dance_name not in song.dances:
song.dances.append(dance_name)
except requests.exceptions.RequestException as error:
print("%s: %s" % ("Tanzschule Woelbing", error))
except Exception as e:
capture_exception(e)