Skip to content

Instantly share code, notes, and snippets.

@arnobaer
Created February 11, 2022 15:38
Show Gist options
  • Save arnobaer/92d41f60a6a6c2cf9466aa9d5f14d234 to your computer and use it in GitHub Desktop.
Save arnobaer/92d41f60a6a6c2cf9466aa9d5f14d234 to your computer and use it in GitHub Desktop.
Darklyrics from cmus for your terminal.
#!/usr/bin/env python3
import subprocess
import urllib.request
import os
import re
import sys
def download(artist: str, album: str) -> str:
url = f"http://www.darklyrics.com/lyrics/{artist}/{album}.html"
with urllib.request.urlopen(url) as f:
return f.read().decode('utf-8')
def main() -> None:
table = subprocess.check_output(['cmus-remote', '-Q']).decode('utf-8')
title = ''
album = ''
artist = ''
artist2 = ''
for line in table.split('\n'):
if 'tag title ' in line:
title = line.replace('tag title ', '')
if 'tag album ' in line:
album = line.replace('tag album ', '').lower().replace(' ', '')
album = album.replace('ö', 'oe').replace('ä', 'ae').replace('ü', 'ue')
if 'tag artist ' in line:
artist2 = line.replace('tag artist ', '')
artist = line.replace('tag artist ', '').lower().replace(' ', '')
s = download(artist, album)
s = s.replace('<br />', '')
s = s.split('<h3>')[1:]
s[-1] = s[-1].split('<div')[0][:-2]
c = re.compile('\<a.+\>\d{1,2}.\ (.+)\<\/a>')
width = os.get_terminal_size().columns if sys.stdout.isatty() else 80
for l in s:
l = l.split('</h3>')
m = c.match(l[0])
if m:
t = m.group(1)
if t[:10].lower() == title[:10].lower():
print()
print(f"{artist2} - {t}".center(width).rstrip())
text = l[1] + 'EE'
text = text.replace('<i>','').replace('</i>', '')
text = text.replace('\n\nEE', '\n')
for line in text.split('\n'):
print(line.center(width).rstrip())
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment