Author: duncan
Date: Wed Nov 14 14:55:15 2007
New Revision: 10116

Log:
[ 1831426 ] menu for unexact matches in lyrics plugin
Patch from Tanja Kotthaus applied


Modified:
   branches/rel-1-7/freevo/src/audio/plugins/lyrics.py
   branches/rel-1/freevo/src/audio/plugins/lyrics.py

Modified: branches/rel-1-7/freevo/src/audio/plugins/lyrics.py
==============================================================================
--- branches/rel-1-7/freevo/src/audio/plugins/lyrics.py (original)
+++ branches/rel-1-7/freevo/src/audio/plugins/lyrics.py Wed Nov 14 14:55:15 2007
@@ -33,9 +33,12 @@
 import urllib, urllib2
 
 from gui.PopupBox import PopupBox
+from gui.AlertBox import AlertBox
 from event import *
 from skin.widgets import ScrollableTextScreen
 
+from BeautifulSoup import BeautifulStoneSoup
+
 LEOLYRICS_AUTH = "Freevo"
 
 class PluginInterface(plugin.ItemPlugin):
@@ -51,66 +54,104 @@
     def __init__(self):
         plugin.ItemPlugin.__init__(self)
 
-    def search_lyrics(self, arg=None, menuw=None):
-        box = PopupBox(text=_('Searching lyrics...'))
-        box.show()
-        lyrics = self.fetch_lyric_leoslyrics()
-
-        if lyrics != False:
-            box.destroy()
-            ShowDetails(menuw, self.item, lyrics)
-        else:
-            box.destroy()
-            box = PopupBox(text=_('Lyrics not found, sorry...'))
-            box.show()
-            time.sleep(3)
-            box.destroy()
-        return
 
     def actions(self, item):
         self.item = item
 
         if item.type == 'audio':
             return [ (self.search_lyrics, _("Show lyrics"), self) ]
-
         return []
 
-    def fetch_lyric_leoslyrics(self):
+
+    def search_lyrics(self, arg=None, menuw=None):
+        box = PopupBox(text=_('Searching lyrics...'))
+        box.show()
+
         result_code = -1
         try:
             url = 
"http://api.leoslyrics.com/api_search.php?auth=%s&artist=%s&songtitle=%s"%(
                 LEOLYRICS_AUTH,
                 urllib.quote(self.item.info['artist'].encode('utf-8')),
                 urllib.quote(self.item.info['title'].encode('utf-8')))
+            _debug_(url,2)
             info = urllib2.urlopen(url).read()
             result_code = re.search('.+<response code="(\d)">.+',info).group(1)
         except:
             print "Error searching for artist/title."
-            return False
+
+        box.destroy()
 
         if result_code == '0': #ok
             #get the hid
             filter_info = re.search('.+hid="(.+)" exactMatch="true".+', info)
             if filter_info:
-                hid = '"' + filter_info.group(1) + '"'
+                # there is a exact match
+                self.fetch_lyric(arg=filter_info.group(1), menuw=menuw)
+            else:
+                # open a submenu with choices
+                self.show_choices(menuw=menuw, info= info)
+        else:
+            box = PopupBox(text=_('Lyrics not found, sorry...'))
+            box.show()
+            time.sleep(2)
+            box.destroy()
+
 
-            #fetch the new url
-            try:
-                url2 =  
"http://api.leoslyrics.com/api_lyrics.php?auth=%s&hid=%s"%(
-                        LEOLYRICS_AUTH,
-                        urllib.quote(hid.encode('utf-8')))
-                lyrics_raw = urllib2.urlopen(url2).read()
-                reg_expr = re.compile('.+<text>(.+)</text>.+', 
re.DOTALL|re.MULTILINE)
-                self.lyrics = re.search(reg_expr, lyrics_raw).group(1)
-                self.lyrics = self.lyrics.replace("&#xD;", os.linesep)
-                self.lyrics = self.lyrics.replace('\r\n','')
-                self.lyrics = self.lyrics.replace('&amp;','&')
-                return Unicode(self.lyrics, encoding='utf-8')
-            except:
-                print "Error fetching lyrics."
-                return False
+
+    def show_choices(self, menuw, info):
+        items = []
+        soup = BeautifulStoneSoup(info,selfClosingTags=['feat'])
+        results = soup.findAll('result')
+        for result in results[:20]:
+            # for performance reasons show the first possibilities only,
+            # the more sensible hits are at the beginning of the list
+            hid = result['hid']
+            title = result.titleTag.string.replace('&amp;', '&')
+            artist = result.artistTag.nameTag.string.replace('&amp;','&')
+            items.append(menu.MenuItem('%s - %s' % (title, artist),
+                         action=self.fetch_lyric, arg=hid))
+        if len(items) > 0:
+            msgtext = _('No exact match. ')
+            msgtext+= _('Here are some sugestions.')
+            box = PopupBox(text = msgtext)
+            box.show()
+            time.sleep(2)
+            box.destroy()
+            choices_menu = menu.Menu(_('Choices'), items)
+            menuw.pushmenu(choices_menu)
         else:
