More playlist functions.
[clinton/xbmc-groove.git] / default.py
CommitLineData
7ea6f166 1import urllib, urllib2, re, xbmcplugin, xbmcgui, xbmc, sys, os, time, pprint, shutil, xbmcaddon, pprint
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
22ACTION_MOVE_UP = 3
23ACTION_MOVE_DOWN = 4
24ACTION_PAGE_UP = 5
25ACTION_PAGE_DOWN = 6
26ACTION_SELECT_ITEM = 7
27ACTION_PREVIOUS_MENU = 10
8817bb2e 28
6ae708d0 29baseDir = os.getcwd()
30resDir = xbmc.translatePath(os.path.join(baseDir, 'resources'))
8817bb2e 31libDir = xbmc.translatePath(os.path.join(resDir, 'lib'))
32imgDir = xbmc.translatePath(os.path.join(resDir, 'img'))
973b4c6c 33thumbDir = os.path.join('special://masterprofile/addon_data/', os.path.join(os.path.basename(os.getcwd()), 'thumb'))
4be42357 34
35baseModeUrl = 'plugin://plugin.audio.groove/'
36playlistUrl = baseModeUrl + '?url=&mode=' + str(MODE_PLAYLIST)
37playlistsUrl = baseModeUrl + '?mode=' + str(MODE_PLAYLISTS)
38favoritesUrl = baseModeUrl + '?mode=' + str(MODE_FAVORITES)
39
3cfead3c 40thumbDef = os.path.join(os.path.basename(os.getcwd()), 'default.tbn')
7ea6f166 41listBackground = os.path.join(imgDir, 'listbackground.png')
8817bb2e 42
43sys.path.append (libDir)
44from GrooveAPI import *
45groovesharkApi = GrooveAPI()
46
47class _Info:
48 def __init__( self, *args, **kwargs ):
49 self.__dict__.update( kwargs )
3fcef5ba 50
7ea6f166 51class 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
8817bb2e 118class Groveshark:
973b4c6c 119
7ea6f166 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'))
2254a6b5 127 fanImg = xbmc.translatePath(os.path.join(baseDir, 'fanart.png'))
8817bb2e 128
2254a6b5 129 settings = xbmcaddon.Addon(id='plugin.audio.groove')
973b4c6c 130 songsearchlimit = settings.getSetting('songsearchlimit')
131 albumsearchlimit = settings.getSetting('albumsearchlimit')
132 artistsearchlimit = settings.getSetting('artistsearchlimit')
2254a6b5 133 username = settings.getSetting('username')
134 password = settings.getSetting('password')
4be42357 135
8817bb2e 136 def __init__( self ):
137 self._handle = int(sys.argv[1])
b738088f 138
8817bb2e 139 def categories(self):
2254a6b5 140
141 xbmc.log(self.username + ", limits: " + str(self.songsearchlimit) + ", " + str(self.albumsearchlimit) + ", " + str(self.artistsearchlimit))
b738088f 142
8817bb2e 143 userid = self._get_login()
b738088f 144
145 # Setup
6ae708d0 146 xbmcplugin.setPluginFanart(int(sys.argv[1]), self.fanImg)
147 if os.path.isdir(thumbDir) == False:
148 os.makedirs(thumbDir)
149
3fcef5ba 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)
36cc00d7 153 self._add_dir('Popular songs', '', MODE_POPULAR_SONGS, self.popularSongsImg, 0)
8817bb2e 154 if (userid != 0):
3fcef5ba 155 self._add_dir('Favorites', '', MODE_FAVORITES, self.favoritesImg, 0)
156 self._add_dir('Playlists', '', MODE_PLAYLISTS, self.playlistImg, 0)
157
8817bb2e 158 def searchSongs(self):
159 query = self._get_keyboard(default="", heading="Search songs")
7ea6f166 160 if (query != ''):
4be42357 161 groovesharkApi.setRemoveDuplicates(True)
2254a6b5 162 songs = groovesharkApi.searchSongs(query, limit = self.songsearchlimit)
8817bb2e 163 if (len(songs) > 0):
6ae708d0 164 self._add_songs_directory(songs)
8817bb2e 165 else:
166 dialog = xbmcgui.Dialog()
7ea6f166 167 dialog.ok('Grooveshark for XBMC', 'No matching songs.')
8817bb2e 168 self.categories()
7ea6f166 169 else:
170 self.categories()
8817bb2e 171
172 def searchAlbums(self):
173 query = self._get_keyboard(default="", heading="Search albums")
7ea6f166 174 if (query != ''):
2254a6b5 175 albums = groovesharkApi.searchAlbums(query, limit = self.albumsearchlimit)
8817bb2e 176 if (len(albums) > 0):
6ae708d0 177 self._add_albums_directory(albums)
8817bb2e 178 else:
179 dialog = xbmcgui.Dialog()
7ea6f166 180 dialog.ok('Grooveshark for XBMC', 'No matching albums.')
8817bb2e 181 self.categories()
7ea6f166 182 else:
183 self.categories()
8817bb2e 184
185 def searchArtists(self):
186 query = self._get_keyboard(default="", heading="Search artists")
7ea6f166 187 if (query != ''):
2254a6b5 188 artists = groovesharkApi.searchArtists(query, limit = self.artistsearchlimit)
8817bb2e 189 if (len(artists) > 0):
6ae708d0 190 self._add_artists_directory(artists)
8817bb2e 191 else:
192 dialog = xbmcgui.Dialog()
7ea6f166 193 dialog.ok('Grooveshark for XBMC', 'No matching artists.')
8817bb2e 194 self.categories()
7ea6f166 195 else:
196 self.categories()
8817bb2e 197
198 def favorites(self):
36cc00d7 199 userid = self._get_login()
200 if (userid != 0):
201 favorites = groovesharkApi.userGetFavoriteSongs(userid)
202 if (len(favorites) > 0):
4be42357 203 self._add_songs_directory(favorites, isFavorites=True)
36cc00d7 204 else:
205 dialog = xbmcgui.Dialog()
7ea6f166 206 dialog.ok('Grooveshark for XBMC', 'You have no favorites.')
36cc00d7 207 self.categories()
8817bb2e 208
36cc00d7 209 def popularSongs(self):
2254a6b5 210 popular = groovesharkApi.popularGetSongs(limit = self.songsearchlimit)
8817bb2e 211 if (len(popular) > 0):
6ae708d0 212 self._add_songs_directory(popular)
8817bb2e 213 else:
214 dialog = xbmcgui.Dialog()
7ea6f166 215 dialog.ok('Grooveshark for XBMC', 'No popular songs.')
8817bb2e 216 self.categories()
36cc00d7 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()
7ea6f166 224 dialog.ok('Grooveshark for XBMC', 'No popular albums.')
36cc00d7 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()
7ea6f166 233 dialog.ok('Grooveshark for XBMC', 'No popular artists.')
36cc00d7 234 self.categories()
8817bb2e 235
236 def playlists(self):
237 userid = self._get_login()
238 if (userid != 0):
239 playlists = groovesharkApi.userGetPlaylists()
240 if (len(playlists) > 0):
6ae708d0 241 self._add_playlists_directory(playlists)
8817bb2e 242 else:
243 dialog = xbmcgui.Dialog()
7ea6f166 244 dialog.ok('Grooveshark for XBMC', 'You have no playlists.')
8817bb2e 245 self.categories()
7ea6f166 246 else:
247 dialog = xbmcgui.Dialog()
248 dialog.ok('Grooveshark for XBMC', 'You must be logged in ', ' to see your playlists.')
249
8817bb2e 250
251 def favorite(self, songid):
252 userid = self._get_login()
253 if (userid != 0):
406ab447 254 xbmc.log("Favorite song: " + str(songid))
8817bb2e 255 groovesharkApi.favoriteSong(songID = songid)
3cfead3c 256 xbmc.executebuiltin('XBMC.Notification(Grooveshark, Added to favorites, 1000, ' + thumbDef + ')')
8817bb2e 257 else:
258 dialog = xbmcgui.Dialog()
7ea6f166 259 dialog.ok('Grooveshark for XBMC', 'You must be logged in', 'to add favorites.')
8817bb2e 260
4be42357 261 def unfavorite(self, songid, prevMode):
8817bb2e 262 userid = self._get_login()
263 if (userid != 0):
4be42357 264 xbmc.log("Unfavorite song: " + str(songid) + ', previous mode was ' + str(prevMode))
8817bb2e 265 groovesharkApi.unfavoriteSong(songID = songid)
7ea6f166 266 xbmc.executebuiltin('XBMC.Notification(Grooveshark for XBMC, Removed from favorites, 1000, ' + thumbDef + ')')
4be42357 267 if (int(prevMode) == MODE_FAVORITES):
7ea6f166 268 xbmc.executebuiltin("Container.Update(" + favoritesUrl + ")")
8817bb2e 269 else:
270 dialog = xbmcgui.Dialog()
7ea6f166 271 dialog.ok('Grooveshark for XBMC', 'You must be logged in', 'to remove favorites.')
3fcef5ba 272
273 def frown(self, songid):
274 userid = self._get_login()
275 if (userid != 0):
406ab447 276 xbmc.log("Frown song: " + str(songid))
3fcef5ba 277 if groovesharkApi.radioFrown(songId = songid) != True:
278 xbmc.log("Unable to frown song " + str(songid))
3cfead3c 279 else:
7ea6f166 280 xbmc.executebuiltin('XBMC.Notification(Grooveshark for XBMC, Frowned, 1000, ' + thumbDef + ')')
3fcef5ba 281 else:
282 dialog = xbmcgui.Dialog()
7ea6f166 283 dialog.ok('Grooveshark for XBMC', 'You must be logged in', 'to frown a song.')
3fcef5ba 284
406ab447 285 def similarArtists(self, artistId):
286 similar = groovesharkApi.artistGetSimilar(artistId, limit = self.artistsearchlimit)
287 if (len(similar) > 0):
288 self._add_artists_directory(similar)
3fcef5ba 289 else:
290 dialog = xbmcgui.Dialog()
7ea6f166 291 dialog.ok('Grooveshark for XBMC', 'No similar artists.')
406ab447 292 self.categories()
36cc00d7 293
294 def makePlaylist(self, albumid, name):
295 userid = self._get_login()
296 if (userid != 0):
3cfead3c 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 != '':
4be42357 301 groovesharkApi.setRemoveDuplicates(True)
3cfead3c 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()
7ea6f166 309 dialog.ok('Grooveshark for XBMC', 'Cannot create playlist ', name)
3cfead3c 310 else:
7ea6f166 311 xbmc.executebuiltin('XBMC.Notification(Grooveshark for XBMC, Playlist created, 1000, ' + thumbDef + ')')
36cc00d7 312 else:
313 dialog = xbmcgui.Dialog()
7ea6f166 314 dialog.ok('Grooveshark for XBMC', 'You must be logged in ', ' to create a playlist.')
36cc00d7 315
316 def removePlaylist(self, playlistid, name):
317 dialog = xbmcgui.Dialog()
7ea6f166 318 if dialog.yesno('Grooveshark for XBMC', name, 'Delete this playlist?') == True:
36cc00d7 319 userid = self._get_login()
320 if (userid != 0):
321 groovesharkApi.playlistDelete(playlistid)
7ea6f166 322 xbmc.executebuiltin("Container.Refresh(" + playlistsUrl + ")")
36cc00d7 323 else:
324 dialog = xbmcgui.Dialog()
7ea6f166 325 dialog.ok('Grooveshark for XBMC', 'You must be logged in ', ' to delete a playlist.')
36cc00d7 326
4be42357 327 def removePlaylistSong(self, playlistid, playlistname, songpos):
328 dialog = xbmcgui.Dialog()
7ea6f166 329 if dialog.yesno('Grooveshark for XBMC', 'Delete this song from the playlist?') == True:
4be42357 330 userid = self._get_login()
331 if (userid != 0):
332 if groovesharkApi.playlistDeleteSong(playlistid, songpos) == 0:
333 dialog = xbmcgui.Dialog()
7ea6f166 334 dialog.ok('Grooveshark for XBMC', 'Failed to remove ', ' song from playlist.')
4be42357 335 else:
7ea6f166 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
4be42357 352 else:
353 dialog = xbmcgui.Dialog()
7ea6f166 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.')
4be42357 359
36cc00d7 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")
3cfead3c 364 if newname == '':
365 return
366 elif groovesharkApi.playlistRename(playlistid, newname) == 0:
36cc00d7 367 dialog = xbmcgui.Dialog()
7ea6f166 368 dialog.ok('Grooveshark for XBMC', 'Cannot rename playlist ', name)
36cc00d7 369 else:
370 xbmc.executebuiltin("Container.Refresh")
371 else:
372 dialog = xbmcgui.Dialog()
7ea6f166 373 dialog.ok('Grooveshark for XBMC', 'You must be logged in ', ' to rename a playlist.')
8817bb2e 374
36cc00d7 375 def album(self, albumid):
4be42357 376 groovesharkApi.setRemoveDuplicates(True)
2254a6b5 377 album = groovesharkApi.albumGetSongs(albumId = albumid, limit = self.songsearchlimit)
6ae708d0 378 self._add_songs_directory(album)
8817bb2e 379
380 def artist(self, artistid):
2254a6b5 381 albums = groovesharkApi.artistGetAlbums(artistId = artistid, limit = self.albumsearchlimit)
406ab447 382 self._add_albums_directory(albums, artistid)
8817bb2e 383
4be42357 384 def playlist(self, playlistid, playlistname):
8817bb2e 385 userid = self._get_login()
386 if (userid != 0):
2254a6b5 387 songs = groovesharkApi.playlistGetSongs(playlistId = playlistid, limit = self.songsearchlimit)
4be42357 388 self._add_songs_directory(songs, playlistid, playlistname)
8817bb2e 389 else:
390 dialog = xbmcgui.Dialog()
7ea6f166 391 dialog.ok('Grooveshark for XBMC', 'You must be logged in', 'to get playlists.')
8817bb2e 392
6ae708d0 393 def playSong(self, item):
394 url = item.getProperty('url')
9b1d8c96 395 xbmc.log("Playing: " + url)
2254a6b5 396 xbmcplugin.setResolvedUrl(handle=int(sys.argv[1]), succeeded=True, listitem=item)
3fcef5ba 397
6ae708d0 398 def songItem(self, id, name, album, artist, duration, thumb, image):
399 url = groovesharkApi.getStreamURL(id)
e6ccfeca 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
6ae708d0 407 item = xbmcgui.ListItem(label = artist + " - " + album + " - " + name, path=url, thumbnailImage=songThm, iconImage=songImg)
2254a6b5 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")
6ae708d0 411 item.setProperty('url', url)
412 item.setProperty('thumb', songThm)
413 item.setProperty('image', songImg)
2254a6b5 414
6ae708d0 415 return item
2254a6b5 416
8817bb2e 417 def _get_keyboard(self, default="", heading="", hidden=False):
3cfead3c 418 kb = xbmc.Keyboard(default, heading, hidden)
419 kb.doModal()
420 if (kb.isConfirmed()):
421 return unicode(kb.getText(), "utf-8")
422 return ''
8817bb2e 423
424 def _get_login(self):
2254a6b5 425 if (self.username == "" or self.password == ""):
8817bb2e 426 dialog = xbmcgui.Dialog()
7ea6f166 427 dialog.ok('Grooveshark for XBMC', 'Unable to login.', 'Check username and password in settings.')
8817bb2e 428 return 0
429 else:
430 if groovesharkApi.loggedInStatus() == 1:
431 groovesharkApi.logout()
2254a6b5 432 userid = groovesharkApi.loginExt(self.username, self.password)
8817bb2e 433 if (userid != 0):
434 xbmc.log("Logged in")
435 return userid
436 else:
437 dialog = xbmcgui.Dialog()
7ea6f166 438 dialog.ok('Grooveshark for XBMC', 'Unable to login.', 'Check username and password in settings.')
8817bb2e 439 return 0
440
6ae708d0 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):
6ae708d0 453 localThumb = os.path.join(xbmc.translatePath(os.path.join(thumbDir, str(id)))) + '.tbn'
454 try:
455 if os.path.isfile(localThumb) == False:
e6ccfeca 456 xbmc.log('Downloading ' + url + ' to ' + localThumb)
6ae708d0 457 loc = urllib.URLopener()
458 loc.retrieve(url, localThumb)
459 except:
460 xbmc.log('URL download failed of ' + url + ' to ' + localThumb)
2254a6b5 461 return(self.defImg)
6ae708d0 462
463 return os.path.join(os.path.join(thumbDir, str(id))) + '.tbn'
464
4be42357 465 def _add_songs_directory(self, songs, playlistid=0, playlistname='', isFavorites=False):
6ae708d0 466 n = len(songs)
467 xbmc.log("Found " + str(n) + " songs...")
8817bb2e 468 i = 0
6ae708d0 469 while i < n:
8817bb2e 470 song = songs[i]
6ae708d0 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]
6ae708d0 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)
4be42357 489 unfav=sys.argv[0]+"?url="+urllib.quote_plus(songurl)+"&mode="+str(MODE_UNFAVORITE)+"&name="+urllib.quote_plus(songname)+"&id="+str(songid)+"&prevmode="
6ae708d0 490 menuItems = []
4be42357 491 if isFavorites == True:
492 unfav = unfav +str(MODE_FAVORITES)
493 else:
7ea6f166 494 menuItems.append(("Grooveshark favorite", "XBMC.RunPlugin("+fav+")"))
495 menuItems.append(("Not Grooveshark favorite", "XBMC.RunPlugin("+unfav+")"))
4be42357 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+")"))
7ea6f166 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+")"))
6ae708d0 502 item.addContextMenuItems(menuItems, replaceItems=False)
31731635 503 xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]),url=u,listitem=item,isFolder=False,totalItems=n)
8817bb2e 504 i = i + 1
505 xbmcplugin.setContent(self._handle, 'songs')
31731635 506 xbmcplugin.setPluginFanart(int(sys.argv[1]), self.fanImg)
8817bb2e 507
406ab447 508 def _add_albums_directory(self, albums, artistid=0):
31731635 509 n = len(albums)
510 xbmc.log("Found " + str(n) + " albums...")
8817bb2e 511 i = 0
31731635 512 while i < n:
8817bb2e 513 album = albums[i]
514 albumArtistName = album[0]
515 albumName = album[2]
516 albumID = album[3]
2254a6b5 517 albumImage = self._get_icon(album[4], 'album-' + str(albumID))
31731635 518 self._add_dir(albumName + " - " + albumArtistName, '', MODE_ALBUM, albumImage, albumID, n)
8817bb2e 519 i = i + 1
406ab447 520 if artistid > 0:
521 self._add_dir('Similar artists...', '', MODE_SIMILAR_ARTISTS, self.artistImg, artistid)
8817bb2e 522 xbmcplugin.setContent(self._handle, 'albums')
523 xbmcplugin.addSortMethod(self._handle, xbmcplugin.SORT_METHOD_ALBUM_IGNORE_THE)
31731635 524 xbmcplugin.setPluginFanart(int(sys.argv[1]), self.fanImg)
8817bb2e 525
6ae708d0 526 def _add_artists_directory(self, artists):
31731635 527 n = len(artists)
528 xbmc.log("Found " + str(n) + " artists...")
8817bb2e 529 i = 0
31731635 530 while i < n:
8817bb2e 531 artist = artists[i]
532 artistName = artist[0]
533 artistID = artist[1]
31731635 534 self._add_dir(artistName, '', MODE_ARTIST, self.artistImg, artistID, n)
8817bb2e 535 i = i + 1
536 xbmcplugin.setContent(self._handle, 'artists')
537 xbmcplugin.addSortMethod(self._handle, xbmcplugin.SORT_METHOD_ARTIST_IGNORE_THE)
31731635 538 xbmcplugin.setPluginFanart(int(sys.argv[1]), self.fanImg)
8817bb2e 539
6ae708d0 540 def _add_playlists_directory(self, playlists):
31731635 541 n = len(playlists)
542 xbmc.log("Found " + str(n) + " playlists...")
8817bb2e 543 i = 0
31731635 544 while i < n:
8817bb2e 545 playlist = playlists[i]
546 playlistName = playlist[0]
547 playlistID = playlist[1]
31731635 548 self._add_dir(playlistName, '', MODE_PLAYLIST, self.playlistImg, playlistID, n)
8817bb2e 549 i = i + 1
550 xbmcplugin.setContent(self._handle, 'files')
551 xbmcplugin.addSortMethod(self._handle, xbmcplugin.SORT_METHOD_PLAYLIST_ORDER)
31731635 552 xbmcplugin.setPluginFanart(int(sys.argv[1]), self.fanImg)
3fcef5ba 553
31731635 554 def _add_dir(self, name, url, mode, iconimage, id, items=1):
8817bb2e 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)
b738088f 557 dir.setInfo( type="Music", infoLabels={ "title": name } )
8817bb2e 558 menuItems = []
36cc00d7 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+")"))
7ea6f166 567 dir.addContextMenuItems(menuItems, replaceItems=False)
31731635 568 return xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]),url=u,listitem=dir,isFolder=True, totalItems=items)
6ae708d0 569
8817bb2e 570
571def 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
589grooveshark = Groveshark();
590
591params=get_params()
592url=None
593name=None
594mode=None
595id=None
596
597try: url=urllib.unquote_plus(params["url"])
598except: pass
599try: name=urllib.unquote_plus(params["name"])
600except: pass
601try: mode=int(params["mode"])
602except: pass
603try: id=int(params["id"])
604except: pass
605
8817bb2e 606if mode==None:
607 grooveshark.categories()
608
609elif mode==MODE_SEARCH_SONGS:
610 grooveshark.searchSongs()
611
612elif mode==MODE_SEARCH_ALBUMS:
613 grooveshark.searchAlbums()
614
615elif mode==MODE_SEARCH_ARTISTS:
616 grooveshark.searchArtists()
617
36cc00d7 618elif mode==MODE_POPULAR_SONGS:
619 grooveshark.popularSongs()
8817bb2e 620
621elif mode==MODE_PLAYLISTS:
622 grooveshark.playlists()
623
624elif mode==MODE_FAVORITES:
625 grooveshark.favorites()
626
627elif mode==MODE_SONG:
8817bb2e 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
b738088f 634 try: thumb=urllib.unquote_plus(params["thumb"])
635 except: pass
636 try: image=urllib.unquote_plus(params["image"])
637 except: pass
6ae708d0 638 song = grooveshark.songItem(id, name, album, artist, duration, thumb, image)
3fcef5ba 639 grooveshark.playSong(song)
8817bb2e 640
641elif mode==MODE_ARTIST:
4be42357 642 grooveshark.artist(id)
8817bb2e 643
644elif mode==MODE_ALBUM:
4be42357 645 grooveshark.album(id)
8817bb2e 646
647elif mode==MODE_PLAYLIST:
4be42357 648 grooveshark.playlist(id, name)
8817bb2e 649
650elif mode==MODE_FAVORITE:
4be42357 651 grooveshark.favorite(id)
8817bb2e 652
653elif mode==MODE_UNFAVORITE:
4be42357 654 try: prevMode=urllib.unquote_plus(params["prevmode"])
655 except: pass
656 grooveshark.unfavorite(id, prevMode)
3fcef5ba 657
406ab447 658elif mode==MODE_SIMILAR_ARTISTS:
4be42357 659 grooveshark.similarArtists(id)
3fcef5ba 660
36cc00d7 661elif mode==MODE_MAKE_PLAYLIST:
4be42357 662 grooveshark.makePlaylist(id, name)
36cc00d7 663
664elif mode==MODE_REMOVE_PLAYLIST:
4be42357 665 grooveshark.removePlaylist(id, name)
36cc00d7 666
667elif mode==MODE_RENAME_PLAYLIST:
4be42357 668 grooveshark.renamePlaylist(id, name)
36cc00d7 669
4be42357 670elif mode==MODE_REMOVE_PLAYLIST_SONG:
671 try: playlistID=urllib.unquote_plus(params["playlistid"])
672 except: pass
673 grooveshark.removePlaylistSong(playlistID, name, id)
36cc00d7 674
7ea6f166 675elif mode==MODE_ADD_PLAYLIST_SONG:
676 grooveshark.addPlaylistSong(id)
677
8817bb2e 678if (mode < MODE_SONG):
679 xbmcplugin.endOfDirectory(int(sys.argv[1]))