Permalink
Browse files

Merge branch 'fix-fetch-lyrics' into integration

  • Loading branch information...
2 parents bb15b34 + 7fbb3d5 commit 5d695355ff58bfdc293e8d51483052189405b10e @multani committed Jan 25, 2011
Showing with 72 additions and 49 deletions.
  1. +39 −23 sonata/info.py
  2. +25 −18 sonata/lyricwiki.py
  3. +8 −8 sonata/main.py
View
@@ -329,18 +329,19 @@ def _update_lyrics(self, songinfo):
'not set.')))
def _check_for_local_lyrics(self, artist, title, song_dir):
- locations = [consts.LYRICS_LOCATION_HOME,
- consts.LYRICS_LOCATION_PATH,
- consts.LYRICS_LOCATION_HOME_ALT,
- consts.LYRICS_LOCATION_PATH_ALT]
+ locations = [
+ consts.LYRICS_LOCATION_HOME,
+ consts.LYRICS_LOCATION_PATH,
+ consts.LYRICS_LOCATION_HOME_ALT,
+ consts.LYRICS_LOCATION_PATH_ALT]
for location in locations:
filename = self.target_lyrics_filename(artist, title,
song_dir, location)
if os.path.exists(filename):
return filename
def get_lyrics_start(self, search_artist, search_title, filename_artist,
- filename_title, song_dir):
+ filename_title, song_dir, force_fetch=False):
filename_artist = misc.strip_all_slashes(filename_artist)
filename_title = misc.strip_all_slashes(filename_title)
filename = self._check_for_local_lyrics(filename_artist,
@@ -356,11 +357,19 @@ def get_lyrics_start(self, search_artist, search_title, filename_artist,
lyrics = f.read()
except IOError:
pass
+
if lyrics == _("Lyrics not found"):
- misc.remove_file(filename)
+ force_fetch = True
+
+ if force_fetch:
+ # Remove all lyrics for this song
+ while filename is not None:
filename = self._check_for_local_lyrics(filename_artist,
filename_title,
song_dir)
+ if filename is not None:
+ misc.remove_file(filename)
+
if filename:
# Re-use lyrics from file:
try:
@@ -510,24 +519,31 @@ def resize_elements(self, notebook_allocation):
def target_lyrics_filename(self, artist, title, song_dir,
force_location=None):
+ """get the filename of the lyrics of a song"""
+
+ cfg = self.config # alias for easier access
+
# FIXME Why did we have this condition here: if self.conn:
- lyrics_loc = force_location if force_location else \
- self.config.lyrics_location
- # Note: *_ALT searching is for compatibility with other mpd clients
- # (like ncmpcpp):
+ lyrics_loc = force_location if force_location else cfg.lyrics_location
+
if song_dir is not None:
song_dir.replace('%', '%%')
+
+ music_dir = cfg.musicdir[cfg.profile_num].replace('%', '%%')
+ pattern1 = "%s-%s.txt"
+ pattern2 = "%s - %s.txt"
+
+ # Note: *_ALT searching is for compatibility with other mpd clients
+ # (like ncmpcpp):
file_map = {
- consts.LYRICS_LOCATION_HOME: ("~/.lyrics", "%s-%s.txt"),
- consts.LYRICS_LOCATION_PATH:
- (self.config.musicdir[self.config.profile_num].replace('%', '%%'),
- song_dir, "%s-%s.txt"),
- consts.LYRICS_LOCATION_HOME_ALT: ("~/.lyrics", "%s - %s.txt"),
- consts.LYRICS_LOCATION_PATH_ALT:
- (self.config.musicdir[self.config.profile_num].replace('%', '%%'),
- song_dir, "%s - %s.txt"),
- }
- return misc.file_from_utf8(misc.file_exists_insensitive(
- os.path.expanduser(
- os.path.join(*file_map[lyrics_loc]))
- % (artist, title)))
+ consts.LYRICS_LOCATION_HOME: ("~/.lyrics", pattern1),
+ consts.LYRICS_LOCATION_PATH: (music_dir, song_dir, pattern1),
+ consts.LYRICS_LOCATION_HOME_ALT: ("~/.lyrics", pattern2),
+ consts.LYRICS_LOCATION_PATH_ALT: (music_dir, song_dir, pattern2),
+ }
+
+ file_path = os.path.join(*file_map[lyrics_loc])
+ file_path = os.path.expanduser(file_path) % (artist, title)
+
+ return misc.file_from_utf8(
+ misc.file_exists_insensitive(file_path))
View
@@ -1,6 +1,8 @@
+from HTMLParser import HTMLParser
import os
import urllib
import re
+import sys
import threading # get_lyrics_start starts a thread get_lyrics_thread
import gobject
@@ -37,34 +39,39 @@ def lyricwiki_editlink(self, songinfo):
(artist, title))
def get_lyrics_thread(self, callback, artist, title):
+
+ re_textarea = re.compile(r'<textarea[^>]*>')
+ NO_LYRICS = '<!-- PUT LYRICS HERE (and delete this entire line) -->'
+
+ def get_content(page):
+ content = page.read()
+ content = re_textarea.split(content)[1].split("</textarea>")[0]
+ # Transform HTML entities, like '&lt;' into '<', of the textarea
+ # content.
+ content = HTMLParser().unescape(content)
+ return content.strip()
+
try:
- lyricpage = urllib.urlopen(('http://lyrics.wikia.com/index.php?'
- 'title=%s:%s&action=edit') \
- % (self.lyricwiki_format(artist),
- self.lyricwiki_format(title))).read()
- content = re.split("<textarea[^>]*>",
- lyricpage)[1].split("</textarea>")[0]
- content = content.strip()
- redir_tag = "#redirect"
- if content[:len(redir_tag)].lower() == redir_tag:
+ addr = 'http://lyrics.wikia.com/index.php?title=%s:%s&action=edit' \
+ % (self.lyricwiki_format(artist), self.lyricwiki_format(title))
+ content = get_content(urllib.urlopen(addr))
+
+ if content.lower().startswith("#redirect"):
addr = "http://lyrics.wikia.com/index.php?title=%s&action=edit" \
% urllib.quote(content.split("[[")[1].split("]]")[0])
- lyricpage = urllib.urlopen(addr).read()
- content = re.split("<textarea[^>]*>",
- lyricpage)[1].split("</textarea>")[0]
- content = content.strip()
- lyrics = content.split(
- "&lt;lyrics&gt;")[1].split("&lt;/lyrics&gt;")[0].strip()
- if lyrics != ('&lt;!-- PUT LYRICS HERE '
- '(and delete this entire line) --&gt;'):
+ content = get_content(urllib.urlopen(addr))
+
+ lyrics = content.split("<lyrics>")[1].split("</lyrics>")[0].strip()
+ if lyrics != NO_LYRICS:
lyrics = misc.unescape_html(lyrics)
lyrics = misc.wiki_to_html(lyrics)
lyrics = lyrics.decode("utf-8")
self.call_back(callback, lyrics=lyrics)
else:
error = _("Lyrics not found")
self.call_back(callback, error=error)
- except:
+ except Exception, e:
+ print >> sys.stderr, "Error while fetching the lyrics:\n%s" % e
error = _("Fetching lyrics failed")
self.call_back(callback, error=error)
View
@@ -2209,15 +2209,15 @@ def on_lyrics_search(self, _event):
ui.show(dialog.vbox)
response = dialog.run()
if response == gtk.RESPONSE_ACCEPT:
- # Delete current lyrics:
- filename = self.info.target_lyrics_filename(artist, title, None,
- consts.LYRICS_LOCATION_HOME)
- misc.remove_file(filename)
# Search for new lyrics:
- self.info.get_lyrics_start(artist_entry.get_text(),
- title_entry.get_text(), artist, title,
- os.path.dirname(mpdh.get(self.songinfo,
- 'file')))
+ self.info.get_lyrics_start(
+ artist_entry.get_text(),
+ title_entry.get_text(),
+ artist,
+ title,
+ os.path.dirname(mpdh.get(self.songinfo, 'file')),
+ force_fetch=True)
+
dialog.destroy()
def mpd_shuffle(self, _action):

0 comments on commit 5d69535

Please sign in to comment.