Big update for v3 API.
authorstephendenham <stephendenham@2dec19e3-eb1d-4749-8193-008c8bba0994>
Sun, 21 Aug 2011 10:40:12 +0000 (10:40 +0000)
committerstephendenham <stephendenham@2dec19e3-eb1d-4749-8193-008c8bba0994>
Sun, 21 Aug 2011 10:40:12 +0000 (10:40 +0000)
git-svn-id: svn://svn.code.sf.net/p/xbmc-groove/code@47 2dec19e3-eb1d-4749-8193-008c8bba0994

addon.xml
changelog.txt
default.py
default.tbn
description.xml
fanart.png
icon.png
resources/img/default.tbn
resources/lib/GroovesharkAPI.py
resources/settings.xml

index d4ab73e..22f0fe0 100644 (file)
--- a/addon.xml
+++ b/addon.xml
@@ -1,6 +1,6 @@
 <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
 <addon id="plugin.audio.groove" name="Grooveshark XBMC"
-       version="0.4.0" provider-name="Stephen Denham">
+       version="0.5.1" provider-name="Stephen Denham">
        <requires>
                <import addon="xbmc.python" version="1.0" />
        </requires>
index 6d76714..b92bc62 100644 (file)
@@ -1,3 +1,11 @@
+0.5.1:
+
+Refactor getting song duration to improve performance.
+
+0.5.0:
+
+Use new version 3 API.
+
 0.4.0:
 
 Add optional 1.0 API for those with a session id.
index d4c6dfa..ff6d2bd 100644 (file)
@@ -1,4 +1,4 @@
-import urllib, sys, os, shutil, re, time, xbmcaddon, xbmcplugin, xbmcgui, xbmc, pickle
+import urllib, sys, os, shutil, re, pickle, time, tempfile, xbmcaddon, xbmcplugin, xbmcgui, xbmc
 MODE_SEARCH_SONGS = 1
 MODE_SEARCH_ALBUMS = 2
 MODE_SEARCH_ARTISTS = 3
@@ -34,6 +34,13 @@ ACTION_PREVIOUS_MENU = 10
 ARTIST_ALBUM_NAME_LABEL = 0
 NAME_ALBUM_ARTIST_LABEL = 1
 
+# Stream marking time (seconds)
+STREAM_MARKING_TIME = 30
+
+songMarkTime = 0
+player = xbmc.Player()
+playTimer = None
+
 baseDir = os.getcwd()
 resDir = xbmc.translatePath(os.path.join(baseDir, 'resources'))
 libDir = xbmc.translatePath(os.path.join(resDir,  'lib'))
@@ -54,7 +61,7 @@ listBackground = os.path.join(imgDir, 'listbackground.png')
 
 sys.path.append (libDir)
 from GroovesharkAPI import GrooveAPI
-from GroovesharkAPI import GrooveAPIv1
+from threading import Event, Thread
 
 try:
     groovesharkApi = GrooveAPI()
@@ -65,7 +72,24 @@ except:
     dialog.ok('Grooveshark XBMC', 'Unable to connect with Grooveshark.', 'Please try again later')
     sys.exit(-1)
   
-
+# Mark song as playing or played
+def markSong(songid, duration):
+    global songMarkTime
+    global playTimer
+    global player
+    if player.isPlayingAudio():
+        tNow = player.getTime()
+        if tNow >= STREAM_MARKING_TIME and songMarkTime == 0:
+            groovesharkApi.markStreamKeyOver30Secs()
+            songMarkTime = tNow
+        elif duration > tNow and duration - tNow < 2 and songMarkTime >= STREAM_MARKING_TIME:
+            playTimer.cancel()
+            songMarkTime = 0
+            groovesharkApi.markSongComplete(songid)
+    else:
+        playTimer.cancel()
+        songMarkTime = 0
+            
 class _Info:
     def __init__( self, *args, **kwargs ):
         self.__dict__.update( kwargs )
@@ -131,7 +155,44 @@ class GroovesharkPlaylistSelect(xbmcgui.WindowDialog):
         elif action == ACTION_MOVE_UP or action == ACTION_MOVE_DOWN or action == ACTION_PAGE_UP or action == ACTION_PAGE_DOWN == 6:
             self.setFocus(self.playlistControl)
             self.setHighlight()
-     
+
+class PlayTimer(Thread):
+    # interval -- floating point number specifying the number of seconds to wait before executing function
+    # function -- the function (or callable object) to be executed
+
+    # iterations -- integer specifying the number of iterations to perform
+    # args -- list of positional arguments passed to function
+    # kwargs -- dictionary of keyword arguments passed to function
+    
+    def __init__(self, interval, function, iterations=0, args=[], kwargs={}):
+        Thread.__init__(self)
+        self.interval = interval
+        self.function = function
+        self.iterations = iterations
+        self.args = args
+        self.kwargs = kwargs
+        self.finished = Event()
+    def run(self):
+        count = 0
+        while not self.finished.isSet() and (self.iterations <= 0 or count < self.iterations):
+            self.finished.wait(self.interval)
+            if not self.finished.isSet():
+                self.function(*self.args, **self.kwargs)
+                count += 1
+    def cancel(self):
+        self.finished.set()
+    
+    def setIterations(self, iterations):
+        self.iterations = iterations
+        
+
+    def getTime(self):
+        return self.iterations * self.interval
+
+
 class Groveshark:
     
     albumImg = xbmc.translatePath(os.path.join(imgDir, 'album.png'))
@@ -153,7 +214,6 @@ class Groveshark:
     songspagelimit = int(settings.getSetting('songspagelimit'))
     username = settings.getSetting('username')
     password = settings.getSetting('password')
-    sessionidv1 = settings.getSetting('sessionidv1')
     userid = 0
     
     def __init__( self ):
@@ -166,8 +226,6 @@ class Groveshark:
             os.makedirs(artDir)
             xbmc.log("Made " + artDir)
             
-        self.groovesharkApiv1 = GrooveAPIv1(self.sessionidv1)
-            
     # Top-level menu
     def categories(self):
 
@@ -180,16 +238,17 @@ class Groveshark:
         self._add_dir('Search for albums...', '', MODE_SEARCH_ALBUMS, self.albumImg, 0)
         self._add_dir('Search for artists...', '', MODE_SEARCH_ARTISTS, self.artistImg, 0)
         self._add_dir(searchArtistsAlbumsName, '', MODE_SEARCH_ARTISTS_ALBUMS, self.artistsAlbumsImg, 0)
-        self._add_dir("Search for user's playlists...", '', MODE_SEARCH_PLAYLISTS, self.usersplaylistsImg, 0)
-        self._add_dir('Popular songs for artist...', '', MODE_ARTIST_POPULAR, self.popularSongsArtistImg, 0)
-        self._add_dir('Popular songs', '', MODE_POPULAR_SONGS, self.popularSongsImg, 0)
+        # Not supported by key
+        #self._add_dir("Search for user's playlists...", '', MODE_SEARCH_PLAYLISTS, self.usersplaylistsImg, 0)
+        self._add_dir('Popular Grooveshark songs for artist...', '', MODE_ARTIST_POPULAR, self.popularSongsArtistImg, 0)
+        self._add_dir('Popular Grooveshark songs', '', MODE_POPULAR_SONGS, self.popularSongsImg, 0)
         if (self.userid != 0):
