Fandom

Music Player Daemon Community Wiki

Conky lyrics

525pages on
this wiki
Add New Page
Talk0 Share

Using a slightly modified mpdlyricsscript to show lyrics in conky.


.conkyrc

#default stuff
# ...

color0 ffffff # text
color1 909090 # topics/icons
mpd_host localhost

TEXT
${if_running mpd}${if_mpd_playing}${execi 5 /home/konni/bin/mpd_lyrics.py}${color1}MPD ${hr 2}
${color0}${font Liberation Mono Regular:size=10}${execi 5 cat /tmp/tracktmp}${font}

${color0}${execi 5 cat /tmp/lyrtmp}
$endif$endif
Lyrics

Showing Lyrics in Conky

mpd_lyrics.py
#! /usr/bin/env python

# MPD lyrics script for conky
# just a slightly modified version of this one
# http://mpd.wikia.com/wiki/Hack:mpdlyricsscript

import os
import time
import urllib
import textwrap
import sys

# Random stuff
splitter = '()()'
lyrstartstr = '<div id="lyrics">'
lyrendstr = '</div>'

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

def same(title):
	if os.access("/tmp/titletmp", os.F_OK):
		f_oldtitle = file("/tmp/titletmp", 'r')
		oldtitle = f_oldtitle.readline()
		f_oldtitle.close()
		if oldtitle == title:
				return 1 
		else:
				f_newtitle = file("/tmp/titletmp", 'w')
				f_newtitle.write(title)
				f_newtitle.close()
				return 0
	else:
		f_newtitle = file("/tmp/titletmp", 'w')
		f_newtitle.write(title)
		f_newtitle.close()
		return 0


# get artist and title
mpcoutput = execute('mpc --format "%artist%' + splitter + '%title%"')[0].split(splitter)
# mpd not playing, e.g. empty playlist
if len(mpcoutput) == 1:
	sys.exit()

# parse title and artist
artist = mpcoutput[0].strip()
title = mpcoutput[1].strip()
urlartist = artist.replace(' ', '%20')
urltitle = title.replace(' ', '%20')

# track has changed, need to fetch new data
if same(title) == 0:
	# fetch lyrics
	html = urllib.urlopen('http://www.lyricsplugin.com/winamp03/plugin/?artist=' + urlartist + '&title=' + urltitle).read()
	
	# gets start and end position of lyrics at html-file
	lyrstart = html.find(lyrstartstr) + len(lyrstartstr)
	lyrend = html.find(lyrendstr, lyrstart)
	
	# put lyrics in lyrics-variable
	lyrics = html[lyrstart:lyrend].strip()
	
	# remove html formatting
	lyrics = lyrics.replace('<br />', ).replace('"', '"').strip()
	# and dos line feeds
	lyrics = lyrics.replace('\r', )

	# wrap lines longer than 80 characters
	new_lyrics = ""
	lines = lyrics.split("\n")
	w = textwrap.TextWrapper(width=80,break_long_words=False,replace_whitespace=False)
	for line in lines:
		if len(line) > 80:
			line = '\n'.join(w.wrap(line))
		new_lyrics += line + "\n"

	# write new track and lyrics
	f_track = file("/tmp/tracktmp", 'w')
	f_track.write(artist + " - " + title + "\n")
	f_track.close()

	f_lyrics = file("/tmp/lyrtmp", 'w')
	f_lyrics.write(new_lyrics)
	f_lyrics.close()

Wikia will not display above code correctly. Line 66 (and other 'replace' methods) should read

lyrics = lyrics.replace('<br />', '').replace('"', '"').strip()


Notice two added apostophes.

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