Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

lyrics: refactor the get_lyrics function to avoid repetition and make…

…s it clearer
  • Loading branch information...
commit 1f36c5c5f1a8ad680cd54b8d811b3b428c99a4ed 1 parent 389cacd
@multani authored
Showing with 21 additions and 18 deletions.
  1. +21 −18 sonata/lyricwiki.py
View
39 sonata/lyricwiki.py
@@ -1,6 +1,7 @@
import os
import urllib
import re
+import sys
import threading # get_lyrics_start starts a thread get_lyrics_thread
import gobject
@@ -37,26 +38,27 @@ def lyricwiki_editlink(self, songinfo):
(artist, title))
def get_lyrics_thread(self, callback, artist, title):
+
+ re_textarea = re.compile(r'<textarea[^>]*>')
+ NO_LYRICS = '&lt;!-- PUT LYRICS HERE (and delete this entire line) --&gt;'
+
+ def get_content(page):
+ content = page.read()
+ content = re_textarea.split(content)[1].split("</textarea>")[0]
+ 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("&lt;lyrics&gt;")[1].split("&lt;/lyrics&gt;")[0].strip()
+ if lyrics != NO_LYRICS:
lyrics = misc.unescape_html(lyrics)
lyrics = misc.wiki_to_html(lyrics)
lyrics = lyrics.decode("utf-8")
@@ -64,7 +66,8 @@ def get_lyrics_thread(self, callback, artist, title):
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)
Please sign in to comment.
Something went wrong with that request. Please try again.