-            self._add_dir('My favorites', '', MODE_FAVORITES, self.favoritesImg, 0)
-            self._add_dir('My playlists', '', MODE_PLAYLISTS, self.playlistImg, 0)
+            self._add_dir('My Grooveshark favorites', '', MODE_FAVORITES, self.favoritesImg, 0)
+            self._add_dir('My Grooveshark playlists', '', MODE_PLAYLISTS, self.playlistImg, 0)
 
     # Search for songs            
     def searchSongs(self):
-        query = self._get_keyboard(default="", heading="Search for songs")
+        query = self._get_keyboard(default="", heading='Search for songs powered by Grooveshark')
         if (query != ''):
             songs = groovesharkApi.getSongSearchResults(query, limit = self.songsearchlimit)
             if (len(songs) > 0):
@@ -203,7 +262,7 @@ class Groveshark:
     
     # Search for albums
     def searchAlbums(self):
-        query = self._get_keyboard(default="", heading="Search for albums")
+        query = self._get_keyboard(default="", heading='Search for albums powered by Grooveshark')
         if (query != ''): 
             albums = groovesharkApi.getAlbumSearchResults(query, limit = self.albumsearchlimit)
             if (len(albums) > 0):
@@ -217,7 +276,7 @@ class Groveshark:
     
     # Search for artists
     def searchArtists(self):
-        query = self._get_keyboard(default="", heading="Search for artists")
+        query = self._get_keyboard(default="", heading='Search for artists powered by Grooveshark')
         if (query != ''): 
             artists = groovesharkApi.getArtistSearchResults(query, limit = self.artistsearchlimit)
             if (len(artists) > 0):
@@ -233,7 +292,7 @@ class Groveshark:
     def searchPlaylists(self):
         query = self._get_keyboard(default="", heading="Username")
         if (query != ''): 
-            playlists = groovesharkApi.getUserPlaylistsEx(query)
+            playlists = groovesharkApi.getUserPlaylistsByUsername(query)
             if (len(playlists) > 0):
                 self._add_playlists_directory(playlists)
             else:
@@ -246,7 +305,7 @@ class Groveshark:
     # Search for artists albums
     def searchArtistsAlbums(self, artistName = None):
         if artistName == None or artistName == searchArtistsAlbumsName:
-            query = self._get_keyboard(default="", heading="Search for artist's albums")
+            query = self._get_keyboard(default="", heading="Search for artist's albums powered by Grooveshark")
         else:
             query = artistName
         if (query != ''): 
@@ -319,10 +378,10 @@ class Groveshark:
             
     # Remove song from favorites
     def unfavorite(self, songid, prevMode=0):
-        userid = self._get_login(version = 1)
+        userid = self._get_login()
         if (userid != 0):
             xbmc.log("Unfavorite song: " + str(songid) + ', previous mode was ' + str(prevMode))
-            self.groovesharkApiv1.unfavorite(songID = songid)
+            groovesharkApi.removeUserFavoriteSongs(songIDs = songid)
             xbmc.executebuiltin('XBMC.Notification(Grooveshark XBMC, Removed from Grooveshark favorites, 1000, ' + thumbDef + ')')
             # Refresh to remove item from directory
             if (int(prevMode) == MODE_FAVORITES):
@@ -377,15 +436,33 @@ class Groveshark:
             
     # Play a song
     def playSong(self, item):
-        songid = item.getProperty('songid')
-        song = groovesharkApi.getSongURLFromSongID(songid)
-        if song != '':
-            item.setPath(song)
-            xbmc.log("Playing: " + song)
+        global playTimer
+        global player
+        if item != None:
+            songid = item.getProperty('songid')
+            stream = groovesharkApi.getSubscriberStreamKey(songid)
+            url = stream['url']
+            item.setPath(url)
+            xbmc.log("Grooveshark playing: " + url)
             xbmcplugin.setResolvedUrl(handle=int(sys.argv[1]), succeeded=True, listitem=item)
+            # Wait for play then start time
+            seconds = 0
+            while seconds < STREAM_MARKING_TIME:
+                try:
+                    if player.isPlayingAudio() == True:
+                        if playTimer != None:
+                            playTimer.cancel()
+                            songMarkTime = 0
+                        duration = int(item.getProperty('duration'))
+                        playTimer = PlayTimer(1, markSong, duration, [songid, duration])
+                        playTimer.start()
+                        break
+                except: pass
+                time.sleep(1)
+                seconds = seconds + 1
         else:
             xbmc.executebuiltin('XBMC.Notification(Grooveshark XBMC, Unable to play song, 1000, ' + thumbDef + ')')
-    
+        
     # Make a song directory item
     def songItem(self, songid, name, album, artist, coverart, trackLabelFormat=ARTIST_ALBUM_NAME_LABEL):
         songImg = self._get_icon(coverart, 'song-' + str(songid) + "-image")
@@ -393,8 +470,9 @@ class Groveshark:
             trackLabel = name + " - " + album + " - " + artist
         else:
             trackLabel = artist + " - " + album + " - " + name
+        duration = self._getSongDuration(songid)
         item = xbmcgui.ListItem(label = trackLabel, thumbnailImage=songImg, iconImage=songImg)
-        item.setInfo( type="music", infoLabels={ "title": name, "album": album, "artist": artist} )
+        item.setInfo( type="music", infoLabels={ "title": name, "album": album, "artist": artist, "duration": duration} )
         item.setProperty('mimetype', 'audio/mpeg')
         item.setProperty("IsPlayable", "true")
         item.setProperty('songid', str(songid))
@@ -402,6 +480,7 @@ class Groveshark:
         item.setProperty('title', name)
         item.setProperty('album', album)
         item.setProperty('artist', artist)
+        item.setProperty('duration', str(duration))
         
         return item
     
@@ -411,7 +490,7 @@ class Groveshark:
         
     # Make a playlist from an album      
     def makePlaylist(self, albumid, name):
-        userid = self._get_login(version = 1)
+        userid = self._get_login()
         if (userid != 0):
             re.split(' - ',name,1)
             nameTokens = re.split(' - ',name,1) # suggested name
@@ -421,7 +500,7 @@ class Groveshark:
                 songids = []
                 for song in album:
                     songids.append(song[1])
-                if self.groovesharkApiv1.playlistCreateUnique(name, songids) == 0:
+                if groovesharkApi.createPlaylist(name, songids) == 0:
                     dialog = xbmcgui.Dialog()
                     dialog.ok('Grooveshark XBMC', 'Cannot create Grooveshark playlist ', name)
                 else:
@@ -432,12 +511,12 @@ class Groveshark:
     
     # Rename a playlist
     def renamePlaylist(self, playlistid, name):
-        userid = self._get_login(version = 1)
+        userid = self._get_login()
         if (userid != 0):
             newname = self._get_keyboard(default=name, heading="Grooveshark playlist name")
             if newname == '':
                 return
-            elif self.groovesharkApiv1.playlistRename(playlistid, newname) == 0:
+            elif groovesharkApi.playlistRename(playlistid, newname) == 0:
                 dialog = xbmcgui.Dialog()
                 dialog.ok('Grooveshark XBMC', 'Cannot rename Grooveshark playlist ', name)
             else:
@@ -451,9 +530,9 @@ class Groveshark:
     def removePlaylist(self, playlistid, name):
         dialog = xbmcgui.Dialog()
         if dialog.yesno('Grooveshark XBMC', name, 'Delete this Grooveshark playlist?') == True:
