1 import urllib
, sys
, os
, shutil
, re
, time
, xbmcaddon
, xbmcplugin
, xbmcgui
, xbmc
, pickle
4 MODE_SEARCH_ARTISTS
= 3
5 MODE_SEARCH_ARTISTS_ALBUMS
= 4
6 MODE_SEARCH_PLAYLISTS
= 5
7 MODE_ARTIST_POPULAR
= 6
23 ACTION_SELECT_ITEM
= 7
24 ACTION_PREVIOUS_MENU
= 10
26 # Formats for track labels
27 ARTIST_ALBUM_NAME_LABEL
= 0
28 NAME_ALBUM_ARTIST_LABEL
= 1
31 resDir
= xbmc
.translatePath(os
.path
.join(baseDir
, 'resources'))
32 libDir
= xbmc
.translatePath(os
.path
.join(resDir
, 'lib'))
33 imgDir
= xbmc
.translatePath(os
.path
.join(resDir
, 'img'))
34 cacheDir
= os
.path
.join(xbmc
.translatePath('special://masterprofile/addon_data/'), os
.path
.basename(os
.getcwd()))
35 thumbDirName
= 'thumb'
36 thumbDir
= os
.path
.join('special://masterprofile/addon_data/', os
.path
.basename(os
.getcwd()), thumbDirName
)
38 baseModeUrl
= 'plugin://plugin.audio.groove/'
39 playlistUrl
= baseModeUrl
+ '?mode=' + str(MODE_PLAYLIST
)
40 playlistsUrl
= baseModeUrl
+ '?mode=' + str(MODE_PLAYLISTS
)
41 favoritesUrl
= baseModeUrl
+ '?mode=' + str(MODE_FAVORITES
)
43 thumbDef
= os
.path
.join(imgDir
, 'default.tbn')
45 sys
.path
.append (libDir
)
46 from GroovesharkAPI
import GrooveAPI
49 groovesharkApi
= GrooveAPI()
50 if groovesharkApi
.pingService() != True:
51 raise StandardError('No Grooveshark service')
53 dialog
= xbmcgui
.Dialog()
54 dialog
.ok('Grooveshark XBMC', 'Unable to connect with Grooveshark.', 'Please try again later')
59 def __init__( self
, *args
, **kwargs
):
60 self
.__dict
__.update( kwargs
)
65 albumImg
= xbmc
.translatePath(os
.path
.join(imgDir
, 'album.png'))
66 artistImg
= xbmc
.translatePath(os
.path
.join(imgDir
, 'artist.png'))
67 artistsAlbumsImg
= xbmc
.translatePath(os
.path
.join(imgDir
, 'artistsalbums.png'))
68 favoritesImg
= xbmc
.translatePath(os
.path
.join(imgDir
, 'favorites.png'))
69 playlistImg
= xbmc
.translatePath(os
.path
.join(imgDir
, 'playlist.png'))
70 usersplaylistsImg
= xbmc
.translatePath(os
.path
.join(imgDir
, 'usersplaylists.png'))
71 popularSongsImg
= xbmc
.translatePath(os
.path
.join(imgDir
, 'popularSongs.png'))
72 popularSongsArtistImg
= xbmc
.translatePath(os
.path
.join(imgDir
, 'popularSongsArtist.png'))
73 songImg
= xbmc
.translatePath(os
.path
.join(imgDir
, 'song.png'))
74 defImg
= xbmc
.translatePath(os
.path
.join(imgDir
, 'default.tbn'))
75 fanImg
= xbmc
.translatePath(os
.path
.join(baseDir
, 'fanart.png'))
77 settings
= xbmcaddon
.Addon(id='plugin.audio.groove')
78 songsearchlimit
= int(settings
.getSetting('songsearchlimit'))
79 albumsearchlimit
= int(settings
.getSetting('albumsearchlimit'))
80 artistsearchlimit
= int(settings
.getSetting('artistsearchlimit'))
81 songspagelimit
= int(settings
.getSetting('songspagelimit'))
82 username
= settings
.getSetting('username')
83 password
= settings
.getSetting('password')
87 self
._handle
= int(sys
.argv
[1])
88 if os
.path
.isdir(cacheDir
) == False:
90 xbmc
.log("Made " + cacheDir
)
91 artDir
= xbmc
.translatePath(thumbDir
)
92 if os
.path
.isdir(artDir
) == False:
94 xbmc
.log("Made " + artDir
)
99 self
.userid
= self
._get
_login
()
102 xbmcplugin
.setPluginFanart(int(sys
.argv
[1]), self
.fanImg
)
104 self
._add
_dir
('Search for songs...', '', MODE_SEARCH_SONGS
, self
.songImg
, 0)
105 self
._add
_dir
('Search for albums...', '', MODE_SEARCH_ALBUMS
, self
.albumImg
, 0)
106 self
._add
_dir
('Search for artists...', '', MODE_SEARCH_ARTISTS
, self
.artistImg
, 0)
107 self
._add
_dir
("Search for artist's albums...", '', MODE_SEARCH_ARTISTS_ALBUMS
, self
.artistsAlbumsImg
, 0)
108 self
._add
_dir
("Search for user's playlists...", '', MODE_SEARCH_PLAYLISTS
, self
.usersplaylistsImg
, 0)
109 self
._add
_dir
('Popular songs for artist...', '', MODE_ARTIST_POPULAR
, self
.popularSongsArtistImg
, 0)
110 self
._add
_dir
('Popular songs', '', MODE_POPULAR_SONGS
, self
.popularSongsImg
, 0)
111 if (self
.userid
!= 0):
112 self
._add
_dir
('My favorites', '', MODE_FAVORITES
, self
.favoritesImg
, 0)
113 self
._add
_dir
('My playlists', '', MODE_PLAYLISTS
, self
.playlistImg
, 0)
116 def searchSongs(self
):
117 query
= self
._get
_keyboard
(default
="", heading
="Search for songs")
119 songs
= groovesharkApi
.getSongSearchResults(query
, limit
= self
.songsearchlimit
)
121 self
._add
_songs
_directory
(songs
)
123 dialog
= xbmcgui
.Dialog()
124 dialog
.ok('Grooveshark XBMC', 'No matching songs.')
130 def searchAlbums(self
):
131 query
= self
._get
_keyboard
(default
="", heading
="Search for albums")
133 albums
= groovesharkApi
.getAlbumSearchResults(query
, limit
= self
.albumsearchlimit
)
134 if (len(albums
) > 0):
135 self
._add
_albums
_directory
(albums
)
137 dialog
= xbmcgui
.Dialog()
138 dialog
.ok('Grooveshark XBMC', 'No matching albums.')
144 def searchArtists(self
):
145 query
= self
._get
_keyboard
(default
="", heading
="Search for artists")
147 artists
= groovesharkApi
.getArtistSearchResults(query
, limit
= self
.artistsearchlimit
)
148 if (len(artists
) > 0):
149 self
._add
_artists
_directory
(artists
)
151 dialog
= xbmcgui
.Dialog()
152 dialog
.ok('Grooveshark XBMC', 'No matching artists.')
157 # Search for playlists
158 def searchPlaylists(self
):
159 query
= self
._get
_keyboard
(default
="", heading
="Username")
161 playlists
= groovesharkApi
.getUserPlaylistsEx(query
)
162 if (len(playlists
) > 0):
163 self
._add
_playlists
_directory
(playlists
)
165 dialog
= xbmcgui
.Dialog()
166 dialog
.ok('Grooveshark XBMC', 'No Grooveshark playlists found.')
171 # Search for artists albums
172 def searchArtistsAlbums(self
):
173 query
= self
._get
_keyboard
(default
="", heading
="Search for artist's albums")
175 artists
= groovesharkApi
.getArtistSearchResults(query
, limit
= self
.artistsearchlimit
)
176 if (len(artists
) > 0):
179 xbmc
.log("Found " + artist
[0] + "...")
180 albums
= groovesharkApi
.getArtistAlbums(artistID
, limit
= self
.albumsearchlimit
)
181 if (len(albums
) > 0):
182 self
._add
_albums
_directory
(albums
)
184 dialog
= xbmcgui
.Dialog()
185 dialog
.ok('Grooveshark XBMC', 'No matching albums.')
188 dialog
= xbmcgui
.Dialog()
189 dialog
.ok('Grooveshark XBMC', 'No matching artists.')
196 userid
= self
._get
_login
()
198 favorites
= groovesharkApi
.getUserFavoriteSongs()
199 if (len(favorites
) > 0):
200 self
._add
_songs
_directory
(favorites
)
202 dialog
= xbmcgui
.Dialog()
203 dialog
.ok('Grooveshark XBMC', 'You have no favorites.')
207 def popularSongs(self
):
208 popular
= groovesharkApi
.getPopularSongsToday(limit
= self
.songsearchlimit
)
209 if (len(popular
) > 0):
210 self
._add
_songs
_directory
(popular
)
212 dialog
= xbmcgui
.Dialog()
213 dialog
.ok('Grooveshark XBMC', 'No popular songs.')
218 userid
= self
._get
_login
()
220 playlists
= groovesharkApi
.getUserPlaylists()
221 if (len(playlists
) > 0):
222 self
._add
_playlists
_directory
(playlists
)
224 dialog
= xbmcgui
.Dialog()
225 dialog
.ok('Grooveshark XBMC', 'You have no Grooveshark playlists.')
228 dialog
= xbmcgui
.Dialog()
229 dialog
.ok('Grooveshark XBMC', 'You must be logged in ', ' to see your Grooveshark playlists.')
231 # Make songs a favorite
232 def favorite(self
, songid
):
233 userid
= self
._get
_login
()
235 xbmc
.log("Favorite song: " + str(songid
))
236 groovesharkApi
.addUserFavoriteSong(songID
= songid
)
237 xbmc
.executebuiltin('XBMC.Notification(Grooveshark XBMC, Added to favorites, 1000, ' + thumbDef
+ ')')
239 dialog
= xbmcgui
.Dialog()
240 dialog
.ok('Grooveshark XBMC', 'You must be logged in', 'to add favorites.')
242 # Show selected album
243 def album(self
, albumid
):
244 album
= groovesharkApi
.getAlbumSongs(albumid
, limit
= self
.songsearchlimit
)
245 self
._add
_songs
_directory
(album
, trackLabelFormat
=NAME_ALBUM_ARTIST_LABEL
)
247 # Show selected artist
248 def artist(self
, artistid
):
249 albums
= groovesharkApi
.getArtistAlbums(artistid
, limit
= self
.albumsearchlimit
)
250 self
._add
_albums
_directory
(albums
)
252 # Show selected playlist
253 def playlist(self
, playlistid
):
254 userid
= self
._get
_login
()
256 songs
= groovesharkApi
.getPlaylistSongs(playlistid
)
257 self
._add
_songs
_directory
(songs
, trackLabelFormat
=NAME_ALBUM_ARTIST_LABEL
)
259 dialog
= xbmcgui
.Dialog()
260 dialog
.ok('Grooveshark XBMC', 'You must be logged in', 'to get Grooveshark playlists.')
262 # Show popular songs of the artist
263 def artistPopularSongs(self
):
264 query
= self
._get
_keyboard
(default
="", heading
="Artist")
266 artists
= groovesharkApi
.getArtistSearchResults(query
, limit
= self
.artistsearchlimit
)
267 if (len(artists
) > 0):
270 xbmc
.log("Found " + artist
[0] + "...")
271 songs
= groovesharkApi
.getArtistPopularSongs(artistID
, limit
= self
.songsearchlimit
)
273 self
._add
_songs
_directory
(songs
, trackLabelFormat
=NAME_ALBUM_ARTIST_LABEL
)
275 dialog
= xbmcgui
.Dialog()
276 dialog
.ok('Grooveshark XBMC', 'No popular songs.')
279 dialog
= xbmcgui
.Dialog()
280 dialog
.ok('Grooveshark XBMC', 'No matching artists.')
286 def playSong(self
, item
):
287 songid
= item
.getProperty('songid')
288 song
= groovesharkApi
.getSongURLFromSongID(songid
)
291 xbmc
.log("Playing: " + song
)
292 xbmcplugin
.setResolvedUrl(handle
=int(sys
.argv
[1]), succeeded
=True, listitem
=item
)
294 xbmc
.executebuiltin('XBMC.Notification(Grooveshark XBMC, Unable to play song, 1000, ' + thumbDef
+ ')')
296 # Make a song directory item
297 def songItem(self
, songid
, name
, album
, artist
, coverart
, trackLabelFormat
=ARTIST_ALBUM_NAME_LABEL
):
298 songImg
= self
._get
_icon
(coverart
, 'song-' + str(songid
) + "-image")
299 if trackLabelFormat
== NAME_ALBUM_ARTIST_LABEL
:
300 trackLabel
= name
+ " - " + album
+ " - " + artist
302 trackLabel
= artist
+ " - " + album
+ " - " + name
303 item
= xbmcgui
.ListItem(label
= trackLabel
, thumbnailImage
=songImg
, iconImage
=songImg
)
304 item
.setInfo( type="music", infoLabels
={ "title": name
, "album": album
, "artist": artist
} )
305 item
.setProperty('mimetype', 'audio/mpeg')
306 item
.setProperty("IsPlayable", "true")
307 item
.setProperty('songid', str(songid
))
308 item
.setProperty('coverart', songImg
)
309 item
.setProperty('title', name
)
310 item
.setProperty('album', album
)
311 item
.setProperty('artist', artist
)
316 def songPage(self
, page
, trackLabelFormat
):
317 self
._add
_songs
_directory
([], trackLabelFormat
, page
)
320 def _get_keyboard(self
, default
="", heading
="", hidden
=False):
321 kb
= xbmc
.Keyboard(default
, heading
, hidden
)
323 if (kb
.isConfirmed()):
324 return unicode(kb
.getText(), "utf-8")
327 # Login to grooveshark
328 def _get_login(self
):
329 if (self
.username
== "" or self
.password
== ""):
330 dialog
= xbmcgui
.Dialog()
331 dialog
.ok('Grooveshark XBMC', 'Unable to login.', 'Check username and password in settings.')
335 uid
= groovesharkApi
.login(self
.username
, self
.password
)
339 dialog
= xbmcgui
.Dialog()
340 dialog
.ok('Grooveshark XBMC', 'Unable to login.', 'Check username and password in settings.')
343 # Get a song directory item
344 def _get_song_item(self
, song
, trackLabelFormat
):
350 return self
.songItem(songid
, name
, album
, artist
, coverart
, trackLabelFormat
)
353 def _get_icon(self
, url
, songid
):
355 localThumb
= os
.path
.join(xbmc
.translatePath(os
.path
.join(thumbDir
, str(songid
)))) + '.tbn'
357 if os
.path
.isfile(localThumb
) == False:
358 loc
= urllib
.URLopener()
359 loc
.retrieve(url
, localThumb
)
361 shutil
.copy2(thumbDef
, localThumb
)
362 return os
.path
.join(os
.path
.join(thumbDir
, str(songid
))) + '.tbn'
366 # Add songs to directory
367 def _add_songs_directory(self
, songs
, trackLabelFormat
=ARTIST_ALBUM_NAME_LABEL
, page
=0):
369 totalSongs
= len(songs
)
373 if page
== 0 and totalSongs
<= self
.songspagelimit
:
374 xbmc
.log("Found " + str(totalSongs
) + " songs...")
379 self
._setSavedSongs
(songs
)
381 songs
= self
._getSavedSongs
()
382 totalSongs
= len(songs
)
385 start
= page
* self
.songspagelimit
386 end
= start
+ self
.songspagelimit
387 songs
= songs
[start
:end
]
390 item
= self
._get
_song
_item
(song
, trackLabelFormat
)
391 coverart
= item
.getProperty('coverart')
396 u
=sys
.argv
[0]+"?mode="+str(MODE_SONG
)+"&name="+urllib
.quote_plus(songname
)+"&id="+str(songid
) \
397 +"&album="+urllib
.quote_plus(songalbum
) \
398 +"&artist="+urllib
.quote_plus(songartist
) \
399 +"&coverart="+urllib
.quote_plus(coverart
)
400 fav
=sys
.argv
[0]+"?mode="+str(MODE_FAVORITE
)+"&name="+urllib
.quote_plus(songname
)+"&id="+str(songid
)
402 menuItems
.append(("Grooveshark favorite", "XBMC.RunPlugin("+fav
+")"))
403 item
.addContextMenuItems(menuItems
, replaceItems
=False)
404 xbmcplugin
.addDirectoryItem(handle
=int(sys
.argv
[1]),url
=u
,listitem
=item
,isFolder
=False, totalItems
=len(songs
))
407 if totalSongs
> page
* self
.songspagelimit
:
408 u
=sys
.argv
[0]+"?mode="+str(MODE_SONG_PAGE
)+"&id=0"+"&page="+str(page
)+"&label="+str(trackLabelFormat
)
409 self
._add
_dir
('More songs...', u
, MODE_SONG_PAGE
, self
.songImg
, 0, totalSongs
- (page
* self
.songspagelimit
))
411 xbmcplugin
.setContent(self
._handle
, 'songs')
412 xbmcplugin
.setPluginFanart(int(sys
.argv
[1]), self
.fanImg
)
414 # Add albums to directory
415 def _add_albums_directory(self
, albums
):
417 xbmc
.log("Found " + str(n
) + " albums...")
421 albumArtistName
= album
[0]
424 albumImage
= self
._get
_icon
(album
[4], 'album-' + str(albumID
))
425 self
._add
_dir
(albumName
+ " - " + albumArtistName
, '', MODE_ALBUM
, albumImage
, albumID
, n
)
427 xbmcplugin
.setContent(self
._handle
, 'albums')
428 xbmcplugin
.addSortMethod(self
._handle
, xbmcplugin
.SORT_METHOD_ALBUM_IGNORE_THE
)
429 xbmcplugin
.setPluginFanart(int(sys
.argv
[1]), self
.fanImg
)
431 # Add artists to directory
432 def _add_artists_directory(self
, artists
):
434 xbmc
.log("Found " + str(n
) + " artists...")
438 artistName
= artist
[0]
440 self
._add
_dir
(artistName
, '', MODE_ARTIST
, self
.artistImg
, artistID
, n
)
442 xbmcplugin
.setContent(self
._handle
, 'artists')
443 xbmcplugin
.addSortMethod(self
._handle
, xbmcplugin
.SORT_METHOD_ARTIST_IGNORE_THE
)
444 xbmcplugin
.setPluginFanart(int(sys
.argv
[1]), self
.fanImg
)
446 # Add playlists to directory
447 def _add_playlists_directory(self
, playlists
):
449 xbmc
.log("Found " + str(n
) + " playlists...")
452 playlist
= playlists
[i
]
453 playlistName
= playlist
[0]
454 playlistID
= playlist
[1]
455 dir = self
._add
_dir
(playlistName
, '', MODE_PLAYLIST
, self
.playlistImg
, playlistID
, n
)
457 xbmcplugin
.setContent(self
._handle
, 'files')
458 xbmcplugin
.addSortMethod(self
._handle
, xbmcplugin
.SORT_METHOD_LABEL
)
459 xbmcplugin
.setPluginFanart(int(sys
.argv
[1]), self
.fanImg
)
461 # Add whatever directory
462 def _add_dir(self
, name
, url
, mode
, iconimage
, id, items
=1):
464 u
=sys
.argv
[0]+"?mode="+str(mode
)+"&name="+urllib
.quote_plus(name
)+"&id="+str(id)
467 dir=xbmcgui
.ListItem(name
, iconImage
=iconimage
, thumbnailImage
=iconimage
)
468 dir.setInfo( type="Music", infoLabels
={ "title": name
} )
469 return xbmcplugin
.addDirectoryItem(handle
=int(sys
.argv
[1]),url
=u
,listitem
=dir,isFolder
=True, totalItems
=items
)
471 def _getSavedSongs(self
):
472 path
= os
.path
.join(cacheDir
, 'songs.dmp')
475 songs
= pickle
.load(f
)
482 def _setSavedSongs(self
, songs
):
484 # Create the 'data' directory if it doesn't exist.
485 if not os
.path
.exists(cacheDir
):
486 os
.makedirs(cacheDir
)
487 path
= os
.path
.join(cacheDir
, 'songs.dmp')
489 pickle
.dump(songs
, f
, protocol
=pickle
.HIGHEST_PROTOCOL
)
492 xbmc
.log("An error occurred saving songs")
497 # Parse URL parameters
500 paramstring
=sys
.argv
[2]
501 xbmc
.log(paramstring
)
502 if len(paramstring
)>=2:
504 cleanedparams
=params
.replace('?','')
505 if (params
[len(params
)-1]=='/'):
506 params
=params
[0:len(params
)-2]
507 pairsofparams
=cleanedparams
.split('&')
509 for i
in range(len(pairsofparams
)):
511 splitparams
=pairsofparams
[i
].split('=')
512 if (len(splitparams
))==2:
513 param
[splitparams
[0]]=splitparams
[1]
517 grooveshark
= Groveshark();
521 try: mode
=int(params
["mode"])
524 try: id=int(params
["id"])
527 # Call function for URL
529 grooveshark
.categories()
531 elif mode
==MODE_SEARCH_SONGS
:
532 grooveshark
.searchSongs()
534 elif mode
==MODE_SEARCH_ALBUMS
:
535 grooveshark
.searchAlbums()
537 elif mode
==MODE_SEARCH_ARTISTS
:
538 grooveshark
.searchArtists()
540 elif mode
==MODE_SEARCH_ARTISTS_ALBUMS
:
541 grooveshark
.searchArtistsAlbums()
543 elif mode
==MODE_SEARCH_PLAYLISTS
:
544 grooveshark
.searchPlaylists()
546 elif mode
==MODE_POPULAR_SONGS
:
547 grooveshark
.popularSongs()
549 elif mode
==MODE_ARTIST_POPULAR
:
550 grooveshark
.artistPopularSongs()
552 elif mode
==MODE_FAVORITES
:
553 grooveshark
.favorites()
555 elif mode
==MODE_PLAYLISTS
:
556 grooveshark
.playlists()
558 elif mode
==MODE_SONG_PAGE
:
559 try: page
=urllib
.unquote_plus(params
["page"])
561 try: label
=urllib
.unquote_plus(params
["label"])
563 grooveshark
.songPage(page
, label
)
565 elif mode
==MODE_SONG
:
566 try: name
=urllib
.unquote_plus(params
["name"])
568 try: album
=urllib
.unquote_plus(params
["album"])
570 try: artist
=urllib
.unquote_plus(params
["artist"])
572 try: coverart
=urllib
.unquote_plus(params
["coverart"])
574 song
= grooveshark
.songItem(id, name
, album
, artist
, coverart
)
575 grooveshark
.playSong(song
)
577 elif mode
==MODE_ARTIST
:
578 grooveshark
.artist(id)
580 elif mode
==MODE_ALBUM
:
581 grooveshark
.album(id)
583 elif mode
==MODE_PLAYLIST
:
584 grooveshark
.playlist(id)
586 elif mode
==MODE_FAVORITE
:
587 grooveshark
.favorite(id)
590 xbmcplugin
.endOfDirectory(int(sys
.argv
[1]))