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