-            userid = self._get_login(version = 1)
+            userid = self._get_login()
             if (userid != 0):
-                if self.groovesharkApiv1.playlistDelete(playlistid) == 0:
+                if groovesharkApi.playlistDelete(playlistid) == 0:
                     dialog = xbmcgui.Dialog()
                     dialog.ok('Grooveshark XBMC', 'Cannot remove Grooveshark playlist ', name)
                 else:
@@ -465,7 +544,7 @@ class Groveshark:
 
     # Add song to playlist
     def addPlaylistSong(self, songid):
-        userid = self._get_login(version = 1)
+        userid = self._get_login()
         if (userid != 0):
             playlists = groovesharkApi.getUserPlaylists()
             if (len(playlists) > 0):
@@ -483,7 +562,7 @@ class Groveshark:
                         if name != '':
                             songIds = []
                             songIds.append(songid)
-                            if self.groovesharkApiv1.playlistCreateUnique(name, songIds) == 0:
+                            if groovesharkApi.createPlaylist(name, songIds) == 0:
                                 dialog = xbmcgui.Dialog()
                                 dialog.ok('Grooveshark XBMC', 'Cannot create Grooveshark playlist ', name)
                             else:
@@ -493,8 +572,13 @@ class Groveshark:
                         playlist = playlists[i]
                         playlistid = playlist[1]
                         xbmc.log("Add song " + str(songid) + " to playlist " + str(playlistid))
-                        ret = self.groovesharkApiv1.playlistAddSong(playlistid, songid, 0)
-                        if ret == 0:
+                        songIDs=[]
+                        songs = groovesharkApi.getPlaylistSongs(playlistid)
+                        for song in songs:
+                            songIDs.append(song[1])
+                        songIDs.append(songid)
+                        ret = groovesharkApi.setPlaylistSongs(playlistid, songIDs)
+                        if ret == False:
                             dialog = xbmcgui.Dialog()
                             dialog.ok('Grooveshark XBMC', 'Cannot add to playlist ')
                         else:    
@@ -508,12 +592,18 @@ class Groveshark:
             dialog.ok('Grooveshark XBMC', 'You must be logged in', 'to add a song to a Grooveshark playlist.')
 
     # Remove song from playlist
-    def removePlaylistSong(self, playlistid, playlistname, songpos):
+    def removePlaylistSong(self, playlistid, playlistname, songid):
         dialog = xbmcgui.Dialog()
         if dialog.yesno('Grooveshark XBMC', 'Delete this song from', 'the Grooveshark playlist?') == True:
-            userid = self._get_login(version = 1)
+            userid = self._get_login()
             if (userid != 0):
-                if self.groovesharkApiv1.playlistDeleteSong(playlistid, songpos) == 0:
+                songs = groovesharkApi.getPlaylistSongs(playlistID)
+                songIDs=[]
+                for song in songs:
+                    if (song[1] != songid):
+                        songIDs.append(song[1])
+                ret = groovesharkApi.setPlaylistSongs(playlistID, songIDs)
+                if ret == False:
                     dialog = xbmcgui.Dialog()
                     dialog.ok('Grooveshark XBMC', 'Failed to remove', 'song from Grooveshark playlist.')
                 else:
@@ -526,7 +616,7 @@ class Groveshark:
       
     # Find similar artists to searched artist
     def similarArtists(self, artistId):
-        similar = self.groovesharkApiv1.artistGetSimilar(artistId, limit = self.artistsearchlimit)
+        similar = groovesharkApi.getSimilarArtists(artistId, limit = self.artistsearchlimit)
         if (len(similar) > 0):
             self._add_artists_directory(similar)
         else:
@@ -543,17 +633,14 @@ class Groveshark:
         return ''
     
     # Login to grooveshark
-    def _get_login(self, version = 2):
+    def _get_login(self):
         if (self.username == "" or self.password == ""):
             dialog = xbmcgui.Dialog()
             dialog.ok('Grooveshark XBMC', 'Unable to login.', 'Check username and password in settings.')
             return 0
         else:
             if self.userid == 0:
-                if version == 1:
-                    uid = self.groovesharkApiv1.login(self.username, self.password)
-                else:
-                    uid = groovesharkApi.login(self.username, self.password)
+                uid = groovesharkApi.login(self.username, self.password)
             if (uid != 0):
                 return uid
             else:
@@ -615,6 +702,8 @@ class Groveshark:
             songid = song[1]
             songalbum = song[2]
             songartist = song[4]
+            
+            
             u=sys.argv[0]+"?mode="+str(MODE_SONG)+"&name="+urllib.quote_plus(songname)+"&id="+str(songid) \
             +"&album="+urllib.quote_plus(songalbum) \
             +"&artist="+urllib.quote_plus(songartist) \
@@ -626,14 +715,13 @@ class Groveshark:
                 unfav = unfav +str(MODE_FAVORITES)
             else:
                 menuItems.append(("Grooveshark favorite", "XBMC.RunPlugin("+fav+")"))
-            if self.sessionidv1 != '':
-                menuItems.append(("Not Grooveshark favorite", "XBMC.RunPlugin("+unfav+")"))
-                if playlistid > 0:
-                    rmplaylstsong=sys.argv[0]+"?playlistid="+str(playlistid)+"&id="+str(id+1)+"&mode="+str(MODE_REMOVE_PLAYLIST_SONG)+"&name="+playlistname
-                    menuItems.append(("Remove from Grooveshark playlist", "XBMC.RunPlugin("+rmplaylstsong+")"))
-                else:
-                    addplaylstsong=sys.argv[0]+"?id="+str(songid)+"&mode="+str(MODE_ADD_PLAYLIST_SONG)
-                    menuItems.append(("Add to Grooveshark playlist", "XBMC.RunPlugin("+addplaylstsong+")"))
+            menuItems.append(("Not Grooveshark favorite", "XBMC.RunPlugin("+unfav+")"))
+            if playlistid > 0:
+                rmplaylstsong=sys.argv[0]+"?playlistid="+str(playlistid)+"&id="+str(songid)+"&mode="+str(MODE_REMOVE_PLAYLIST_SONG)+"&name="+playlistname
+                menuItems.append(("Remove from Grooveshark playlist", "XBMC.RunPlugin("+rmplaylstsong+")"))
+            else:
+                addplaylstsong=sys.argv[0]+"?id="+str(songid)+"&mode="+str(MODE_ADD_PLAYLIST_SONG)
+                menuItems.append(("Add to Grooveshark playlist", "XBMC.RunPlugin("+addplaylstsong+")"))
             item.addContextMenuItems(menuItems, replaceItems=False)
             xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]),url=u,listitem=item,isFolder=False, totalItems=len(songs))
             id = id + 1
@@ -659,8 +747,9 @@ class Groveshark:
             albumImage = self._get_icon(album[4], 'album-' + str(albumID))
             self._add_dir(albumName + " - " + albumArtistName, '', MODE_ALBUM, albumImage, albumID, n)
             i = i + 1
-        if artistid > 0 and self.sessionidv1 != '':
-            self._add_dir('Similar artists...', '', MODE_SIMILAR_ARTISTS, self.artistImg, artistid)
+        # Not supported by key
+        #if artistid > 0:
+        #    self._add_dir('Similar artists...', '', MODE_SIMILAR_ARTISTS, self.artistImg, artistid)
         xbmcplugin.setContent(self._handle, 'albums')
         xbmcplugin.addSortMethod(self._handle, xbmcplugin.SORT_METHOD_ALBUM_IGNORE_THE)
         xbmcplugin.setPluginFanart(int(sys.argv[1]), self.fanImg)
