X-Git-Url: https://git.hcoop.net/clinton/xbmc-groove.git/blobdiff_plain/6ae708d007bcbed8c7f668f47217b162f763a17e..e58dda71a07558849b55f1f9138a4f238afb275a:/default.py diff --git a/default.py b/default.py index cb5aa16..5c98792 100644 --- a/default.py +++ b/default.py @@ -1,162 +1,228 @@ -import urllib, urllib2, re, xbmcplugin, xbmcgui, xbmc, sys, os, time, pprint, shutil - -# plugin constants -__plugin__ = "Grooveshark" -__author__ = "Stephen Denham" -__url__ = "" -__svn_url__ = "" -__version__ = "0.0.1" -__svn_revision__ = "" -__XBMC_Revision__ = "" +import urllib, sys, os, shutil, re, xbmcaddon, xbmcplugin, xbmcgui, xbmc MODE_SEARCH_SONGS = 1 MODE_SEARCH_ALBUMS = 2 MODE_SEARCH_ARTISTS = 3 -MODE_POPULAR = 4 +MODE_POPULAR_SONGS = 4 MODE_FAVORITES = 5 MODE_PLAYLISTS = 6 MODE_ALBUM = 7 MODE_ARTIST = 8 MODE_PLAYLIST = 9 -MODE_SONG = 10 -MODE_FAVORITE = 11 -MODE_UNFAVORITE = 12 -MODE_SIMILAR_SONG = 13 -MODE_SIMILAR_ARTIST = 14 -MODE_FROWN = 15 - -songsearchlimit = 0 -albumsearchlimit = 0 -artistsearchlimit = 0 - -lastID = 0 +MODE_SIMILAR_ARTISTS = 10 +MODE_SONG = 11 +MODE_FAVORITE = 12 +MODE_UNFAVORITE = 13 +MODE_MAKE_PLAYLIST = 14 +MODE_REMOVE_PLAYLIST = 15 +MODE_RENAME_PLAYLIST = 16 +MODE_REMOVE_PLAYLIST_SONG = 17 +MODE_ADD_PLAYLIST_SONG = 18 + +ACTION_MOVE_LEFT = 1 +ACTION_MOVE_UP = 3 +ACTION_MOVE_DOWN = 4 +ACTION_PAGE_UP = 5 +ACTION_PAGE_DOWN = 6 +ACTION_SELECT_ITEM = 7 +ACTION_PREVIOUS_MENU = 10 baseDir = os.getcwd() resDir = xbmc.translatePath(os.path.join(baseDir, 'resources')) libDir = xbmc.translatePath(os.path.join(resDir, 'lib')) imgDir = xbmc.translatePath(os.path.join(resDir, 'img')) -thumbDir = os.path.join('special://masterprofile/plugin_data/music', os.path.join(os.path.basename(os.getcwd()), 'thumb')) +thumbDir = os.path.join('special://masterprofile/addon_data/', os.path.join(os.path.basename(os.getcwd()), 'thumb')) + +baseModeUrl = 'plugin://plugin.audio.groove/' +playlistUrl = baseModeUrl + '?mode=' + str(MODE_PLAYLIST) +playlistsUrl = baseModeUrl + '?mode=' + str(MODE_PLAYLISTS) +favoritesUrl = baseModeUrl + '?mode=' + str(MODE_FAVORITES) + +thumbDef = os.path.join(os.path.basename(os.getcwd()), 'default.tbn') +listBackground = os.path.join(imgDir, 'listbackground.png') sys.path.append (libDir) -from GrooveAPI import * -groovesharkApi = GrooveAPI() +from GrooveAPI import GrooveAPI +try: + groovesharkApi = GrooveAPI() +except: + dialog = xbmcgui.Dialog() + dialog.ok('Grooveshark XBMC', 'Unable to connect with Grooveshark.', 'Please try again later') + sys.exit(-1) + class _Info: def __init__( self, *args, **kwargs ): self.__dict__.update( kwargs ) - -class GrovesharkPlayer(xbmc.Player): - # Player Class: calls function when song changes or playback ends - def __init__(self, *args, **kwargs): - xbmc.Player.__init__(self) - self.function = kwargs[ "function" ] - - def onPlayBackStopped(self): - print "onPlayBackStopped" - xbmc.sleep(300) - if (not xbmc.Player().isPlayingAudio()): - self.function(0) - - def onPlayBackEnded(self): - print "onPlayBackEnded" - xbmc.sleep(300) - if (not xbmc.Player().isPlayingAudio()): - self.function(1) - - def onPlayBackStarted(self): - print "onPlayBackStarted" - self.function(2) +# 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: - - albumImg = xbmc.translatePath(os.path.join(imgDir, 'album.png')) - artistImg = xbmc.translatePath(os.path.join(imgDir, 'artist.png')) - favoritesImg = xbmc.translatePath(os.path.join(imgDir, 'favorites.png')) - playlistImg = xbmc.translatePath(os.path.join(imgDir, 'playlist.png')) - popularImg = xbmc.translatePath(os.path.join(imgDir, 'popular.png')) - songImg = xbmc.translatePath(os.path.join(imgDir, 'song.png')) - defImg = xbmc.translatePath(os.path.join(imgDir, 'default.tbn')) - fanImg = xbmc.translatePath(os.path.join(baseDir, 'fanart.jpg')) - - songsearchlimit = xbmcplugin.getSetting('songsearchlimit') - albumsearchlimit = xbmcplugin.getSetting('albumsearchlimit') - artistsearchlimit = xbmcplugin.getSetting('artistsearchlimit') - - + + albumImg = xbmc.translatePath(os.path.join(imgDir, 'album.png')) + artistImg = xbmc.translatePath(os.path.join(imgDir, 'artist.png')) + favoritesImg = xbmc.translatePath(os.path.join(imgDir, 'favorites.png')) + playlistImg = xbmc.translatePath(os.path.join(imgDir, 'playlist.png')) + popularSongsImg = xbmc.translatePath(os.path.join(imgDir, 'popularSongs.png')) + songImg = xbmc.translatePath(os.path.join(imgDir, 'song.png')) + defImg = xbmc.translatePath(os.path.join(imgDir, 'default.tbn')) + fanImg = xbmc.translatePath(os.path.join(baseDir, 'fanart.png')) + + settings = xbmcaddon.Addon(id='plugin.audio.groove') + songsearchlimit = settings.getSetting('songsearchlimit') + albumsearchlimit = settings.getSetting('albumsearchlimit') + artistsearchlimit = settings.getSetting('artistsearchlimit') + username = settings.getSetting('username') + password = settings.getSetting('password') + userid = 0 + def __init__( self ): self._handle = int(sys.argv[1]) + # Top-level menu def categories(self): - - userid = self._get_login() + + self.userid = self._get_login() # Setup - groovesharkApi.setRemoveDuplicates(True) xbmcplugin.setPluginFanart(int(sys.argv[1]), self.fanImg) if os.path.isdir(thumbDir) == False: os.makedirs(thumbDir) - self._add_dir('Search songs', '', MODE_SEARCH_SONGS, self.songImg, 0) - self._add_dir('Search albums', '', MODE_SEARCH_ALBUMS, self.albumImg, 0) - self._add_dir('Search artists', '', MODE_SEARCH_ARTISTS, self.artistImg, 0) - self._add_dir('Popular', '', MODE_POPULAR, self.popularImg, 0) - if (userid != 0): + self._add_dir('Search songs...', '', MODE_SEARCH_SONGS, self.songImg, 0) + self._add_dir('Search albums...', '', MODE_SEARCH_ALBUMS, self.albumImg, 0) + self._add_dir('Search artists...', '', MODE_SEARCH_ARTISTS, self.artistImg, 0) + self._add_dir('Popular songs', '', MODE_POPULAR_SONGS, self.popularSongsImg, 0) + if (self.userid != 0): self._add_dir('Favorites', '', MODE_FAVORITES, self.favoritesImg, 0) self._add_dir('Playlists', '', MODE_PLAYLISTS, self.playlistImg, 0) - + + # Search for songs def searchSongs(self): query = self._get_keyboard(default="", heading="Search songs") - if (query): - songs = groovesharkApi.searchSongs(query, limit = xbmcplugin.getSetting('songsearchlimit')) + if (query != ''): + groovesharkApi.setRemoveDuplicates(True) + songs = groovesharkApi.searchSongs(query, limit = self.songsearchlimit) if (len(songs) > 0): self._add_songs_directory(songs) else: dialog = xbmcgui.Dialog() - dialog.ok('Grooveshark', 'No matching songs.') + dialog.ok('Grooveshark XBMC', 'No matching songs.') self.categories() + else: + self.categories() + # Search for albums def searchAlbums(self): query = self._get_keyboard(default="", heading="Search albums") - if (query): - albums = groovesharkApi.searchAlbums(query, limit = xbmcplugin.getSetting('albumsearchlimit')) + if (query != ''): + albums = groovesharkApi.searchAlbums(query, limit = self.albumsearchlimit) if (len(albums) > 0): self._add_albums_directory(albums) else: dialog = xbmcgui.Dialog() - dialog.ok('Grooveshark', 'No matching albums.') + dialog.ok('Grooveshark XBMC', 'No matching albums.') self.categories() + else: + self.categories() + # Search for artists def searchArtists(self): query = self._get_keyboard(default="", heading="Search artists") - if (query): - artists = groovesharkApi.searchArtists(query, limit = xbmcplugin.getSetting('artistsearchlimit')) + if (query != ''): + artists = groovesharkApi.searchArtists(query, limit = self.artistsearchlimit) if (len(artists) > 0): self._add_artists_directory(artists) else: dialog = xbmcgui.Dialog() - dialog.ok('Grooveshark', 'No matching artists.') + dialog.ok('Grooveshark XBMC', 'No matching artists.') self.categories() + else: + self.categories() + # Get my favorites def favorites(self): userid = self._get_login() if (userid != 0): favorites = groovesharkApi.userGetFavoriteSongs(userid) if (len(favorites) > 0): - self._add_songs_directory(favorites) + self._add_songs_directory(favorites, isFavorites=True) else: dialog = xbmcgui.Dialog() - dialog.ok('Grooveshark', 'You have no favorites.') + dialog.ok('Grooveshark XBMC', 'You have no favorites.') self.categories() - def popular(self): - popular = groovesharkApi.popularGetSongs(limit = xbmcplugin.getSetting('songsearchlimit')) + # Get popular songs + def popularSongs(self): + popular = groovesharkApi.popularGetSongs(limit = self.songsearchlimit) if (len(popular) > 0): self._add_songs_directory(popular) else: dialog = xbmcgui.Dialog() - dialog.ok('Grooveshark', 'No popular songs.') + dialog.ok('Grooveshark XBMC', 'No popular songs.') self.categories() - + + # Get my playlists def playlists(self): userid = self._get_login() if (userid != 0): @@ -165,134 +231,246 @@ class Groveshark: self._add_playlists_directory(playlists) else: dialog = xbmcgui.Dialog() - dialog.ok('Grooveshark', 'You have no playlists.') + 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 see your Grooveshark playlists.') + # Make songs a favorite def favorite(self, songid): userid = self._get_login() if (userid != 0): - xbmc.log("Favorite playSong: " + str(songid)) + xbmc.log("Favorite song: " + str(songid)) groovesharkApi.favoriteSong(songID = songid) + xbmc.executebuiltin('XBMC.Notification(Grooveshark XBMC, Added to favorites, 1000, ' + thumbDef + ')') else: dialog = xbmcgui.Dialog() - dialog.ok('Grooveshark', 'You must be logged in', 'to add favorites.') + dialog.ok('Grooveshark XBMC', 'You must be logged in', 'to add favorites.') - def unfavorite(self, songid): + # Remove song from favorites + def unfavorite(self, songid, prevMode=0): userid = self._get_login() if (userid != 0): - xbmc.log("Unfavorite playSong: " + str(songid)) + xbmc.log("Unfavorite song: " + str(songid) + ', previous mode was ' + str(prevMode)) groovesharkApi.unfavoriteSong(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', 'You must be logged in', 'to remove favorites.') + dialog.ok('Grooveshark XBMC', 'You must be logged in', 'to remove Grooveshark favorites.') + # 'Frown' a song def frown(self, songid): userid = self._get_login() if (userid != 0): - xbmc.log("Frown playSong: " + str(songid)) + xbmc.log("Frown song: " + str(songid)) if groovesharkApi.radioFrown(songId = songid) != True: xbmc.log("Unable to frown song " + str(songid)) + else: + xbmc.executebuiltin('XBMC.Notification(Grooveshark XBMC, Frowned, 1000, ' + thumbDef + ')') else: dialog = xbmcgui.Dialog() - dialog.ok('Grooveshark', 'You must be logged in', 'to frown a song.') + dialog.ok('Grooveshark XBMC', 'You must be logged in', 'to frown a Grooveshark song.') - def similarSong(self, songid): + # Find similar artists to searched artist + def similarArtists(self, artistId): + similar = groovesharkApi.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() + + # Make a playlist from an album + def makePlaylist(self, albumid, name): userid = self._get_login() if (userid != 0): - xbmc.log("Frown playSong: " + str(songid)) - if groovesharkApi.radioSong(songId = songid) and groovesharkApi.radioStartSongs() == True: - self.playNext() - else: + re.split(' - ',name,1) + nameTokens = re.split(' - ',name,1) # suggested name + name = self._get_keyboard(default=nameTokens[0], heading="Grooveshark playlist name") + if name != '': + groovesharkApi.setRemoveDuplicates(True) + album = groovesharkApi.albumGetSongs(albumid, self.songsearchlimit) + songids = [] + for song in album: + songids.append(song[1]) + if groovesharkApi.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() + if (userid != 0): + newname = self._get_keyboard(default=name, heading="Grooveshark playlist name") + if newname == '': + return + elif groovesharkApi.playlistRename(playlistid, newname) == 0: dialog = xbmcgui.Dialog() - dialog.ok('Grooveshark', 'Cannot start radio') + 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', 'You must be logged in', 'to update radio song.') + 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() + if (userid != 0): + if groovesharkApi.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.') - def similarArtist(self, artistId): + # Add song to playlist + def addPlaylistSong(self, songid): userid = self._get_login() if (userid != 0): - xbmc.log("Add radio artist of playSong: " + str(artistId)) - if groovesharkApi.radioArtist(artistId = artistId) and groovesharkApi.radioStartArtists() == True: - self.playNext() + playlists = groovesharkApi.userGetPlaylists() + 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 groovesharkApi.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 = groovesharkApi.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', 'Cannot start radio') + dialog.ok('Grooveshark XBMC', 'You have no Grooveshark playlists.') + self.categories() else: dialog = xbmcgui.Dialog() - dialog.ok('Grooveshark', 'You must be logged in', 'to update radio artists.') - - def album(self,albumid): - album = groovesharkApi.albumGetSongs(albumId = albumid, limit = xbmcplugin.getSetting('songsearchlimit')) + 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() + if dialog.yesno('Grooveshark XBMC', 'Delete this song from the Grooveshark playlist?') == True: + userid = self._get_login() + if (userid != 0): + if groovesharkApi.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("Container.Refresh(" + playlistUrl + "&id="+str(playlistid) + "&name=" + playlistname + ")") + else: + dialog = xbmcgui.Dialog() + dialog.ok('Grooveshark XBMC', 'You must be logged in ', ' to delete a song from a Grooveshark playlist.') + + # Show selected album + def album(self, albumid): + groovesharkApi.setRemoveDuplicates(True) + album = groovesharkApi.albumGetSongs(albumId = albumid, limit = self.songsearchlimit) self._add_songs_directory(album) - + + # Show selected artist def artist(self, artistid): - albums = groovesharkApi.artistGetAlbums(artistId = artistid, limit = xbmcplugin.getSetting('albumsearchlimit')) - self._add_albums_directory(albums) + albums = groovesharkApi.artistGetAlbums(artistId = artistid, limit = self.albumsearchlimit) + self._add_albums_directory(albums, artistid) - def playlist(self, playlistid): + # Show selected playlist + def playlist(self, playlistid, playlistname): userid = self._get_login() if (userid != 0): - songs = groovesharkApi.playlistGetSongs(playlistId = playlistid, limit = xbmcplugin.getSetting('songsearchlimit')) - self._add_songs_directory(songs) + songs = groovesharkApi.playlistGetSongs(playlistId = playlistid, limit = self.songsearchlimit) + self._add_songs_directory(songs, playlistid, playlistname) else: dialog = xbmcgui.Dialog() - dialog.ok('Grooveshark', 'You must be logged in', 'to get playlists.') + dialog.ok('Grooveshark XBMC', 'You must be logged in', 'to get Grooveshark playlists.') + # Play a song def playSong(self, item): - url = item.getProperty('url') + url = groovesharkApi.getStreamURL(item.getProperty('id')) + item.setPath(url) xbmc.log("Playing: " + url) - xbmc.Player( xbmc.PLAYER_CORE_MPLAYER ).play(url, item) + xbmcplugin.setResolvedUrl(handle=int(sys.argv[1]), succeeded=True, listitem=item) - def playNext(self): - item = self._get_song_item(groovesharkApi.radioNextSong()[0]) - url = item.getProperty('url') - self.player = GrovesharkPlayer(xbmc.PLAYER_CORE_PAPLAYER, function=self.onPlayBackChanged) - self.player.play(url, item) - + # Make a song directory item def songItem(self, id, name, album, artist, duration, thumb, image): - url = groovesharkApi.getStreamURL(id) - songImg = self._get_icon(image, str(id) + "-image") - print songImg - if songImg == "": - songImg = image - songThm = self._get_icon(thumb, str(id) + "-thumb") - if songThm == "": - songThm = thumb - item = xbmcgui.ListItem(label = artist + " - " + album + " - " + name, path=url, thumbnailImage=songThm, iconImage=songImg) - item.setInfo( type="Music", infoLabels={ "title": name, "duration": duration, "album": album, "artist": artist} ) - item.setProperty('url', url) + # Only try to get the image + if image != "": + songImg = self._get_icon(image, 'song-' + str(id) + "-image") + songThm = songImg + else: + songThm = self._get_icon(thumb, 'song-' + str(id) + "-thumb") + songImg = songThm + item = xbmcgui.ListItem(label = artist + " - " + album + " - " + name, thumbnailImage=songThm, iconImage=songImg) + item.setInfo( type="music", infoLabels={ "title": name, "duration": duration, "album": album, "artist": artist} ) + item.setProperty('mimetype', 'audio/mpeg') + item.setProperty("IsPlayable", "true") + item.setProperty('id', str(id)) item.setProperty('thumb', songThm) item.setProperty('image', songImg) - item.setProperty('mimetype', 'audio/mpeg') return item - + + # Get keyboard input def _get_keyboard(self, default="", heading="", hidden=False): - kb = xbmc.Keyboard(default, heading, hidden) - kb.doModal() - if (kb.isConfirmed()): - return unicode(kb.getText(), "utf-8") - return '' + kb = xbmc.Keyboard(default, heading, hidden) + kb.doModal() + if (kb.isConfirmed()): + return unicode(kb.getText(), "utf-8") + return '' + # Login to grooveshark def _get_login(self): - username = xbmcplugin.getSetting('username') - password = xbmcplugin.getSetting('password') - if (username == "" or password == ""): + if (self.username == "" or self.password == ""): dialog = xbmcgui.Dialog() - dialog.ok('Grooveshark', 'Unable to login.', 'Check username and password in settings.') + dialog.ok('Grooveshark XBMC', 'Unable to login.', 'Check username and password in settings.') return 0 else: - if groovesharkApi.loggedInStatus() == 1: - groovesharkApi.logout() - userid = groovesharkApi.loginExt(username, password) - if (userid != 0): - xbmc.log("Logged in") - return userid + if self.userid == 0: + uid = groovesharkApi.loginExt(self.username, self.password) + if (uid != 0): + return uid else: dialog = xbmcgui.Dialog() - dialog.ok('Grooveshark', 'Unable to login.', 'Check username and password in settings.') + dialog.ok('Grooveshark XBMC', 'Unable to login.', 'Check username and password in settings.') return 0 + # Get a song directory item def _get_song_item(self, song): name = song[0] id = song[1] @@ -305,29 +483,25 @@ class Groveshark: # File download def _get_icon(self, url, id): - # Get the channel icon localThumb = os.path.join(xbmc.translatePath(os.path.join(thumbDir, str(id)))) + '.tbn' try: if os.path.isfile(localThumb) == False: + xbmc.log('Downloading ' + url + ' to ' + localThumb) loc = urllib.URLopener() loc.retrieve(url, localThumb) except: - xbmc.log('URL download failed of ' + url + ' to ' + localThumb) - try: - shutil.copyfile(self.defImg, localThumb) - except: pass - return "" + shutil.copy2(self.defImg, localThumb) return os.path.join(os.path.join(thumbDir, str(id))) + '.tbn' - - def _add_songs_directory(self, songs): + + # Add songs to directory + def _add_songs_directory(self, songs, playlistid=0, playlistname='', isFavorites=False): n = len(songs) xbmc.log("Found " + str(n) + " songs...") i = 0 while i < n: song = songs[i] item = self._get_song_item(song) - songurl = item.getProperty('url') songthumb = item.getProperty('thumb') songimage = item.getProperty('image') songname = song[0] @@ -335,95 +509,109 @@ class Groveshark: songduration = song[2] songalbum = song[3] songartist = song[6] - songartistid = song[7] - u=sys.argv[0]+"?url="+urllib.quote_plus(songurl) \ - +"&mode="+str(MODE_SONG)+"&name="+urllib.quote_plus(songname)+"&id=" \ - +str(songid) \ + 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) \ +"&duration="+str(songduration) \ +"&thumb="+urllib.quote_plus(songthumb) \ +"&image="+urllib.quote_plus(songimage) - fav=sys.argv[0]+"?url="+urllib.quote_plus(songurl)+"&mode="+str(MODE_FAVORITE)+"&name="+urllib.quote_plus(songname)+"&id="+str(songid) - unfav=sys.argv[0]+"?url="+urllib.quote_plus(songurl)+"&mode="+str(MODE_UNFAVORITE)+"&name="+urllib.quote_plus(songname)+"&id="+str(songid) - similarArtist=sys.argv[0]+"?mode="+str(MODE_SIMILAR_ARTIST)+"&id="+str(songartistid) - similarSong=sys.argv[0]+"?mode="+str(MODE_SIMILAR_SONG)+"&id="+str(songid) - #frown=sys.argv[0]+"?mode="+str(MODE_FROWN)+"&id="+str(songid) + 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+")")) - menuItems.append(("Not Grooveshark Favorite", "XBMC.RunPlugin("+unfav+")")) - menuItems.append(("Listen to similar artist", "XBMC.RunPlugin("+similarArtist+")")) - menuItems.append(("Listen to similar song", "XBMC.RunPlugin("+similarSong+")")) - # menuItems.append(("No thanks!", "XBMC.RunPlugin("+frown+")")) + if isFavorites == True: + unfav = unfav +str(MODE_FAVORITES) + else: + menuItems.append(("Grooveshark favorite", "XBMC.RunPlugin("+fav+")")) + menuItems.append(("Not Grooveshark favorite", "XBMC.RunPlugin("+unfav+")")) + if playlistid > 0: + rmplaylstsong=sys.argv[0]+"?playlistid="+str(playlistid)+"&id="+str(i+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) + xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]),url=u,listitem=item,isFolder=False, totalItems=n) i = i + 1 + xbmcplugin.setContent(self._handle, 'songs') - - def _add_albums_directory(self, albums): - xbmc.log("Found " + str(len(albums)) + " albums...") + xbmcplugin.setPluginFanart(int(sys.argv[1]), self.fanImg) + + # Add albums to directory + def _add_albums_directory(self, albums, artistid=0): + n = len(albums) + xbmc.log("Found " + str(n) + " albums...") i = 0 - while i < len(albums): + while i < n: album = albums[i] albumArtistName = album[0] albumName = album[2] albumID = album[3] - albumImage = album[4] - self._add_dir(albumName + " - " + albumArtistName, '', MODE_ALBUM, albumImage, albumID) + 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: + 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) + + # Add artists to directory def _add_artists_directory(self, artists): - xbmc.log("Found " + str(len(artists)) + " artists...") + n = len(artists) + xbmc.log("Found " + str(n) + " artists...") i = 0 - while i < len(artists): + while i < n: artist = artists[i] artistName = artist[0] artistID = artist[1] - self._add_dir(artistName, '', MODE_ARTIST, self.artistImg, artistID) + self._add_dir(artistName, '', MODE_ARTIST, self.artistImg, artistID, n) i = i + 1 xbmcplugin.setContent(self._handle, 'artists') xbmcplugin.addSortMethod(self._handle, xbmcplugin.SORT_METHOD_ARTIST_IGNORE_THE) - + xbmcplugin.setPluginFanart(int(sys.argv[1]), self.fanImg) + + # Add playlists to directory def _add_playlists_directory(self, playlists): - xbmc.log("Found " + str(len(playlists)) + " playlists...") + n = len(playlists) + xbmc.log("Found " + str(n) + " playlists...") i = 0 - while i < len(playlists): + while i < n: playlist = playlists[i] playlistName = playlist[0] playlistID = playlist[1] - self._add_dir(playlistName, '', MODE_PLAYLIST, self.playlistImg, playlistID) + self._add_dir(playlistName, '', MODE_PLAYLIST, self.playlistImg, playlistID, n) i = i + 1 xbmcplugin.setContent(self._handle, 'files') xbmcplugin.addSortMethod(self._handle, xbmcplugin.SORT_METHOD_PLAYLIST_ORDER) + xbmcplugin.setPluginFanart(int(sys.argv[1]), self.fanImg) - def _add_dir(self, name, url, mode, iconimage, id): - u=sys.argv[0]+"?url="+urllib.quote_plus(url)+"&mode="+str(mode)+"&name="+urllib.quote_plus(name)+"&id="+str(id) + # Add whatever directory + def _add_dir(self, name, url, mode, iconimage, id, items=1): + + u=sys.argv[0]+"?mode="+str(mode)+"&name="+urllib.quote_plus(name)+"&id="+str(id) dir=xbmcgui.ListItem(name, iconImage=iconimage, thumbnailImage=iconimage) dir.setInfo( type="Music", infoLabels={ "title": name } ) - # Codes from http://xbmc-scripting.googlecode.com/svn/trunk/Script%20Templates/common/gui/codes.py + + # Custom menu items menuItems = [] - menuItems.append(("Select", "XBMC.executebuiltin(Action(7))")) - dir.addContextMenuItems(menuItems, replaceItems=True) - return xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]),url=u,listitem=dir,isFolder=True) - - def onPlayBackChanged(self, change): - print "onPlayBackChanged " + str(change) - if change == 0: - groovesharkApi.radioEnabled = False - self.musicplaylist.clear() - if change == 1 and groovesharkApi.radioTurnedOn() == True: - print "playnext" - self.player.playNext() - if change == 1 and groovesharkApi.radioTurnedOn() == False: - print "clear playlist" - self.musicplaylist.clear() - - + 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) + + +# Parse URL parameters def get_params(): param=[] paramstring=sys.argv[2] + xbmc.log(paramstring) if len(paramstring)>=2: params=sys.argv[2] cleanedparams=params.replace('?','') @@ -436,29 +624,25 @@ def get_params(): splitparams=pairsofparams[i].split('=') if (len(splitparams))==2: param[splitparams[0]]=splitparams[1] - print param return param +# Main grooveshark = Groveshark(); - + params=get_params() -url=None -name=None mode=None -id=None - -try: url=urllib.unquote_plus(params["url"]) -except: pass -try: name=urllib.unquote_plus(params["name"]) -except: pass try: mode=int(params["mode"]) except: pass -try: id=int(params["id"]) -except: pass -if (id > 0): - lastID = id +if mode > MODE_FAVORITES: + name=None + id=None + try: name=urllib.unquote_plus(params["name"]) + except: pass + try: id=int(params["id"]) + except: pass +# Call function for URL if mode==None: grooveshark.categories() @@ -471,18 +655,16 @@ elif mode==MODE_SEARCH_ALBUMS: elif mode==MODE_SEARCH_ARTISTS: grooveshark.searchArtists() -elif mode==MODE_POPULAR: - grooveshark.popular() - -elif mode==MODE_PLAYLISTS: - grooveshark.playlists() +elif mode==MODE_POPULAR_SONGS: + grooveshark.popularSongs() elif mode==MODE_FAVORITES: grooveshark.favorites() +elif mode==MODE_PLAYLISTS: + grooveshark.playlists() + 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"]) @@ -497,28 +679,42 @@ elif mode==MODE_SONG: grooveshark.playSong(song) elif mode==MODE_ARTIST: - grooveshark.artist(lastID) + grooveshark.artist(id) elif mode==MODE_ALBUM: - grooveshark.album(lastID) + grooveshark.album(id) elif mode==MODE_PLAYLIST: - grooveshark.playlist(lastID) + grooveshark.playlist(id, name) elif mode==MODE_FAVORITE: - grooveshark.favorite(lastID) + grooveshark.favorite(id) elif mode==MODE_UNFAVORITE: - grooveshark.unfavorite(lastID) - -elif mode==MODE_SIMILAR_ARTIST: - grooveshark.similarArtist(lastID) + try: prevMode=int(urllib.unquote_plus(params["prevmode"])) + except: + prevMode = 0 + grooveshark.unfavorite(id, prevMode) -elif mode==MODE_SIMILAR_SONG: - grooveshark.similarSong(lastID) +elif mode==MODE_SIMILAR_ARTISTS: + grooveshark.similarArtists(id) -elif mode==MODE_FROWN: - grooveshark.frown(lastID) +elif mode==MODE_MAKE_PLAYLIST: + grooveshark.makePlaylist(id, name) -if (mode < MODE_SONG): +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]))