From 052028f16d52133106cdbe53c3be845171e4d0db Mon Sep 17 00:00:00 2001 From: stephendenham Date: Thu, 27 Jan 2011 15:44:11 +0000 Subject: [PATCH] Add optional 1.0 API for those with a session id. git-svn-id: svn://svn.code.sf.net/p/xbmc-groove/code@44 2dec19e3-eb1d-4749-8193-008c8bba0994 --- addon.xml | 2 +- changelog.txt | 4 + default.py | 328 +++++++++++- description.xml | 2 +- methods.txt | 302 ----------- resources/language/English/strings.xml | 1 + resources/lib/GrooveAPI.py | 693 ------------------------- resources/lib/GroovesharkAPI.py | 198 ++++++- resources/settings.xml | 8 + viewapi1sessionid.py | 5 + 10 files changed, 518 insertions(+), 1025 deletions(-) delete mode 100644 methods.txt delete mode 100644 resources/lib/GrooveAPI.py create mode 100644 viewapi1sessionid.py diff --git a/addon.xml b/addon.xml index 4b1fb11..d4ab73e 100644 --- a/addon.xml +++ b/addon.xml @@ -1,6 +1,6 @@ + version="0.4.0" provider-name="Stephen Denham"> diff --git a/changelog.txt b/changelog.txt index 60fbcef..6d76714 100644 --- a/changelog.txt +++ b/changelog.txt @@ -1,3 +1,7 @@ +0.4.0: + +Add optional 1.0 API for those with a session id. + 0.3.0: Get artist's albums. diff --git a/default.py b/default.py index f07928a..d44a236 100644 --- a/default.py +++ b/default.py @@ -12,8 +12,15 @@ MODE_ALBUM = 10 MODE_ARTIST = 11 MODE_PLAYLIST = 12 MODE_SONG_PAGE = 13 -MODE_SONG = 14 -MODE_FAVORITE = 15 +MODE_SIMILAR_ARTISTS = 14 +MODE_SONG = 15 +MODE_FAVORITE = 16 +MODE_UNFAVORITE = 17 +MODE_MAKE_PLAYLIST = 18 +MODE_REMOVE_PLAYLIST = 19 +MODE_RENAME_PLAYLIST = 20 +MODE_REMOVE_PLAYLIST_SONG = 21 +MODE_ADD_PLAYLIST_SONG = 22 ACTION_MOVE_LEFT = 1 ACTION_MOVE_UP = 3 @@ -43,9 +50,11 @@ favoritesUrl = baseModeUrl + '?mode=' + str(MODE_FAVORITES) searchArtistsAlbumsName = "Search for artist's albums..." thumbDef = os.path.join(imgDir, 'default.tbn') +listBackground = os.path.join(imgDir, 'listbackground.png') sys.path.append (libDir) from GroovesharkAPI import GrooveAPI +from GroovesharkAPI import GrooveAPIv1 try: groovesharkApi = GrooveAPI() @@ -61,6 +70,67 @@ class _Info: def __init__( self, *args, **kwargs ): self.__dict__.update( kwargs ) +# Window dialog to select a grooveshark playlist +class GroovesharkPlaylistSelect(xbmcgui.WindowDialog): + + def __init__(self, items=[]): + gap = int(self.getHeight()/100) + w = int(self.getWidth()*0.5) + h = self.getHeight()-30*gap + rw = self.getWidth() + rh = self.getHeight() + x = rw/2 - w/2 + y = rh/2 -h/2 + + self.imgBg = xbmcgui.ControlImage(x+gap, 5*gap+y, w-2*gap, h-5*gap, listBackground) + self.addControl(self.imgBg) + + self.playlistControl = xbmcgui.ControlList(2*gap+x, y+3*gap+30, w-4*gap, h-10*gap, textColor='0xFFFFFFFF', selectedColor='0xFFFF4242', itemTextYOffset=0, itemHeight=50, alignmentY = 0) + self.addControl(self.playlistControl) + + self.lastPos = 0 + self.isSelecting = False + self.selected = -1 + listitems = [] + for playlist in items: + listitems.append(xbmcgui.ListItem(playlist[0])) + listitems.append(xbmcgui.ListItem('New...')) + self.playlistControl.addItems(listitems) + self.setFocus(self.playlistControl) + self.playlistControl.selectItem(0) + item = self.playlistControl.getListItem(self.lastPos) + item.select(True) + + # Highlight selected item + def setHighlight(self): + if self.isSelecting: + return + else: + self.isSelecting = True + + pos = self.playlistControl.getSelectedPosition() + if pos >= 0: + item = self.playlistControl.getListItem(self.lastPos) + item.select(False) + item = self.playlistControl.getListItem(pos) + item.select(True) + self.lastPos = pos + self.isSelecting = False + + # Control - select + def onControl(self, control): + if control == self.playlistControl: + self.selected = self.playlistControl.getSelectedPosition() + self.close() + + # Action - close or up/down + def onAction(self, action): + if action == ACTION_PREVIOUS_MENU: + self.selected = -1 + self.close() + 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 Groveshark: @@ -83,6 +153,7 @@ class Groveshark: songspagelimit = int(settings.getSetting('songspagelimit')) username = settings.getSetting('username') password = settings.getSetting('password') + sessionidv1 = settings.getSetting('sessionidv1') userid = 0 def __init__( self ): @@ -94,7 +165,9 @@ class Groveshark: if os.path.isdir(artDir) == False: os.makedirs(artDir) xbmc.log("Made " + artDir) - + + self.groovesharkApiv1 = GrooveAPIv1(self.sessionidv1) + # Top-level menu def categories(self): @@ -184,7 +257,7 @@ class Groveshark: xbmc.log("Found " + artist[0] + "...") albums = groovesharkApi.getArtistAlbums(artistID, limit = self.albumsearchlimit) if (len(albums) > 0): - self._add_albums_directory(albums) + self._add_albums_directory(albums, artistID) else: dialog = xbmcgui.Dialog() dialog.ok('Grooveshark XBMC', 'No matching albums.') @@ -202,7 +275,7 @@ class Groveshark: if (userid != 0): favorites = groovesharkApi.getUserFavoriteSongs() if (len(favorites) > 0): - self._add_songs_directory(favorites) + self._add_songs_directory(favorites, isFavorites=True) else: dialog = xbmcgui.Dialog() dialog.ok('Grooveshark XBMC', 'You have no favorites.') @@ -243,6 +316,21 @@ class Groveshark: else: dialog = xbmcgui.Dialog() dialog.ok('Grooveshark XBMC', 'You must be logged in', 'to add favorites.') + + # Remove song from favorites + def unfavorite(self, songid, prevMode=0): + userid = self._get_login(version = 1) + if (userid != 0): + xbmc.log("Unfavorite song: " + str(songid) + ', previous mode was ' + str(prevMode)) + self.groovesharkApiv1.unfavorite(songID = songid) + xbmc.executebuiltin('XBMC.Notification(Grooveshark XBMC, Removed from Grooveshark favorites, 1000, ' + thumbDef + ')') + # Refresh to remove item from directory + if (int(prevMode) == MODE_FAVORITES): + xbmc.executebuiltin("Container.Refresh(" + favoritesUrl + ")") + else: + dialog = xbmcgui.Dialog() + dialog.ok('Grooveshark XBMC', 'You must be logged in', 'to remove Grooveshark favorites.') + # Show selected album def album(self, albumid): @@ -252,14 +340,17 @@ class Groveshark: # Show selected artist def artist(self, artistid): albums = groovesharkApi.getArtistAlbums(artistid, limit = self.albumsearchlimit) - self._add_albums_directory(albums) + self._add_albums_directory(albums, artistid) # Show selected playlist - def playlist(self, playlistid): + def playlist(self, playlistid, playlistname, version = 2): userid = self._get_login() if (userid != 0): - songs = groovesharkApi.getPlaylistSongs(playlistid) - self._add_songs_directory(songs, trackLabelFormat=NAME_ALBUM_ARTIST_LABEL) + if version == 1: + songs = self.groovesharkApiv1.playlistGetSongs(playlistid) + else: + songs = groovesharkApi.getPlaylistSongs(playlistid) + self._add_songs_directory(songs, trackLabelFormat=NAME_ALBUM_ARTIST_LABEL, playlistid=playlistid, playlistname=playlistname) else: dialog = xbmcgui.Dialog() dialog.ok('Grooveshark XBMC', 'You must be logged in', 'to get Grooveshark playlists.') @@ -318,8 +409,133 @@ class Groveshark: return item # Next page of songs - def songPage(self, page, trackLabelFormat): - self._add_songs_directory([], trackLabelFormat, page) + def songPage(self, page, trackLabelFormat, playlistid = 0, playlistname = ''): + self._add_songs_directory([], trackLabelFormat, page, playlistid = playlistid, playlistname = playlistname) + + # Make a playlist from an album + def makePlaylist(self, albumid, name): + userid = self._get_login(version = 1) + if (userid != 0): + re.split(' - ',name,1) + nameTokens = re.split(' - ',name,1) # suggested name + name = self._get_keyboard(default=nameTokens[0], heading="Grooveshark playlist name") + if name != '': + album = groovesharkApi.getAlbumSongs(albumid, limit = self.songsearchlimit) + songids = [] + for song in album: + songids.append(song[1]) + if self.groovesharkApiv1.playlistCreateUnique(name, songids) == 0: + dialog = xbmcgui.Dialog() + dialog.ok('Grooveshark XBMC', 'Cannot create Grooveshark playlist ', name) + else: + xbmc.executebuiltin('XBMC.Notification(Grooveshark XBMC, Grooveshark playlist created, 1000, ' + thumbDef + ')') + else: + dialog = xbmcgui.Dialog() + dialog.ok('Grooveshark XBMC', 'You must be logged in ', ' to create a Grooveshark playlist.') + + # Rename a playlist + def renamePlaylist(self, playlistid, name): + userid = self._get_login(version = 1) + if (userid != 0): + newname = self._get_keyboard(default=name, heading="Grooveshark playlist name") + if newname == '': + return + elif self.groovesharkApiv1.playlistRename(playlistid, newname) == 0: + dialog = xbmcgui.Dialog() + dialog.ok('Grooveshark XBMC', 'Cannot rename Grooveshark playlist ', name) + else: + # Refresh to show new item name + xbmc.executebuiltin("Container.Refresh") + else: + dialog = xbmcgui.Dialog() + dialog.ok('Grooveshark XBMC', 'You must be logged in ', ' to rename a Grooveshark playlist.') + + # Remove a playlist + 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) + if (userid != 0): + if self.groovesharkApiv1.playlistDelete(playlistid) == 0: + dialog = xbmcgui.Dialog() + dialog.ok('Grooveshark XBMC', 'Cannot remove Grooveshark playlist ', name) + else: + # Refresh to remove item from directory + xbmc.executebuiltin("Container.Refresh(" + playlistsUrl + ")") + else: + dialog = xbmcgui.Dialog() + dialog.ok('Grooveshark XBMC', 'You must be logged in ', ' to delete a Grooveshark playlist.') + + # Add song to playlist + def addPlaylistSong(self, songid): + userid = self._get_login(version = 1) + if (userid != 0): + playlists = groovesharkApi.getUserPlaylists() + if (len(playlists) > 0): + ret = 0 + # Select the playlist + playlistSelect = GroovesharkPlaylistSelect(items=playlists) + playlistSelect.setFocus(playlistSelect.playlistControl) + playlistSelect.doModal() + i = playlistSelect.selected + del playlistSelect + if i > -1: + # Add a new playlist + if i >= len(playlists): + name = self._get_keyboard(default='', heading="Grooveshark playlist name") + if name != '': + songIds = [] + songIds.append(songid) + if self.groovesharkApiv1.playlistCreateUnique(name, songIds) == 0: + dialog = xbmcgui.Dialog() + dialog.ok('Grooveshark XBMC', 'Cannot create Grooveshark playlist ', name) + else: + xbmc.executebuiltin('XBMC.Notification(Grooveshark XBMC, Grooveshark playlist created, 1000, ' + thumbDef + ')') + # Existing playlist + else: + 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: + dialog = xbmcgui.Dialog() + dialog.ok('Grooveshark XBMC', 'Cannot add to playlist ') + else: + xbmc.executebuiltin('XBMC.Notification(Grooveshark XBMC, Added song to Grooveshark playlist, 1000, ' + thumbDef + ')') + else: + dialog = xbmcgui.Dialog() + dialog.ok('Grooveshark XBMC', 'You have no Grooveshark playlists.') + self.categories() + else: + dialog = xbmcgui.Dialog() + 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): + dialog = xbmcgui.Dialog(version = 1) + if dialog.yesno('Grooveshark XBMC', 'Delete this song from the Grooveshark playlist?') == True: + userid = self._get_login(version = 1) + if (userid != 0): + if self.groovesharkApiv1.playlistDeleteSong(playlistid, songpos) == 0: + dialog = xbmcgui.Dialog() + dialog.ok('Grooveshark XBMC', 'Failed to remove', 'song from Grooveshark playlist.') + else: + # Refresh to remove item from directory + xbmc.executebuiltin('XBMC.Notification(Grooveshark XBMC, Removed song from Grooveshark playlist, 1000, ' + thumbDef + ')') + xbmc.executebuiltin("Container.Update(" + playlistUrl + "&id="+str(playlistid) + "&name=" + playlistname + "&version=1)") + else: + dialog = xbmcgui.Dialog() + dialog.ok('Grooveshark XBMC', 'You must be logged in', 'to delete a song from a Grooveshark playlist.') + + # Find similar artists to searched artist + def similarArtists(self, artistId): + similar = self.groovesharkApiv1.artistGetSimilar(artistId, limit = self.artistsearchlimit) + if (len(similar) > 0): + self._add_artists_directory(similar) + else: + dialog = xbmcgui.Dialog() + dialog.ok('Grooveshark XBMC', 'No similar artists.') + self.categories() # Get keyboard input def _get_keyboard(self, default="", heading="", hidden=False): @@ -330,14 +546,17 @@ class Groveshark: return '' # Login to grooveshark - def _get_login(self): + def _get_login(self, version = 2): 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: - uid = groovesharkApi.login(self.username, self.password) + if version == 1: + uid = self.groovesharkApiv1.login(self.username, self.password) + else: + uid = groovesharkApi.login(self.username, self.password) if (uid != 0): return uid else: @@ -369,7 +588,7 @@ class Groveshark: return thumbDef # Add songs to directory - def _add_songs_directory(self, songs, trackLabelFormat=ARTIST_ALBUM_NAME_LABEL, page=0): + def _add_songs_directory(self, songs, trackLabelFormat=ARTIST_ALBUM_NAME_LABEL, page=0, playlistid=0, playlistname='', isFavorites=False): totalSongs = len(songs) page = int(page) @@ -391,6 +610,7 @@ class Groveshark: end = start + self.songspagelimit songs = songs[start:end] + id = 0 for song in songs: item = self._get_song_item(song, trackLabelFormat) coverart = item.getProperty('coverart') @@ -403,21 +623,34 @@ class Groveshark: +"&artist="+urllib.quote_plus(songartist) \ +"&coverart="+urllib.quote_plus(coverart) fav=sys.argv[0]+"?mode="+str(MODE_FAVORITE)+"&name="+urllib.quote_plus(songname)+"&id="+str(songid) + unfav=sys.argv[0]+"?mode="+str(MODE_UNFAVORITE)+"&name="+urllib.quote_plus(songname)+"&id="+str(songid)+"&prevmode=" menuItems = [] - menuItems.append(("Grooveshark favorite", "XBMC.RunPlugin("+fav+")")) + if isFavorites == True: + 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+")")) item.addContextMenuItems(menuItems, replaceItems=False) xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]),url=u,listitem=item,isFolder=False, totalItems=len(songs)) + id = id + 1 page = page + 1 if totalSongs > page * self.songspagelimit: - u=sys.argv[0]+"?mode="+str(MODE_SONG_PAGE)+"&id=0"+"&page="+str(page)+"&label="+str(trackLabelFormat) + u=sys.argv[0]+"?mode="+str(MODE_SONG_PAGE)+"&id=playlistid"+"&page="+str(page)+"&label="+str(trackLabelFormat)+"&name="+playlistname self._add_dir('More songs...', u, MODE_SONG_PAGE, self.songImg, 0, totalSongs - (page * self.songspagelimit)) xbmcplugin.setContent(self._handle, 'songs') xbmcplugin.setPluginFanart(int(sys.argv[1]), self.fanImg) # Add albums to directory - def _add_albums_directory(self, albums): + def _add_albums_directory(self, albums, artistid=0): n = len(albums) xbmc.log("Found " + str(n) + " albums...") i = 0 @@ -429,6 +662,8 @@ 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) xbmcplugin.setContent(self._handle, 'albums') xbmcplugin.addSortMethod(self._handle, xbmcplugin.SORT_METHOD_ALBUM_IGNORE_THE) xbmcplugin.setPluginFanart(int(sys.argv[1]), self.fanImg) @@ -465,12 +700,27 @@ class Groveshark: # Add whatever directory def _add_dir(self, name, url, mode, iconimage, id, items=1): + if url == '': u=sys.argv[0]+"?mode="+str(mode)+"&name="+urllib.quote_plus(name)+"&id="+str(id) else: u = url dir=xbmcgui.ListItem(name, iconImage=iconimage, thumbnailImage=iconimage) 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) + return xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]),url=u,listitem=dir,isFolder=True, totalItems=items) def _getSavedSongs(self): @@ -497,8 +747,6 @@ class Groveshark: xbmc.log("An error occurred saving songs") pass - - # Parse URL parameters def get_params(): param=[] @@ -528,6 +776,9 @@ except: pass id=0 try: id=int(params["id"]) except: pass +name = None +try: name=urllib.unquote_plus(params["name"]) +except: pass # Call function for URL if mode==None: @@ -543,10 +794,6 @@ elif mode==MODE_SEARCH_ARTISTS: grooveshark.searchArtists() elif mode==MODE_SEARCH_ARTISTS_ALBUMS: - try: name=urllib.unquote_plus(params["name"]) - except: - name = None - pass grooveshark.searchArtistsAlbums(name) elif mode==MODE_SEARCH_PLAYLISTS: @@ -569,11 +816,9 @@ elif mode==MODE_SONG_PAGE: except: pass try: label=urllib.unquote_plus(params["label"]) except: pass - grooveshark.songPage(page, label) + grooveshark.songPage(page, label, id, name) elif mode==MODE_SONG: - try: name=urllib.unquote_plus(params["name"]) - except: pass try: album=urllib.unquote_plus(params["album"]) except: pass try: artist=urllib.unquote_plus(params["artist"]) @@ -590,10 +835,39 @@ elif mode==MODE_ALBUM: grooveshark.album(id) elif mode==MODE_PLAYLIST: - grooveshark.playlist(id) + version = 2 + try: version=urllib.unquote_plus(params["version"]) + except: pass + grooveshark.playlist(id, name, version) elif mode==MODE_FAVORITE: grooveshark.favorite(id) +elif mode==MODE_UNFAVORITE: + try: prevMode=int(urllib.unquote_plus(params["prevmode"])) + except: + prevMode = 0 + grooveshark.unfavorite(id, prevMode) + +elif mode==MODE_SIMILAR_ARTISTS: + grooveshark.similarArtists(id) + +elif mode==MODE_MAKE_PLAYLIST: + grooveshark.makePlaylist(id, name) + +elif mode==MODE_REMOVE_PLAYLIST: + grooveshark.removePlaylist(id, name) + +elif mode==MODE_RENAME_PLAYLIST: + grooveshark.renamePlaylist(id, name) + +elif mode==MODE_REMOVE_PLAYLIST_SONG: + try: playlistID=urllib.unquote_plus(params["playlistid"]) + except: pass + grooveshark.removePlaylistSong(playlistID, name, id) + +elif mode==MODE_ADD_PLAYLIST_SONG: + grooveshark.addPlaylistSong(id) + if mode < MODE_SONG: xbmcplugin.endOfDirectory(int(sys.argv[1])) diff --git a/description.xml b/description.xml index 6503b7d..f34ea24 100644 --- a/description.xml +++ b/description.xml @@ -18,7 +18,7 @@ Grooveshark XBMC. - 0.3.0 + 0.4.0