@@ -706,17 +795,17 @@ class Groveshark:
         dir.setInfo( type="Music", infoLabels={ "title": name } )
         
         # Custom menu items
-        if self.sessionidv1 != '':
-            menuItems = []
-            if mode == MODE_ALBUM:
-                mkplaylst=sys.argv[0]+"?mode="+str(MODE_MAKE_PLAYLIST)+"&name="+name+"&id="+str(id)
-                menuItems.append(("Make Grooveshark playlist", "XBMC.RunPlugin("+mkplaylst+")"))
-            if mode == MODE_PLAYLIST:
-                rmplaylst=sys.argv[0]+"?mode="+str(MODE_REMOVE_PLAYLIST)+"&name="+urllib.quote_plus(name)+"&id="+str(id)
-                menuItems.append(("Delete Grooveshark playlist", "XBMC.RunPlugin("+rmplaylst+")"))
-                mvplaylst=sys.argv[0]+"?mode="+str(MODE_RENAME_PLAYLIST)+"&name="+urllib.quote_plus(name)+"&id="+str(id)
-                menuItems.append(("Rename Grooveshark playlist", "XBMC.RunPlugin("+mvplaylst+")"))
-            dir.addContextMenuItems(menuItems, replaceItems=False)
+        menuItems = []
+        if mode == MODE_ALBUM:
+            mkplaylst=sys.argv[0]+"?mode="+str(MODE_MAKE_PLAYLIST)+"&name="+name+"&id="+str(id)
+            menuItems.append(("Make Grooveshark playlist", "XBMC.RunPlugin("+mkplaylst+")"))
+        # Broken rename/delete are broken in API
+        if mode == MODE_PLAYLIST:
+            rmplaylst=sys.argv[0]+"?mode="+str(MODE_REMOVE_PLAYLIST)+"&name="+urllib.quote_plus(name)+"&id="+str(id)
+            menuItems.append(("Delete Grooveshark playlist", "XBMC.RunPlugin("+rmplaylst+")"))
+            mvplaylst=sys.argv[0]+"?mode="+str(MODE_RENAME_PLAYLIST)+"&name="+urllib.quote_plus(name)+"&id="+str(id)
+            menuItems.append(("Rename Grooveshark playlist", "XBMC.RunPlugin("+mvplaylst+")"))
+        dir.addContextMenuItems(menuItems, replaceItems=False)
         
         return xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]),url=u,listitem=dir,isFolder=True, totalItems=items)
     
@@ -743,6 +832,49 @@ class Groveshark:
         except:
             xbmc.log("An error occurred saving songs")
             pass
+
+    def _getSongDuration(self, songid):
+        path = os.path.join(cacheDir, 'duration.dmp')
+        id = int(songid)
+        durations = []
+        duration = -1
+        
+        # Try cache first
+        try:
+            f = open(path, 'rb')
+            durations = pickle.load(f)
+            for song in durations:
+                if song[0] == id:
+                    duration = song[1]
+            f.close()
+        except:
+            pass
+
+        if duration < 0:
+            stream = groovesharkApi.getSubscriberStreamKey(songid)
+            usecs = stream['uSecs']
+            if usecs < 60000000:
+                usecs = usecs * 10 # Some durations are 10x to small
+            duration = usecs / 1000000
+            song = [id, duration]
+            durations.append(song)                
+            self._setSongDuration(durations)
+        
+        return duration
+
+    def _setSongDuration(self, durations):            
+        try:
+            # Create the 'data' directory if it doesn't exist.
+            if not os.path.exists(cacheDir):
+                os.makedirs(cacheDir)
+            path = os.path.join(cacheDir, 'duration.dmp')
+            f = open(path, 'wb')
+            pickle.dump(durations, f, protocol=pickle.HIGHEST_PROTOCOL)
+            f.close()
+        except:
+            xbmc.log("An error occurred saving durations")
+            pass
+
     
 # Parse URL parameters
 def get_params():
dissimilarity index 99%
index 0d4c247..13716fb 100644 (file)
Binary files a/default.tbn and b/default.tbn differ
index f34ea24..0ac420e 100644 (file)
        <type>9</type>
 
        <!-- (required) Title -->
-       <title>Grooveshark XBMC.</title>
+       <title>Grooveshark XBMC</title>
 
        <!-- (required) Major.minor.build -->
-       <version>0.4.0</version>
+       <version>0.5.1</version>
 
        <!--
                (required) author name & email. at least one author name is required
dissimilarity index 84%
index ca3ab1b..3c21238 100644 (file)
Binary files a/fanart.png and b/fanart.png differ
dissimilarity index 99%
index 28c5b5a..45e98ea 100644 (file)
Binary files a/icon.png and b/icon.png differ
dissimilarity index 99%
index cf0373f..13716fb 100644 (file)
Binary files a/resources/img/default.tbn and b/resources/img/default.tbn differ
index a205924..2f8f340 100644 (file)
@@ -1,6 +1,6 @@
-import socket, hmac, urllib, urllib2, pprint, md5, re, sha, time, random, os, pickle, uuid, tempfile, pprint
+import socket, hmac, urllib2, pprint, md5, os, pickle, tempfile, time, re, groovesharkAccess
 
-SESSION_EXPIRY = 518400 # 6 days in seconds
+SESSION_EXPIRY = 1209600 # 2 weeks
 
 # GrooveAPI constants
 THUMB_URL = 'http://beta.grooveshark.com/static/amazonart/m'
@@ -9,350 +9,16 @@ ALBUM_LIMIT = 15
 ARTIST_LIMIT = 15
 SONG_SUFFIX = '.mp3'
 
