3 Scraper for https://www.musixmatch.com/
7 https://github.com/rtcq/syncedlyrics
17 __title__ =
"musixmatchlrc"
26 self.
SEARCH_URL =
'https://apic-desktop.musixmatch.com/ws/1.1/%s'
30 "authority":
"apic-desktop.musixmatch.com",
31 "cookie":
"AWSELBCORS=0; AWSELB=0",
32 "User-Agent":
"Mozilla/5.0 (Windows NT 10.0; WOW64; rv:51.0) Gecko/20100101 Firefox/51.0",
39 tokenpath = os.path.join(PROFILE,
'musixmatch_token')
42 tokendata = json.load(tokenfile)
44 cached_token = tokendata.get(
"token")
45 expiration_time = tokendata.get(
"expiration_time")
46 if cached_token
and expiration_time
and self.
current_time < expiration_time:
47 self.
token = cached_token
51 query = [(
'user_language',
'en'), (
'app_id',
'web-desktop-app-v1.0'), (
't', self.
current_time)]
52 response = self.
session.
get(url, params=query, timeout=10)
53 result = response.json()
56 if 'message' in result
and 'body' in result[
"message"]
and 'user_token' in result[
"message"][
"body"]:
57 self.
token = result[
"message"][
"body"][
"user_token"]
60 tokendata[
'token'] = self.
token
61 tokendata[
'expiration_time'] = expiration_time
63 json.dump(tokendata, tokenfile)
71 log(
"%s: searching lyrics for %s - %s" % (__title__, song.artist, song.title), debug=self.
DEBUG)
72 lyrics = Lyrics(settings=self.
settings)
74 lyrics.source = __title__
76 artist = song.artist.replace(
' ',
'+')
77 title = song.title.replace(
' ',
'+')
78 search =
'%s - %s' % (artist, title)
81 query = [(
'q', search), (
'page_size',
'5'), (
'page',
'1'), (
'app_id',
'web-desktop-app-v1.0'), (
'usertoken', self.
token), (
't', self.
current_time)]
82 response = requests.get(url, params=query, timeout=10)
83 result = response.json()
87 if 'message' in result
and 'body' in result[
"message"]
and 'track_list' in result[
"message"][
"body"]
and result[
"message"][
"body"][
"track_list"]:
88 for item
in result[
"message"][
"body"][
"track_list"]:
89 artistname = item[
'track'][
'artist_name']
90 songtitle = item[
'track'][
'track_name']
91 trackid = item[
'track'][
'track_id']
92 if (difflib.SequenceMatcher(
None, artist.lower(), artistname.lower()).ratio() > 0.8)
and (difflib.SequenceMatcher(
None, title.lower(), songtitle.lower()).ratio() > 0.8):
93 links.append((artistname +
' - ' + songtitle, trackid, artistname, songtitle))
106 title,trackid,artist,song = link
108 log(
'%s: search track id: %s' % (__title__, trackid), debug=self.
DEBUG)
110 query = [(
'track_id', trackid), (
'subtitle_format',
'lrc'), (
'app_id',
'web-desktop-app-v1.0'), (
'usertoken', self.
token), (
't', self.
current_time)]
111 response = requests.get(url, params=query, timeout=10)
112 result = response.json()
115 if 'message' in result
and 'body' in result[
"message"]
and 'subtitle' in result[
"message"][
"body"]
and 'subtitle_body' in result[
"message"][
"body"][
"subtitle"]:
116 lyrics = result[
"message"][
"body"][
"subtitle"][
"subtitle_body"]