1 import urllib
, sys
, os
, shutil
, re
, time
, xbmcaddon
, xbmcplugin
, xbmcgui
, xbmc
4 MODE_SEARCH_ARTISTS
= 3
19 ACTION_SELECT_ITEM
= 7
20 ACTION_PREVIOUS_MENU
= 10
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 cacheDir
= os
.path
.join(xbmc
.translatePath('special://masterprofile/addon_data/'), os
.path
.basename(os
.getcwd()))
27 thumbDirName
= 'thumb'
28 thumbDir
= os
.path
.join('special://masterprofile/addon_data/', os
.path
.basename(os
.getcwd()), thumbDirName
)
30 baseModeUrl
= 'plugin://plugin.audio.groove/'
31 playlistUrl
= baseModeUrl
+ '?mode=' + str(MODE_PLAYLIST
)
32 playlistsUrl
= baseModeUrl
+ '?mode=' + str(MODE_PLAYLISTS
)
33 favoritesUrl
= baseModeUrl
+ '?mode=' + str(MODE_FAVORITES
)
35 thumbDef
= os
.path
.join(imgDir
, 'default.tbn')
37 sys
.path
.append (libDir
)
38 from GroovesharkAPI
import GrooveAPI
41 groovesharkApi
= GrooveAPI()
42 if groovesharkApi
.pingService() != True:
43 raise StandardError('No Grooveshark service')
45 dialog
= xbmcgui
.Dialog()
46 dialog
.ok('Grooveshark XBMC', 'Unable to connect with Grooveshark.', 'Please try again later')
51 def __init__( self
, *args
, **kwargs
):
52 self
.__dict
__.update( kwargs
)
57 albumImg
= xbmc
.translatePath(os
.path
.join(imgDir
, 'album.png'))
58 artistImg
= xbmc
.translatePath(os
.path
.join(imgDir
, 'artist.png'))
59 favoritesImg
= xbmc
.translatePath(os
.path
.join(imgDir
, 'favorites.png'))
60 playlistImg
= xbmc
.translatePath(os
.path
.join(imgDir
, 'playlist.png'))
61 popularSongsImg
= xbmc
.translatePath(os
.path
.join(imgDir
, 'popularSongs.png'))
62 songImg
= xbmc
.translatePath(os
.path
.join(imgDir
, 'song.png'))
63 defImg
= xbmc
.translatePath(os
.path
.join(imgDir
, 'default.tbn'))
64 fanImg
= xbmc
.translatePath(os
.path
.join(baseDir
, 'fanart.png'))
66 settings
= xbmcaddon
.Addon(id='plugin.audio.groove')
67 songsearchlimit
= int(settings
.getSetting('songsearchlimit'))
68 albumsearchlimit
= int(settings
.getSetting('albumsearchlimit'))
69 artistsearchlimit
= int(settings
.getSetting('artistsearchlimit'))
70 username
= settings
.getSetting('username')
71 password
= settings
.getSetting('password')
75 self
._handle
= int(sys
.argv
[1])
76 if os
.path
.isdir(cacheDir
) == False:
78 xbmc
.log("Made " + cacheDir
)
79 if os
.path
.isdir(thumbDir
) == False:
80 arttDir
= os
.path
.join(cacheDir
, thumbDirName
)
82 xbmc
.log("Made " + arttDir
)
87 self
.userid
= self
._get
_login
()
90 xbmcplugin
.setPluginFanart(int(sys
.argv
[1]), self
.fanImg
)
92 self
._add
_dir
('Search songs...', '', MODE_SEARCH_SONGS
, self
.songImg
, 0)
93 self
._add
_dir
('Search albums...', '', MODE_SEARCH_ALBUMS
, self
.albumImg
, 0)
94 self
._add
_dir
('Search artists...', '', MODE_SEARCH_ARTISTS
, self
.artistImg
, 0)
95 self
._add
_dir
('Popular songs', '', MODE_POPULAR_SONGS
, self
.popularSongsImg
, 0)
96 if (self
.userid
!= 0):
97 self
._add
_dir
('Favorites', '', MODE_FAVORITES
, self
.favoritesImg
, 0)
98 self
._add
_dir
('Playlists', '', MODE_PLAYLISTS
, self
.playlistImg
, 0)
101 def searchSongs(self
):
102 query
= self
._get
_keyboard
(default
="", heading
="Search songs")
104 songs
= groovesharkApi
.getSongSearchResults(query
, limit
= self
.songsearchlimit
)
106 self
._add
_songs
_directory
(songs
)
108 dialog
= xbmcgui
.Dialog()
109 dialog
.ok('Grooveshark XBMC', 'No matching songs.')
115 def searchAlbums(self
):
116 query
= self
._get
_keyboard
(default
="", heading
="Search albums")
118 albums
= groovesharkApi
.getAlbumSearchResults(query
, limit
= self
.albumsearchlimit
)
119 if (len(albums
) > 0):
120 self
._add
_albums
_directory
(albums
)
122 dialog
= xbmcgui
.Dialog()
123 dialog
.ok('Grooveshark XBMC', 'No matching albums.')
129 def searchArtists(self
):
130 query
= self
._get
_keyboard
(default
="", heading
="Search artists")
132 artists
= groovesharkApi
.getArtistSearchResults(query
, limit
= self
.artistsearchlimit
)
133 if (len(artists
) > 0):
134 self
._add
_artists
_directory
(artists
)
136 dialog
= xbmcgui
.Dialog()
137 dialog
.ok('Grooveshark XBMC', 'No matching artists.')
144 userid
= self
._get
_login
()
146 favorites
= groovesharkApi
.getUserFavoriteSongs()
147 if (len(favorites
) > 0):
148 self
._add
_songs
_directory
(favorites
)
150 dialog
= xbmcgui
.Dialog()
151 dialog
.ok('Grooveshark XBMC', 'You have no favorites.')
155 def popularSongs(self
):
156 popular
= groovesharkApi
.getPopularSongsToday(limit
= self
.songsearchlimit
)
157 if (len(popular
) > 0):
158 self
._add
_songs
_directory
(popular
)
160 dialog
= xbmcgui
.Dialog()
161 dialog
.ok('Grooveshark XBMC', 'No popular songs.')
166 userid
= self
._get
_login
()
168 playlists
= groovesharkApi
.getUserPlaylists()
169 if (len(playlists
) > 0):
170 self
._add
_playlists
_directory
(playlists
)
172 dialog
= xbmcgui
.Dialog()
173 dialog
.ok('Grooveshark XBMC', 'You have no Grooveshark playlists.')
176 dialog
= xbmcgui
.Dialog()
177 dialog
.ok('Grooveshark XBMC', 'You must be logged in ', ' to see your Grooveshark playlists.')
179 # Make songs a favorite
180 def favorite(self
, songid
):
181 userid
= self
._get
_login
()
183 xbmc
.log("Favorite song: " + str(songid
))
184 groovesharkApi
.addUserFavoriteSong(songID
= songid
)
185 xbmc
.executebuiltin('XBMC.Notification(Grooveshark XBMC, Added to favorites, 1000, ' + thumbDef
+ ')')
187 dialog
= xbmcgui
.Dialog()
188 dialog
.ok('Grooveshark XBMC', 'You must be logged in', 'to add favorites.')
190 # Show selected album
191 def album(self
, albumid
):
192 album
= groovesharkApi
.getAlbumSongs(albumid
, limit
= self
.songsearchlimit
)
193 self
._add
_songs
_directory
(album
)
195 # Show selected artist
196 def artist(self
, artistid
):
197 albums
= groovesharkApi
.getArtistAlbums(artistid
, limit
= self
.albumsearchlimit
)
198 self
._add
_albums
_directory
(albums
)
200 # Show selected playlist
201 def playlist(self
, playlistid
):
202 userid
= self
._get
_login
()
204 songs
= groovesharkApi
.getPlaylistSongs(playlistid
)
205 self
._add
_songs
_directory
(songs
)
207 dialog
= xbmcgui
.Dialog()
208 dialog
.ok('Grooveshark XBMC', 'You must be logged in', 'to get Grooveshark playlists.')
211 def playSong(self
, item
):
212 songid
= item
.getProperty('songid')
213 song
= groovesharkApi
.getSongURLFromSongID(songid
)
216 xbmc
.log("Playing: " + song
)
217 xbmcplugin
.setResolvedUrl(handle
=int(sys
.argv
[1]), succeeded
=True, listitem
=item
)
219 xbmc
.executebuiltin('XBMC.Notification(Grooveshark XBMC, Unable to play song, 1000, ' + thumbDef
+ ')')
221 # Make a song directory item
222 def songItem(self
, songid
, name
, album
, artist
, coverart
):
223 songImg
= self
._get
_icon
(coverart
, 'song-' + str(songid
) + "-image")
224 item
= xbmcgui
.ListItem(label
= artist
+ " - " + album
+ " - " + name
, thumbnailImage
=songImg
, iconImage
=songImg
)
225 item
.setInfo( type="music", infoLabels
={ "title": name
, "album": album
, "artist": artist
} )
226 item
.setProperty('mimetype', 'audio/mpeg')
227 item
.setProperty("IsPlayable", "true")
228 item
.setProperty('songid', str(songid
))
229 item
.setProperty('coverart', songImg
)
230 item
.setProperty('title', name
)
231 item
.setProperty('album', album
)
232 item
.setProperty('artist', artist
)
237 def _get_keyboard(self
, default
="", heading
="", hidden
=False):
238 kb
= xbmc
.Keyboard(default
, heading
, hidden
)
240 if (kb
.isConfirmed()):
241 return unicode(kb
.getText(), "utf-8")
244 # Login to grooveshark
245 def _get_login(self
):
246 if (self
.username
== "" or self
.password
== ""):
247 dialog
= xbmcgui
.Dialog()
248 dialog
.ok('Grooveshark XBMC', 'Unable to login.', 'Check username and password in settings.')
252 uid
= groovesharkApi
.login(self
.username
, self
.password
)
256 dialog
= xbmcgui
.Dialog()
257 dialog
.ok('Grooveshark XBMC', 'Unable to login.', 'Check username and password in settings.')
260 # Get a song directory item
261 def _get_song_item(self
, song
):
267 return self
.songItem(songid
, name
, album
, artist
, coverart
)
270 def _get_icon(self
, url
, songid
):
272 localThumb
= os
.path
.join(xbmc
.translatePath(os
.path
.join(thumbDir
, str(songid
)))) + '.tbn'
274 if os
.path
.isfile(localThumb
) == False:
275 loc
= urllib
.URLopener()
276 loc
.retrieve(url
, localThumb
)
278 shutil
.copy2(thumbDef
, localThumb
)
279 return os
.path
.join(os
.path
.join(thumbDir
, str(songid
))) + '.tbn'
283 # Add songs to directory
284 def _add_songs_directory(self
, songs
):
286 xbmc
.log("Found " + str(n
) + " songs...")
290 item
= self
._get
_song
_item
(song
)
291 coverart
= item
.getProperty('coverart')
296 u
=sys
.argv
[0]+"?mode="+str(MODE_SONG
)+"&name="+urllib
.quote_plus(songname
)+"&id="+str(songid
) \
297 +"&album="+urllib
.quote_plus(songalbum
) \
298 +"&artist="+urllib
.quote_plus(songartist
) \
299 +"&coverart="+urllib
.quote_plus(coverart
)
300 fav
=sys
.argv
[0]+"?mode="+str(MODE_FAVORITE
)+"&name="+urllib
.quote_plus(songname
)+"&id="+str(songid
)
302 menuItems
.append(("Grooveshark favorite", "XBMC.RunPlugin("+fav
+")"))
303 item
.addContextMenuItems(menuItems
, replaceItems
=False)
304 xbmcplugin
.addDirectoryItem(handle
=int(sys
.argv
[1]),url
=u
,listitem
=item
,isFolder
=False, totalItems
=n
)
307 xbmcplugin
.setContent(self
._handle
, 'songs')
308 xbmcplugin
.setPluginFanart(int(sys
.argv
[1]), self
.fanImg
)
310 # Add albums to directory
311 def _add_albums_directory(self
, albums
):
313 xbmc
.log("Found " + str(n
) + " albums...")
317 albumArtistName
= album
[0]
320 albumImage
= self
._get
_icon
(album
[4], 'album-' + str(albumID
))
321 self
._add
_dir
(albumName
+ " - " + albumArtistName
, '', MODE_ALBUM
, albumImage
, albumID
, n
)
323 xbmcplugin
.setContent(self
._handle
, 'albums')
324 xbmcplugin
.addSortMethod(self
._handle
, xbmcplugin
.SORT_METHOD_ALBUM_IGNORE_THE
)
325 xbmcplugin
.setPluginFanart(int(sys
.argv
[1]), self
.fanImg
)
327 # Add artists to directory
328 def _add_artists_directory(self
, artists
):
330 xbmc
.log("Found " + str(n
) + " artists...")
334 artistName
= artist
[0]
336 self
._add
_dir
(artistName
, '', MODE_ARTIST
, self
.artistImg
, artistID
, n
)
338 xbmcplugin
.setContent(self
._handle
, 'artists')
339 xbmcplugin
.addSortMethod(self
._handle
, xbmcplugin
.SORT_METHOD_ARTIST_IGNORE_THE
)
340 xbmcplugin
.setPluginFanart(int(sys
.argv
[1]), self
.fanImg
)
342 # Add playlists to directory
343 def _add_playlists_directory(self
, playlists
):
345 xbmc
.log("Found " + str(n
) + " playlists...")
348 playlist
= playlists
[i
]
349 playlistName
= playlist
[0]
350 playlistID
= playlist
[1]
351 self
._add
_dir
(playlistName
, '', MODE_PLAYLIST
, self
.playlistImg
, playlistID
, n
)
353 xbmcplugin
.setContent(self
._handle
, 'files')
354 xbmcplugin
.addSortMethod(self
._handle
, xbmcplugin
.SORT_METHOD_PLAYLIST_ORDER
)
355 xbmcplugin
.setPluginFanart(int(sys
.argv
[1]), self
.fanImg
)
357 # Add whatever directory
358 def _add_dir(self
, name
, url
, mode
, iconimage
, id, items
=1):
360 u
=sys
.argv
[0]+"?mode="+str(mode
)+"&name="+urllib
.quote_plus(name
)+"&id="+str(id)
361 dir=xbmcgui
.ListItem(name
, iconImage
=iconimage
, thumbnailImage
=iconimage
)
362 dir.setInfo( type="Music", infoLabels
={ "title": name
} )
363 return xbmcplugin
.addDirectoryItem(handle
=int(sys
.argv
[1]),url
=u
,listitem
=dir,isFolder
=True, totalItems
=items
)
366 # Parse URL parameters
369 paramstring
=sys
.argv
[2]
370 xbmc
.log(paramstring
)
371 if len(paramstring
)>=2:
373 cleanedparams
=params
.replace('?','')
374 if (params
[len(params
)-1]=='/'):
375 params
=params
[0:len(params
)-2]
376 pairsofparams
=cleanedparams
.split('&')
378 for i
in range(len(pairsofparams
)):
380 splitparams
=pairsofparams
[i
].split('=')
381 if (len(splitparams
))==2:
382 param
[splitparams
[0]]=splitparams
[1]
386 grooveshark
= Groveshark();
390 try: mode
=int(params
["mode"])
393 try: id=int(params
["id"])
396 # Call function for URL
398 grooveshark
.categories()
400 elif mode
==MODE_SEARCH_SONGS
:
401 grooveshark
.searchSongs()
403 elif mode
==MODE_SEARCH_ALBUMS
:
404 grooveshark
.searchAlbums()
406 elif mode
==MODE_SEARCH_ARTISTS
:
407 grooveshark
.searchArtists()
409 elif mode
==MODE_POPULAR_SONGS
:
410 grooveshark
.popularSongs()
412 elif mode
==MODE_FAVORITES
:
413 grooveshark
.favorites()
415 elif mode
==MODE_PLAYLISTS
:
416 grooveshark
.playlists()
418 elif mode
==MODE_SONG
:
419 try: name
=urllib
.unquote_plus(params
["name"])
421 try: album
=urllib
.unquote_plus(params
["album"])
423 try: artist
=urllib
.unquote_plus(params
["artist"])
425 try: coverart
=urllib
.unquote_plus(params
["coverart"])
427 song
= grooveshark
.songItem(id, name
, album
, artist
, coverart
)
428 grooveshark
.playSong(song
)
430 elif mode
==MODE_ARTIST
:
431 grooveshark
.artist(id)
433 elif mode
==MODE_ALBUM
:
434 grooveshark
.album(id)
436 elif mode
==MODE_PLAYLIST
:
437 grooveshark
.playlist(id)
439 elif mode
==MODE_FAVORITE
:
440 grooveshark
.favorite(id)
443 xbmcplugin
.endOfDirectory(int(sys
.argv
[1]))