-# GrooveSong constants
-DOMAIN = "grooveshark.com"
-HOME_URL = "http://listen." + DOMAIN
-API_URL = "http://cowbell." + DOMAIN + "/more.php"
-SERVICE_URL = "http://cowbell." + DOMAIN + "/service.php"
-TOKEN_URL = "http://cowbell." + DOMAIN + "/more.php"
-
-CLIENT_NAME = "gslite"
-CLIENT_VERSION = "20101012.37"
-HEADERS = {"Content-Type": "application/json",
-       "User-Agent": "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12 (.NET CLR 3.5.30729)",                        
-       "Referer": "http://listen.grooveshark.com/main.swf?cowbell=fe87233106a6cef919a1294fb2c3c05f"}
-
-RE_SESSION = re.compile('"sessionID":"\s*?([A-z0-9]+)"')
-RANDOM_CHARS = "1234567890abcdef"
-
-# Get a song
-class GrooveSong:
-       
-       def __init__(self):
-
-               import simplejson
-               self.simplejson = simplejson
-               
-               self.cacheDir = os.path.join(tempfile.gettempdir(), 'groovesong')
-               self._lastSessionTime = 0
-               self.uuid = self._getUUID()
-               
-               self._getSavedSession()
-               # session ids last 1 week
-               if self.sessionID == '' or time.time() - self._lastSessionTime >= SESSION_EXPIRY:
-                       self.sessionID = self._getSession(HOME_URL)
-                       if self.sessionID == '':
-                               raise StandardError('Failed to get session id')
-                       else:
-                               print "New GrooveSong session id: " + self.sessionID
-                               self._setSavedSession()
-               
-       # The actual call to the API
-       def _callRemote(self, method, params, type="default"):
-               postData = {
-                       "header": {
-                               "client": CLIENT_NAME,
-                               "clientRevision": CLIENT_VERSION,
-                               "uuid": self.uuid,
-                               "session": self.sessionID},
-                               "country": {"IPR":"1021", "ID":"223", "CC1":"0", "CC2":"0", "CC3":"0", "CC4":"2147483648"},
-                               "privacy": 1,
-                       "parameters": params,
-                       "method": method}
-               
-               token = self._getMethodToken(method)
-               if token == None:
-                       raise StandardError("Cannot get token")
-
-               postData["header"]["token"] = token
-               if type == "service":
-                       url = SERVICE_URL + "?" + method
-               else:
-                       url = API_URL + "?" + method
-
-               postData = self.simplejson.dumps(postData)
-               print "GrooveSong URL: " + url
-               request = urllib2.Request(url, postData, HEADERS)
-               
-               response = urllib2.urlopen(request).read()
-               try:
-                       response = self.simplejson.loads(response)
-                       print "GrooveSong Response..."
-                       pprint.pprint(response)
-               except:
-                       raise StandardError("API error: " + response)
-               try:
-                       response["fault"]
-               except KeyError:
-                       return response
-               else:
-                       raise StandardError("API error: " + response["fault"]["message"])       
-
-       # Generate a random uuid
-       def _getUUID(self):
-               return str(uuid.uuid4())
-
-       #  Make a token ready for a request header
-       def _getMethodToken(self, method):
-               self._token = self._getCommunicationToken()
-               if self._token == None:
-                       return None
-
-               randomChars = ""
-               while 6 > len(randomChars):
-                       randomChars = randomChars + random.choice(RANDOM_CHARS)
-
-               token = sha.new(method + ":" + self._token + ":quitStealinMahShit:" + randomChars).hexdigest()
-
-               return randomChars + token
-
-       # Generate a communication token
-       def _getCommunicationToken(self):
-               params = {"secretKey": self._getSecretKey(self.sessionID)}
-               postData = {
-                       "header": {
-                               "client": CLIENT_NAME,
-                               "clientRevision": CLIENT_VERSION,
-                               "uuid": self.uuid,
-                               "session": self.sessionID},
-                               "country": {"IPR":"1021", "ID":"223", "CC1":"0", "CC2":"0", "CC3":"0", "CC4":"2147483648"},
-                               "privacy": 1,
-                       "parameters": params,
-                       "method": "getCommunicationToken"}
-               
-               postData = self.simplejson.dumps(postData)
-               request = urllib2.Request(TOKEN_URL, postData, HEADERS)
-               response = urllib2.urlopen(request).read()
-               try:
-                       response = self.simplejson.loads(response)
-               except:
-                       raise StandardError("API error: " + response)
-               try:
-                       response["fault"]
-               except KeyError:
-                       return response["result"]
-               else:
-                       return None     
-               
-       # Generate a secret key from a sessionID
-       def _getSecretKey(self, sessionID):
-               return md5.new(sessionID).hexdigest()
-       
-       # Get a session id from some HTML
-       def _getSession(self, html):
-               html = urllib2.urlopen(HOME_URL).read()
-               session = RE_SESSION.search(html)
-               if session:
-                       self._lastSessionTime = time.time()
-                       return session.group(1)
-               else:
-                       return None
-               
-       def _getSavedSession(self):
-               path = os.path.join(self.cacheDir, 'session.dmp')
-               try:
-                       f = open(path, 'rb')
-                       session = pickle.load(f)
-                       self.sessionID = session['sessionID']
-                       self._lastSessionTime = session['lastSessionTime']
-                       f.close()
-               except:
-                       self.sessionID = ''
-                       self._lastSessionTime = 0
-                       pass            
-
-       def _setSavedSession(self):                     
-               try:
-                       # Create the 'data' directory if it doesn't exist.
-                       if not os.path.exists(self.cacheDir):
-                               os.makedirs(self.cacheDir)
-                       path = os.path.join(self.cacheDir, 'session.dmp')
-                       f = open(path, 'wb')
-                       session = {'sessionID' : self.sessionID, 'lastSessionTime' : self._lastSessionTime}
-                       pickle.dump(session, f, protocol=pickle.HIGHEST_PROTOCOL)
-                       f.close()
-               except:
-                       print "An error occurred during save session"
-                       pass
-       
-       # Gets a stream key and host to get song content
-       def _getStreamDetails(self, songID):
-               params = {
-                       "songID": songID,
-                       "prefetch": False,
-                       "mobile": False, 
-                       "country": {"IPR":"1021","ID":"223", "CC1":"0", "CC2":"0", "CC3":"0", "CC4":"2147483648"}
-                       }
-               response = self._callRemote("getStreamKeyFromSongIDEx", params)
-               try: 
-                       self._lastStreamKey = response["result"]["streamKey"]
-                       self._lastStreamServer = response["result"]["ip"]
-                       self._lastStreamServerID = response["result"]["streamServerID"]
-                       return True
-               except:
-                       return False
-               
-       # Tells Grooveshark you have downloaded a song
-       def _markSongDownloaded(self, songID):
-               params = {
-                       "streamKey": self._lastStreamKey,
-                       "streamServerID": self._lastStreamServerID,
-                       "songID": songID}
-               self._callRemote("markSongDownloaded", params)
-
-       # Get the song URL
-       def getSongURL(self, songID):
-               if self._getStreamDetails(songID) == True:
-                       postData = {"streamKey": self._lastStreamKey}
-                       postData = urllib.urlencode(postData)
-                       return "http://" + self._lastStreamServer + "/stream.php?" + str(postData)
-               else:
-                       return ''
-
-class GrooveAPIv1:
-       
-       def __init__(self, sessionID = 0):
-               import simplejson
-               self.simplejson = simplejson
-               timeout = 40
-               socket.setdefaulttimeout(timeout)
-               self.loggedIn = 0
-               self.userId = 0
-               self.sessionID = sessionID
-               
-       def callRemote(self, method, params={}):
-               data = {'header': {'sessionID': self.sessionID}, 'method': method, 'parameters': params}
-               data = self.simplejson.dumps(data)
-               req = urllib2.Request("http://api.grooveshark.com/ws/1.0/?json")
-               req.add_header('Host', 'api.grooveshark.com')
-               req.add_header('Content-type', 'text/json')
-               req.add_header('Content-length', str(len(data)))
-               req.add_data(data)
-               response = urllib2.urlopen(req)
-               result = response.read()
-               response.close()
-               try:
-                       result = self.simplejson.loads(result)
-                       if 'fault' in result:
-                               if result['fault']['code'] == 8: #Session ID has expired. Get a new and try again if possible.
-                                       print 'GrooveShark: SessionID expired'
-                                       return []
-                       return result
-               except:
-                       return []               
-               
-       def login(self, username, password):
-               if self.loggedIn == 1:
-                       return self.userId
-               token = md5.new(username.lower() + md5.new(password).hexdigest()).hexdigest()
-               result = self.callRemote("session.loginExt", {"username": username, "token": token})
-               if 'result' in result:
-                       if 'userID' in result['result']:
-                               self.loggedIn = 1
-                               self.userId = result['result']['userID']
-                               return result['result']['userID'] 
-               else:
-                       return 0
-               
-       def unfavorite(self, songID):
-               return self.callRemote("song.unfavorite", {"songID": songID})
-               
-       def playlistCreate(self, name, about):
-               if self.loggedIn == 1:
-                       result = self.callRemote("playlist.create", {"name": name, "about": about})
-                       if 'result' in result:
-                               return result['result']['playlistID']
-                       else:
-                               return 0
-               else:
-                       return 0
-
-       def playlistCreateUnique(self, name, songIds):
-               if self.loggedIn == 1:
-                       result = self.callRemote("playlist.createunique", {"name": name, "songIDs": songIds})
-                       if 'result' in result:
-                               return result['result']['playlistID']
-                       else:
-                               return 0
-               else:
-                       return 0
-                       
-       def playlistDelete(self, playlistId):
-               if self.loggedIn == 1:
-                       result =  self.callRemote("playlist.delete", {"playlistID": playlistId})
-                       if 'fault' in result:
-                               return 0
-                       else:
-                               return 1
-               else:
-                       return 0
-
-       def playlistRename(self, playlistId, name):
-               if self.loggedIn == 1:
-                       result = self.callRemote("playlist.rename", {"playlistID": playlistId, "name": name})
-                       if 'fault' in result:
-                               return 0
-                       else:
-                               return 1
-               else:
-                       return 0
-
-       def playlistClearSongs(self, playlistId):
-               if self.loggedIn == 1:
-                       return self.callRemote("playlist.clearSongs", {"playlistID": playlistId})
-
-       def playlistAddSong(self, playlistId, songId, position):
-               if self.loggedIn == 1:
-                       result = self.callRemote("playlist.addSong", {"playlistID": playlistId, "songID": songId, "position": position})
-                       if 'fault' in result:
-                               return 0
-                       else:
-                               return 1
-               else:
-                       return 0
-               
-       def playlistDeleteSong(self, playlistId, position):
-               if self.loggedIn == 1:
-                       result = self.callRemote("playlist.removeSong", {"playlistID": playlistId, "position": position})
-                       if 'fault' in result:
-                               return 0
-                       else:
-                               return 1
-               else:
-                       return 0
-                       
-       def playlistReplace(self, playlistId, songIds):
-               if self.loggedIn == 1:
-                       result = self.callRemote("playlist.replace", {"playlistID": playlistId, "songIDs": songIds})
-                       if 'fault' in result:
-                               return 0
-                       else:
-                               return 1
-               else:
-                       return 0
-               
-       def artistGetSimilar(self, artistId, limit):
-               items = self.callRemote("artist.getSimilar", {"artistID": artistId, "limit": limit})
-               if 'result' in items:
-                       i = 0
-                       list = []
-                       artists = items['result']['artists']
-                       while(i < len(artists)):
-                               s = artists[i]
-                               list.append([s['artistName'].encode('ascii', 'ignore'),\
-                               s['artistID']])
-                               i = i + 1
-                       return list
-               else:
-                       return []
-
 # Main API
 class GrooveAPI:
 
