Slight performance improvement. Playlists bug fix.
[clinton/xbmc-groove.git] / default.py
CommitLineData
aa0c1263 1import urllib, urllib2, re, xbmcplugin, xbmcgui, xbmc, sys, os, time, shutil, xbmcaddon
8817bb2e 2
8817bb2e 3MODE_SEARCH_SONGS = 1
4MODE_SEARCH_ALBUMS = 2
5MODE_SEARCH_ARTISTS = 3
36cc00d7 6MODE_POPULAR_SONGS = 4
8817bb2e 7MODE_FAVORITES = 5
8MODE_PLAYLISTS = 6
9MODE_ALBUM = 7
10MODE_ARTIST = 8
11MODE_PLAYLIST = 9
406ab447 12MODE_SIMILAR_ARTISTS = 10
13MODE_SONG = 11
14MODE_FAVORITE = 12
15MODE_UNFAVORITE = 13
36cc00d7 16MODE_MAKE_PLAYLIST = 14
17MODE_REMOVE_PLAYLIST = 15
18MODE_RENAME_PLAYLIST = 16
4be42357 19MODE_REMOVE_PLAYLIST_SONG = 17
7ea6f166 20MODE_ADD_PLAYLIST_SONG = 18
21
38df1fa5 22ACTION_MOVE_LEFT = 1
7ea6f166 23ACTION_MOVE_UP = 3
24ACTION_MOVE_DOWN = 4
25ACTION_PAGE_UP = 5
26ACTION_PAGE_DOWN = 6
27ACTION_SELECT_ITEM = 7
28ACTION_PREVIOUS_MENU = 10
8817bb2e 29
6ae708d0 30baseDir = os.getcwd()
31resDir = xbmc.translatePath(os.path.join(baseDir, 'resources'))
8817bb2e 32libDir = xbmc.translatePath(os.path.join(resDir, 'lib'))
33imgDir = xbmc.translatePath(os.path.join(resDir, 'img'))
973b4c6c 34thumbDir = os.path.join('special://masterprofile/addon_data/', os.path.join(os.path.basename(os.getcwd()), 'thumb'))
4be42357 35
36baseModeUrl = 'plugin://plugin.audio.groove/'
37playlistUrl = baseModeUrl + '?url=&mode=' + str(MODE_PLAYLIST)
38playlistsUrl = baseModeUrl + '?mode=' + str(MODE_PLAYLISTS)
39favoritesUrl = baseModeUrl + '?mode=' + str(MODE_FAVORITES)
40
3cfead3c 41thumbDef = os.path.join(os.path.basename(os.getcwd()), 'default.tbn')
7ea6f166 42listBackground = os.path.join(imgDir, 'listbackground.png')
8817bb2e 43
44sys.path.append (libDir)
45from GrooveAPI import *
46groovesharkApi = GrooveAPI()
47
48class _Info:
49 def __init__( self, *args, **kwargs ):
50 self.__dict__.update( kwargs )
3fcef5ba 51
7ea6f166 52class GroovesharkPlaylistSelect(xbmcgui.WindowDialog):
53
38df1fa5 54 def __init__(self, items=[]):
38df1fa5 55 gap = int(self.getHeight()/100)
aa0c1263 56 w = int(self.getWidth()*0.5)
38df1fa5 57 h = self.getHeight()-30*gap
7ea6f166 58 rw = self.getWidth()
59 rh = self.getHeight()
60 x = rw/2 - w/2
61 y = rh/2 -h/2
62
38df1fa5 63 self.imgBg = xbmcgui.ControlImage(x+gap, 5*gap+y, w-2*gap, h-5*gap, listBackground)
7ea6f166 64 self.addControl(self.imgBg)
aa0c1263 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)
38df1fa5 67 self.addControl(self.playlistControl)
68
7ea6f166 69 self.lastPos = 0
70 self.isSelecting = False
38df1fa5 71 self.selected = -1
7ea6f166 72 listitems = []
7ea6f166 73 for playlist in items:
7ea6f166 74 listitems.append(xbmcgui.ListItem(playlist[0]))
38df1fa5 75 listitems.append(xbmcgui.ListItem('New...'))
76 self.playlistControl.addItems(listitems)
77 self.setFocus(self.playlistControl)
aa0c1263 78 self.playlistControl.selectItem(0)
38df1fa5 79 item = self.playlistControl.getListItem(self.lastPos)
7ea6f166 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
38df1fa5 89 pos = self.playlistControl.getSelectedPosition()
7ea6f166 90 if pos >= 0:
38df1fa5 91 item = self.playlistControl.getListItem(self.lastPos)
7ea6f166 92 item.select(False)
38df1fa5 93 item = self.playlistControl.getListItem(pos)
7ea6f166 94 item.select(True)
95 self.lastPos = pos
96 self.isSelecting = False
97
aa0c1263 98 # Control
99 def onControl(self, control):
100 if control == self.playlistControl:
101 self.selected = self.playlistControl.getSelectedPosition()
102 self.close()
103
7ea6f166 104 def onAction(self, action):
aa0c1263 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()
7ea6f166 111
8817bb2e 112class Groveshark:
973b4c6c 113
7ea6f166 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'))
2254a6b5 121 fanImg = xbmc.translatePath(os.path.join(baseDir, 'fanart.png'))
8817bb2e 122
2254a6b5 123 settings = xbmcaddon.Addon(id='plugin.audio.groove')
973b4c6c 124 songsearchlimit = settings.getSetting('songsearchlimit')
125 albumsearchlimit = settings.getSetting('albumsearchlimit')
126 artistsearchlimit = settings.getSetting('artistsearchlimit')
2254a6b5 127 username = settings.getSetting('username')
128 password = settings.getSetting('password')
38df1fa5 129 userid = 0
4be42357 130
8817bb2e 131 def __init__( self ):
132 self._handle = int(sys.argv[1])
b738088f 133
8817bb2e 134 def categories(self):
2254a6b5 135
136 xbmc.log(self.username + ", limits: " + str(self.songsearchlimit) + ", " + str(self.albumsearchlimit) + ", " + str(self.artistsearchlimit))
b738088f 137
38df1fa5 138 self.userid = self._get_login()
b738088f 139
140 # Setup
6ae708d0 141 xbmcplugin.setPluginFanart(int(sys.argv[1]), self.fanImg)
142 if os.path.isdir(thumbDir) == False:
143 os.makedirs(thumbDir)
144
3fcef5ba 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)
36cc00d7 148 self._add_dir('Popular songs', '', MODE_POPULAR_SONGS, self.popularSongsImg, 0)
38df1fa5 149 if (self.userid != 0):
3fcef5ba 150 self._add_dir('Favorites', '', MODE_FAVORITES, self.favoritesImg, 0)
151 self._add_dir('Playlists', '', MODE_PLAYLISTS, self.playlistImg, 0)
152
8817bb2e 153 def searchSongs(self):
154 query = self._get_keyboard(default="", heading="Search songs")
7ea6f166 155 if (query != ''):
4be42357 156 groovesharkApi.setRemoveDuplicates(True)
2254a6b5 157 songs = groovesharkApi.searchSongs(query, limit = self.songsearchlimit)
8817bb2e 158 if (len(songs) > 0):
6ae708d0 159 self._add_songs_directory(songs)
8817bb2e 160 else:
161 dialog = xbmcgui.Dialog()
38df1fa5 162 dialog.ok('Grooveshark XBMC', 'No matching songs.')
8817bb2e 163 self.categories()
7ea6f166 164 else:
165 self.categories()
8817bb2e 166
167 def searchAlbums(self):
168 query = self._get_keyboard(default="", heading="Search albums")
7ea6f166 169 if (query != ''):
2254a6b5 170 albums = groovesharkApi.searchAlbums(query, limit = self.albumsearchlimit)
8817bb2e 171 if (len(albums) > 0):
6ae708d0 172 self._add_albums_directory(albums)
8817bb2e 173 else:
174 dialog = xbmcgui.Dialog()
38df1fa5 175 dialog.ok('Grooveshark XBMC', 'No matching albums.')
8817bb2e 176 self.categories()
7ea6f166 177 else:
178 self.categories()
8817bb2e 179
180 def searchArtists(self):
181 query = self._get_keyboard(default="", heading="Search artists")
7ea6f166 182 if (query != ''):
2254a6b5 183 artists = groovesharkApi.searchArtists(query, limit = self.artistsearchlimit)
8817bb2e 184 if (len(artists) > 0):
6ae708d0 185 self._add_artists_directory(artists)
8817bb2e 186 else:
187 dialog = xbmcgui.Dialog()
38df1fa5 188 dialog.ok('Grooveshark XBMC', 'No matching artists.')
8817bb2e 189 self.categories()
7ea6f166 190 else:
191 self.categories()
8817bb2e 192
193 def favorites(self):
36cc00d7 194 userid = self._get_login()
195 if (userid != 0):
196 favorites = groovesharkApi.userGetFavoriteSongs(userid)
197 if (len(favorites) > 0):
4be42357 198 self._add_songs_directory(favorites, isFavorites=True)
36cc00d7 199 else:
200 dialog = xbmcgui.Dialog()
38df1fa5 201 dialog.ok('Grooveshark XBMC', 'You have no favorites.')
36cc00d7 202 self.categories()
8817bb2e 203
36cc00d7 204 def popularSongs(self):
2254a6b5 205 popular = groovesharkApi.popularGetSongs(limit = self.songsearchlimit)
8817bb2e 206 if (len(popular) > 0):
6ae708d0 207 self._add_songs_directory(popular)
8817bb2e 208 else:
209 dialog = xbmcgui.Dialog()
38df1fa5 210 dialog.ok('Grooveshark XBMC', 'No popular songs.')
8817bb2e 211 self.categories()
36cc00d7 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()
38df1fa5 219 dialog.ok('Grooveshark XBMC', 'No popular albums.')
36cc00d7 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()
38df1fa5 228 dialog.ok('Grooveshark XBMC', 'No popular artists.')
36cc00d7 229 self.categories()
8817bb2e 230
231 def playlists(self):
232 userid = self._get_login()
233 if (userid != 0):
234 playlists = groovesharkApi.userGetPlaylists()
235 if (len(playlists) > 0):
6ae708d0 236 self._add_playlists_directory(playlists)
8817bb2e 237 else:
238 dialog = xbmcgui.Dialog()
38df1fa5 239 dialog.ok('Grooveshark XBMC', 'You have no Grooveshark playlists.')
8817bb2e 240 self.categories()
7ea6f166 241 else:
242 dialog = xbmcgui.Dialog()
38df1fa5 243 dialog.ok('Grooveshark XBMC', 'You must be logged in ', ' to see your Grooveshark playlists.')
7ea6f166 244
8817bb2e 245
246 def favorite(self, songid):
247 userid = self._get_login()
248 if (userid != 0):
406ab447 249 xbmc.log("Favorite song: " + str(songid))
8817bb2e 250 groovesharkApi.favoriteSong(songID = songid)
38df1fa5 251 xbmc.executebuiltin('XBMC.Notification(Grooveshark XBMC, Added to favorites, 1000, ' + thumbDef + ')')
8817bb2e 252 else:
253 dialog = xbmcgui.Dialog()
38df1fa5 254 dialog.ok('Grooveshark XBMC', 'You must be logged in', 'to add favorites.')
8817bb2e 255
4be42357 256 def unfavorite(self, songid, prevMode):
8817bb2e 257 userid = self._get_login()
258 if (userid != 0):
4be42357 259 xbmc.log("Unfavorite song: " + str(songid) + ', previous mode was ' + str(prevMode))
8817bb2e 260 groovesharkApi.unfavoriteSong(songID = songid)
aa0c1263 261 xbmc.executebuiltin('XBMC.Notification(Grooveshark XBMC, Removed from Grooveshark favorites, 1000, ' + thumbDef + ')')
4be42357 262 if (int(prevMode) == MODE_FAVORITES):
7ea6f166 263 xbmc.executebuiltin("Container.Update(" + favoritesUrl + ")")
8817bb2e 264 else:
265 dialog = xbmcgui.Dialog()
aa0c1263 266 dialog.ok('Grooveshark XBMC', 'You must be logged in', 'to remove Grooveshark favorites.')
3fcef5ba 267
268 def frown(self, songid):
269 userid = self._get_login()
270 if (userid != 0):
406ab447 271 xbmc.log("Frown song: " + str(songid))
3fcef5ba 272 if groovesharkApi.radioFrown(songId = songid) != True:
273 xbmc.log("Unable to frown song " + str(songid))
3cfead3c 274 else:
38df1fa5 275 xbmc.executebuiltin('XBMC.Notification(Grooveshark XBMC, Frowned, 1000, ' + thumbDef + ')')
3fcef5ba 276 else:
277 dialog = xbmcgui.Dialog()
aa0c1263 278 dialog.ok('Grooveshark XBMC', 'You must be logged in', 'to frown a Grooveshark song.')
3fcef5ba 279
406ab447 280 def similarArtists(self, artistId):
281 similar = groovesharkApi.artistGetSimilar(artistId, limit = self.artistsearchlimit)
282 if (len(similar) > 0):
283 self._add_artists_directory(similar)
3fcef5ba 284 else:
285 dialog = xbmcgui.Dialog()
38df1fa5 286 dialog.ok('Grooveshark XBMC', 'No similar artists.')
406ab447 287 self.categories()
36cc00d7 288
289 def makePlaylist(self, albumid, name):
290 userid = self._get_login()
291 if (userid != 0):
3cfead3c 292 re.split(' - ',name,1)
293 nameTokens = re.split(' - ',name,1)
38df1fa5 294 name = self._get_keyboard(default=nameTokens[0], heading="Grooveshark playlist name")
3cfead3c 295 if name != '':
4be42357 296 groovesharkApi.setRemoveDuplicates(True)
3cfead3c 297 album = groovesharkApi.albumGetSongs(albumid, self.songsearchlimit)
298 songids = []
299 for song in album:
300 songids.append(song[1])
38df1fa5 301 if groovesharkApi.playlistCreateUnique(name, songids) == 0:
3cfead3c 302 dialog = xbmcgui.Dialog()
38df1fa5 303 dialog.ok('Grooveshark XBMC', 'Cannot create Grooveshark playlist ', name)
3cfead3c 304 else:
38df1fa5 305 xbmc.executebuiltin('XBMC.Notification(Grooveshark XBMC, Grooveshark playlist created, 1000, ' + thumbDef + ')')
36cc00d7 306 else:
307 dialog = xbmcgui.Dialog()
38df1fa5 308 dialog.ok('Grooveshark XBMC', 'You must be logged in ', ' to create a Grooveshark playlist.')
36cc00d7 309
310 def removePlaylist(self, playlistid, name):
311 dialog = xbmcgui.Dialog()
38df1fa5 312 if dialog.yesno('Grooveshark XBMC', name, 'Delete this Grooveshark playlist?') == True:
36cc00d7 313 userid = self._get_login()
314 if (userid != 0):
315 groovesharkApi.playlistDelete(playlistid)
7ea6f166 316 xbmc.executebuiltin("Container.Refresh(" + playlistsUrl + ")")
36cc00d7 317 else:
318 dialog = xbmcgui.Dialog()
38df1fa5 319 dialog.ok('Grooveshark XBMC', 'You must be logged in ', ' to delete a Grooveshark playlist.')
36cc00d7 320
4be42357 321 def removePlaylistSong(self, playlistid, playlistname, songpos):
322 dialog = xbmcgui.Dialog()
38df1fa5 323 if dialog.yesno('Grooveshark XBMC', 'Delete this song from the Grooveshark playlist?') == True:
4be42357 324 userid = self._get_login()
325 if (userid != 0):
326 if groovesharkApi.playlistDeleteSong(playlistid, songpos) == 0:
327 dialog = xbmcgui.Dialog()
38df1fa5 328 dialog.ok('Grooveshark XBMC', 'Failed to remove ', ' song from Grooveshark playlist.')
4be42357 329 else:
7ea6f166 330 xbmc.executebuiltin("Container.Refresh(" + playlistUrl + "&id="+str(playlistid) + "&name=" + playlistname + ",replace)")
331 else:
332 dialog = xbmcgui.Dialog()
38df1fa5 333 dialog.ok('Grooveshark XBMC', 'You must be logged in ', ' to delete a song from a Grooveshark playlist.')
7ea6f166 334
335 def addPlaylistSong(self, songid):
336 userid = self._get_login()
337 if (userid != 0):
338 playlists = groovesharkApi.userGetPlaylists()
339 if (len(playlists) > 0):
38df1fa5 340 ret = 0
341 playlistSelect = GroovesharkPlaylistSelect(items=playlists)
342 playlistSelect.setFocus(playlistSelect.playlistControl)
7ea6f166 343 playlistSelect.doModal()
38df1fa5 344 i = playlistSelect.selected
7ea6f166 345 del playlistSelect
38df1fa5 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 + ')')
4be42357 369 else:
370 dialog = xbmcgui.Dialog()
38df1fa5 371 dialog.ok('Grooveshark XBMC', 'You have no Grooveshark playlists.')
7ea6f166 372 self.categories()
373 else:
374 dialog = xbmcgui.Dialog()
38df1fa5 375 dialog.ok('Grooveshark XBMC', 'You must be logged in ', ' to add a song to a Grooveshark playlist.')
4be42357 376
36cc00d7 377 def renamePlaylist(self, playlistid, name):
378 userid = self._get_login()
379 if (userid != 0):
aa0c1263 380 newname = self._get_keyboard(default=name, heading="Grooveshark playlist name")
3cfead3c 381 if newname == '':
382 return
383 elif groovesharkApi.playlistRename(playlistid, newname) == 0:
36cc00d7 384 dialog = xbmcgui.Dialog()
38df1fa5 385 dialog.ok('Grooveshark XBMC', 'Cannot rename Grooveshark playlist ', name)
36cc00d7 386 else:
387 xbmc.executebuiltin("Container.Refresh")
388 else:
389 dialog = xbmcgui.Dialog()
38df1fa5 390 dialog.ok('Grooveshark XBMC', 'You must be logged in ', ' to rename a Grooveshark playlist.')
8817bb2e 391
36cc00d7 392 def album(self, albumid):
4be42357 393 groovesharkApi.setRemoveDuplicates(True)
2254a6b5 394 album = groovesharkApi.albumGetSongs(albumId = albumid, limit = self.songsearchlimit)
6ae708d0 395 self._add_songs_directory(album)
8817bb2e 396
397 def artist(self, artistid):
2254a6b5 398 albums = groovesharkApi.artistGetAlbums(artistId = artistid, limit = self.albumsearchlimit)
406ab447 399 self._add_albums_directory(albums, artistid)
8817bb2e 400
4be42357 401 def playlist(self, playlistid, playlistname):
8817bb2e 402 userid = self._get_login()
403 if (userid != 0):
2254a6b5 404 songs = groovesharkApi.playlistGetSongs(playlistId = playlistid, limit = self.songsearchlimit)
4be42357 405 self._add_songs_directory(songs, playlistid, playlistname)
8817bb2e 406 else:
407 dialog = xbmcgui.Dialog()
38df1fa5 408 dialog.ok('Grooveshark XBMC', 'You must be logged in', 'to get Grooveshark playlists.')
8817bb2e 409
6ae708d0 410 def playSong(self, item):
411 url = item.getProperty('url')
9b1d8c96 412 xbmc.log("Playing: " + url)
2254a6b5 413 xbmcplugin.setResolvedUrl(handle=int(sys.argv[1]), succeeded=True, listitem=item)
3fcef5ba 414
6ae708d0 415 def songItem(self, id, name, album, artist, duration, thumb, image):
416 url = groovesharkApi.getStreamURL(id)
e6ccfeca 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
6ae708d0 424 item = xbmcgui.ListItem(label = artist + " - " + album + " - " + name, path=url, thumbnailImage=songThm, iconImage=songImg)
2254a6b5 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")
6ae708d0 428 item.setProperty('url', url)
429 item.setProperty('thumb', songThm)
430 item.setProperty('image', songImg)
2254a6b5 431
6ae708d0 432 return item
2254a6b5 433
8817bb2e 434 def _get_keyboard(self, default="", heading="", hidden=False):
3cfead3c 435 kb = xbmc.Keyboard(default, heading, hidden)
436 kb.doModal()
437 if (kb.isConfirmed()):
438 return unicode(kb.getText(), "utf-8")
439 return ''
8817bb2e 440
441 def _get_login(self):
2254a6b5 442 if (self.username == "" or self.password == ""):
8817bb2e 443 dialog = xbmcgui.Dialog()
38df1fa5 444 dialog.ok('Grooveshark XBMC', 'Unable to login.', 'Check username and password in settings.')
8817bb2e 445 return 0
446 else:
38df1fa5 447 if self.userid == 0:
448 uid = groovesharkApi.loginExt(self.username, self.password)
449 if (uid != 0):
8817bb2e 450 xbmc.log("Logged in")
38df1fa5 451 return uid
8817bb2e 452 else:
453 dialog = xbmcgui.Dialog()
38df1fa5 454 dialog.ok('Grooveshark XBMC', 'Unable to login.', 'Check username and password in settings.')
8817bb2e 455 return 0
456
6ae708d0 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):
6ae708d0 469 localThumb = os.path.join(xbmc.translatePath(os.path.join(thumbDir, str(id)))) + '.tbn'
470 try:
471 if os.path.isfile(localThumb) == False:
e6ccfeca 472 xbmc.log('Downloading ' + url + ' to ' + localThumb)
6ae708d0 473 loc = urllib.URLopener()
474 loc.retrieve(url, localThumb)
475 except:
476 xbmc.log('URL download failed of ' + url + ' to ' + localThumb)
2254a6b5 477 return(self.defImg)
6ae708d0 478
479 return os.path.join(os.path.join(thumbDir, str(id))) + '.tbn'
480
4be42357 481 def _add_songs_directory(self, songs, playlistid=0, playlistname='', isFavorites=False):
6ae708d0 482 n = len(songs)
483 xbmc.log("Found " + str(n) + " songs...")
aa0c1263 484 dirItems = []
8817bb2e 485 i = 0
6ae708d0 486 while i < n:
8817bb2e 487 song = songs[i]
6ae708d0 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]
6ae708d0 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)
4be42357 506 unfav=sys.argv[0]+"?url="+urllib.quote_plus(songurl)+"&mode="+str(MODE_UNFAVORITE)+"&name="+urllib.quote_plus(songname)+"&id="+str(songid)+"&prevmode="
6ae708d0 507 menuItems = []
4be42357 508 if isFavorites == True:
509 unfav = unfav +str(MODE_FAVORITES)
510 else:
7ea6f166 511 menuItems.append(("Grooveshark favorite", "XBMC.RunPlugin("+fav+")"))
512 menuItems.append(("Not Grooveshark favorite", "XBMC.RunPlugin("+unfav+")"))
4be42357 513 if playlistid > 0:
514 rmplaylstsong=sys.argv[0]+"?playlistid="+str(playlistid)+"&id="+str(i+1)+"&mode="+str(MODE_REMOVE_PLAYLIST_SONG)+"&name="+playlistname
aa0c1263 515 menuItems.append(("Remove from Grooveshark playlist", "XBMC.RunPlugin("+rmplaylstsong+")"))
7ea6f166 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+")"))
6ae708d0 519 item.addContextMenuItems(menuItems, replaceItems=False)
aa0c1263 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 = []
8817bb2e 525 i = i + 1
aa0c1263 526
527 xbmcplugin.addDirectoryItems(int(sys.argv[1]), dirItems ,n)
8817bb2e 528 xbmcplugin.setContent(self._handle, 'songs')
31731635 529 xbmcplugin.setPluginFanart(int(sys.argv[1]), self.fanImg)
8817bb2e 530
406ab447 531 def _add_albums_directory(self, albums, artistid=0):
31731635 532 n = len(albums)
533 xbmc.log("Found " + str(n) + " albums...")
8817bb2e 534 i = 0
31731635 535 while i < n:
8817bb2e 536 album = albums[i]
537 albumArtistName = album[0]
538 albumName = album[2]
539 albumID = album[3]
2254a6b5 540 albumImage = self._get_icon(album[4], 'album-' + str(albumID))
31731635 541 self._add_dir(albumName + " - " + albumArtistName, '', MODE_ALBUM, albumImage, albumID, n)
8817bb2e 542 i = i + 1
406ab447 543 if artistid > 0:
544 self._add_dir('Similar artists...', '', MODE_SIMILAR_ARTISTS, self.artistImg, artistid)
8817bb2e 545 xbmcplugin.setContent(self._handle, 'albums')
546 xbmcplugin.addSortMethod(self._handle, xbmcplugin.SORT_METHOD_ALBUM_IGNORE_THE)
31731635 547 xbmcplugin.setPluginFanart(int(sys.argv[1]), self.fanImg)
8817bb2e 548
6ae708d0 549 def _add_artists_directory(self, artists):
31731635 550 n = len(artists)
551 xbmc.log("Found " + str(n) + " artists...")
8817bb2e 552 i = 0
31731635 553 while i < n:
8817bb2e 554 artist = artists[i]
555 artistName = artist[0]
556 artistID = artist[1]
31731635 557 self._add_dir(artistName, '', MODE_ARTIST, self.artistImg, artistID, n)
8817bb2e 558 i = i + 1
559 xbmcplugin.setContent(self._handle, 'artists')
560 xbmcplugin.addSortMethod(self._handle, xbmcplugin.SORT_METHOD_ARTIST_IGNORE_THE)
31731635 561 xbmcplugin.setPluginFanart(int(sys.argv[1]), self.fanImg)
8817bb2e 562
6ae708d0 563 def _add_playlists_directory(self, playlists):
31731635 564 n = len(playlists)
565 xbmc.log("Found " + str(n) + " playlists...")
8817bb2e 566 i = 0
31731635 567 while i < n:
8817bb2e 568 playlist = playlists[i]
569 playlistName = playlist[0]
570 playlistID = playlist[1]
31731635 571 self._add_dir(playlistName, '', MODE_PLAYLIST, self.playlistImg, playlistID, n)
8817bb2e 572 i = i + 1
573 xbmcplugin.setContent(self._handle, 'files')
574 xbmcplugin.addSortMethod(self._handle, xbmcplugin.SORT_METHOD_PLAYLIST_ORDER)
31731635 575 xbmcplugin.setPluginFanart(int(sys.argv[1]), self.fanImg)
3fcef5ba 576
31731635 577 def _add_dir(self, name, url, mode, iconimage, id, items=1):
8817bb2e 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)
b738088f 580 dir.setInfo( type="Music", infoLabels={ "title": name } )
8817bb2e 581 menuItems = []
36cc00d7 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)
aa0c1263 587 menuItems.append(("Delete Grooveshark playlist", "XBMC.RunPlugin("+rmplaylst+")"))
36cc00d7 588 mvplaylst=sys.argv[0]+"?mode="+str(MODE_RENAME_PLAYLIST)+"&name="+urllib.quote_plus(name)+"&id="+str(id)
aa0c1263 589 menuItems.append(("Rename Grooveshark playlist", "XBMC.RunPlugin("+mvplaylst+")"))
7ea6f166 590 dir.addContextMenuItems(menuItems, replaceItems=False)
31731635 591 return xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]),url=u,listitem=dir,isFolder=True, totalItems=items)
6ae708d0 592
8817bb2e 593
594def 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
612grooveshark = Groveshark();
613
614params=get_params()
615url=None
616name=None
617mode=None
618id=None
619
620try: url=urllib.unquote_plus(params["url"])
621except: pass
622try: name=urllib.unquote_plus(params["name"])
623except: pass
624try: mode=int(params["mode"])
625except: pass
626try: id=int(params["id"])
627except: pass
628
8817bb2e 629if mode==None:
630 grooveshark.categories()
631
632elif mode==MODE_SEARCH_SONGS:
633 grooveshark.searchSongs()
634
635elif mode==MODE_SEARCH_ALBUMS:
636 grooveshark.searchAlbums()
637
638elif mode==MODE_SEARCH_ARTISTS:
639 grooveshark.searchArtists()
640
36cc00d7 641elif mode==MODE_POPULAR_SONGS:
642 grooveshark.popularSongs()
8817bb2e 643
644elif mode==MODE_PLAYLISTS:
645 grooveshark.playlists()
646
647elif mode==MODE_FAVORITES:
648 grooveshark.favorites()
649
650elif mode==MODE_SONG:
8817bb2e 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
b738088f 657 try: thumb=urllib.unquote_plus(params["thumb"])
658 except: pass
659 try: image=urllib.unquote_plus(params["image"])
660 except: pass
6ae708d0 661 song = grooveshark.songItem(id, name, album, artist, duration, thumb, image)
3fcef5ba 662 grooveshark.playSong(song)
8817bb2e 663
664elif mode==MODE_ARTIST:
4be42357 665 grooveshark.artist(id)
8817bb2e 666
667elif mode==MODE_ALBUM:
4be42357 668 grooveshark.album(id)
8817bb2e 669
670elif mode==MODE_PLAYLIST:
4be42357 671 grooveshark.playlist(id, name)
8817bb2e 672
673elif mode==MODE_FAVORITE:
4be42357 674 grooveshark.favorite(id)
8817bb2e 675
676elif mode==MODE_UNFAVORITE:
4be42357 677 try: prevMode=urllib.unquote_plus(params["prevmode"])
678 except: pass
679 grooveshark.unfavorite(id, prevMode)
3fcef5ba 680
406ab447 681elif mode==MODE_SIMILAR_ARTISTS:
4be42357 682 grooveshark.similarArtists(id)
3fcef5ba 683
36cc00d7 684elif mode==MODE_MAKE_PLAYLIST:
4be42357 685 grooveshark.makePlaylist(id, name)
36cc00d7 686
687elif mode==MODE_REMOVE_PLAYLIST:
4be42357 688 grooveshark.removePlaylist(id, name)
36cc00d7 689
690elif mode==MODE_RENAME_PLAYLIST:
4be42357 691 grooveshark.renamePlaylist(id, name)
36cc00d7 692
4be42357 693elif mode==MODE_REMOVE_PLAYLIST_SONG:
694 try: playlistID=urllib.unquote_plus(params["playlistid"])
695 except: pass
696 grooveshark.removePlaylistSong(playlistID, name, id)
36cc00d7 697
7ea6f166 698elif mode==MODE_ADD_PLAYLIST_SONG:
699 grooveshark.addPlaylistSong(id)
700
8817bb2e 701if (mode < MODE_SONG):
702 xbmcplugin.endOfDirectory(int(sys.argv[1]))