Fandom

Music Player Daemon Community Wiki

Mpdlyricsscript

525pages on
this wiki
Add New Page
Talk0 Share

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.


#! /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 = '

' lyrendstr = '
'
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('
', ).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()

Also on Fandom

Random Wiki