1 import urllib
, urllib2
, re
, xbmcplugin
, xbmcgui
, xbmc
, sys
, os
, time
, pprint
, shutil
, xbmcaddon
5 MODE_SEARCH_ARTISTS
= 3
12 MODE_SIMILAR_ARTISTS
= 10
16 MODE_MAKE_PLAYLIST
= 14
17 MODE_REMOVE_PLAYLIST
= 15
18 MODE_RENAME_PLAYLIST
= 16
23 resDir
= xbmc
.translatePath(os
.path
.join(baseDir
, 'resources'))
24 libDir
= xbmc
.translatePath(os
.path
.join(resDir
, 'lib'))
25 imgDir
= xbmc
.translatePath(os
.path
.join(resDir
, 'img'))
26 thumbDir
= os
.path
.join('special://masterprofile/addon_data/', os
.path
.join(os
.path
.basename(os
.getcwd()), 'thumb'))
28 sys
.path
.append (libDir
)
29 from GrooveAPI
import *
30 groovesharkApi
= GrooveAPI()
33 def __init__( self
, *args
, **kwargs
):
34 self
.__dict
__.update( kwargs
)
38 albumImg
= xbmc
.translatePath(os
.path
.join(imgDir
, 'album.png'))
39 artistImg
= xbmc
.translatePath(os
.path
.join(imgDir
, 'artist.png'))
40 favoritesImg
= xbmc
.translatePath(os
.path
.join(imgDir
, 'favorites.png'))
41 playlistImg
= xbmc
.translatePath(os
.path
.join(imgDir
, 'playlist.png'))
42 popularSongsImg
= xbmc
.translatePath(os
.path
.join(imgDir
, 'popularSongs.png'))
43 songImg
= xbmc
.translatePath(os
.path
.join(imgDir
, 'song.png'))
44 defImg
= xbmc
.translatePath(os
.path
.join(imgDir
, 'default.tbn'))
45 fanImg
= xbmc
.translatePath(os
.path
.join(baseDir
, 'fanart.png'))
47 settings
= xbmcaddon
.Addon(id='plugin.audio.groove')
48 songsearchlimit
= settings
.getSetting('songsearchlimit')
49 albumsearchlimit
= settings
.getSetting('albumsearchlimit')
50 artistsearchlimit
= settings
.getSetting('artistsearchlimit')
51 username
= settings
.getSetting('username')
52 password
= settings
.getSetting('password')
55 self
._handle
= int(sys
.argv
[1])
59 xbmc
.log(self
.username
+ ", limits: " + str(self
.songsearchlimit
) + ", " + str(self
.albumsearchlimit
) + ", " + str(self
.artistsearchlimit
))
61 userid
= self
._get
_login
()
64 groovesharkApi
.setRemoveDuplicates(True)
65 xbmcplugin
.setPluginFanart(int(sys
.argv
[1]), self
.fanImg
)
66 if os
.path
.isdir(thumbDir
) == False:
69 self
._add
_dir
('Search songs', '', MODE_SEARCH_SONGS
, self
.songImg
, 0)
70 self
._add
_dir
('Search albums', '', MODE_SEARCH_ALBUMS
, self
.albumImg
, 0)
71 self
._add
_dir
('Search artists', '', MODE_SEARCH_ARTISTS
, self
.artistImg
, 0)
72 self
._add
_dir
('Popular songs', '', MODE_POPULAR_SONGS
, self
.popularSongsImg
, 0)
74 self
._add
_dir
('Favorites', '', MODE_FAVORITES
, self
.favoritesImg
, 0)
75 self
._add
_dir
('Playlists', '', MODE_PLAYLISTS
, self
.playlistImg
, 0)
77 def searchSongs(self
):
78 query
= self
._get
_keyboard
(default
="", heading
="Search songs")
80 songs
= groovesharkApi
.searchSongs(query
, limit
= self
.songsearchlimit
)
82 self
._add
_songs
_directory
(songs
)
84 dialog
= xbmcgui
.Dialog()
85 dialog
.ok('Grooveshark', 'No matching songs.')
88 def searchAlbums(self
):
89 query
= self
._get
_keyboard
(default
="", heading
="Search albums")
91 albums
= groovesharkApi
.searchAlbums(query
, limit
= self
.albumsearchlimit
)
93 self
._add
_albums
_directory
(albums
)
95 dialog
= xbmcgui
.Dialog()
96 dialog
.ok('Grooveshark', 'No matching albums.')
99 def searchArtists(self
):
100 query
= self
._get
_keyboard
(default
="", heading
="Search artists")
102 artists
= groovesharkApi
.searchArtists(query
, limit
= self
.artistsearchlimit
)
103 if (len(artists
) > 0):
104 self
._add
_artists
_directory
(artists
)
106 dialog
= xbmcgui
.Dialog()
107 dialog
.ok('Grooveshark', 'No matching artists.')
111 userid
= self
._get
_login
()
113 favorites
= groovesharkApi
.userGetFavoriteSongs(userid
)
114 if (len(favorites
) > 0):
115 self
._add
_songs
_directory
(favorites
)
117 dialog
= xbmcgui
.Dialog()
118 dialog
.ok('Grooveshark', 'You have no favorites.')
121 def popularSongs(self
):
122 popular
= groovesharkApi
.popularGetSongs(limit
= self
.songsearchlimit
)
123 if (len(popular
) > 0):
124 self
._add
_songs
_directory
(popular
)
126 dialog
= xbmcgui
.Dialog()
127 dialog
.ok('Grooveshark', 'No popular songs.')
130 def popularAlbums(self
):
131 popular
= groovesharkApi
.popularGetAlbums(limit
= self
.albumsearchlimit
)
132 if (len(popular
) > 0):
133 self
._add
_albums
_directory
(popular
)
135 dialog
= xbmcgui
.Dialog()
136 dialog
.ok('Grooveshark', 'No popular albums.')
139 def popularArtists(self
):
140 popular
= groovesharkApi
.popularGetArtists(limit
= self
.artistsearchlimit
)
141 if (len(popular
) > 0):
142 self
._add
_artists
_directory
(popular
)
144 dialog
= xbmcgui
.Dialog()
145 dialog
.ok('Grooveshark', 'No popular artists.')
149 userid
= self
._get
_login
()
151 playlists
= groovesharkApi
.userGetPlaylists()
152 if (len(playlists
) > 0):
153 self
._add
_playlists
_directory
(playlists
)
155 dialog
= xbmcgui
.Dialog()
156 dialog
.ok('Grooveshark', 'You have no playlists.')
159 def favorite(self
, songid
):
160 userid
= self
._get
_login
()
162 xbmc
.log("Favorite song: " + str(songid
))
163 groovesharkApi
.favoriteSong(songID
= songid
)
165 dialog
= xbmcgui
.Dialog()
166 dialog
.ok('Grooveshark', 'You must be logged in', 'to add favorites.')
168 def unfavorite(self
, songid
):
169 userid
= self
._get
_login
()
171 xbmc
.log("Unfavorite song: " + str(songid
))
172 groovesharkApi
.unfavoriteSong(songID
= songid
)
174 dialog
= xbmcgui
.Dialog()
175 dialog
.ok('Grooveshark', 'You must be logged in', 'to remove favorites.')
177 def frown(self
, songid
):
178 userid
= self
._get
_login
()
180 xbmc
.log("Frown song: " + str(songid
))
181 if groovesharkApi
.radioFrown(songId
= songid
) != True:
182 xbmc
.log("Unable to frown song " + str(songid
))
184 dialog
= xbmcgui
.Dialog()
185 dialog
.ok('Grooveshark', 'You must be logged in', 'to frown a song.')
187 def similarArtists(self
, artistId
):
188 similar
= groovesharkApi
.artistGetSimilar(artistId
, limit
= self
.artistsearchlimit
)
189 if (len(similar
) > 0):
190 self
._add
_artists
_directory
(similar
)
192 dialog
= xbmcgui
.Dialog()
193 dialog
.ok('Grooveshark', 'No similar artists.')
196 def makePlaylist(self
, albumid
, name
):
197 userid
= self
._get
_login
()
199 name_re
= re
.compile(r
'[- ]')
200 nameTokens
= name_re
.split("- ")
201 name
= self
._get
_keyboard
(default
=nameTokens
[1], heading
="Playlist name")
202 album
= groovesharkApi
.albumGetSongs(albumid
, self
.songsearchlimit
)
205 songids
.append(song
[1])
206 id = groovesharkApi
.playlistCreateUnique(name
, songids
)
208 dialog
= xbmcgui
.Dialog()
209 dialog
.ok('Grooveshark', 'Cannot create playlist ', name
)
211 dialog
= xbmcgui
.Dialog()
212 dialog
.ok('Grooveshark', 'You must be logged in ', ' to create a playlist.')
214 def removePlaylist(self
, playlistid
, name
):
215 dialog
= xbmcgui
.Dialog()
216 if dialog
.yesno('Grooveshark', name
, 'Delete this playlist?') == True:
217 userid
= self
._get
_login
()
219 groovesharkApi
.playlistDelete(playlistid
)
220 xbmc
.executebuiltin("Container.Refresh")
222 dialog
= xbmcgui
.Dialog()
223 dialog
.ok('Grooveshark', 'You must be logged in ', ' to delete a playlist.')
225 def renamePlaylist(self
, playlistid
, name
):
226 userid
= self
._get
_login
()
228 newname
= self
._get
_keyboard
(default
=name
, heading
="Playlist name")
229 if groovesharkApi
.playlistRename(playlistid
, newname
) == 0:
230 dialog
= xbmcgui
.Dialog()
231 dialog
.ok('Grooveshark', 'Cannot rename playlist ', name
)
233 xbmc
.executebuiltin("Container.Refresh")
235 dialog
= xbmcgui
.Dialog()
236 dialog
.ok('Grooveshark', 'You must be logged in ', ' to rename a playlist.')
238 def album(self
, albumid
):
239 album
= groovesharkApi
.albumGetSongs(albumId
= albumid
, limit
= self
.songsearchlimit
)
240 self
._add
_songs
_directory
(album
)
242 def artist(self
, artistid
):
243 albums
= groovesharkApi
.artistGetAlbums(artistId
= artistid
, limit
= self
.albumsearchlimit
)
244 self
._add
_albums
_directory
(albums
, artistid
)
246 def playlist(self
, playlistid
):
247 userid
= self
._get
_login
()
249 songs
= groovesharkApi
.playlistGetSongs(playlistId
= playlistid
, limit
= self
.songsearchlimit
)
250 self
._add
_songs
_directory
(songs
)
252 dialog
= xbmcgui
.Dialog()
253 dialog
.ok('Grooveshark', 'You must be logged in', 'to get playlists.')
255 def playSong(self
, item
):
256 url
= item
.getProperty('url')
257 xbmc
.log("Playing: " + url
)
258 xbmcplugin
.setResolvedUrl(handle
=int(sys
.argv
[1]), succeeded
=True, listitem
=item
)
260 def songItem(self
, id, name
, album
, artist
, duration
, thumb
, image
):
261 url
= groovesharkApi
.getStreamURL(id)
262 # Only try to get the image
264 songImg
= self
._get
_icon
(image
, 'song-' + str(id) + "-image")
267 songThm
= self
._get
_icon
(thumb
, 'song-' + str(id) + "-thumb")
269 item
= xbmcgui
.ListItem(label
= artist
+ " - " + album
+ " - " + name
, path
=url
, thumbnailImage
=songThm
, iconImage
=songImg
)
270 item
.setInfo( type="music", infoLabels
={ "title": name
, "duration": duration
, "album": album
, "artist": artist
} )
271 item
.setProperty('mimetype', 'audio/mpeg')
272 item
.setProperty("IsPlayable", "true")
273 item
.setProperty('url', url
)
274 item
.setProperty('thumb', songThm
)
275 item
.setProperty('image', songImg
)
279 def _get_keyboard(self
, default
="", heading
="", hidden
=False):
280 kb
= xbmc
.Keyboard(default
, heading
, hidden
)
282 if (kb
.isConfirmed()):
283 return unicode(kb
.getText(), "utf-8")
286 def _get_login(self
):
287 if (self
.username
== "" or self
.password
== ""):
288 dialog
= xbmcgui
.Dialog()
289 dialog
.ok('Grooveshark', 'Unable to login.', 'Check username and password in settings.')
292 if groovesharkApi
.loggedInStatus() == 1:
293 groovesharkApi
.logout()
294 userid
= groovesharkApi
.loginExt(self
.username
, self
.password
)
296 xbmc
.log("Logged in")
299 dialog
= xbmcgui
.Dialog()
300 dialog
.ok('Grooveshark', 'Unable to login.', 'Check username and password in settings.')
303 def _get_song_item(self
, song
):
311 return self
.songItem(id, name
, album
, artist
, duration
, thumb
, image
)
314 def _get_icon(self
, url
, id):
315 localThumb
= os
.path
.join(xbmc
.translatePath(os
.path
.join(thumbDir
, str(id)))) + '.tbn'
317 if os
.path
.isfile(localThumb
) == False:
318 xbmc
.log('Downloading ' + url
+ ' to ' + localThumb
)
319 loc
= urllib
.URLopener()
320 loc
.retrieve(url
, localThumb
)
322 xbmc
.log('URL download failed of ' + url
+ ' to ' + localThumb
)
325 return os
.path
.join(os
.path
.join(thumbDir
, str(id))) + '.tbn'
327 def _add_songs_directory(self
, songs
):
329 xbmc
.log("Found " + str(n
) + " songs...")
333 item
= self
._get
_song
_item
(song
)
334 songurl
= item
.getProperty('url')
335 songthumb
= item
.getProperty('thumb')
336 songimage
= item
.getProperty('image')
339 songduration
= song
[2]
342 u
=sys
.argv
[0]+"?url="+urllib
.quote_plus(songurl
) \
343 +"&mode="+str(MODE_SONG
)+"&name="+urllib
.quote_plus(songname
)+"&id=" \
345 +"&album="+urllib
.quote_plus(songalbum
) \
346 +"&artist="+urllib
.quote_plus(songartist
) \
347 +"&duration="+str(songduration
) \
348 +"&thumb="+urllib
.quote_plus(songthumb
) \
349 +"&image="+urllib
.quote_plus(songimage
)
350 fav
=sys
.argv
[0]+"?url="+urllib
.quote_plus(songurl
)+"&mode="+str(MODE_FAVORITE
)+"&name="+urllib
.quote_plus(songname
)+"&id="+str(songid
)
351 unfav
=sys
.argv
[0]+"?url="+urllib
.quote_plus(songurl
)+"&mode="+str(MODE_UNFAVORITE
)+"&name="+urllib
.quote_plus(songname
)+"&id="+str(songid
)
353 menuItems
.append(("Grooveshark Favorite", "XBMC.RunPlugin("+fav
+")"))
354 menuItems
.append(("Not Grooveshark Favorite", "XBMC.RunPlugin("+unfav
+")"))
355 item
.addContextMenuItems(menuItems
, replaceItems
=False)
356 xbmcplugin
.addDirectoryItem(handle
=int(sys
.argv
[1]),url
=u
,listitem
=item
,isFolder
=False,totalItems
=n
)
358 xbmcplugin
.setContent(self
._handle
, 'songs')
359 xbmcplugin
.setPluginFanart(int(sys
.argv
[1]), self
.fanImg
)
361 def _add_albums_directory(self
, albums
, artistid
=0):
363 xbmc
.log("Found " + str(n
) + " albums...")
367 albumArtistName
= album
[0]
370 albumImage
= self
._get
_icon
(album
[4], 'album-' + str(albumID
))
371 self
._add
_dir
(albumName
+ " - " + albumArtistName
, '', MODE_ALBUM
, albumImage
, albumID
, n
)
374 self
._add
_dir
('Similar artists...', '', MODE_SIMILAR_ARTISTS
, self
.artistImg
, artistid
)
375 xbmcplugin
.setContent(self
._handle
, 'albums')
376 xbmcplugin
.addSortMethod(self
._handle
, xbmcplugin
.SORT_METHOD_ALBUM_IGNORE_THE
)
377 xbmcplugin
.setPluginFanart(int(sys
.argv
[1]), self
.fanImg
)
379 def _add_artists_directory(self
, artists
):
381 xbmc
.log("Found " + str(n
) + " artists...")
385 artistName
= artist
[0]
387 self
._add
_dir
(artistName
, '', MODE_ARTIST
, self
.artistImg
, artistID
, n
)
389 xbmcplugin
.setContent(self
._handle
, 'artists')
390 xbmcplugin
.addSortMethod(self
._handle
, xbmcplugin
.SORT_METHOD_ARTIST_IGNORE_THE
)
391 xbmcplugin
.setPluginFanart(int(sys
.argv
[1]), self
.fanImg
)
393 def _add_playlists_directory(self
, playlists
):
395 xbmc
.log("Found " + str(n
) + " playlists...")
398 playlist
= playlists
[i
]
399 playlistName
= playlist
[0]
400 playlistID
= playlist
[1]
401 self
._add
_dir
(playlistName
, '', MODE_PLAYLIST
, self
.playlistImg
, playlistID
, n
)
403 xbmcplugin
.setContent(self
._handle
, 'files')
404 xbmcplugin
.addSortMethod(self
._handle
, xbmcplugin
.SORT_METHOD_PLAYLIST_ORDER
)
405 xbmcplugin
.setPluginFanart(int(sys
.argv
[1]), self
.fanImg
)
407 def _add_dir(self
, name
, url
, mode
, iconimage
, id, items
=1):
408 u
=sys
.argv
[0]+"?url="+urllib
.quote_plus(url
)+"&mode="+str(mode
)+"&name="+urllib
.quote_plus(name
)+"&id="+str(id)
409 dir=xbmcgui
.ListItem(name
, iconImage
=iconimage
, thumbnailImage
=iconimage
)
410 dir.setInfo( type="Music", infoLabels
={ "title": name
} )
411 # Codes from http://xbmc-scripting.googlecode.com/svn/trunk/Script%20Templates/common/gui/codes.py
413 menuItems
.append(("Select", "XBMC.executebuiltin(Action(7))"))
414 if mode
== MODE_ALBUM
:
415 mkplaylst
=sys
.argv
[0]+"?mode="+str(MODE_MAKE_PLAYLIST
)+"&name="+name
+"&id="+str(id)
416 menuItems
.append(("Make Grooveshark playlist", "XBMC.RunPlugin("+mkplaylst
+")"))
417 if mode
== MODE_PLAYLIST
:
418 rmplaylst
=sys
.argv
[0]+"?mode="+str(MODE_REMOVE_PLAYLIST
)+"&name="+urllib
.quote_plus(name
)+"&id="+str(id)
419 menuItems
.append(("Delete playlist", "XBMC.RunPlugin("+rmplaylst
+")"))
420 mvplaylst
=sys
.argv
[0]+"?mode="+str(MODE_RENAME_PLAYLIST
)+"&name="+urllib
.quote_plus(name
)+"&id="+str(id)
421 menuItems
.append(("Rename playlist", "XBMC.RunPlugin("+mvplaylst
+")"))
422 dir.addContextMenuItems(menuItems
, replaceItems
=True)
423 return xbmcplugin
.addDirectoryItem(handle
=int(sys
.argv
[1]),url
=u
,listitem
=dir,isFolder
=True, totalItems
=items
)
428 paramstring
=sys
.argv
[2]
429 if len(paramstring
)>=2:
431 cleanedparams
=params
.replace('?','')
432 if (params
[len(params
)-1]=='/'):
433 params
=params
[0:len(params
)-2]
434 pairsofparams
=cleanedparams
.split('&')
436 for i
in range(len(pairsofparams
)):
438 splitparams
=pairsofparams
[i
].split('=')
439 if (len(splitparams
))==2:
440 param
[splitparams
[0]]=splitparams
[1]
444 grooveshark
= Groveshark();
452 try: url
=urllib
.unquote_plus(params
["url"])
454 try: name
=urllib
.unquote_plus(params
["name"])
456 try: mode
=int(params
["mode"])
458 try: id=int(params
["id"])
465 grooveshark
.categories()
467 elif mode
==MODE_SEARCH_SONGS
:
468 grooveshark
.searchSongs()
470 elif mode
==MODE_SEARCH_ALBUMS
:
471 grooveshark
.searchAlbums()
473 elif mode
==MODE_SEARCH_ARTISTS
:
474 grooveshark
.searchArtists()
476 elif mode
==MODE_POPULAR_SONGS
:
477 grooveshark
.popularSongs()
479 elif mode
==MODE_PLAYLISTS
:
480 grooveshark
.playlists()
482 elif mode
==MODE_FAVORITES
:
483 grooveshark
.favorites()
485 elif mode
==MODE_SONG
:
486 try: name
=urllib
.unquote_plus(params
["name"])
488 try: album
=urllib
.unquote_plus(params
["album"])
490 try: artist
=urllib
.unquote_plus(params
["artist"])
492 try: duration
=int(params
["duration"])
494 try: thumb
=urllib
.unquote_plus(params
["thumb"])
496 try: image
=urllib
.unquote_plus(params
["image"])
498 song
= grooveshark
.songItem(id, name
, album
, artist
, duration
, thumb
, image
)
499 grooveshark
.playSong(song
)
501 elif mode
==MODE_ARTIST
:
502 grooveshark
.artist(lastID
)
504 elif mode
==MODE_ALBUM
:
505 grooveshark
.album(lastID
)
507 elif mode
==MODE_PLAYLIST
:
508 grooveshark
.playlist(lastID
)
510 elif mode
==MODE_FAVORITE
:
511 grooveshark
.favorite(lastID
)
513 elif mode
==MODE_UNFAVORITE
:
514 grooveshark
.unfavorite(lastID
)
516 elif mode
==MODE_SIMILAR_ARTISTS
:
517 grooveshark
.similarArtists(lastID
)
519 elif mode
==MODE_MAKE_PLAYLIST
:
520 grooveshark
.makePlaylist(lastID
, name
)
522 elif mode
==MODE_REMOVE_PLAYLIST
:
523 grooveshark
.removePlaylist(lastID
, name
)
525 elif mode
==MODE_RENAME_PLAYLIST
:
526 grooveshark
.renamePlaylist(lastID
, name
)
529 if (mode
< MODE_SONG
):
530 xbmcplugin
.endOfDirectory(int(sys
.argv
[1]))