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
, artistName
= ""):
174 query
= self
._get
_keyboard
(default
="", heading
="Search for artist's albums")
178 artists
= groovesharkApi
.getArtistSearchResults(query
, limit
= self
.artistsearchlimit
)
179 if (len(artists
) > 0):
182 xbmc
.log("Found " + artist
[0] + "...")
183 albums
= groovesharkApi
.getArtistAlbums(artistID
, limit
= self
.albumsearchlimit
)
184 if (len(albums
) > 0):
185 self
._add
_albums
_directory
(albums
)
187 dialog
= xbmcgui
.Dialog()
188 dialog
.ok('Grooveshark XBMC', 'No matching albums.')
191 dialog
= xbmcgui
.Dialog()
192 dialog
.ok('Grooveshark XBMC', 'No matching artists.')
199 userid
= self
._get
_login
()
201 favorites
= groovesharkApi
.getUserFavoriteSongs()
202 if (len(favorites
) > 0):
203 self
._add
_songs
_directory
(favorites
)
205 dialog
= xbmcgui
.Dialog()
206 dialog
.ok('Grooveshark XBMC', 'You have no favorites.')
210 def popularSongs(self
):
211 popular
= groovesharkApi
.getPopularSongsToday(limit
= self
.songsearchlimit
)
212 if (len(popular
) > 0):
213 self
._add
_songs
_directory
(popular
)
215 dialog
= xbmcgui
.Dialog()
216 dialog
.ok('Grooveshark XBMC', 'No popular songs.')
221 userid
= self
._get
_login
()
223 playlists
= groovesharkApi
.getUserPlaylists()
224 if (len(playlists
) > 0):
225 self
._add
_playlists
_directory
(playlists
)
227 dialog
= xbmcgui
.Dialog()
228 dialog
.ok('Grooveshark XBMC', 'You have no Grooveshark playlists.')
231 dialog
= xbmcgui
.Dialog()
232 dialog
.ok('Grooveshark XBMC', 'You must be logged in ', ' to see your Grooveshark playlists.')
234 # Make songs a favorite
235 def favorite(self
, songid
):
236 userid
= self
._get
_login
()
238 xbmc
.log("Favorite song: " + str(songid
))
239 groovesharkApi
.addUserFavoriteSong(songID
= songid
)
240 xbmc
.executebuiltin('XBMC.Notification(Grooveshark XBMC, Added to favorites, 1000, ' + thumbDef
+ ')')
242 dialog
= xbmcgui
.Dialog()
243 dialog
.ok('Grooveshark XBMC', 'You must be logged in', 'to add favorites.')
245 # Show selected album
246 def album(self
, albumid
):
247 album
= groovesharkApi
.getAlbumSongs(albumid
, limit
= self
.songsearchlimit
)
248 self
._add
_songs
_directory
(album
, trackLabelFormat
=NAME_ALBUM_ARTIST_LABEL
)
250 # Show selected artist
251 def artist(self
, artistid
):
252 albums
= groovesharkApi
.getArtistAlbums(artistid
, limit
= self
.albumsearchlimit
)
253 self
._add
_albums
_directory
(albums
)
255 # Show selected playlist
256 def playlist(self
, playlistid
):
257 userid
= self
._get
_login
()
259 songs
= groovesharkApi
.getPlaylistSongs(playlistid
)
260 self
._add
_songs
_directory
(songs
, trackLabelFormat
=NAME_ALBUM_ARTIST_LABEL
)
262 dialog
= xbmcgui
.Dialog()
263 dialog
.ok('Grooveshark XBMC', 'You must be logged in', 'to get Grooveshark playlists.')
265 # Show popular songs of the artist
266 def artistPopularSongs(self
):
267 query
= self
._get
_keyboard
(default
="", heading
="Artist")
269 artists
= groovesharkApi
.getArtistSearchResults(query
, limit
= self
.artistsearchlimit
)
270 if (len(artists
) > 0):
273 xbmc
.log("Found " + artist
[0] + "...")
274 songs
= groovesharkApi
.getArtistPopularSongs(artistID
, limit
= self
.songsearchlimit
)
276 self
._add
_songs
_directory
(songs
, trackLabelFormat
=NAME_ALBUM_ARTIST_LABEL
)
278 dialog
= xbmcgui
.Dialog()
279 dialog
.ok('Grooveshark XBMC', 'No popular songs.')
282 dialog
= xbmcgui
.Dialog()
283 dialog
.ok('Grooveshark XBMC', 'No matching artists.')
289 def playSong(self
, item
):
290 songid
= item
.getProperty('songid')
291 song
= groovesharkApi
.getSongURLFromSongID(songid
)
294 xbmc
.log("Playing: " + song
)
295 xbmcplugin
.setResolvedUrl(handle
=int(sys
.argv
[1]), succeeded
=True, listitem
=item
)
297 xbmc
.executebuiltin('XBMC.Notification(Grooveshark XBMC, Unable to play song, 1000, ' + thumbDef
+ ')')
299 # Make a song directory item
300 def songItem(self
, songid
, name
, album
, artist
, coverart
, trackLabelFormat
=ARTIST_ALBUM_NAME_LABEL
):
301 songImg
= self
._get
_icon
(coverart
, 'song-' + str(songid
) + "-image")
302 if trackLabelFormat
== NAME_ALBUM_ARTIST_LABEL
:
303 trackLabel
= name
+ " - " + album
+ " - " + artist
305 trackLabel
= artist
+ " - " + album
+ " - " + name
306 item
= xbmcgui
.ListItem(label
= trackLabel
, thumbnailImage
=songImg
, iconImage
=songImg
)
307 item
.setInfo( type="music", infoLabels
={ "title": name
, "album": album
, "artist": artist
} )
308 item
.setProperty('mimetype', 'audio/mpeg')
309 item
.setProperty("IsPlayable", "true")
310 item
.setProperty('songid', str(songid
))
311 item
.setProperty('coverart', songImg
)
312 item
.setProperty('title', name
)
313 item
.setProperty('album', album
)
314 item
.setProperty('artist', artist
)
319 def songPage(self
, page
, trackLabelFormat
):
320 self
._add
_songs
_directory
([], trackLabelFormat
, page
)
323 def _get_keyboard(self
, default
="", heading
="", hidden
=False):
324 kb
= xbmc
.Keyboard(default
, heading
, hidden
)
326 if (kb
.isConfirmed()):
327 return unicode(kb
.getText(), "utf-8")
330 # Login to grooveshark
331 def _get_login(self
):
332 if (self
.username
== "" or self
.password
== ""):
333 dialog
= xbmcgui
.Dialog()
334 dialog
.ok('Grooveshark XBMC', 'Unable to login.', 'Check username and password in settings.')
338 uid
= groovesharkApi
.login(self
.username
, self
.password
)
342 dialog
= xbmcgui
.Dialog()
343 dialog
.ok('Grooveshark XBMC', 'Unable to login.', 'Check username and password in settings.')
346 # Get a song directory item
347 def _get_song_item(self
, song
, trackLabelFormat
):
353 return self
.songItem(songid
, name
, album
, artist
, coverart
, trackLabelFormat
)
356 def _get_icon(self
, url
, songid
):
358 localThumb
= os
.path
.join(xbmc
.translatePath(os
.path
.join(thumbDir
, str(songid
)))) + '.tbn'
360 if os
.path
.isfile(localThumb
) == False:
361 loc
= urllib
.URLopener()
362 loc
.retrieve(url
, localThumb
)
364 shutil
.copy2(thumbDef
, localThumb
)
365 return os
.path
.join(os
.path
.join(thumbDir
, str(songid
))) + '.tbn'
369 # Add songs to directory
370 def _add_songs_directory(self
, songs
, trackLabelFormat
=ARTIST_ALBUM_NAME_LABEL
, page
=0):
372 totalSongs
= len(songs
)
376 if page
== 0 and totalSongs
<= self
.songspagelimit
:
377 xbmc
.log("Found " + str(totalSongs
) + " songs...")
382 self
._setSavedSongs
(songs
)
384 songs
= self
._getSavedSongs
()
385 totalSongs
= len(songs
)
388 start
= page
* self
.songspagelimit
389 end
= start
+ self
.songspagelimit
390 songs
= songs
[start
:end
]
393 item
= self
._get
_song
_item
(song
, trackLabelFormat
)
394 coverart
= item
.getProperty('coverart')
399 u
=sys
.argv
[0]+"?mode="+str(MODE_SONG
)+"&name="+urllib
.quote_plus(songname
)+"&id="+str(songid
) \
400 +"&album="+urllib
.quote_plus(songalbum
) \
401 +"&artist="+urllib
.quote_plus(songartist
) \
402 +"&coverart="+urllib
.quote_plus(coverart
)
403 fav
=sys
.argv
[0]+"?mode="+str(MODE_FAVORITE
)+"&name="+urllib
.quote_plus(songname
)+"&id="+str(songid
)
405 menuItems
.append(("Grooveshark favorite", "XBMC.RunPlugin("+fav
+")"))
406 item
.addContextMenuItems(menuItems
, replaceItems
=False)
407 xbmcplugin
.addDirectoryItem(handle
=int(sys
.argv
[1]),url
=u
,listitem
=item
,isFolder
=False, totalItems
=len(songs
))
410 if totalSongs
> page
* self
.songspagelimit
:
411 u
=sys
.argv
[0]+"?mode="+str(MODE_SONG_PAGE
)+"&id=0"+"&page="+str(page
)+"&label="+str(trackLabelFormat
)
412 self
._add
_dir
('More songs...', u
, MODE_SONG_PAGE
, self
.songImg
, 0, totalSongs
- (page
* self
.songspagelimit
))
414 xbmcplugin
.setContent(self
._handle
, 'songs')
415 xbmcplugin
.setPluginFanart(int(sys
.argv
[1]), self
.fanImg
)
417 # Add albums to directory
418 def _add_albums_directory(self
, albums
):
420 xbmc
.log("Found " + str(n
) + " albums...")
424 albumArtistName
= album
[0]
427 albumImage
= self
._get
_icon
(album
[4], 'album-' + str(albumID
))
428 self
._add
_dir
(albumName
+ " - " + albumArtistName
, '', MODE_ALBUM
, albumImage
, albumID
, n
)
430 xbmcplugin
.setContent(self
._handle
, 'albums')
431 xbmcplugin
.addSortMethod(self
._handle
, xbmcplugin
.SORT_METHOD_ALBUM_IGNORE_THE
)
432 xbmcplugin
.setPluginFanart(int(sys
.argv
[1]), self
.fanImg
)
434 # Add artists to directory
435 def _add_artists_directory(self
, artists
):
437 xbmc
.log("Found " + str(n
) + " artists...")
441 artistName
= artist
[0]
443 self
._add
_dir
(artistName
, '', MODE_ARTIST
, self
.artistImg
, artistID
, n
)
445 xbmcplugin
.setContent(self
._handle
, 'artists')
446 xbmcplugin
.addSortMethod(self
._handle
, xbmcplugin
.SORT_METHOD_ARTIST_IGNORE_THE
)
447 xbmcplugin
.setPluginFanart(int(sys
.argv
[1]), self
.fanImg
)
449 # Add playlists to directory
450 def _add_playlists_directory(self
, playlists
):
452 xbmc
.log("Found " + str(n
) + " playlists...")
455 playlist
= playlists
[i
]
456 playlistName
= playlist
[0]
457 playlistID
= playlist
[1]
458 dir = self
._add
_dir
(playlistName
, '', MODE_PLAYLIST
, self
.playlistImg
, playlistID
, n
)
460 xbmcplugin
.setContent(self
._handle
, 'files')
461 xbmcplugin
.addSortMethod(self
._handle
, xbmcplugin
.SORT_METHOD_LABEL
)
462 xbmcplugin
.setPluginFanart(int(sys
.argv
[1]), self
.fanImg
)
464 # Add whatever directory
465 def _add_dir(self
, name
, url
, mode
, iconimage
, id, items
=1):
467 u
=sys
.argv
[0]+"?mode="+str(mode
)+"&name="+urllib
.quote_plus(name
)+"&id="+str(id)
470 dir=xbmcgui
.ListItem(name
, iconImage
=iconimage
, thumbnailImage
=iconimage
)
471 dir.setInfo( type="Music", infoLabels
={ "title": name
} )
472 return xbmcplugin
.addDirectoryItem(handle
=int(sys
.argv
[1]),url
=u
,listitem
=dir,isFolder
=True, totalItems
=items
)
474 def _getSavedSongs(self
):
475 path
= os
.path
.join(cacheDir
, 'songs.dmp')
478 songs
= pickle
.load(f
)
485 def _setSavedSongs(self
, songs
):
487 # Create the 'data' directory if it doesn't exist.
488 if not os
.path
.exists(cacheDir
):
489 os
.makedirs(cacheDir
)
490 path
= os
.path
.join(cacheDir
, 'songs.dmp')
492 pickle
.dump(songs
, f
, protocol
=pickle
.HIGHEST_PROTOCOL
)
495 xbmc
.log("An error occurred saving songs")
500 # Parse URL parameters
503 paramstring
=sys
.argv
[2]
504 xbmc
.log(paramstring
)
505 if len(paramstring
)>=2:
507 cleanedparams
=params
.replace('?','')
508 if (params
[len(params
)-1]=='/'):
509 params
=params
[0:len(params
)-2]
510 pairsofparams
=cleanedparams
.split('&')
512 for i
in range(len(pairsofparams
)):
514 splitparams
=pairsofparams
[i
].split('=')
515 if (len(splitparams
))==2:
516 param
[splitparams
[0]]=splitparams
[1]
520 grooveshark
= Groveshark();
524 try: mode
=int(params
["mode"])
527 try: id=int(params
["id"])
530 # Call function for URL
532 grooveshark
.categories()
534 elif mode
==MODE_SEARCH_SONGS
:
535 grooveshark
.searchSongs()
537 elif mode
==MODE_SEARCH_ALBUMS
:
538 grooveshark
.searchAlbums()
540 elif mode
==MODE_SEARCH_ARTISTS
:
541 grooveshark
.searchArtists()
543 elif mode
==MODE_SEARCH_ARTISTS_ALBUMS
:
544 try: name
=urllib
.unquote_plus(params
["name"])
548 grooveshark
.searchArtistsAlbums(name
)
550 elif mode
==MODE_SEARCH_PLAYLISTS
:
551 grooveshark
.searchPlaylists()
553 elif mode
==MODE_POPULAR_SONGS
:
554 grooveshark
.popularSongs()
556 elif mode
==MODE_ARTIST_POPULAR
:
557 grooveshark
.artistPopularSongs()
559 elif mode
==MODE_FAVORITES
:
560 grooveshark
.favorites()
562 elif mode
==MODE_PLAYLISTS
:
563 grooveshark
.playlists()
565 elif mode
==MODE_SONG_PAGE
:
566 try: page
=urllib
.unquote_plus(params
["page"])
568 try: label
=urllib
.unquote_plus(params
["label"])
570 grooveshark
.songPage(page
, label
)
572 elif mode
==MODE_SONG
:
573 try: name
=urllib
.unquote_plus(params
["name"])
575 try: album
=urllib
.unquote_plus(params
["album"])
577 try: artist
=urllib
.unquote_plus(params
["artist"])
579 try: coverart
=urllib
.unquote_plus(params
["coverart"])
581 song
= grooveshark
.songItem(id, name
, album
, artist
, coverart
)
582 grooveshark
.playSong(song
)
584 elif mode
==MODE_ARTIST
:
585 grooveshark
.artist(id)
587 elif mode
==MODE_ALBUM
:
588 grooveshark
.album(id)
590 elif mode
==MODE_PLAYLIST
:
591 grooveshark
.playlist(id)
593 elif mode
==MODE_FAVORITE
:
594 grooveshark
.favorite(id)
597 xbmcplugin
.endOfDirectory(int(sys
.argv
[1]))