Skip to content

Commit

Permalink
lyrics/60-google.py: fix lyrics not loading fully
Browse files Browse the repository at this point in the history
  • Loading branch information
astroanax committed Nov 3, 2023
1 parent cc19b01 commit 3fde694
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
1 change: 1 addition & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
ncmpc 0.50 - not yet released
* lyrics/musixmatch: add new lyrics extension
* lyrics/google: fix partial loading of lyrics

ncmpc 0.49 - (2023-08-04)
* fix UI freeze if lyrics plugin is stuck
Expand Down
26 changes: 21 additions & 5 deletions lyrics/60-google.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,30 @@ def normalize_parameter(s):
title = title.replace(" ", "+")
r = requests.get(
f"{base_url}/search?q={artist}+{title}+lyrics",
headers={"client": "google-csbe"},
headers={
"authority": "www.google.com",
# doesn't seem to work without this
"user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36",
"accept": "text/html,application/xhtml+xml"
}
)
try:
r.raise_for_status()
except:
exit(1)
soup = bs4.BeautifulSoup(r.text, "html5lib")
results = [x for x in soup.select("div") if len(x.select("div")) == 0]
# element with the longest text node is usually a match
results.sort(key=lambda n: len(n.text))
print(results[-1].text)
try:
lyrics_container = soup.select_one("div[data-lyricid]")
if not lyrics_container: raise IndexError
print(lyrics_container)
for tag in lyrics_container:
lyrics = tag.find_all("span")
for lyric in lyrics:
print(lyric.text)
break
except IndexError:
print("Lyrics not found :(")
exit(1)
except Exception as e:
print("Unknown error: ", e)
exit(2)

0 comments on commit 3fde694

Please sign in to comment.