-            return False
+            box = PopupBox(text= _('Lyrics not found, sorry...'))
+            box.show()
+            time.sleep(3)
+            box.destroy()
+
+
+
+    def fetch_lyric(self, arg=None, menuw=None):
+        #fetch the new url
+        try:
+            hid = '"' + arg + '"'
+            url2 =  "http://api.leoslyrics.com/api_lyrics.php?auth=%s&hid=%s"%(
+                    LEOLYRICS_AUTH,
+                    urllib.quote(hid.encode('utf-8')))
+            lyrics_raw = urllib2.urlopen(url2).read()
+            reg_expr = re.compile('.+<text>(.+)</text>.+', 
re.DOTALL|re.MULTILINE)
+            lyrics = re.search(reg_expr, lyrics_raw).group(1)
+            lyrics = lyrics.replace("&#xD;", os.linesep)
+            lyrics = lyrics.replace('\r\n','')
+            lyrics = lyrics.replace('&amp;','&')
+            lyrics = Unicode(lyrics, encoding='utf-8')
+        except:
+            print "Error fetching lyrics."
+
+        if lyrics:
+            ShowDetails(menuw, self.item, lyrics)
+        else:
+            box = PopupBox(text=_('Lyrics not found, sorry...'))
+            box.show()
+            time.sleep(2)
+            box.destroy()
+
 
 ########################
 # Show Text

Modified: branches/rel-1/freevo/src/audio/plugins/lyrics.py
==============================================================================
--- branches/rel-1/freevo/src/audio/plugins/lyrics.py   (original)
+++ branches/rel-1/freevo/src/audio/plugins/lyrics.py   Wed Nov 14 14:55:15 2007
@@ -33,9 +33,12 @@
 import urllib, urllib2
 
 from gui.PopupBox import PopupBox
+from gui.AlertBox import AlertBox
 from event import *
 from skin.widgets import ScrollableTextScreen
 
+from BeautifulSoup import BeautifulStoneSoup
+
 LEOLYRICS_AUTH = "Freevo"
 
 class PluginInterface(plugin.ItemPlugin):
@@ -51,66 +54,104 @@
     def __init__(self):
         plugin.ItemPlugin.__init__(self)
 
-    def search_lyrics(self, arg=None, menuw=None):
-        box = PopupBox(text=_('Searching lyrics...'))
-        box.show()
-        lyrics = self.fetch_lyric_leoslyrics()
-
-        if lyrics != False:
-            box.destroy()
-            ShowDetails(menuw, self.item, lyrics)
-        else:
-            box.destroy()
-            box = PopupBox(text=_('Lyrics not found, sorry...'))
-            box.show()
-            time.sleep(3)
-            box.destroy()
-        return
 
     def actions(self, item):
         self.item = item
 
         if item.type == 'audio':
             return [ (self.search_lyrics, _("Show lyrics"), self) ]
-
         return []
 
-    def fetch_lyric_leoslyrics(self):
+
+    def search_lyrics(self, arg=None, menuw=None):
+        box = PopupBox(text=_('Searching lyrics...'))
+        box.show()
+
         result_code = -1
         try:
             url = 
"http://api.leoslyrics.com/api_search.php?auth=%s&artist=%s&songtitle=%s"%(
                 LEOLYRICS_AUTH,
                 urllib.quote(self.item.info['artist'].encode('utf-8')),
                 urllib.quote(self.item.info['title'].encode('utf-8')))
