Fix artist's album bug.
[clinton/xbmc-groove.git] / default.py
CommitLineData
97289139 1import urllib, sys, os, shutil, re, time, xbmcaddon, xbmcplugin, xbmcgui, xbmc, pickle
8817bb2e 2MODE_SEARCH_SONGS = 1
3MODE_SEARCH_ALBUMS = 2
4MODE_SEARCH_ARTISTS = 3
86f629ea 5MODE_SEARCH_ARTISTS_ALBUMS = 4
6MODE_SEARCH_PLAYLISTS = 5
97289139 7MODE_ARTIST_POPULAR = 6
8MODE_POPULAR_SONGS = 7
9MODE_FAVORITES = 8
10MODE_PLAYLISTS = 9
11MODE_ALBUM = 10
12MODE_ARTIST = 11
13MODE_PLAYLIST = 12
14MODE_SONG_PAGE = 13
15MODE_SONG = 14
16MODE_FAVORITE = 15
7ea6f166 17
38df1fa5 18ACTION_MOVE_LEFT = 1
7ea6f166 19ACTION_MOVE_UP = 3
20ACTION_MOVE_DOWN = 4
21ACTION_PAGE_UP = 5
22ACTION_PAGE_DOWN = 6
23ACTION_SELECT_ITEM = 7
24ACTION_PREVIOUS_MENU = 10
8817bb2e 25
86f629ea 26# Formats for track labels
27ARTIST_ALBUM_NAME_LABEL = 0
28NAME_ALBUM_ARTIST_LABEL = 1
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'))
7ce01be6 34cacheDir = os.path.join(xbmc.translatePath('special://masterprofile/addon_data/'), os.path.basename(os.getcwd()))
35thumbDirName = 'thumb'
36thumbDir = os.path.join('special://masterprofile/addon_data/', os.path.basename(os.getcwd()), thumbDirName)
4be42357 37
38baseModeUrl = 'plugin://plugin.audio.groove/'
e278f474 39playlistUrl = baseModeUrl + '?mode=' + str(MODE_PLAYLIST)
4be42357 40playlistsUrl = baseModeUrl + '?mode=' + str(MODE_PLAYLISTS)
41favoritesUrl = baseModeUrl + '?mode=' + str(MODE_FAVORITES)
42
3d95dfcb 43searchArtistsAlbumsName = "Search for artist's albums..."
44
7ce01be6 45thumbDef = os.path.join(imgDir, 'default.tbn')
8817bb2e 46
47sys.path.append (libDir)
7ce01be6 48from GroovesharkAPI import GrooveAPI
49
a3ad8f73 50try:
51 groovesharkApi = GrooveAPI()
7ce01be6 52 if groovesharkApi.pingService() != True:
53 raise StandardError('No Grooveshark service')
a3ad8f73 54except:
55 dialog = xbmcgui.Dialog()
56 dialog.ok('Grooveshark XBMC', 'Unable to connect with Grooveshark.', 'Please try again later')
57 sys.exit(-1)
58
8817bb2e 59
60class _Info:
61 def __init__( self, *args, **kwargs ):
62 self.__dict__.update( kwargs )
e278f474 63
7ce01be6 64
8817bb2e 65class Groveshark:
973b4c6c 66
7ea6f166 67 albumImg = xbmc.translatePath(os.path.join(imgDir, 'album.png'))
68 artistImg = xbmc.translatePath(os.path.join(imgDir, 'artist.png'))
86f629ea 69 artistsAlbumsImg = xbmc.translatePath(os.path.join(imgDir, 'artistsalbums.png'))
7ea6f166 70 favoritesImg = xbmc.translatePath(os.path.join(imgDir, 'favorites.png'))
71 playlistImg = xbmc.translatePath(os.path.join(imgDir, 'playlist.png'))
86f629ea 72 usersplaylistsImg = xbmc.translatePath(os.path.join(imgDir, 'usersplaylists.png'))
7ea6f166 73 popularSongsImg = xbmc.translatePath(os.path.join(imgDir, 'popularSongs.png'))
97289139 74 popularSongsArtistImg = xbmc.translatePath(os.path.join(imgDir, 'popularSongsArtist.png'))
7ea6f166 75 songImg = xbmc.translatePath(os.path.join(imgDir, 'song.png'))
76 defImg = xbmc.translatePath(os.path.join(imgDir, 'default.tbn'))
2254a6b5 77 fanImg = xbmc.translatePath(os.path.join(baseDir, 'fanart.png'))
8817bb2e 78
2254a6b5 79 settings = xbmcaddon.Addon(id='plugin.audio.groove')
7ce01be6 80 songsearchlimit = int(settings.getSetting('songsearchlimit'))
81 albumsearchlimit = int(settings.getSetting('albumsearchlimit'))
82 artistsearchlimit = int(settings.getSetting('artistsearchlimit'))
97289139 83 songspagelimit = int(settings.getSetting('songspagelimit'))
2254a6b5 84 username = settings.getSetting('username')
85 password = settings.getSetting('password')
38df1fa5 86 userid = 0
4be42357 87
8817bb2e 88 def __init__( self ):
89 self._handle = int(sys.argv[1])
7ce01be6 90 if os.path.isdir(cacheDir) == False:
91 os.makedirs(cacheDir)
92 xbmc.log("Made " + cacheDir)
164e42d8 93 artDir = xbmc.translatePath(thumbDir)
94 if os.path.isdir(artDir) == False:
3a794693 95 os.makedirs(artDir)
96 xbmc.log("Made " + artDir)
b738088f 97
e278f474 98 # Top-level menu
8817bb2e 99 def categories(self):
2254a6b5 100
38df1fa5 101 self.userid = self._get_login()
b738088f 102
103 # Setup
6ae708d0 104 xbmcplugin.setPluginFanart(int(sys.argv[1]), self.fanImg)
6ae708d0 105
86f629ea 106 self._add_dir('Search for songs...', '', MODE_SEARCH_SONGS, self.songImg, 0)
107 self._add_dir('Search for albums...', '', MODE_SEARCH_ALBUMS, self.albumImg, 0)
108 self._add_dir('Search for artists...', '', MODE_SEARCH_ARTISTS, self.artistImg, 0)
3d95dfcb 109 self._add_dir(searchArtistsAlbumsName, '', MODE_SEARCH_ARTISTS_ALBUMS, self.artistsAlbumsImg, 0)
86f629ea 110 self._add_dir("Search for user's playlists...", '', MODE_SEARCH_PLAYLISTS, self.usersplaylistsImg, 0)
97289139 111 self._add_dir('Popular songs for artist...', '', MODE_ARTIST_POPULAR, self.popularSongsArtistImg, 0)
36cc00d7 112 self._add_dir('Popular songs', '', MODE_POPULAR_SONGS, self.popularSongsImg, 0)
38df1fa5 113 if (self.userid != 0):
86f629ea 114 self._add_dir('My favorites', '', MODE_FAVORITES, self.favoritesImg, 0)
115 self._add_dir('My playlists', '', MODE_PLAYLISTS, self.playlistImg, 0)
e278f474 116
117 # Search for songs
8817bb2e 118 def searchSongs(self):
86f629ea 119 query = self._get_keyboard(default="", heading="Search for songs")
7ea6f166 120 if (query != ''):
7ce01be6 121 songs = groovesharkApi.getSongSearchResults(query, limit = self.songsearchlimit)
8817bb2e 122 if (len(songs) > 0):
6ae708d0 123 self._add_songs_directory(songs)
8817bb2e 124 else:
125 dialog = xbmcgui.Dialog()
38df1fa5 126 dialog.ok('Grooveshark XBMC', 'No matching songs.')
8817bb2e 127 self.categories()
7ea6f166 128 else:
129 self.categories()
8817bb2e 130
e278f474 131 # Search for albums
8817bb2e 132 def searchAlbums(self):
86f629ea 133 query = self._get_keyboard(default="", heading="Search for albums")
7ea6f166 134 if (query != ''):
7ce01be6 135 albums = groovesharkApi.getAlbumSearchResults(query, limit = self.albumsearchlimit)
8817bb2e 136 if (len(albums) > 0):
6ae708d0 137 self._add_albums_directory(albums)
8817bb2e 138 else:
139 dialog = xbmcgui.Dialog()
38df1fa5 140 dialog.ok('Grooveshark XBMC', 'No matching albums.')
8817bb2e 141 self.categories()
7ea6f166 142 else:
143 self.categories()
8817bb2e 144
e278f474 145 # Search for artists
8817bb2e 146 def searchArtists(self):
86f629ea 147 query = self._get_keyboard(default="", heading="Search for artists")
7ea6f166 148 if (query != ''):
7ce01be6 149 artists = groovesharkApi.getArtistSearchResults(query, limit = self.artistsearchlimit)
8817bb2e 150 if (len(artists) > 0):
6ae708d0 151 self._add_artists_directory(artists)
8817bb2e 152 else:
153 dialog = xbmcgui.Dialog()
38df1fa5 154 dialog.ok('Grooveshark XBMC', 'No matching artists.')
8817bb2e 155 self.categories()
7ea6f166 156 else:
157 self.categories()
86f629ea 158
159 # Search for playlists
160 def searchPlaylists(self):
97289139 161 query = self._get_keyboard(default="", heading="Username")
86f629ea 162 if (query != ''):
163 playlists = groovesharkApi.getUserPlaylistsEx(query)
164 if (len(playlists) > 0):
165 self._add_playlists_directory(playlists)
166 else:
167 dialog = xbmcgui.Dialog()
168 dialog.ok('Grooveshark XBMC', 'No Grooveshark playlists found.')
169 self.categories()
170 else:
171 self.categories()
172
173 # Search for artists albums
3d95dfcb 174 def searchArtistsAlbums(self, artistName = None):
175 if artistName == None or artistName == searchArtistsAlbumsName:
99f72740 176 query = self._get_keyboard(default="", heading="Search for artist's albums")
177 else:
178 query = artistName
86f629ea 179 if (query != ''):
180 artists = groovesharkApi.getArtistSearchResults(query, limit = self.artistsearchlimit)
181 if (len(artists) > 0):
182 artist = artists[0]
183 artistID = artist[1]
184 xbmc.log("Found " + artist[0] + "...")
185 albums = groovesharkApi.getArtistAlbums(artistID, limit = self.albumsearchlimit)
186 if (len(albums) > 0):
187 self._add_albums_directory(albums)
188 else:
189 dialog = xbmcgui.Dialog()
190 dialog.ok('Grooveshark XBMC', 'No matching albums.')
191 self.categories()
192 else:
193 dialog = xbmcgui.Dialog()
194 dialog.ok('Grooveshark XBMC', 'No matching artists.')
195 self.categories()
196 else:
197 self.categories()
198
e278f474 199 # Get my favorites
8817bb2e 200 def favorites(self):
36cc00d7 201 userid = self._get_login()
202 if (userid != 0):
7ce01be6 203 favorites = groovesharkApi.getUserFavoriteSongs()
36cc00d7 204 if (len(favorites) > 0):
7ce01be6 205 self._add_songs_directory(favorites)
36cc00d7 206 else:
207 dialog = xbmcgui.Dialog()
38df1fa5 208 dialog.ok('Grooveshark XBMC', 'You have no favorites.')
36cc00d7 209 self.categories()
8817bb2e 210
e278f474 211 # Get popular songs
36cc00d7 212 def popularSongs(self):
7ce01be6 213 popular = groovesharkApi.getPopularSongsToday(limit = self.songsearchlimit)
8817bb2e 214 if (len(popular) > 0):
6ae708d0 215 self._add_songs_directory(popular)
8817bb2e 216 else:
217 dialog = xbmcgui.Dialog()
38df1fa5 218 dialog.ok('Grooveshark XBMC', 'No popular songs.')
8817bb2e 219 self.categories()
36cc00d7 220
e278f474 221 # Get my playlists
8817bb2e 222 def playlists(self):
223 userid = self._get_login()
224 if (userid != 0):
7ce01be6 225 playlists = groovesharkApi.getUserPlaylists()
8817bb2e 226 if (len(playlists) > 0):
6ae708d0 227 self._add_playlists_directory(playlists)
8817bb2e 228 else:
229 dialog = xbmcgui.Dialog()
38df1fa5 230 dialog.ok('Grooveshark XBMC', 'You have no Grooveshark playlists.')
8817bb2e 231 self.categories()
7ea6f166 232 else:
233 dialog = xbmcgui.Dialog()
38df1fa5 234 dialog.ok('Grooveshark XBMC', 'You must be logged in ', ' to see your Grooveshark playlists.')
7ea6f166 235
e278f474 236 # Make songs a favorite
8817bb2e 237 def favorite(self, songid):
238 userid = self._get_login()
239 if (userid != 0):
406ab447 240 xbmc.log("Favorite song: " + str(songid))
7ce01be6 241 groovesharkApi.addUserFavoriteSong(songID = songid)
38df1fa5 242 xbmc.executebuiltin('XBMC.Notification(Grooveshark XBMC, Added to favorites, 1000, ' + thumbDef + ')')
8817bb2e 243 else:
244 dialog = xbmcgui.Dialog()
38df1fa5 245 dialog.ok('Grooveshark XBMC', 'You must be logged in', 'to add favorites.')
e278f474 246
247 # Show selected album
36cc00d7 248 def album(self, albumid):
7ce01be6 249 album = groovesharkApi.getAlbumSongs(albumid, limit = self.songsearchlimit)
86f629ea 250 self._add_songs_directory(album, trackLabelFormat=NAME_ALBUM_ARTIST_LABEL)
e278f474 251
252 # Show selected artist
8817bb2e 253 def artist(self, artistid):
7ce01be6 254 albums = groovesharkApi.getArtistAlbums(artistid, limit = self.albumsearchlimit)
7fda21f0 255 self._add_albums_directory(albums)
8817bb2e 256
e278f474 257 # Show selected playlist
7ce01be6 258 def playlist(self, playlistid):
8817bb2e 259 userid = self._get_login()
260 if (userid != 0):
7ce01be6 261 songs = groovesharkApi.getPlaylistSongs(playlistid)
86f629ea 262 self._add_songs_directory(songs, trackLabelFormat=NAME_ALBUM_ARTIST_LABEL)
8817bb2e 263 else:
264 dialog = xbmcgui.Dialog()
38df1fa5 265 dialog.ok('Grooveshark XBMC', 'You must be logged in', 'to get Grooveshark playlists.')
8817bb2e 266
97289139 267 # Show popular songs of the artist
268 def artistPopularSongs(self):
269 query = self._get_keyboard(default="", heading="Artist")
270 if (query != ''):
271 artists = groovesharkApi.getArtistSearchResults(query, limit = self.artistsearchlimit)
272 if (len(artists) > 0):
273 artist = artists[0]
274 artistID = artist[1]
275 xbmc.log("Found " + artist[0] + "...")
276 songs = groovesharkApi.getArtistPopularSongs(artistID, limit = self.songsearchlimit)
277 if (len(songs) > 0):
278 self._add_songs_directory(songs, trackLabelFormat=NAME_ALBUM_ARTIST_LABEL)
279 else:
280 dialog = xbmcgui.Dialog()
281 dialog.ok('Grooveshark XBMC', 'No popular songs.')
282 self.categories()
283 else:
284 dialog = xbmcgui.Dialog()
285 dialog.ok('Grooveshark XBMC', 'No matching artists.')
286 self.categories()
287 else:
288 self.categories()
289
e278f474 290 # Play a song
6ae708d0 291 def playSong(self, item):
7ce01be6 292 songid = item.getProperty('songid')
293 song = groovesharkApi.getSongURLFromSongID(songid)
cbb0985e 294 if song != '':
7ce01be6 295 item.setPath(song)
296 xbmc.log("Playing: " + song)
297 xbmcplugin.setResolvedUrl(handle=int(sys.argv[1]), succeeded=True, listitem=item)
298 else:
cbb0985e 299 xbmc.executebuiltin('XBMC.Notification(Grooveshark XBMC, Unable to play song, 1000, ' + thumbDef + ')')
3fcef5ba 300
e278f474 301 # Make a song directory item
86f629ea 302 def songItem(self, songid, name, album, artist, coverart, trackLabelFormat=ARTIST_ALBUM_NAME_LABEL):
7ce01be6 303 songImg = self._get_icon(coverart, 'song-' + str(songid) + "-image")
86f629ea 304 if trackLabelFormat == NAME_ALBUM_ARTIST_LABEL:
305 trackLabel = name + " - " + album + " - " + artist
306 else:
307 trackLabel = artist + " - " + album + " - " + name
308 item = xbmcgui.ListItem(label = trackLabel, thumbnailImage=songImg, iconImage=songImg)
7ce01be6 309 item.setInfo( type="music", infoLabels={ "title": name, "album": album, "artist": artist} )
2254a6b5 310 item.setProperty('mimetype', 'audio/mpeg')
311 item.setProperty("IsPlayable", "true")
7ce01be6 312 item.setProperty('songid', str(songid))
313 item.setProperty('coverart', songImg)
314 item.setProperty('title', name)
315 item.setProperty('album', album)
316 item.setProperty('artist', artist)
317
6ae708d0 318 return item
2254a6b5 319
97289139 320 # Next page of songs
321 def songPage(self, page, trackLabelFormat):
322 self._add_songs_directory([], trackLabelFormat, page)
323
e278f474 324 # Get keyboard input
8817bb2e 325 def _get_keyboard(self, default="", heading="", hidden=False):
3cfead3c 326 kb = xbmc.Keyboard(default, heading, hidden)
327 kb.doModal()
328 if (kb.isConfirmed()):
329 return unicode(kb.getText(), "utf-8")
330 return ''
8817bb2e 331
e278f474 332 # Login to grooveshark
8817bb2e 333 def _get_login(self):
2254a6b5 334 if (self.username == "" or self.password == ""):
8817bb2e 335 dialog = xbmcgui.Dialog()
38df1fa5 336 dialog.ok('Grooveshark XBMC', 'Unable to login.', 'Check username and password in settings.')
8817bb2e 337 return 0
338 else:
38df1fa5 339 if self.userid == 0:
7ce01be6 340 uid = groovesharkApi.login(self.username, self.password)
38df1fa5 341 if (uid != 0):
38df1fa5 342 return uid
8817bb2e 343 else:
344 dialog = xbmcgui.Dialog()
38df1fa5 345 dialog.ok('Grooveshark XBMC', 'Unable to login.', 'Check username and password in settings.')
8817bb2e 346 return 0
347
e278f474 348 # Get a song directory item
86f629ea 349 def _get_song_item(self, song, trackLabelFormat):
6ae708d0 350 name = song[0]
7ce01be6 351 songid = song[1]
352 album = song[2]
353 artist = song[4]
354 coverart = song[6]
86f629ea 355 return self.songItem(songid, name, album, artist, coverart, trackLabelFormat)
6ae708d0 356
357 # File download
7ce01be6 358 def _get_icon(self, url, songid):
359 if url != 'None':
360 localThumb = os.path.join(xbmc.translatePath(os.path.join(thumbDir, str(songid)))) + '.tbn'
361 try:
362 if os.path.isfile(localThumb) == False:
363 loc = urllib.URLopener()
364 loc.retrieve(url, localThumb)
365 except:
366 shutil.copy2(thumbDef, localThumb)
367 return os.path.join(os.path.join(thumbDir, str(songid))) + '.tbn'
368 else:
369 return thumbDef
e278f474 370
371 # Add songs to directory
97289139 372 def _add_songs_directory(self, songs, trackLabelFormat=ARTIST_ALBUM_NAME_LABEL, page=0):
373
374 totalSongs = len(songs)
375 page = int(page)
376
377 # No pages needed
378 if page == 0 and totalSongs <= self.songspagelimit:
379 xbmc.log("Found " + str(totalSongs) + " songs...")
380 # Pages
381 else:
382 # Cache all songs
383 if page == 0:
384 self._setSavedSongs(songs)
385 else:
386 songs = self._getSavedSongs()
387 totalSongs = len(songs)
388
389 if totalSongs > 0:
390 start = page * self.songspagelimit
391 end = start + self.songspagelimit
392 songs = songs[start:end]
393
394 for song in songs:
86f629ea 395 item = self._get_song_item(song, trackLabelFormat)
7ce01be6 396 coverart = item.getProperty('coverart')
6ae708d0 397 songname = song[0]
398 songid = song[1]
7ce01be6 399 songalbum = song[2]
400 songartist = song[4]
e278f474 401 u=sys.argv[0]+"?mode="+str(MODE_SONG)+"&name="+urllib.quote_plus(songname)+"&id="+str(songid) \
6ae708d0 402 +"&album="+urllib.quote_plus(songalbum) \
403 +"&artist="+urllib.quote_plus(songartist) \
7ce01be6 404 +"&coverart="+urllib.quote_plus(coverart)
e278f474 405 fav=sys.argv[0]+"?mode="+str(MODE_FAVORITE)+"&name="+urllib.quote_plus(songname)+"&id="+str(songid)
6ae708d0 406 menuItems = []
7ce01be6 407 menuItems.append(("Grooveshark favorite", "XBMC.RunPlugin("+fav+")"))
6ae708d0 408 item.addContextMenuItems(menuItems, replaceItems=False)
97289139 409 xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]),url=u,listitem=item,isFolder=False, totalItems=len(songs))
410
411 page = page + 1
412 if totalSongs > page * self.songspagelimit:
413 u=sys.argv[0]+"?mode="+str(MODE_SONG_PAGE)+"&id=0"+"&page="+str(page)+"&label="+str(trackLabelFormat)
414 self._add_dir('More songs...', u, MODE_SONG_PAGE, self.songImg, 0, totalSongs - (page * self.songspagelimit))
cb06c186 415
8817bb2e 416 xbmcplugin.setContent(self._handle, 'songs')
31731635 417 xbmcplugin.setPluginFanart(int(sys.argv[1]), self.fanImg)
e278f474 418
419 # Add albums to directory
7ce01be6 420 def _add_albums_directory(self, albums):
31731635 421 n = len(albums)
422 xbmc.log("Found " + str(n) + " albums...")
8817bb2e 423 i = 0
31731635 424 while i < n:
8817bb2e 425 album = albums[i]
426 albumArtistName = album[0]
427 albumName = album[2]
428 albumID = album[3]
2254a6b5 429 albumImage = self._get_icon(album[4], 'album-' + str(albumID))
31731635 430 self._add_dir(albumName + " - " + albumArtistName, '', MODE_ALBUM, albumImage, albumID, n)
8817bb2e 431 i = i + 1
432 xbmcplugin.setContent(self._handle, 'albums')
433 xbmcplugin.addSortMethod(self._handle, xbmcplugin.SORT_METHOD_ALBUM_IGNORE_THE)
31731635 434 xbmcplugin.setPluginFanart(int(sys.argv[1]), self.fanImg)
e278f474 435
436 # Add artists to directory
6ae708d0 437 def _add_artists_directory(self, artists):
31731635 438 n = len(artists)
439 xbmc.log("Found " + str(n) + " artists...")
8817bb2e 440 i = 0
31731635 441 while i < n:
8817bb2e 442 artist = artists[i]
443 artistName = artist[0]
444 artistID = artist[1]
31731635 445 self._add_dir(artistName, '', MODE_ARTIST, self.artistImg, artistID, n)
8817bb2e 446 i = i + 1
447 xbmcplugin.setContent(self._handle, 'artists')
448 xbmcplugin.addSortMethod(self._handle, xbmcplugin.SORT_METHOD_ARTIST_IGNORE_THE)
31731635 449 xbmcplugin.setPluginFanart(int(sys.argv[1]), self.fanImg)
e278f474 450
451 # Add playlists to directory
6ae708d0 452 def _add_playlists_directory(self, playlists):
31731635 453 n = len(playlists)
454 xbmc.log("Found " + str(n) + " playlists...")
8817bb2e 455 i = 0
31731635 456 while i < n:
8817bb2e 457 playlist = playlists[i]
458 playlistName = playlist[0]
459 playlistID = playlist[1]
86f629ea 460 dir = self._add_dir(playlistName, '', MODE_PLAYLIST, self.playlistImg, playlistID, n)
8817bb2e 461 i = i + 1
462 xbmcplugin.setContent(self._handle, 'files')
86f629ea 463 xbmcplugin.addSortMethod(self._handle, xbmcplugin.SORT_METHOD_LABEL)
31731635 464 xbmcplugin.setPluginFanart(int(sys.argv[1]), self.fanImg)
3fcef5ba 465
e278f474 466 # Add whatever directory
31731635 467 def _add_dir(self, name, url, mode, iconimage, id, items=1):
97289139 468 if url == '':
469 u=sys.argv[0]+"?mode="+str(mode)+"&name="+urllib.quote_plus(name)+"&id="+str(id)
470 else:
471 u = url
8817bb2e 472 dir=xbmcgui.ListItem(name, iconImage=iconimage, thumbnailImage=iconimage)
b738088f 473 dir.setInfo( type="Music", infoLabels={ "title": name } )
31731635 474 return xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]),url=u,listitem=dir,isFolder=True, totalItems=items)
97289139 475
476 def _getSavedSongs(self):
477 path = os.path.join(cacheDir, 'songs.dmp')
478 try:
479 f = open(path, 'rb')
480 songs = pickle.load(f)
481 f.close()
482 except:
483 songs = []
484 pass
485 return songs
486
487 def _setSavedSongs(self, songs):
488 try:
489 # Create the 'data' directory if it doesn't exist.
490 if not os.path.exists(cacheDir):
491 os.makedirs(cacheDir)
492 path = os.path.join(cacheDir, 'songs.dmp')
493 f = open(path, 'wb')
494 pickle.dump(songs, f, protocol=pickle.HIGHEST_PROTOCOL)
495 f.close()
496 except:
497 xbmc.log("An error occurred saving songs")
498 pass
499
6ae708d0 500
e278f474 501
502# Parse URL parameters
8817bb2e 503def get_params():
504 param=[]
505 paramstring=sys.argv[2]
e278f474 506 xbmc.log(paramstring)
8817bb2e 507 if len(paramstring)>=2:
508 params=sys.argv[2]
509 cleanedparams=params.replace('?','')
510 if (params[len(params)-1]=='/'):
511 params=params[0:len(params)-2]
512 pairsofparams=cleanedparams.split('&')
513 param={}
514 for i in range(len(pairsofparams)):
515 splitparams={}
516 splitparams=pairsofparams[i].split('=')
517 if (len(splitparams))==2:
518 param[splitparams[0]]=splitparams[1]
8817bb2e 519 return param
520
e278f474 521# Main
8817bb2e 522grooveshark = Groveshark();
e278f474 523
8817bb2e 524params=get_params()
8817bb2e 525mode=None
8817bb2e 526try: mode=int(params["mode"])
527except: pass
7ce01be6 528id=0
529try: id=int(params["id"])
530except: pass
e278f474 531
532# Call function for URL
8817bb2e 533if mode==None:
534 grooveshark.categories()
535
536elif mode==MODE_SEARCH_SONGS:
537 grooveshark.searchSongs()
538
539elif mode==MODE_SEARCH_ALBUMS:
540 grooveshark.searchAlbums()
541
542elif mode==MODE_SEARCH_ARTISTS:
543 grooveshark.searchArtists()
86f629ea 544
545elif mode==MODE_SEARCH_ARTISTS_ALBUMS:
a2e75b14 546 try: name=urllib.unquote_plus(params["name"])
547 except:
3d95dfcb 548 name = None
a2e75b14 549 pass
550 grooveshark.searchArtistsAlbums(name)
86f629ea 551
552elif mode==MODE_SEARCH_PLAYLISTS:
553 grooveshark.searchPlaylists()
8817bb2e 554
36cc00d7 555elif mode==MODE_POPULAR_SONGS:
556 grooveshark.popularSongs()
97289139 557
558elif mode==MODE_ARTIST_POPULAR:
a2e75b14 559 grooveshark.artistPopularSongs()
8817bb2e 560
8817bb2e 561elif mode==MODE_FAVORITES:
562 grooveshark.favorites()
563
e278f474 564elif mode==MODE_PLAYLISTS:
565 grooveshark.playlists()
97289139 566
567elif mode==MODE_SONG_PAGE:
568 try: page=urllib.unquote_plus(params["page"])
569 except: pass
570 try: label=urllib.unquote_plus(params["label"])
571 except: pass
572 grooveshark.songPage(page, label)
e278f474 573
8817bb2e 574elif mode==MODE_SONG:
7ce01be6 575 try: name=urllib.unquote_plus(params["name"])
576 except: pass
8817bb2e 577 try: album=urllib.unquote_plus(params["album"])
578 except: pass
579 try: artist=urllib.unquote_plus(params["artist"])
580 except: pass
7ce01be6 581 try: coverart=urllib.unquote_plus(params["coverart"])
8817bb2e 582 except: pass
7ce01be6 583 song = grooveshark.songItem(id, name, album, artist, coverart)
3fcef5ba 584 grooveshark.playSong(song)
8817bb2e 585
586elif mode==MODE_ARTIST:
4be42357 587 grooveshark.artist(id)
8817bb2e 588
589elif mode==MODE_ALBUM:
4be42357 590 grooveshark.album(id)
8817bb2e 591
592elif mode==MODE_PLAYLIST:
7ce01be6 593 grooveshark.playlist(id)
8817bb2e 594
595elif mode==MODE_FAVORITE:
4be42357 596 grooveshark.favorite(id)
97289139 597
e278f474 598if mode < MODE_SONG:
8817bb2e 599 xbmcplugin.endOfDirectory(int(sys.argv[1]))