-       sessionID = ''
-       userID = 0
-       host = 'api.grooveshark.com'
-       lastSessionTime = 0
+       _ip = '0.0.0.0'
+       _country = ''
+       _sessionID = ''
+       _userID = 0
+       _lastSessionTime = 0
+       _lastStreamKey = ''
+       _lastStreamServerID = ''
 
        # Constructor
        def __init__(self):
@@ -360,74 +26,54 @@ class GrooveAPI:
                import simplejson
                self.simplejson = simplejson
                socket.setdefaulttimeout(40)
-               
-               self.cacheDir = os.path.join(tempfile.gettempdir(), 'grooveapi')
+               self.cacheDir = os.path.join(tempfile.gettempdir(), 'groovesharkapi')
                if os.path.isdir(self.cacheDir) == False:
                        os.makedirs(self.cacheDir)
                        print "Made " + self.cacheDir
-
                self._getSavedSession()
-               # session ids last 1 week
-               if self.sessionID == '' or time.time()- self.lastSessionTime >= SESSION_EXPIRY:
-                       self.sessionID = self._getSessionID()
-                       if self.sessionID == '':
+               # session ids last 2 weeks
+               if self._sessionID == '' or time.time()- self._lastSessionTime >= SESSION_EXPIRY:
+                       self._sessionID = self._getSessionID()
+                       self._ip = self._getIP()
+                       self._country = self._getCountry()
+                       if self._sessionID == '':
                                raise StandardError('Failed to get session id')
                        else:
-                               print "New GrooveAPI session id: " + self.sessionID
+                               print "New GrooveAPI session id: " + self._sessionID
                                self._setSavedSession()
 
-       # Sort keys             
-       def _keySort(self, d):
-               return [(k,d[k]) for k in sorted(d.keys())]
-       
-       # Make a message sig
-       def _createMessageSig(self, method, params, secret):
-               args = self._keySort(params);
-               data = '';
-               for arg in args:
-                       data += str(arg[0])
-                       data += str(arg[1])
-               data = method + data
+       # Call to API
+       def _callRemote(self, method, params):
+               self._setParams(params)
+               return groovesharkAccess.callRemote(method, self._sessionID)
                
-               h = hmac.new(secret, data)
-               return h.hexdigest()
-       
-       # The actual call to the API
-       def _callRemote(self, method, params = {}):
-               url = 'http://%s/ws/2.1/?method=%s&%s&wsKey=wordpress&sig=%s&format=json' % (self.host, method, urllib.urlencode(params), self._createMessageSig(method, params, 'd6c59291620c6eaa5bf94da08fae0ecc'))
-               print url
-               req = urllib2.Request(url)
-               response = urllib2.urlopen(req)
-               result = response.read()
-               print "Response..."
-               pprint.pprint(result)
-               response.close()
-               try:
-                       result = self.simplejson.loads(result)
-                       return result
-               except:
-                       return []
-
        # Get a session id
        def _getSessionID(self):
                params = {}
                result = self._callRemote('startSession', params)
-               self.lastSessionTime = time.time()
-               return result['result']['sessionID']
+               self._lastSessionTime = time.time()
+               if 'result' in result:
+                       return result['result']['sessionID']
+               else:
+                       return ''
        
        def _getSavedSession(self):
                path = os.path.join(self.cacheDir, 'session.dmp')
                try:
                        f = open(path, 'rb')
                        session = pickle.load(f)
-                       self.sessionID = session['sessionID']
-                       self.lastSessionTime = session['lastSessionTime']
-                       self.userID = session['userID']
+                       self._sessionID = session['sessionID']
+                       self._lastSessionTime = session['lastSessionTime']
+                       self._userID = session['userID']
+                       self._ip = session['ip']
+                       self._country = session['country']
                        f.close()
                except:
-                       self.sessionID = ''
-                       self.lastSessionTime = 0
-                       self.userID = 0
+                       self._sessionID = ''
+                       self._lastSessionTime = 0
+                       self._userID = 0
+                       self._ip = '0.0.0.0'
+                       self._country = ''              
                        pass            
 
        def _setSavedSession(self):                     
@@ -437,44 +83,102 @@ class GrooveAPI:
                                os.makedirs(self.cacheDir)
                        path = os.path.join(self.cacheDir, 'session.dmp')
                        f = open(path, 'wb')
