# # Copyright (C) 2009 Anton Khirnov # # Nephilim is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # Nephilim is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with Nephilim. If not, see . # from PyQt4 import QtGui,QtCore from PyQt4.QtCore import QVariant import logging import socket from ..plugin import Plugin import LyricWiki_client class wgLyrics(QtGui.QWidget): txtView = None # text-object p = None # plugin def __init__(self, p, parent=None): QtGui.QWidget.__init__(self, parent) self.p = p self.curLyrics = '' self.txtView = QtGui.QTextEdit(self) self.txtView.setReadOnly(True) self.setLayout(QtGui.QVBoxLayout()) self.layout().setSpacing(0) self.layout().setMargin(0) self.layout().addWidget(self.txtView) def set_lyrics(self, song, lyrics): self.txtView.clear() if song: self.txtView.insertHtml('%s\n
%s
'\ '
\n\n'%(song.title(), song.artist())) else: return if lyrics: self.txtView.insertPlainText(lyrics) else: self.txtView.insertPlainText('Lyrics not found.') class Lyrics(Plugin): o = None DEFAULTS = {'sites' : ['lyricwiki'], 'lyricdir' : '$musicdir/$songdir', 'lyricname' : '.lyric_mpclient_$artist_$album_$song'} def _load(self): self.o = wgLyrics(self) self.connect(self.mpclient(), QtCore.SIGNAL('song_changed'), self.refresh) def _unload(self): self.o = None self.disconnect(self.mpclient(), QtCore.SIGNAL('song_changed'), self.refresh) def getInfo(self): return "Show (and fetch) the lyrics of the currently playing song." def _get_dock_widget(self): return self._create_dock(self.o) def refresh(self): lyrics = None song = self.mpclient().current_song() if not song: self.o.set_lyrics(None, None) return for site in self.settings().value(self.name() + '/sites').toStringList(): lyrics = eval('self.fetch_%s(song)'%site) if lyrics: self.o.set_lyrics(song, lyrics) return self.o.set_lyrics(song, None) def fetch_lyricwiki(self, song): soap = LyricWiki_client.LyricWikiBindingSOAP("http://lyricwiki.org/server.php") req = LyricWiki_client.getSongRequest() req.Artist = song.artist() req.Song = song.title() try: result = soap.getSong(req).Return.Lyrics.decode('utf-8').encode('iso8859').decode('utf-8') except socket.error, e: logging.error('Error downloading lyrics from LyricWiki: %s.'%e) return None return result if result != 'Not found' else None