+            _debug_(url,2)
             info = urllib2.urlopen(url).read()
             result_code = re.search('.+<response code="(\d)">.+',info).group(1)
         except:
             print "Error searching for artist/title."
-            return False
+
+        box.destroy()
 
         if result_code == '0': #ok
             #get the hid
             filter_info = re.search('.+hid="(.+)" exactMatch="true".+', info)
             if filter_info:
-                hid = '"' + filter_info.group(1) + '"'
+                # there is a exact match
+                self.fetch_lyric(arg=filter_info.group(1), menuw=menuw)
+            else:
+                # open a submenu with choices
+                self.show_choices(menuw=menuw, info= info)
+        else:
+            box = PopupBox(text=_('Lyrics not found, sorry...'))
+            box.show()
+            time.sleep(2)
+            box.destroy()
+
 
-            #fetch the new url
-            try:
-                url2 =  
"http://api.leoslyrics.com/api_lyrics.php?auth=%s&hid=%s"%(
-                        LEOLYRICS_AUTH,
-                        urllib.quote(hid.encode('utf-8')))
-                lyrics_raw = urllib2.urlopen(url2).read()
-                reg_expr = re.compile('.+<text>(.+)</text>.+', 
re.DOTALL|re.MULTILINE)
-                self.lyrics = re.search(reg_expr, lyrics_raw).group(1)
-                self.lyrics = self.lyrics.replace("&#xD;", os.linesep)
-                self.lyrics = self.lyrics.replace('\r\n','')
-                self.lyrics = self.lyrics.replace('&amp;','&')
-                return Unicode(self.lyrics, encoding='utf-8')
-            except:
-                print "Error fetching lyrics."
-                return False
+
+    def show_choices(self, menuw, info):
+        items = []
+        soup = BeautifulStoneSoup(info,selfClosingTags=['feat'])
+        results = soup.findAll('result')
+        for result in results[:20]:
+            # for performance reasons show the first possibilities only,
+            # the more sensible hits are at the beginning of the list
+            hid = result['hid']
+            title = result.titleTag.string.replace('&amp;', '&')
+            artist = result.artistTag.nameTag.string.replace('&amp;','&')
+            items.append(menu.MenuItem('%s - %s' % (title, artist),
+                         action=self.fetch_lyric, arg=hid))
+        if len(items) > 0:
+            msgtext = _('No exact match. ')
+            msgtext+= _('Here are some sugestions.')
+            box = PopupBox(text = msgtext)
+            box.show()
+            time.sleep(2)
+            box.destroy()
+            choices_menu = menu.Menu(_('Choices'), items)
+            menuw.pushmenu(choices_menu)
         else:
-            return False
+            box = PopupBox(text= _('Lyrics not found, sorry...'))
+            box.show()
+            time.sleep(3)
+            box.destroy()
+
+
+
+    def fetch_lyric(self, arg=None, menuw=None):
+        #fetch the new url
+        try:
+            hid = '"' + arg + '"'
+            url2 =  "http://api.leoslyrics.com/api_lyrics.php?auth=%s&hid=%s"%(
+                    LEOLYRICS_AUTH,
+                    urllib.quote(hid.encode('utf-8')))
+            lyrics_raw = urllib2.urlopen(url2).read()
+            reg_expr = re.compile('.+<text>(.+)</text>.+', 
re.DOTALL|re.MULTILINE)
+            lyrics = re.search(reg_expr, lyrics_raw).group(1)
+            lyrics = lyrics.replace("&#xD;", os.linesep)
+            lyrics = lyrics.replace('\r\n','')
+            lyrics = lyrics.replace('&amp;','&')
+            lyrics = Unicode(lyrics, encoding='utf-8')
+        except:
+            print "Error fetching lyrics."
+
+        if lyrics:
+            ShowDetails(menuw, self.item, lyrics)
+        else:
+            box = PopupBox(text=_('Lyrics not found, sorry...'))
+            box.show()
+            time.sleep(2)
+            box.destroy()
+
 
 ########################
 # Show Text

-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems?  Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >> http://get.splunk.com/
_______________________________________________
Freevo-cvslog mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/freevo-cvslog

Reply via email to