check artist name when multiple artists may be returned
[clinton/xbmc-groove.git] / default.py
index 75cc6b0..796d1da 100644 (file)
@@ -16,7 +16,7 @@
 #    along with xbmc-groove.  If not, see <http://www.gnu.org/licenses/>.
 
 
-import urllib, sys, os, shutil, re, pickle, time, traceback, xbmcaddon, xbmcplugin, xbmcgui, xbmc
+import urllib, urllib2, sys, os, shutil, re, pickle, time, traceback, xbmcaddon, xbmcplugin, xbmcgui, xbmc
 
 __addon__     = xbmcaddon.Addon('plugin.audio.groove')
 __addonname__ = __addon__.getAddonInfo('name')
@@ -355,7 +355,8 @@ class Grooveshark:
         if (query != ''): 
             artists = groovesharkApi.getArtistSearchResults(query, limit = self.artistsearchlimit)
             if (len(artists) > 0):
-                artist = artists[0]
+                # check for artist name match, first result is sometimes not the closest lexical match
+                artist = next ((a for a in artists if a[0].lower() == query.lower()), artists[0])
                 artistID = artist[1]
                 if __debugging__ :
                     xbmc.log("Found " + artist[0] + "...")
@@ -464,7 +465,11 @@ class Grooveshark:
         if (query != ''): 
             artists = groovesharkApi.getArtistSearchResults(query, limit = self.artistsearchlimit)
             if (len(artists) > 0):
-                artist = artists[0]
+                # check for exact artist name match, sometimes a more
+                # popular artist is returned first (e.g. 'Angel Dust'
+                # gets you 'Faith No More' because of their popular
+                # album 'Angel Dust')
+                artist = next ((a for a in artists if a[0].lower() == query.lower()), artists[0])
                 artistID = artist[1]
                 if __debugging__ :
                     xbmc.log("Found " + artist[0] + "...")
@@ -728,8 +733,12 @@ class Grooveshark:
             localThumb = os.path.join(xbmc.translatePath(os.path.join(thumbDir, str(songid)))) + '.tbn'
             try:
                 if os.path.isfile(localThumb) == False:
-                    loc = urllib.URLopener()
-                    loc.retrieve(url, localThumb)
+                    headers = { 'User-Agent' : 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/32.0.1700.102 Chrome/32.0.1700.102 Safari/537.36' }
+                    req = urllib2.Request(url, None, headers)
+                    loc = urllib2.urlopen(req)
+                    output = open(localThumb,'wb')
+                    output.write(loc.read())
+                    output.close()
             except:
                 shutil.copy2(thumbDef, localThumb)
             return os.path.join(os.path.join(thumbDir, str(songid))) + '.tbn'
@@ -819,7 +828,7 @@ class Grooveshark:
         while i < n:
             album = albums[i]
             albumID = album[3]
-            if isverified or groovesharkApi.getDoesAlbumExist(albumID):                    
+            if isverified:
                 albumArtistName = album[0]
                 albumName = album[2]
                 albumImage = self._get_icon(album[4], 'album-' + str(albumID))
@@ -844,11 +853,8 @@ class Grooveshark:
         while i < n:
             artist = artists[i]
             artistID = artist[1]
-            if groovesharkApi.getDoesArtistExist(artistID):                    
-                artistName = artist[0]
-                self._add_dir(artistName, '', MODE_ARTIST, self.artistImg, artistID, itemsExisting)
-            else:
-                itemsExisting = itemsExisting - 1
+            artistName = artist[0]
+            self._add_dir(artistName, '', MODE_ARTIST, self.artistImg, artistID, itemsExisting)
             i = i + 1
         xbmcplugin.setContent(self._handle, 'artists')
         xbmcplugin.addSortMethod(self._handle, xbmcplugin.SORT_METHOD_ARTIST_IGNORE_THE)
@@ -929,21 +935,22 @@ class Grooveshark:
     def _getSongStream(self, songid):
         idSong = int(songid)
         stream = None
-        streams = []
+        streams = {}
         path = os.path.join(cacheDir, 'streams.dmp')
         try:
             f = open(path, 'rb')
             streams = pickle.load(f)
-            for song in streams:
-                if song[0] == idSong:
-                    duration = song[1]
-                    url = song[2]
-                    key = song[3]
-                    server = song[4]
-                    stream = [idSong, duration, url, key, server]
-                    if __debugging__ :
-                        xbmc.log("Found " + str(idSong) + " in stream cache")
-                    break;
+            song = streams[songid]
+
+            duration = song[1]
+            url = song[2]
+            key = song[3]
+            server = song[4]
+            stream = [idSong, duration, url, key, server]
+
+            if __debugging__ :
+                xbmc.log("Found " + str(idSong) + " in stream cache")
+
             f.close()
         except:
             pass
@@ -963,7 +970,7 @@ class Grooveshark:
         
     def _addSongStream(self, stream):
         streams = self._getStreams()           
-        streams.append(stream)                
+        streams[int(stream[0])] = stream
         path = os.path.join(cacheDir, 'streams.dmp')
         try:
             f = open(path, 'wb')
@@ -979,21 +986,17 @@ class Grooveshark:
         stream[1] = self._setDuration(stream[1])
         streams = self._getStreams()
         path = os.path.join(cacheDir, 'streams.dmp')
-        i = 0
 
-        for song in streams:
-            if song[0] == idStream:
-                streams[i] = stream
-                try:
-                    f = open(path, 'wb')
-                    pickle.dump(streams, f, protocol=pickle.HIGHEST_PROTOCOL)
-                    f.close()
-                    if __debugging__ :
-                        xbmc.log("Updated " + str(idStream) + " in stream cache")
-                    break;
-                except:
-                    xbmc.log("An error occurred setting stream")                    
-            i = i + 1
+        try:
+            streams[idStream] = stream
+            f = open(path, 'wb')
+            pickle.dump(streams, f, protocol=pickle.HIGHEST_PROTOCOL)
+            f.close()
+            if __debugging__ :
+                xbmc.log("Updated " + str(idStream) + " in stream cache")
+
+        except:
+            xbmc.log("An error occurred setting stream")
     
     def _getStreams(self):
         path = os.path.join(cacheDir, 'streams.dmp')
@@ -1002,7 +1005,7 @@ class Grooveshark:
             streams = pickle.load(f)
             f.close()
         except:
-            streams = []
+            streams = {}
             pass
         return streams