diff options
author | Anton Khirnov <wyskas@gmail.com> | 2009-09-03 18:09:59 +0200 |
---|---|---|
committer | Anton Khirnov <wyskas@gmail.com> | 2009-09-03 18:41:12 +0200 |
commit | 3cad6f70f5111db38e0b392a2f1a270ec927dd03 (patch) | |
tree | d054fb8a5d23287e0b6f5eeff77ff56a99371baf | |
parent | b674c1a0fbc4612e65139fc0f4f6d0b0a41329cb (diff) |
Lyrics: fix Lyricwiki. again.
i hope they stop doing that
also switch to QXmlStreamReader for parsing lyrics.
-rw-r--r-- | nephilim/plugins/Lyrics.py | 43 |
1 files changed, 25 insertions, 18 deletions
diff --git a/nephilim/plugins/Lyrics.py b/nephilim/plugins/Lyrics.py index 99eff72..2bd3ea4 100644 --- a/nephilim/plugins/Lyrics.py +++ b/nephilim/plugins/Lyrics.py @@ -19,7 +19,6 @@ from PyQt4 import QtGui, QtCore, QtNetwork from PyQt4.QtCore import QVariant import os -import re from lxml import etree from ..plugin import Plugin @@ -80,7 +79,7 @@ class LyricsWidget(QtGui.QWidget): #### public #### def set_lyrics(self, song, lyrics, flags = 0): - """Set currently displayed lyrics for song. flags parameter is + """Set currently displayed lyrics (unicode string) for song. flags parameter is unused now.""" if not song: self.__label.clear() @@ -95,7 +94,7 @@ class LyricsWidget(QtGui.QWidget): %(song['title'], song['artist'], song['album'])) if lyrics: self.logger.info('Setting new lyrics.') - self.__text_view.insertPlainText(lyrics.decode('utf-8')) + self.__text_view.insertPlainText(lyrics) self.lyrics_loaded = True else: self.logger.info('Lyrics not found.') @@ -195,23 +194,31 @@ class Lyrics(Plugin): return self.finish() self.logger.info('Found Lyricwiki song URL: %s.'%url) - self.rep = self.nam.get(QtNetwork.QNetworkRequest(url)) + # XXX temporary hack to work around lyricwiki.org -> lyrics.wikia.org transition + url.setHost('lyrics.wikia.com') + url.setPath('/lyrics%s'%url.path()) + req = QtNetwork.QNetworkRequest(url) + self.rep = self.nam.get(req) self.rep.finished.connect(self.__handle_lyrics) def __handle_lyrics(self): - #TODO this should use Qt xml functions too lyrics = '' - page = unicode(self.rep.readAll(), encoding = 'utf-8') - page = re.sub('<br>|<br/>|<br />', '\n', page) - try: - html = etree.HTML(page) - except etree.XMLSyntaxError, e: - self.logger.error('Error parsing lyrics: %s' %e) - return self.finish() + xml = QtCore.QXmlStreamReader(self.rep) + while not xml.atEnd(): + token = xml.readNext() + if token == QtCore.QXmlStreamReader.StartElement: + if xml.name() == 'div' and xml.attributes().value('class') == 'lyricbox': + while not xml.atEnd(): + token = xml.readNext() + if token == QtCore.QXmlStreamReader.EndElement and xml.name() == 'div': + break + elif token == QtCore.QXmlStreamReader.StartElement and xml.name() == 'br': + lyrics += '\n' + elif token == QtCore.QXmlStreamReader.Characters: + lyrics += xml.text() + if xml.hasError(): + self.logger.warning('Error parsing lyrics: %s'%xml.errorString()) - for elem in html.iterfind('.//div'): - if elem.get('class') == 'lyricbox': - lyrics += etree.tostring(elem, method = 'text', encoding = 'utf-8') self.finish(lyrics) class FetchAnimelyrics(common.MetadataFetcher): @@ -363,7 +370,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() + lyrics = file.read().decode('utf-8') file.close() if lyrics: return self.o.set_lyrics(song, lyrics) @@ -374,7 +381,7 @@ class Lyrics(Plugin): fetcher.fetch(song) def save_lyrics_file(self, lyrics, path = None): - """Save lyrics to a file specified in path. + """Save lyrics (unicode string) to a file specified in path. If path is None, then a default value is used.""" self.logger.info('Saving lyrics...') try: @@ -382,7 +389,7 @@ class Lyrics(Plugin): file = open(path, 'w') else: file = open(self.__lyrics_path, 'w') - file.write(lyrics) + file.write(lyrics.encode('utf-8')) file.close() self.logger.info('Lyrics successfully saved.') except IOError, e: |