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