Skip to content

Commit

Permalink
lyrics: refactor the get_lyrics function to avoid repetition and make…
Browse files Browse the repository at this point in the history
…s it clearer
  • Loading branch information
multani committed Jan 25, 2011
1 parent 389cacd commit 1f36c5c
Showing 1 changed file with 21 additions and 18 deletions.
39 changes: 21 additions & 18 deletions 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
Expand Down Expand Up @@ -37,34 +38,36 @@ 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")
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)

Expand Down

0 comments on commit 1f36c5c

Please sign in to comment.