| 1 | import urllib, sys, os, shutil, re, xbmcaddon, xbmcplugin, xbmcgui, xbmc |
| 2 | |
| 3 | MODE_SEARCH_SONGS = 1 |
| 4 | MODE_SEARCH_ALBUMS = 2 |
| 5 | MODE_SEARCH_ARTISTS = 3 |
| 6 | MODE_POPULAR_SONGS = 4 |
| 7 | MODE_FAVORITES = 5 |
| 8 | MODE_PLAYLISTS = 6 |
| 9 | MODE_ALBUM = 7 |
| 10 | MODE_ARTIST = 8 |
| 11 | MODE_PLAYLIST = 9 |
| 12 | MODE_SIMILAR_ARTISTS = 10 |
| 13 | MODE_SONG = 11 |
| 14 | MODE_FAVORITE = 12 |
| 15 | MODE_UNFAVORITE = 13 |
| 16 | MODE_MAKE_PLAYLIST = 14 |
| 17 | MODE_REMOVE_PLAYLIST = 15 |
| 18 | MODE_RENAME_PLAYLIST = 16 |
| 19 | MODE_REMOVE_PLAYLIST_SONG = 17 |
| 20 | MODE_ADD_PLAYLIST_SONG = 18 |
| 21 | |
| 22 | ACTION_MOVE_LEFT = 1 |
| 23 | ACTION_MOVE_UP = 3 |
| 24 | ACTION_MOVE_DOWN = 4 |
| 25 | ACTION_PAGE_UP = 5 |
| 26 | ACTION_PAGE_DOWN = 6 |
| 27 | ACTION_SELECT_ITEM = 7 |
| 28 | ACTION_PREVIOUS_MENU = 10 |
| 29 | |
| 30 | baseDir = os.getcwd() |
| 31 | resDir = xbmc.translatePath(os.path.join(baseDir, 'resources')) |
| 32 | libDir = xbmc.translatePath(os.path.join(resDir, 'lib')) |
| 33 | imgDir = xbmc.translatePath(os.path.join(resDir, 'img')) |
| 34 | thumbDir = os.path.join('special://masterprofile/addon_data/', os.path.join(os.path.basename(os.getcwd()), 'thumb')) |
| 35 | |
| 36 | baseModeUrl = 'plugin://plugin.audio.groove/' |
| 37 | playlistUrl = baseModeUrl + '?mode=' + str(MODE_PLAYLIST) |
| 38 | playlistsUrl = baseModeUrl + '?mode=' + str(MODE_PLAYLISTS) |
| 39 | favoritesUrl = baseModeUrl + '?mode=' + str(MODE_FAVORITES) |
| 40 | |
| 41 | thumbDef = os.path.join(os.path.basename(os.getcwd()), 'default.tbn') |
| 42 | listBackground = os.path.join(imgDir, 'listbackground.png') |
| 43 | |
| 44 | sys.path.append (libDir) |
| 45 | from GrooveAPI import GrooveAPI |
| 46 | try: |
| 47 | groovesharkApi = GrooveAPI() |
| 48 | except: |
| 49 | dialog = xbmcgui.Dialog() |
| 50 | dialog.ok('Grooveshark XBMC', 'Unable to connect with Grooveshark.', 'Please try again later') |
| 51 | sys.exit(-1) |
| 52 | |
| 53 | |
| 54 | class _Info: |
| 55 | def __init__( self, *args, **kwargs ): |
| 56 | self.__dict__.update( kwargs ) |
| 57 | |
| 58 | # Window dialog to select a grooveshark playlist |
| 59 | class GroovesharkPlaylistSelect(xbmcgui.WindowDialog): |
| 60 | |
| 61 | def __init__(self, items=[]): |
| 62 | gap = int(self.getHeight()/100) |
| 63 | w = int(self.getWidth()*0.5) |
| 64 | h = self.getHeight()-30*gap |
| 65 | rw = self.getWidth() |
| 66 | rh = self.getHeight() |
| 67 | x = rw/2 - w/2 |
| 68 | y = rh/2 -h/2 |
| 69 | |
| 70 | self.imgBg = xbmcgui.ControlImage(x+gap, 5*gap+y, w-2*gap, h-5*gap, listBackground) |
| 71 | self.addControl(self.imgBg) |
| 72 | |
| 73 | 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) |
| 74 | self.addControl(self.playlistControl) |
| 75 | |
| 76 | self.lastPos = 0 |
| 77 | self.isSelecting = False |
| 78 | self.selected = -1 |
| 79 | listitems = [] |
| 80 | for playlist in items: |
| 81 | listitems.append(xbmcgui.ListItem(playlist[0])) |
| 82 | listitems.append(xbmcgui.ListItem('New...')) |
| 83 | self.playlistControl.addItems(listitems) |
| 84 | self.setFocus(self.playlistControl) |
| 85 | self.playlistControl.selectItem(0) |
| 86 | item = self.playlistControl.getListItem(self.lastPos) |
| 87 | item.select(True) |
| 88 | |
| 89 | # Highlight selected item |
| 90 | def setHighlight(self): |
| 91 | if self.isSelecting: |
| 92 | return |
| 93 | else: |
| 94 | self.isSelecting = True |
| 95 | |
| 96 | pos = self.playlistControl.getSelectedPosition() |
| 97 | if pos >= 0: |
| 98 | item = self.playlistControl.getListItem(self.lastPos) |
| 99 | item.select(False) |
| 100 | item = self.playlistControl.getListItem(pos) |
| 101 | item.select(True) |
| 102 | self.lastPos = pos |
| 103 | self.isSelecting = False |
| 104 | |
| 105 | # Control - select |
| 106 | def onControl(self, control): |
| 107 | if control == self.playlistControl: |
| 108 | self.selected = self.playlistControl.getSelectedPosition() |
| 109 | self.close() |
| 110 | |
| 111 | # Action - close or up/down |
| 112 | def onAction(self, action): |
| 113 | if action == ACTION_PREVIOUS_MENU: |
| 114 | self.selected = -1 |
| 115 | self.close() |
| 116 | elif action == ACTION_MOVE_UP or action == ACTION_MOVE_DOWN or action == ACTION_PAGE_UP or action == ACTION_PAGE_DOWN == 6: |
| 117 | self.setFocus(self.playlistControl) |
| 118 | self.setHighlight() |
| 119 | |
| 120 | class Groveshark: |
| 121 | |
| 122 | albumImg = xbmc.translatePath(os.path.join(imgDir, 'album.png')) |
| 123 | artistImg = xbmc.translatePath(os.path.join(imgDir, 'artist.png')) |
| 124 | favoritesImg = xbmc.translatePath(os.path.join(imgDir, 'favorites.png')) |
| 125 | playlistImg = xbmc.translatePath(os.path.join(imgDir, 'playlist.png')) |
| 126 | popularSongsImg = xbmc.translatePath(os.path.join(imgDir, 'popularSongs.png')) |
| 127 | songImg = xbmc.translatePath(os.path.join(imgDir, 'song.png')) |
| 128 | defImg = xbmc.translatePath(os.path.join(imgDir, 'default.tbn')) |
| 129 | fanImg = xbmc.translatePath(os.path.join(baseDir, 'fanart.png')) |
| 130 | |
| 131 | settings = xbmcaddon.Addon(id='plugin.audio.groove') |
| 132 | songsearchlimit = settings.getSetting('songsearchlimit') |
| 133 | albumsearchlimit = settings.getSetting('albumsearchlimit') |
| 134 | artistsearchlimit = settings.getSetting('artistsearchlimit') |
| 135 | username = settings.getSetting('username') |
| 136 | password = settings.getSetting('password') |
| 137 | userid = 0 |
| 138 | |
| 139 | def __init__( self ): |
| 140 | self._handle = int(sys.argv[1]) |
| 141 | |
| 142 | # Top-level menu |
| 143 | def categories(self): |
| 144 | |
| 145 | self.userid = self._get_login() |
| 146 | |
| 147 | # Setup |
| 148 | xbmcplugin.setPluginFanart(int(sys.argv[1]), self.fanImg) |
| 149 | if os.path.isdir(thumbDir) == False: |
| 150 | os.makedirs(thumbDir) |
| 151 | |
| 152 | self._add_dir('Search songs...', '', MODE_SEARCH_SONGS, self.songImg, 0) |
| 153 | self._add_dir('Search albums...', '', MODE_SEARCH_ALBUMS, self.albumImg, 0) |
| 154 | self._add_dir('Search artists...', '', MODE_SEARCH_ARTISTS, self.artistImg, 0) |
| 155 | self._add_dir('Popular songs', '', MODE_POPULAR_SONGS, self.popularSongsImg, 0) |
| 156 | if (self.userid != 0): |
| 157 | self._add_dir('Favorites', '', MODE_FAVORITES, self.favoritesImg, 0) |
| 158 | self._add_dir('Playlists', '', MODE_PLAYLISTS, self.playlistImg, 0) |
| 159 | |
| 160 | # Search for songs |
| 161 | def searchSongs(self): |
| 162 | query = self._get_keyboard(default="", heading="Search songs") |
| 163 | if (query != ''): |
| 164 | groovesharkApi.setRemoveDuplicates(True) |
| 165 | songs = groovesharkApi.searchSongs(query, limit = self.songsearchlimit) |
| 166 | if (len(songs) > 0): |
| 167 | self._add_songs_directory(songs) |
| 168 | else: |
| 169 | dialog = xbmcgui.Dialog() |
| 170 | dialog.ok('Grooveshark XBMC', 'No matching songs.') |
| 171 | self.categories() |
| 172 | else: |
| 173 | self.categories() |
| 174 | |
| 175 | # Search for albums |
| 176 | def searchAlbums(self): |
| 177 | query = self._get_keyboard(default="", heading="Search albums") |
| 178 | if (query != ''): |
| 179 | albums = groovesharkApi.searchAlbums(query, limit = self.albumsearchlimit) |
| 180 | if (len(albums) > 0): |
| 181 | self._add_albums_directory(albums) |
| 182 | else: |
| 183 | dialog = xbmcgui.Dialog() |
| 184 | dialog.ok('Grooveshark XBMC', 'No matching albums.') |
| 185 | self.categories() |
| 186 | else: |
| 187 | self.categories() |
| 188 | |
| 189 | # Search for artists |
| 190 | def searchArtists(self): |
| 191 | query = self._get_keyboard(default="", heading="Search artists") |
| 192 | if (query != ''): |
| 193 | artists = groovesharkApi.searchArtists(query, limit = self.artistsearchlimit) |
| 194 | if (len(artists) > 0): |
| 195 | self._add_artists_directory(artists) |
| 196 | else: |
| 197 | dialog = xbmcgui.Dialog() |
| 198 | dialog.ok('Grooveshark XBMC', 'No matching artists.') |
| 199 | self.categories() |
| 200 | else: |
| 201 | self.categories() |
| 202 | |
| 203 | # Get my favorites |
| 204 | def favorites(self): |
| 205 | userid = self._get_login() |
| 206 | if (userid != 0): |
| 207 | favorites = groovesharkApi.userGetFavoriteSongs(userid) |
| 208 | if (len(favorites) > 0): |
| 209 | self._add_songs_directory(favorites, isFavorites=True) |
| 210 | else: |
| 211 | dialog = xbmcgui.Dialog() |
| 212 | dialog.ok('Grooveshark XBMC', 'You have no favorites.') |
| 213 | self.categories() |
| 214 | |
| 215 | # Get popular songs |
| 216 | def popularSongs(self): |
| 217 | popular = groovesharkApi.popularGetSongs(limit = self.songsearchlimit) |
| 218 | if (len(popular) > 0): |
| 219 | self._add_songs_directory(popular) |
| 220 | else: |
| 221 | dialog = xbmcgui.Dialog() |
| 222 | dialog.ok('Grooveshark XBMC', 'No popular songs.') |
| 223 | self.categories() |
| 224 | |
| 225 | # Get my playlists |
| 226 | def playlists(self): |
| 227 | userid = self._get_login() |
| 228 | if (userid != 0): |
| 229 | playlists = groovesharkApi.userGetPlaylists() |
| 230 | if (len(playlists) > 0): |
| 231 | self._add_playlists_directory(playlists) |
| 232 | else: |
| 233 | dialog = xbmcgui.Dialog() |
| 234 | dialog.ok('Grooveshark XBMC', 'You have no Grooveshark playlists.') |
| 235 | self.categories() |
| 236 | else: |
| 237 | dialog = xbmcgui.Dialog() |
| 238 | dialog.ok('Grooveshark XBMC', 'You must be logged in ', ' to see your Grooveshark playlists.') |
| 239 | |
| 240 | # Make songs a favorite |
| 241 | def favorite(self, songid): |
| 242 | userid = self._get_login() |
| 243 | if (userid != 0): |
| 244 | xbmc.log("Favorite song: " + str(songid)) |
| 245 | groovesharkApi.favoriteSong(songID = songid) |
| 246 | xbmc.executebuiltin('XBMC.Notification(Grooveshark XBMC, Added to favorites, 1000, ' + thumbDef + ')') |
| 247 | else: |
| 248 | dialog = xbmcgui.Dialog() |
| 249 | dialog.ok('Grooveshark XBMC', 'You must be logged in', 'to add favorites.') |
| 250 | |
| 251 | # Remove song from favorites |
| 252 | def unfavorite(self, songid, prevMode=0): |
| 253 | userid = self._get_login() |
| 254 | if (userid != 0): |
| 255 | xbmc.log("Unfavorite song: " + str(songid) + ', previous mode was ' + str(prevMode)) |
| 256 | groovesharkApi.unfavoriteSong(songID = songid) |
| 257 | xbmc.executebuiltin('XBMC.Notification(Grooveshark XBMC, Removed from Grooveshark favorites, 1000, ' + thumbDef + ')') |
| 258 | # Refresh to remove item from directory |
| 259 | if (int(prevMode) == MODE_FAVORITES): |
| 260 | xbmc.executebuiltin("Container.Refresh(" + favoritesUrl + ")") |
| 261 | else: |
| 262 | dialog = xbmcgui.Dialog() |
| 263 | dialog.ok('Grooveshark XBMC', 'You must be logged in', 'to remove Grooveshark favorites.') |
| 264 | |
| 265 | # 'Frown' a song |
| 266 | def frown(self, songid): |
| 267 | userid = self._get_login() |
| 268 | if (userid != 0): |
| 269 | xbmc.log("Frown song: " + str(songid)) |
| 270 | if groovesharkApi.radioFrown(songId = songid) != True: |
| 271 | xbmc.log("Unable to frown song " + str(songid)) |
| 272 | else: |
| 273 | xbmc.executebuiltin('XBMC.Notification(Grooveshark XBMC, Frowned, 1000, ' + thumbDef + ')') |
| 274 | else: |
| 275 | dialog = xbmcgui.Dialog() |
| 276 | dialog.ok('Grooveshark XBMC', 'You must be logged in', 'to frown a Grooveshark song.') |
| 277 | |
| 278 | # Find similar artists to searched artist |
| 279 | def similarArtists(self, artistId): |
| 280 | similar = groovesharkApi.artistGetSimilar(artistId, limit = self.artistsearchlimit) |
| 281 | if (len(similar) > 0): |
| 282 | self._add_artists_directory(similar) |
| 283 | else: |
| 284 | dialog = xbmcgui.Dialog() |
| 285 | dialog.ok('Grooveshark XBMC', 'No similar artists.') |
| 286 | self.categories() |
| 287 | |
| 288 | # Make a playlist from an album |
| 289 | def makePlaylist(self, albumid, name): |
| 290 | userid = self._get_login() |
| 291 | if (userid != 0): |
| 292 | re.split(' - ',name,1) |
| 293 | nameTokens = re.split(' - ',name,1) # suggested name |
| 294 | name = self._get_keyboard(default=nameTokens[0], heading="Grooveshark playlist name") |
| 295 | if name != '': |
| 296 | groovesharkApi.setRemoveDuplicates(True) |
| 297 | album = groovesharkApi.albumGetSongs(albumid, self.songsearchlimit) |
| 298 | songids = [] |
| 299 | for song in album: |
| 300 | songids.append(song[1]) |
| 301 | if groovesharkApi.playlistCreateUnique(name, songids) == 0: |
| 302 | dialog = xbmcgui.Dialog() |
| 303 | dialog.ok('Grooveshark XBMC', 'Cannot create Grooveshark playlist ', name) |
| 304 | else: |
| 305 | xbmc.executebuiltin('XBMC.Notification(Grooveshark XBMC, Grooveshark playlist created, 1000, ' + thumbDef + ')') |
| 306 | else: |
| 307 | dialog = xbmcgui.Dialog() |
| 308 | dialog.ok('Grooveshark XBMC', 'You must be logged in ', ' to create a Grooveshark playlist.') |
| 309 | |
| 310 | # Rename a playlist |
| 311 | def renamePlaylist(self, playlistid, name): |
| 312 | userid = self._get_login() |
| 313 | if (userid != 0): |
| 314 | newname = self._get_keyboard(default=name, heading="Grooveshark playlist name") |
| 315 | if newname == '': |
| 316 | return |
| 317 | elif groovesharkApi.playlistRename(playlistid, newname) == 0: |
| 318 | dialog = xbmcgui.Dialog() |
| 319 | dialog.ok('Grooveshark XBMC', 'Cannot rename Grooveshark playlist ', name) |
| 320 | else: |
| 321 | # Refresh to show new item name |
| 322 | xbmc.executebuiltin("Container.Refresh") |
| 323 | else: |
| 324 | dialog = xbmcgui.Dialog() |
| 325 | dialog.ok('Grooveshark XBMC', 'You must be logged in ', ' to rename a Grooveshark playlist.') |
| 326 | |
| 327 | # Remove a playlist |
| 328 | def removePlaylist(self, playlistid, name): |
| 329 | dialog = xbmcgui.Dialog() |
| 330 | if dialog.yesno('Grooveshark XBMC', name, 'Delete this Grooveshark playlist?') == True: |
| 331 | userid = self._get_login() |
| 332 | if (userid != 0): |
| 333 | if groovesharkApi.playlistDelete(playlistid) == 0: |
| 334 | dialog = xbmcgui.Dialog() |
| 335 | dialog.ok('Grooveshark XBMC', 'Cannot remove Grooveshark playlist ', name) |
| 336 | else: |
| 337 | # Refresh to remove item from directory |
| 338 | xbmc.executebuiltin("Container.Refresh(" + playlistsUrl + ")") |
| 339 | else: |
| 340 | dialog = xbmcgui.Dialog() |
| 341 | dialog.ok('Grooveshark XBMC', 'You must be logged in ', ' to delete a Grooveshark playlist.') |
| 342 | |
| 343 | # Add song to playlist |
| 344 | def addPlaylistSong(self, songid): |
| 345 | userid = self._get_login() |
| 346 | if (userid != 0): |
| 347 | playlists = groovesharkApi.userGetPlaylists() |
| 348 | if (len(playlists) > 0): |
| 349 | ret = 0 |
| 350 | # Select the playlist |
| 351 | playlistSelect = GroovesharkPlaylistSelect(items=playlists) |
| 352 | playlistSelect.setFocus(playlistSelect.playlistControl) |
| 353 | playlistSelect.doModal() |
| 354 | i = playlistSelect.selected |
| 355 | del playlistSelect |
| 356 | if i > -1: |
| 357 | # Add a new playlist |
| 358 | if i >= len(playlists): |
| 359 | name = self._get_keyboard(default='', heading="Grooveshark playlist name") |
| 360 | if name != '': |
| 361 | songIds = [] |
| 362 | songIds.append(songid) |
| 363 | if groovesharkApi.playlistCreateUnique(name, songIds) == 0: |
| 364 | dialog = xbmcgui.Dialog() |
| 365 | dialog.ok('Grooveshark XBMC', 'Cannot create Grooveshark playlist ', name) |
| 366 | else: |
| 367 | xbmc.executebuiltin('XBMC.Notification(Grooveshark XBMC, Grooveshark playlist created, 1000, ' + thumbDef + ')') |
| 368 | # Existing playlist |
| 369 | else: |
| 370 | playlist = playlists[i] |
| 371 | playlistid = playlist[1] |
| 372 | xbmc.log("Add song " + str(songid) + " to playlist " + str(playlistid)) |
| 373 | ret = groovesharkApi.playlistAddSong(playlistid, songid, 0) |
| 374 | if ret == 0: |
| 375 | dialog = xbmcgui.Dialog() |
| 376 | dialog.ok('Grooveshark XBMC', 'Cannot add to playlist ') |
| 377 | else: |
| 378 | xbmc.executebuiltin('XBMC.Notification(Grooveshark XBMC, Added song to Grooveshark playlist, 1000, ' + thumbDef + ')') |
| 379 | else: |
| 380 | dialog = xbmcgui.Dialog() |
| 381 | dialog.ok('Grooveshark XBMC', 'You have no Grooveshark playlists.') |
| 382 | self.categories() |
| 383 | else: |
| 384 | dialog = xbmcgui.Dialog() |
| 385 | dialog.ok('Grooveshark XBMC', 'You must be logged in ', ' to add a song to a Grooveshark playlist.') |
| 386 | |
| 387 | # Remove song from playlist |
| 388 | def removePlaylistSong(self, playlistid, playlistname, songpos): |
| 389 | dialog = xbmcgui.Dialog() |
| 390 | if dialog.yesno('Grooveshark XBMC', 'Delete this song from the Grooveshark playlist?') == True: |
| 391 | userid = self._get_login() |
| 392 | if (userid != 0): |
| 393 | if groovesharkApi.playlistDeleteSong(playlistid, songpos) == 0: |
| 394 | dialog = xbmcgui.Dialog() |
| 395 | dialog.ok('Grooveshark XBMC', 'Failed to remove ', ' song from Grooveshark playlist.') |
| 396 | else: |
| 397 | # Refresh to remove item from directory |
| 398 | xbmc.executebuiltin("Container.Refresh(" + playlistUrl + "&id="+str(playlistid) + "&name=" + playlistname + ")") |
| 399 | else: |
| 400 | dialog = xbmcgui.Dialog() |
| 401 | dialog.ok('Grooveshark XBMC', 'You must be logged in ', ' to delete a song from a Grooveshark playlist.') |
| 402 | |
| 403 | # Show selected album |
| 404 | def album(self, albumid): |
| 405 | groovesharkApi.setRemoveDuplicates(True) |
| 406 | album = groovesharkApi.albumGetSongs(albumId = albumid, limit = self.songsearchlimit) |
| 407 | self._add_songs_directory(album) |
| 408 | |
| 409 | # Show selected artist |
| 410 | def artist(self, artistid): |
| 411 | albums = groovesharkApi.artistGetAlbums(artistId = artistid, limit = self.albumsearchlimit) |
| 412 | self._add_albums_directory(albums, artistid) |
| 413 | |
| 414 | # Show selected playlist |
| 415 | def playlist(self, playlistid, playlistname): |
| 416 | userid = self._get_login() |
| 417 | if (userid != 0): |
| 418 | songs = groovesharkApi.playlistGetSongs(playlistId = playlistid, limit = self.songsearchlimit) |
| 419 | self._add_songs_directory(songs, playlistid, playlistname) |
| 420 | else: |
| 421 | dialog = xbmcgui.Dialog() |
| 422 | dialog.ok('Grooveshark XBMC', 'You must be logged in', 'to get Grooveshark playlists.') |
| 423 | |
| 424 | # Play a song |
| 425 | def playSong(self, item): |
| 426 | url = groovesharkApi.getStreamURL(item.getProperty('id')) |
| 427 | item.setPath(url) |
| 428 | xbmc.log("Playing: " + url) |
| 429 | xbmcplugin.setResolvedUrl(handle=int(sys.argv[1]), succeeded=True, listitem=item) |
| 430 | |
| 431 | # Make a song directory item |
| 432 | def songItem(self, id, name, album, artist, duration, thumb, image): |
| 433 | # Only try to get the image |
| 434 | if image != "": |
| 435 | songImg = self._get_icon(image, 'song-' + str(id) + "-image") |
| 436 | songThm = songImg |
| 437 | else: |
| 438 | songThm = self._get_icon(thumb, 'song-' + str(id) + "-thumb") |
| 439 | songImg = songThm |
| 440 | item = xbmcgui.ListItem(label = artist + " - " + album + " - " + name, thumbnailImage=songThm, iconImage=songImg) |
| 441 | item.setInfo( type="music", infoLabels={ "title": name, "duration": duration, "album": album, "artist": artist} ) |
| 442 | item.setProperty('mimetype', 'audio/mpeg') |
| 443 | item.setProperty("IsPlayable", "true") |
| 444 | item.setProperty('id', str(id)) |
| 445 | item.setProperty('thumb', songThm) |
| 446 | item.setProperty('image', songImg) |
| 447 | return item |
| 448 | |
| 449 | # Get keyboard input |
| 450 | def _get_keyboard(self, default="", heading="", hidden=False): |
| 451 | kb = xbmc.Keyboard(default, heading, hidden) |
| 452 | kb.doModal() |
| 453 | if (kb.isConfirmed()): |
| 454 | return unicode(kb.getText(), "utf-8") |
| 455 | return '' |
| 456 | |
| 457 | # Login to grooveshark |
| 458 | def _get_login(self): |
| 459 | if (self.username == "" or self.password == ""): |
| 460 | dialog = xbmcgui.Dialog() |
| 461 | dialog.ok('Grooveshark XBMC', 'Unable to login.', 'Check username and password in settings.') |
| 462 | return 0 |
| 463 | else: |
| 464 | if self.userid == 0: |
| 465 | uid = groovesharkApi.loginExt(self.username, self.password) |
| 466 | if (uid != 0): |
| 467 | return uid |
| 468 | else: |
| 469 | dialog = xbmcgui.Dialog() |
| 470 | dialog.ok('Grooveshark XBMC', 'Unable to login.', 'Check username and password in settings.') |
| 471 | return 0 |
| 472 | |
| 473 | # Get a song directory item |
| 474 | def _get_song_item(self, song): |
| 475 | name = song[0] |
| 476 | id = song[1] |
| 477 | duration = song[2] |
| 478 | album = song[3] |
| 479 | artist = song[6] |
| 480 | thumb = song[8] |
| 481 | image = song[9] |
| 482 | return self.songItem(id, name, album, artist, duration, thumb, image) |
| 483 | |
| 484 | # File download |
| 485 | def _get_icon(self, url, id): |
| 486 | localThumb = os.path.join(xbmc.translatePath(os.path.join(thumbDir, str(id)))) + '.tbn' |
| 487 | try: |
| 488 | if os.path.isfile(localThumb) == False: |
| 489 | xbmc.log('Downloading ' + url + ' to ' + localThumb) |
| 490 | loc = urllib.URLopener() |
| 491 | loc.retrieve(url, localThumb) |
| 492 | except: |
| 493 | shutil.copy2(self.defImg, localThumb) |
| 494 | |
| 495 | return os.path.join(os.path.join(thumbDir, str(id))) + '.tbn' |
| 496 | |
| 497 | # Add songs to directory |
| 498 | def _add_songs_directory(self, songs, playlistid=0, playlistname='', isFavorites=False): |
| 499 | n = len(songs) |
| 500 | xbmc.log("Found " + str(n) + " songs...") |
| 501 | i = 0 |
| 502 | while i < n: |
| 503 | song = songs[i] |
| 504 | item = self._get_song_item(song) |
| 505 | songthumb = item.getProperty('thumb') |
| 506 | songimage = item.getProperty('image') |
| 507 | songname = song[0] |
| 508 | songid = song[1] |
| 509 | songduration = song[2] |
| 510 | songalbum = song[3] |
| 511 | songartist = song[6] |
| 512 | u=sys.argv[0]+"?mode="+str(MODE_SONG)+"&name="+urllib.quote_plus(songname)+"&id="+str(songid) \ |
| 513 | +"&album="+urllib.quote_plus(songalbum) \ |
| 514 | +"&artist="+urllib.quote_plus(songartist) \ |
| 515 | +"&duration="+str(songduration) \ |
| 516 | +"&thumb="+urllib.quote_plus(songthumb) \ |
| 517 | +"&image="+urllib.quote_plus(songimage) |
| 518 | fav=sys.argv[0]+"?mode="+str(MODE_FAVORITE)+"&name="+urllib.quote_plus(songname)+"&id="+str(songid) |
| 519 | unfav=sys.argv[0]+"?mode="+str(MODE_UNFAVORITE)+"&name="+urllib.quote_plus(songname)+"&id="+str(songid)+"&prevmode=" |
| 520 | menuItems = [] |
| 521 | if isFavorites == True: |
| 522 | unfav = unfav +str(MODE_FAVORITES) |
| 523 | else: |
| 524 | menuItems.append(("Grooveshark favorite", "XBMC.RunPlugin("+fav+")")) |
| 525 | menuItems.append(("Not Grooveshark favorite", "XBMC.RunPlugin("+unfav+")")) |
| 526 | if playlistid > 0: |
| 527 | rmplaylstsong=sys.argv[0]+"?playlistid="+str(playlistid)+"&id="+str(i+1)+"&mode="+str(MODE_REMOVE_PLAYLIST_SONG)+"&name="+playlistname |
| 528 | menuItems.append(("Remove from Grooveshark playlist", "XBMC.RunPlugin("+rmplaylstsong+")")) |
| 529 | else: |
| 530 | addplaylstsong=sys.argv[0]+"?id="+str(songid)+"&mode="+str(MODE_ADD_PLAYLIST_SONG) |
| 531 | menuItems.append(("Add to Grooveshark playlist", "XBMC.RunPlugin("+addplaylstsong+")")) |
| 532 | item.addContextMenuItems(menuItems, replaceItems=False) |
| 533 | xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]),url=u,listitem=item,isFolder=False, totalItems=n) |
| 534 | i = i + 1 |
| 535 | |
| 536 | xbmcplugin.setContent(self._handle, 'songs') |
| 537 | xbmcplugin.setPluginFanart(int(sys.argv[1]), self.fanImg) |
| 538 | |
| 539 | # Add albums to directory |
| 540 | def _add_albums_directory(self, albums, artistid=0): |
| 541 | n = len(albums) |
| 542 | xbmc.log("Found " + str(n) + " albums...") |
| 543 | i = 0 |
| 544 | while i < n: |
| 545 | album = albums[i] |
| 546 | albumArtistName = album[0] |
| 547 | albumName = album[2] |
| 548 | albumID = album[3] |
| 549 | albumImage = self._get_icon(album[4], 'album-' + str(albumID)) |
| 550 | self._add_dir(albumName + " - " + albumArtistName, '', MODE_ALBUM, albumImage, albumID, n) |
| 551 | i = i + 1 |
| 552 | if artistid > 0: |
| 553 | self._add_dir('Similar artists...', '', MODE_SIMILAR_ARTISTS, self.artistImg, artistid) |
| 554 | xbmcplugin.setContent(self._handle, 'albums') |
| 555 | xbmcplugin.addSortMethod(self._handle, xbmcplugin.SORT_METHOD_ALBUM_IGNORE_THE) |
| 556 | xbmcplugin.setPluginFanart(int(sys.argv[1]), self.fanImg) |
| 557 | |
| 558 | # Add artists to directory |
| 559 | def _add_artists_directory(self, artists): |
| 560 | n = len(artists) |
| 561 | xbmc.log("Found " + str(n) + " artists...") |
| 562 | i = 0 |
| 563 | while i < n: |
| 564 | artist = artists[i] |
| 565 | artistName = artist[0] |
| 566 | artistID = artist[1] |
| 567 | self._add_dir(artistName, '', MODE_ARTIST, self.artistImg, artistID, n) |
| 568 | i = i + 1 |
| 569 | xbmcplugin.setContent(self._handle, 'artists') |
| 570 | xbmcplugin.addSortMethod(self._handle, xbmcplugin.SORT_METHOD_ARTIST_IGNORE_THE) |
| 571 | xbmcplugin.setPluginFanart(int(sys.argv[1]), self.fanImg) |
| 572 | |
| 573 | # Add playlists to directory |
| 574 | def _add_playlists_directory(self, playlists): |
| 575 | n = len(playlists) |
| 576 | xbmc.log("Found " + str(n) + " playlists...") |
| 577 | i = 0 |
| 578 | while i < n: |
| 579 | playlist = playlists[i] |
| 580 | playlistName = playlist[0] |
| 581 | playlistID = playlist[1] |
| 582 | self._add_dir(playlistName, '', MODE_PLAYLIST, self.playlistImg, playlistID, n) |
| 583 | i = i + 1 |
| 584 | xbmcplugin.setContent(self._handle, 'files') |
| 585 | xbmcplugin.addSortMethod(self._handle, xbmcplugin.SORT_METHOD_PLAYLIST_ORDER) |
| 586 | xbmcplugin.setPluginFanart(int(sys.argv[1]), self.fanImg) |
| 587 | |
| 588 | # Add whatever directory |
| 589 | def _add_dir(self, name, url, mode, iconimage, id, items=1): |
| 590 | |
| 591 | u=sys.argv[0]+"?mode="+str(mode)+"&name="+urllib.quote_plus(name)+"&id="+str(id) |
| 592 | dir=xbmcgui.ListItem(name, iconImage=iconimage, thumbnailImage=iconimage) |
| 593 | dir.setInfo( type="Music", infoLabels={ "title": name } ) |
| 594 | |
| 595 | # Custom menu items |
| 596 | menuItems = [] |
| 597 | if mode == MODE_ALBUM: |
| 598 | mkplaylst=sys.argv[0]+"?mode="+str(MODE_MAKE_PLAYLIST)+"&name="+name+"&id="+str(id) |
| 599 | menuItems.append(("Make Grooveshark playlist", "XBMC.RunPlugin("+mkplaylst+")")) |
| 600 | if mode == MODE_PLAYLIST: |
| 601 | rmplaylst=sys.argv[0]+"?mode="+str(MODE_REMOVE_PLAYLIST)+"&name="+urllib.quote_plus(name)+"&id="+str(id) |
| 602 | menuItems.append(("Delete Grooveshark playlist", "XBMC.RunPlugin("+rmplaylst+")")) |
| 603 | mvplaylst=sys.argv[0]+"?mode="+str(MODE_RENAME_PLAYLIST)+"&name="+urllib.quote_plus(name)+"&id="+str(id) |
| 604 | menuItems.append(("Rename Grooveshark playlist", "XBMC.RunPlugin("+mvplaylst+")")) |
| 605 | |
| 606 | dir.addContextMenuItems(menuItems, replaceItems=False) |
| 607 | return xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]),url=u,listitem=dir,isFolder=True, totalItems=items) |
| 608 | |
| 609 | |
| 610 | # Parse URL parameters |
| 611 | def get_params(): |
| 612 | param=[] |
| 613 | paramstring=sys.argv[2] |
| 614 | xbmc.log(paramstring) |
| 615 | if len(paramstring)>=2: |
| 616 | params=sys.argv[2] |
| 617 | cleanedparams=params.replace('?','') |
| 618 | if (params[len(params)-1]=='/'): |
| 619 | params=params[0:len(params)-2] |
| 620 | pairsofparams=cleanedparams.split('&') |
| 621 | param={} |
| 622 | for i in range(len(pairsofparams)): |
| 623 | splitparams={} |
| 624 | splitparams=pairsofparams[i].split('=') |
| 625 | if (len(splitparams))==2: |
| 626 | param[splitparams[0]]=splitparams[1] |
| 627 | return param |
| 628 | |
| 629 | # Main |
| 630 | grooveshark = Groveshark(); |
| 631 | |
| 632 | params=get_params() |
| 633 | mode=None |
| 634 | try: mode=int(params["mode"]) |
| 635 | except: pass |
| 636 | |
| 637 | if mode > MODE_FAVORITES: |
| 638 | name=None |
| 639 | id=None |
| 640 | try: name=urllib.unquote_plus(params["name"]) |
| 641 | except: pass |
| 642 | try: id=int(params["id"]) |
| 643 | except: pass |
| 644 | |
| 645 | # Call function for URL |
| 646 | if mode==None: |
| 647 | grooveshark.categories() |
| 648 | |
| 649 | elif mode==MODE_SEARCH_SONGS: |
| 650 | grooveshark.searchSongs() |
| 651 | |
| 652 | elif mode==MODE_SEARCH_ALBUMS: |
| 653 | grooveshark.searchAlbums() |
| 654 | |
| 655 | elif mode==MODE_SEARCH_ARTISTS: |
| 656 | grooveshark.searchArtists() |
| 657 | |
| 658 | elif mode==MODE_POPULAR_SONGS: |
| 659 | grooveshark.popularSongs() |
| 660 | |
| 661 | elif mode==MODE_FAVORITES: |
| 662 | grooveshark.favorites() |
| 663 | |
| 664 | elif mode==MODE_PLAYLISTS: |
| 665 | grooveshark.playlists() |
| 666 | |
| 667 | elif mode==MODE_SONG: |
| 668 | try: album=urllib.unquote_plus(params["album"]) |
| 669 | except: pass |
| 670 | try: artist=urllib.unquote_plus(params["artist"]) |
| 671 | except: pass |
| 672 | try: duration=int(params["duration"]) |
| 673 | except: pass |
| 674 | try: thumb=urllib.unquote_plus(params["thumb"]) |
| 675 | except: pass |
| 676 | try: image=urllib.unquote_plus(params["image"]) |
| 677 | except: pass |
| 678 | song = grooveshark.songItem(id, name, album, artist, duration, thumb, image) |
| 679 | grooveshark.playSong(song) |
| 680 | |
| 681 | elif mode==MODE_ARTIST: |
| 682 | grooveshark.artist(id) |
| 683 | |
| 684 | elif mode==MODE_ALBUM: |
| 685 | grooveshark.album(id) |
| 686 | |
| 687 | elif mode==MODE_PLAYLIST: |
| 688 | grooveshark.playlist(id, name) |
| 689 | |
| 690 | elif mode==MODE_FAVORITE: |
| 691 | grooveshark.favorite(id) |
| 692 | |
| 693 | elif mode==MODE_UNFAVORITE: |
| 694 | try: prevMode=int(urllib.unquote_plus(params["prevmode"])) |
| 695 | except: |
| 696 | prevMode = 0 |
| 697 | grooveshark.unfavorite(id, prevMode) |
| 698 | |
| 699 | elif mode==MODE_SIMILAR_ARTISTS: |
| 700 | grooveshark.similarArtists(id) |
| 701 | |
| 702 | elif mode==MODE_MAKE_PLAYLIST: |
| 703 | grooveshark.makePlaylist(id, name) |
| 704 | |
| 705 | elif mode==MODE_REMOVE_PLAYLIST: |
| 706 | grooveshark.removePlaylist(id, name) |
| 707 | |
| 708 | elif mode==MODE_RENAME_PLAYLIST: |
| 709 | grooveshark.renamePlaylist(id, name) |
| 710 | |
| 711 | elif mode==MODE_REMOVE_PLAYLIST_SONG: |
| 712 | try: playlistID=urllib.unquote_plus(params["playlistid"]) |
| 713 | except: pass |
| 714 | grooveshark.removePlaylistSong(playlistID, name, id) |
| 715 | |
| 716 | elif mode==MODE_ADD_PLAYLIST_SONG: |
| 717 | grooveshark.addPlaylistSong(id) |
| 718 | |
| 719 | if mode < MODE_SONG: |
| 720 | xbmcplugin.endOfDirectory(int(sys.argv[1])) |