Skip to content

Commit

Permalink
lyricwiki: workaround shutdown of lyricwiki web API
Browse files Browse the repository at this point in the history
Ported to replace original lyricwiki support.
  • Loading branch information
tuukka authored and bebarino committed Sep 22, 2009
1 parent 475a694 commit 1cd70cc
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 44 deletions.
1 change: 0 additions & 1 deletion README
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ REQUIREMENTS:
(Required) MPD 0.12 or newer, possibly on another computer
(Optional) Gnome-python-extras for enhanced system tray
(Optional) taglib and tagpy for editing metadata
(Optional) ZSI 2.0 or newer for automatic lyrics fetching
(Optional) dbus-python for mmkeys, single instance support
(Building) GCC
(Building) python-dev (on some distros)
Expand Down
54 changes: 11 additions & 43 deletions sonata/lyricwiki.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@

import os, urllib

import os
import urllib
import re
import threading # get_lyrics_start starts a thread get_lyrics_thread

from socket import getdefaulttimeout as socketgettimeout
from socket import setdefaulttimeout as socketsettimeout

import gobject
ServiceProxy = None # importing tried when needed

import misc
import mpdhelper as mpdh
Expand Down Expand Up @@ -38,43 +34,17 @@ def lyricwiki_editlink(self, songinfo):
(artist, title))

def get_lyrics_thread(self, callback, artist, title):
# FIXME locking...
global ServiceProxy
if ServiceProxy is None:
try:
from ZSI import ServiceProxy
# Make sure we have the right version..
if not hasattr(ServiceProxy, 'ServiceProxy'):
ServiceProxy = None
except ImportError:
ServiceProxy = None
if ServiceProxy is None:
self.call_back(callback, None, None, error=_("ZSI not found, fetching lyrics support disabled."))
return

# FIXME locking...
if self.lyricServer is None:
wsdlFile = "http://lyricwiki.org/server.php?wsdl"
try:
self.lyricServer = True
timeout = socketgettimeout()
socketsettimeout(consts.LYRIC_TIMEOUT)
self.lyricServer = ServiceProxy.ServiceProxy(wsdlFile, cachedir=os.path.expanduser("~/.service_proxy_dir"))
except:
self.lyricServer = None
socketsettimeout(timeout)
error = _("Couldn't connect to LyricWiki")
self.call_back(callback, error=error)
return

try:
timeout = socketgettimeout()
socketsettimeout(consts.LYRIC_TIMEOUT)
lyrics = self.lyricServer.getSong(artist=self.lyricwiki_format(artist), song=self.lyricwiki_format(title))['return']["lyrics"]
if lyrics.lower() != "not found":
lyricpage = urllib.urlopen("http://lyricwiki.org/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]
if content.startswith("#REDIRECT [["):
addr = "http://lyricwiki.org/index.php?title=%s&action=edit" % urllib.quote(content.split("[[")[1].split("]]")[0])
content = urllib.urlopen(addr).read()
lyrics = content.split("&lt;lyrics&gt;")[1].split("&lt;/lyrics&gt;")[0]
if lyrics.strip() != "&lt;!-- PUT LYRICS HERE (and delete this entire line) --&gt;":
lyrics = misc.unescape_html(lyrics)
lyrics = misc.wiki_to_html(lyrics)
lyrics = lyrics.encode("ISO-8859-1")
lyrics = lyrics.decode("utf-8")
self.call_back(callback, lyrics=lyrics)
else:
error = _("Lyrics not found")
Expand All @@ -83,7 +53,5 @@ def get_lyrics_thread(self, callback, artist, title):
error = _("Fetching lyrics failed")
self.call_back(callback, error=error)

socketsettimeout(timeout)

def call_back(self, callback, lyrics=None, error=None):
gobject.timeout_add(0, callback, lyrics, error)

0 comments on commit 1cd70cc

Please sign in to comment.