-                       session = { 'sessionID' : self.sessionID, 'lastSessionTime' : self.lastSessionTime, 'userID': self.userID}
+                       session = { 'sessionID' : self._sessionID, 'lastSessionTime' : self._lastSessionTime, 'userID': self._userID, 'ip' : self._ip, 'country' : self._country } 
                        pickle.dump(session, f, protocol=pickle.HIGHEST_PROTOCOL)
                        f.close()
                except:
                        print "An error occurred during save session"
                        pass
+
+       def _setParams(self, params):                   
+               try:
+                       # Create the directory if it doesn't exist.
+                       if not os.path.exists(self.cacheDir):
+                               os.makedirs(self.cacheDir)
+                       path = os.path.join(self.cacheDir, 'params.dmp')
+                       f = open(path, 'wb')
+                       pickle.dump(params, f, protocol=pickle.HIGHEST_PROTOCOL)
+                       f.close()
+               except:
+                       print "An error occurred during save params"
+                       pass
        
-       # Make user authentication token
-       def _getUserToken(self, username, password):
-               return md5.new(username.lower() + md5.new(password).hexdigest()).hexdigest()
+       # Get IP
+       def _getIP(self):
+               try:
+                       myip = urllib2.urlopen('http://whatismyip.org').read()
+                       if re.match("^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$", myip):
+                               print "IP is " + myip
+                               return myip
+               except:
+                       return '0.0.0.0'
 
+       # Get country
+       def _getCountry(self):
+               params = { 'ip' : self._ip }
+               response = self._callRemote("getCountry", params)
+               return response['result']
+       
+       # Get userid from name
+       def _getUserIDFromUsername(self, username):
+               result = self._callRemote('getUserIDFromUsername', {'username' : username})
+               if 'result' in result and result['result']['UserID'] > 0:
+                       return result['result']['UserID']
+               else:
+                       return 0
+       
        # Authenticates the user for current API session
-       def _authenticateUser(self, username, token):
-               params = {'sessionID': self.sessionID, 'username': username, 'token': token}
-               result = self._callRemote('authenticateUser', params)
-               return result['result']['UserID']
+       def _authenticate(self, login, password):
+               md5pwd = md5.new(password).hexdigest()
+               params = {'login': login, 'password': md5pwd}
+               
+               result = self._callRemote('authenticate', params)
+               uid = result['result']['UserID']
+               if (uid > 0):
+                       return uid
+               else:
+                       return 0
+       
+       # Check the service
+       def pingService(self,):
+               result = self._callRemote('pingService', {});
+               if 'result' in result and result['result'] != '':
+                       return True
+               else:
+                       return False
        
        # Login
        def login(self, username, password):
-               if self.userID <= 0:
+               if self._userID <= 0:
                        # Check cache
                        self._getSavedSession()
-                       if self.userID <= 0: 
-                               token = self._getUserToken(username, password)
-                               self.userID = self._authenticateUser(username, token)
-                               if self.userID > 0:
+                       if self._userID <= 0: 
+                               self._userID = self._authenticate(username, password)
+                               if self._userID > 0:
                                        self._setSavedSession()
-               return self.userID
+               return self._userID
                
        # Logs the user out
        def logout(self):
-               result = self._callRemote('logout', {'sessionID' : self.sessionID})
+               result = self._callRemote('logout', {'sessionID' : self._sessionID})
                if 'result' in result and result['result']['success'] == True: 
-                       self.userID = 0
+                       self._userID = 0
                        self._setSavedSession()
                        return True
                return False
 
+       # Gets a stream key and host to get song content
+       def getSubscriberStreamKey(self, songID):
+               params = { "songID": songID, "country": self._country }
+               response = self._callRemote("getSubscriberStreamKey", params)
+               try: 
+                       self._lastStreamKey = response["result"]["StreamKey"]
+                       self._lastStreamServerID = response["result"]["StreamServerID"]
+                       res = response["result"]
+                       return res
+               except:
+                       return False
+
        # Search for albums
        def getArtistSearchResults(self, query, limit=ARTIST_LIMIT):
                result = self._callRemote('getArtistSearchResults', {'query' : query,'limit' : limit})
@@ -493,7 +197,7 @@ class GrooveAPI:
                
        # Search for songs
        def getSongSearchResults(self, query, limit=SONG_LIMIT):
-               result = self._callRemote('getSongSearchResultsEx', {'query' : query, 'limit' : limit})
+               result = self._callRemote('getSongSearchResults', {'query' : query, 'country' : self._country, 'limit' : limit})
                if 'result' in result:
                        return self._parseSongs(result)
                else:
@@ -509,7 +213,7 @@ class GrooveAPI:
 
        # Get album songs
        def getAlbumSongs(self, albumID, limit=SONG_LIMIT):
-               result = self._callRemote('getAlbumSongsEx', {'albumID' : albumID, 'limit' : limit})
+               result = self._callRemote('getAlbumSongs', {'albumID' : albumID, 'limit' : limit})
                if 'result' in result:
                        return self._parseSongs(result)
                else:
@@ -534,31 +238,17 @@ class GrooveAPI:
 
        # Gets the favorite songs of the logged-in user
        def getUserFavoriteSongs(self):
-               if (self.userID == 0):
+               if (self._userID == 0):
                        return [];
-               result = self._callRemote('getUserFavoriteSongs', {'sessionID' : self.sessionID})
+               result = self._callRemote('getUserFavoriteSongs', {})
                if 'result' in result:
                        return self._parseSongs(result)
                else:
                        return []
-               
-       # Add song to user favorites
-       def addUserFavoriteSong(self, songID):
-               if (self.userID == 0):
-                       return False;
-               result = self._callRemote('addUserFavoriteSong', {'sessionID' : self.sessionID, 'songID' : songID})
-               return result['result']['success']
 
-       # Get the url to link to a song on Grooveshark
-       def getSongURLFromSongID(self, songID):
-               song = GrooveSong()
-               url = song.getSongURL(songID)
-               print "Got song URL " + url
-               return url
-
-       # Get the url to link to a song on Grooveshark
-       def getSongInfo(self, songID):
-               result = self._callRemote('getSongInfoEx', {'songID' : songID})
+       # Get song info
+       def getSongsInfo(self, songIDs):
+               result = self._callRemote('getSongsInfo', {'songIDs' : songIDs})
                if 'result' in result and 'SongID' in result['result']:
                        info = result['result']
                        if 'CoverArtFilename' in info and info['CoverArtFilename'] != None:
@@ -568,30 +258,36 @@ class GrooveAPI:
                        return info
                else:
                        return 'None'
+               
+       # Add song to user favorites
+       def addUserFavoriteSong(self, songID):
+               if (self._userID == 0):
+                       return False;
+               result = self._callRemote('addUserFavoriteSong', {'songID' : songID})
+               return result['result']['success']
+
+       # Remove songs from user favorites
+       def removeUserFavoriteSongs(self, songIDs):
+               if (self._userID == 0):
+                       return False;
+               result = self._callRemote('removeUserFavoriteSongs', {'songIDs' : songIDs})
+               return result['result']['success']
 
        # Gets the playlists of the logged-in user
        def getUserPlaylists(self):
-               if (self.userID == 0):
+               if (self._userID == 0):
                        return [];
