Music Player Daemon Community Wiki


525pages on
this wiki
Add New Page
Talk0 Share
#!/usr/bin/env python
"""MPD lyrics script
   Usage: `python mpdlyrics' or `./mpdlyrics'
   Requires: mpc

- `replace' argument handles UnicodeEncodeError
- Terminal variable
- Python-3 compatibile

import os
  import urllib.request
  urlopen = urllib.request.urlopen
except ImportError:
  import urllib
  urlopen = urllib.urlopen

splitter = '()()'
lyrstartstr = b'<div id="lyrics">'
lyrendstr = b'</div>'

def execute(cmd):
  a = os.popen(cmd)
  return a.readlines()

# get artist and title
mpcoutput = execute(
              'mpc --format "%artist%' + splitter + '%title%"'

# parses title and artist
title = mpcoutput[1].strip()
artist = mpcoutput[0].strip()
title_ = title.replace(' ', '%20')
artist_ = artist.replace(' ', '%20')

# fetch lyrics
html = urlopen(
         '' + 
         artist_ + '&title=' + title_

# start and end position of lyrics at html-file
lyrstart = html.find(lyrstartstr)
lyrstart = lyrstart + len(lyrstartstr)
lyrend = html.find(lyrendstr, lyrstart)

# put lyrics in lyrics-variable
lyrics = html[lyrstart:lyrend].strip()

# little string manipulation
lyrics = lyrics.replace(b'<br />', b'').replace(b'"', b'"').strip()

# Opens new gnome-terminal titled "MPD Lyrics" and executes vim in read-only mode
execin = 'gnome-terminal -t "MPD Lyrics" -x vim -R'

# Opens new xfce4-terminal titled "<artist> - <title>" and executes less
#execin = 'terminal -T "%s - %s" -x less' %(artist, title)

if execin == "":
  lyrfile = open("/tmp/lyrtmp", 'w')
  lyrfile.write(lyrics.decode("utf-8", "replace"))
  os.system(execin + " /tmp/lyrtmp")

Ad blocker interference detected!

Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.

Also on Fandom

Random Wiki