Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve lyrics functionality #137

Merged
merged 2 commits into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,4 +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
50 changes: 50 additions & 0 deletions lyrics/25-musixmatch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/usr/bin/env python3
import requests
import bs4
import sys
import html

def normalize(s):
for i in [' ', '\'']:
s = s.replace(i, '-')
for i in [',', '(', ')']:
s = s.replace(i, '')
return html.escape(s)

def main():
artist = normalize(sys.argv[1])
title = normalize(sys.argv[2])

try:
musixmatch_url = "https://www.musixmatch.com/lyrics/"
r = requests.get(
musixmatch_url + artist + "/" + title,
headers = {
"Host": "www.musixmatch.com",
# emulate a linux user
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36",
"Accept": "*/*"
}
)

if r.status_code == 404:
print("Lyrics not found :(", file=sys.stderr)
exit(1)

soup = bs4.BeautifulSoup(r.text, features="lxml")
p_tags = soup.find_all("p", {"class": "mxm-lyrics__content"})
if len(p_tags) == 0:
# Sometimes musixmatch shows a "Restricted Lyrics" page with a 200 status code
print("Unable to get lyrics :(")
exit(1)

for p in p_tags:
print(p.text)
exit(1)

except Exception as e:
print("Unknown error: ", e, file=sys.stderr)
exit(2)

if __name__ == '__main__':
main()
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)
1 change: 1 addition & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ if enable_lyrics_screen
conf.set_quoted('LYRICS_PLUGIN_DIR', lyrics_plugin_dir)
install_data(
'lyrics/20-azlyrics.py',
'lyrics/25-musixmatch.py',
'lyrics/30-karaoke_texty.py',
'lyrics/40-tekstowo.py',
'lyrics/50-genius.py',
Expand Down
Loading