summaryrefslogtreecommitdiff
path: root/nephilim/plugins/Lyrics.py
diff options
context:
space:
mode:
authorAnton Khirnov <wyskas@gmail.com>2009-08-10 09:52:52 +0200
committerAnton Khirnov <wyskas@gmail.com>2009-08-10 15:33:40 +0200
commitd78ddf35321c1eae3724617bd05a5e11f1b9394f (patch)
tree0c093ce5a9e7093b4a4d81ba7bb4bef5e97524f8 /nephilim/plugins/Lyrics.py
parent57299a0b3d8b6eb6376d38a7499564b95a76721b (diff)
Lyrics: fix and reenable LyricWiki
also get rid of dependency on ZSI in process.
Diffstat (limited to 'nephilim/plugins/Lyrics.py')
-rw-r--r--nephilim/plugins/Lyrics.py47
1 files changed, 29 insertions, 18 deletions
diff --git a/nephilim/plugins/Lyrics.py b/nephilim/plugins/Lyrics.py
index 819735c..b99aef2 100644
--- a/nephilim/plugins/Lyrics.py
+++ b/nephilim/plugins/Lyrics.py
@@ -15,8 +15,8 @@
# along with Nephilim. If not, see <http://www.gnu.org/licenses/>.
#
-from PyQt4 import QtGui,QtCore
-from PyQt4.QtCore import QVariant
+from PyQt4 import QtGui, QtCore
+from PyQt4.QtCore import QVariant
import socket
@@ -24,11 +24,13 @@ from ..plugin import Plugin
from .. import misc
_available_sites = []
-#try:
-# import LyricWiki_client
-# _available_sites.append('lyricwiki')
-#except ImportError:
-# print 'Lyrics: error importing LyricWiki. Make sure that ZSI is installed.'
+try:
+ import re
+ import urllib
+ from lxml import etree
+ _available_sites.append('lyricwiki')
+except ImportError:
+ print 'Lyrics: error importing LyricWiki. Make sure that lxml is installed.'
class wgLyrics(QtGui.QWidget):
txtView = None # text-object
@@ -50,7 +52,7 @@ class wgLyrics(QtGui.QWidget):
self.connect(self.p, QtCore.SIGNAL('new_lyrics_fetched'), self.set_lyrics)
- def set_lyrics(self, song, lyrics):
+ def set_lyrics(self, song, lyrics, flags = 0):
if not song:
return self.txtView.clear()
@@ -66,7 +68,7 @@ class wgLyrics(QtGui.QWidget):
if lyrics:
self.logger.info('Setting new lyrics.')
- self.txtView.insertPlainText(lyrics)
+ self.txtView.insertPlainText(lyrics.decode('utf-8'))
else:
self.logger.info('Lyrics not found.')
self.txtView.insertPlainText('Lyrics not found.')
@@ -76,6 +78,7 @@ class Lyrics(Plugin):
sites = []
lyrics_dir = None
lyrics_path = None
+
DEFAULTS = {'sites' : ['lyricwiki'], 'lyricdir' : '$musicdir/$songdir',
'lyricname' : '.lyrics_nephilim_$artist_$album_$title', 'store' : True}
@@ -116,7 +119,7 @@ class Lyrics(Plugin):
try:
self.logger.info('Trying to read lyrics from file %s.'%self.lyrics_path)
file = open(self.lyrics_path, 'r')
- lyrics = file.read().decode('utf-8')
+ lyrics = file.read()
file.close()
if lyrics:
return self.emit(QtCore.SIGNAL('new_lyrics_fetched'), song, lyrics)
@@ -136,7 +139,7 @@ class Lyrics(Plugin):
if lyrics:
try:
file = open(self.lyrics_path, 'w')
- file.write(lyrics.encode('utf-8'))
+ file.write(lyrics)
file.close()
except IOError, e:
self.logger.error('Error saving lyrics: %s'%e)
@@ -145,18 +148,26 @@ class Lyrics(Plugin):
self.emit(QtCore.SIGNAL('new_lyrics_fetched'), song, None)
def fetch_lyricwiki(self, song):
- soap = LyricWiki_client.LyricWikiBindingSOAP("http://lyricwiki.org/server.php")
- req = LyricWiki_client.getSongRequest()
- req.Artist = song.artist().encode('utf-8').decode('iso8859').encode('utf-8')
- req.Song = song.title().encode('utf-8').decode('iso8859').encode('utf-8')
+ url = 'http://lyricwiki.org/api.php?%s' %urllib.urlencode({'func':'getSong',
+ 'artist':song.artist(), 'song':song.title(), 'fmt':'xml'})
try:
- result = soap.getSong(req).Return.Lyrics.decode('utf-8').encode('iso8859').decode('utf-8')
+ # get url for lyrics
+ tree = etree.HTML(urllib.urlopen(url).read())
+ if tree.find('.//lyrics').text == 'Not found':
+ return None
+ #get page with lyrics and change <br> tags to newlines
+ url = tree.find('.//url').text
+ page = re.sub('<br>|<br/>|<br />', '\n', urllib.urlopen(url).read())
+ html = etree.HTML(page)
+ lyrics = ''
+ for elem in html.iterfind('.//div'):
+ if elem.get('class') == 'lyricbox':
+ lyrics += etree.tostring(elem, method = 'text', encoding = 'utf-8')
+ return lyrics
except socket.error, e:
self.logger.error('Error downloading lyrics from LyricWiki: %s.'%e)
return None
- return result if result != 'Not found' else None
-
class SettingsWidgetLyrics(Plugin.SettingsWidget):
lyricdir = None
lyricname = None