Commits

Vladimir Kolev committed 4b6677f

Added Chartlyrics service to the lyrics search and updated the lyrlibdb library

  • Participants
  • Parent commits 9fcbb7d
  • Tags gsharkdown-0.3.2

Comments (0)

Files changed (2)

         if select[1] != None:
             index = self.playlist.get_path(select[1])[0]
             song = self.playitems[index]
-            songid = lib.lyrdblib.search(song['ArtistName'],
+            lyric = lib.lyrdblib.search(song['ArtistName'],
                                          song['SongName'])
-            if "ERROR" in songid:
-                self.show_error(songid)
+            if "ERROR" in lyric:
+                self.show_error(lyric)
             else:
-                lyric = lib.lyrdblib.getlyrics(songid)
                 builder = gtk.Builder()
                 builder.add_from_file('%s/data/lyrics_dialog.ui' % BASEPATH)
                 lyrics = builder.get_object('dialog1')
 #-*- encoding: utf-8 -*-
 
 import urllib2
+from xml.etree import ElementTree
 
 __doc__ = """Lyrbrary to work with the lyrdb webservices"""
 __author__ = "Vladimir Kolev <vladimir.r.kolev@gmail.com>"
 
 LOOKUP_URL = "http://webservices.lyrdb.com/lookup.php?q=%s&for=%s&agent=gSharkDown"
 GETLYR_URL = "http://webservices.lyrdb.com/getlyr.php?q=%s"
+CHART_URL = "http://api.chartlyrics.com/apiv1.asmx/SearchLyricDirect?artist=%s&song=%s"
 
 def search(artist, track, method="match"):
-    art = artist.replace(" ", "+")
-    track = track.replace(" ", "+")
-    query = art + "|" + track
-    
-    if method == "trackname":
-        query = track
-    elif method == "artist":
-        query = art
-    elif method == "fullt":
-        query = art + "+" + track
+    first = searchlyr(artist, track, method)
+    if "error" in first:
+        second = searchchart(artist, track)
+        if "error" in second:
+            lyrics = "ERROR: Cant't find lyrics for %s - %s" % (artist,
+                                                               track)
+        else:
+            lyrics = second
+    else:
+        lyrics = getlyrics(first)
 
-    req = urllib2.urlopen(LOOKUP_URL % (query, method))
-    result = req.read()
-    if "error" in result:
-        return "ERROR: Can't find lyrics for this track"
-    elif result == "":
-        return "ERROR: Can't find lyrics for this track"
-    else:
-        t = result.splitlines()
-        return t[0].split("\\")[0]
+    return lyrics
+
 
 def getlyrics(songid):
     req = urllib2.urlopen(GETLYR_URL % songid)
     lyric = req.read()
     return lyric
+
+def searchlyr(artist, track, method):
+    query = artist.replace(" ", "+")
+    query += "|"
+    query += track.replace(" ", "+")
+
+    if method == "trackname":
+        query = track.replace(" ", "+")
+    elif method == "artist":
+        query = artist.replace(" ", "+")
+    elif method == "fullt":
+        query = artist.replace(" ", "+")
+        query += "+"
+        query += track.replace(" ", "+")
+
+    req = urllib2.urlopen(LOOKUP_URL % (query, method))
+    result = req.read()
+    resp = ""
+    if "error" in result:
+        resp = "error"
+    elif result == "":
+        resp = "error"
+    else:
+        t = result.splitlines()
+        resp = t[0].split("\\")[0]
+    return resp
+
+def searchchart(artist, track):
+    artist = artist.replace(" ", "%20")
+    track = track.replace(" ", "%20")
+
+    req = urllib2.urlopen(CHART_URL % (artist, track))
+    result = req.read()
+    resp = ""
+    if "<LyricChecksum>" in result:
+        lyric = ElementTree.fromstring(result)
+        lyr = lyric.getchildren()[len(lyric.getchildren()) - 1]
+        resp = lyr.text
+    else:
+        resp = "error"
+    return resp