Skip to content

Commit

Permalink
fixed azlyrics
Browse files Browse the repository at this point in the history
  • Loading branch information
xnetcat committed Aug 30, 2022
1 parent cdaf376 commit c87fd6d
Showing 1 changed file with 27 additions and 11 deletions.
38 changes: 27 additions & 11 deletions spotdl/providers/lyrics/azlyrics.py
Expand Up @@ -16,6 +16,25 @@ class AzLyrics(LyricsProvider):
AZLyrics lyrics provider class.
"""

def __init__(self):
super().__init__()

self.session = requests.Session()
self.session.headers.update(self.headers)

# Not sure if this is needed
# but it doesn't hurt
self.session.get("https://www.azlyrics.com/")

resp = self.session.get("https://www.azlyrics.com/geo.js")

# extract value from js code
js_code = resp.text
start_index = js_code.find("value\"") + 9
end_index = js_code[start_index:].find("\");")

self.x_code = js_code[start_index:start_index+end_index]

def get_lyrics(self, name: str, artists: List[str], **_) -> Optional[str]:
"""
Try to get lyrics from azlyrics
Expand All @@ -31,13 +50,12 @@ def get_lyrics(self, name: str, artists: List[str], **_) -> Optional[str]:
# Join every artist by comma in artists
artist_str = ", ".join(artist for artist in artists if artist)

song_name = name.replace(" ", "+").lower()
song_artists = artist_str.replace(" ", "+").lower()
song_artists = song_artists.replace(",", "%2C")
params = {
"q": f"{artist_str} - {name}",
"x": self.x_code,
}

url = f"https://search.azlyrics.com/search.php?q={song_name}+{artists}"

response = requests.get(url, headers=self.headers)
response = self.session.get("https://search.azlyrics.com/search.php", params=params)
soup = BeautifulSoup(response.content, "html.parser")

td_tags = soup.find_all("td")
Expand All @@ -55,7 +73,7 @@ def get_lyrics(self, name: str, artists: List[str], **_) -> Optional[str]:
if lyrics_url.strip() == "":
return None

response = requests.get(lyrics_url, headers=self.headers)
response = self.session.get(lyrics_url)
soup = BeautifulSoup(response.content, "html.parser")

# Find all divs that don't have a class
Expand All @@ -64,9 +82,7 @@ def get_lyrics(self, name: str, artists: List[str], **_) -> Optional[str]:
# Find the div with the longest text
lyrics_div = sorted(div_tags, key=lambda x: len(x.text))[-1]

lyrics = lyrics_div.get_text()

# Remove the 3 first new lines
lyrics = lyrics[3:]
# extract lyrics from div and clean it up
lyrics = lyrics_div.get_text().strip()

return lyrics

0 comments on commit c87fd6d

Please sign in to comment.