-               result = self._callRemote('getUserPlaylists', {'sessionID' : self.sessionID})
+               result = self._callRemote('getUserPlaylists', {})
                if 'result' in result:
                        return self._parsePlaylists(result)
                else:
                        return []
                
-       # Get userid from name
-       def _getUserIDFromUsername(self, username):
-               result = self._callRemote('getUserIDFromUsername', {'username' : username})
-               if 'result' in result and result['result']['UserID'] > 0:
-                       return result['result']['UserID']
-               else:
-                       return 0
-
        # Gets the playlists of the logged-in user
-       def getUserPlaylistsEx(self, username):
+       def getUserPlaylistsByUsername(self, username):
                userID = self._getUserIDFromUsername(username)
                if (userID > 0):
-                       result = self._callRemote('getUserPlaylistsEx', {'userID' : userID})
+                       result = self._callRemote('getUserPlaylistsByUserID', {'userID' : userID})
                        if 'result' in result and result['result']['playlists'] != None:
                                playlists = result['result']['playlists']
                                return self._parsePlaylists(playlists)
@@ -600,7 +296,7 @@ class GrooveAPI:
 
        # Creates a playlist with songs
        def createPlaylist(self, name, songIDs):
-               result = self._callRemote('createPlaylist', {'name' : name, 'songIDs' : songIDs, 'sessionID' : self.sessionID})
+               result = self._callRemote('createPlaylist', {'name' : name, 'songIDs' : songIDs})
                if 'result' in result and result['result']['success'] == True: 
                        return result['result']['playlistID']
                elif 'errors' in result:
@@ -608,7 +304,7 @@ class GrooveAPI:
 
        # Sets the songs for a playlist
        def setPlaylistSongs(self, playlistID, songIDs):
-               result = self._callRemote('setPlaylistSongs', {'playlistID' : playlistID, 'songIDs' : songIDs, 'sessionID' : self.sessionID})
+               result = self._callRemote('setPlaylistSongs', {'playlistID' : playlistID, 'songIDs' : songIDs})
                if 'result' in result and result['result']['success'] == True: 
                        return True
                else:
@@ -621,15 +317,47 @@ class GrooveAPI:
                        return self._parseSongs(result)
                else:
                        return []
+               
+                       
+       def playlistDelete(self, playlistId):
+               result =  self._callRemote("deletePlaylist", {"playlistID": playlistId})
+               if 'fault' in result:
+                       return 0
+               else:
+                       return 1
 
-       # Check the service
-       def pingService(self,):
-               result = self._callRemote('pingService', {});
-               if 'result' in result and result['result'] != '':
-                       return True
+       def playlistRename(self, playlistId, name):
+               result = self._callRemote("renamePlaylist", {"playlistID": playlistId, "name": name})
+               if 'fault' in result:
+                       return 0
                else:
-                       return False
+                       return 1
+               
+       def getSimilarArtists(self, artistId, limit):
+               items = self._callRemote("getSimilarArtists", {"artistID": artistId, "limit": limit})
+               if 'result' in items:
+                       i = 0
+                       list = []
+                       artists = items['result']['artists']
+                       while(i < len(artists)):
+                               s = artists[i]
+                               list.append([s['artistName'].encode('ascii', 'ignore'),\
+                               s['artistID']])
+                               i = i + 1
+                       return list
+               else:
+                       return []               
        
+       # After 30s play time
+       def markStreamKeyOver30Secs(self):
+               params = { "streamKey" : self._lastStreamKey, "streamServerID" : self._lastStreamServerID }
+               self._callRemote("markStreamKeyOver30Secs", params)
+
+       # Song complete
+       def markSongComplete(self, songid):             
+               params = { "songID" : songid, "streamKey" : self._lastStreamKey, "streamServerID" : self._lastStreamServerID }
+               self._callRemote("markSongComplete", params)
+
        # Extract song data     
        def _parseSongs(self, items, limit=0):
                if 'result' in items:
@@ -669,7 +397,7 @@ class GrooveAPI:
                                else:
                                        s = items['result'][i]
                                if 'CoverArtFilename' not in s:
-                                       info = self.getSongInfo(s['SongID'])
+                                       info = self.getSongsInfo(s['SongID'])
                                        coverart = info['CoverArtFilename']
                                elif s['CoverArtFilename'] != None:
                                        coverart = THUMB_URL+s['CoverArtFilename'].encode('ascii', 'ignore')
@@ -735,15 +463,15 @@ class GrooveAPI:
                i = 0
                list = []
                if 'result' in items:
-                       playlists = items['result']
+                       playlists = items['result']['playlists']
                elif len(items) > 0:
                        playlists = items
                else:
                        return []
-               
+
                while (i < len(playlists)):
                        s = playlists[i]
-                       list.append([s['Name'].encode('ascii', 'ignore'), s['PlaylistID']])
+                       list.append([str(s['PlaylistName']).encode('ascii', 'ignore'), s['PlaylistID']])
                        i = i + 1
                return list
 
@@ -753,11 +481,14 @@ class GrooveAPI:
 #groovesharkApi = GrooveAPI()
 #res = groovesharkApi.pingService()
 #res = groovesharkApi.login(sys.argv[1], sys.argv[2])
-#songIDs = "[23404546,23401810,23401157]"
+#songIds = []
+#songIds.append('28645456')
+#songIds.append('26579347')
+#res=groovesharkApi.playlistRename(58197714, 'renamed playlist2')
 #res = groovesharkApi.createPlaylist("Test", songIDs)
-#res = groovesharkApi.setPlaylistSongs('42873478', songIDs)
+#res = groovesharkApi.setPlaylistSongs('58197714',songIds)
 #pprint.pprint(res)
-#res = groovesharkApi.getPlaylistSongs('42873478')
+#res = groovesharkApi.getPlaylistSongs('58197714')
 #res = groovesharkApi.getSongSearchResults('jimmy jazz', 3)
 #res = groovesharkApi.getPopularSongsToday(3)
 #res = groovesharkApi.getSongURLFromSongID('26579347')
@@ -766,11 +497,11 @@ class GrooveAPI:
 #res = groovesharkApi.getArtistSearchResults('the clash', 3)
 #res = groovesharkApi.getUserFavoriteSongs()
 #res = groovesharkApi.getUserPlaylists()
-#res = groovesharkApi.getSongInfo('27425375')
+#res = groovesharkApi.getSongInfos('27425375')
 #res = groovesharkApi.getPlaylistSongs(40902662)
 #res = groovesharkApi.addUserFavoriteSong('27425375')
 #res = groovesharkApi.logout()
-#res = groovesharkApi.getUserPlaylistsEx('stephendenham')
+#res = groovesharkApi.getUserPlaylistsByUsername('stephendenham')
 #res = groovesharkApi.getArtistPopularSongs('3707')
 #
 #pprint.pprint(res)
index b1665ee..26de019 100644 (file)
@@ -9,8 +9,4 @@
    <setting id="artistsearchlimit" type="labelenum" label="30004" default="10" values="5|10|15|20|25|30|40|50|60|70|80|90|100" />
    <setting id="songspagelimit" type="labelenum" label="30005" default="100" values="10|20|30|40|50|60|70|80|90|100|200|500" />
    </category>
-   <category label="Advanced">
-   <setting type="lsep" label="Only set this is if you have a valid Grooveshark v1.0 API session id." />
-   <setting id="sessionidv1" type="text" label="30006" default=""/>
-   </category>
 </settings>