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