973b4c6c |
1 | import urllib, urllib2, re, xbmcplugin, xbmcgui, xbmc, sys, os, time, pprint, shutil, xbmcaddon |
8817bb2e |
2 | |
8817bb2e |
3 | MODE_SEARCH_SONGS = 1 |
4 | MODE_SEARCH_ALBUMS = 2 |
5 | MODE_SEARCH_ARTISTS = 3 |
36cc00d7 |
6 | MODE_POPULAR_SONGS = 4 |
8817bb2e |
7 | MODE_FAVORITES = 5 |
8 | MODE_PLAYLISTS = 6 |
9 | MODE_ALBUM = 7 |
10 | MODE_ARTIST = 8 |
11 | MODE_PLAYLIST = 9 |
406ab447 |
12 | MODE_SIMILAR_ARTISTS = 10 |
13 | MODE_SONG = 11 |
14 | MODE_FAVORITE = 12 |
15 | MODE_UNFAVORITE = 13 |
36cc00d7 |
16 | MODE_MAKE_PLAYLIST = 14 |
17 | MODE_REMOVE_PLAYLIST = 15 |
18 | MODE_RENAME_PLAYLIST = 16 |
4be42357 |
19 | MODE_REMOVE_PLAYLIST_SONG = 17 |
8817bb2e |
20 | |
6ae708d0 |
21 | baseDir = os.getcwd() |
22 | resDir = xbmc.translatePath(os.path.join(baseDir, 'resources')) |
8817bb2e |
23 | libDir = xbmc.translatePath(os.path.join(resDir, 'lib')) |
24 | imgDir = xbmc.translatePath(os.path.join(resDir, 'img')) |
973b4c6c |
25 | thumbDir = os.path.join('special://masterprofile/addon_data/', os.path.join(os.path.basename(os.getcwd()), 'thumb')) |
4be42357 |
26 | |
27 | baseModeUrl = 'plugin://plugin.audio.groove/' |
28 | playlistUrl = baseModeUrl + '?url=&mode=' + str(MODE_PLAYLIST) |
29 | playlistsUrl = baseModeUrl + '?mode=' + str(MODE_PLAYLISTS) |
30 | favoritesUrl = baseModeUrl + '?mode=' + str(MODE_FAVORITES) |
31 | |
3cfead3c |
32 | thumbDef = os.path.join(os.path.basename(os.getcwd()), 'default.tbn') |
8817bb2e |
33 | |
34 | sys.path.append (libDir) |
35 | from GrooveAPI import * |
36 | groovesharkApi = GrooveAPI() |
37 | |
38 | class _Info: |
39 | def __init__( self, *args, **kwargs ): |
40 | self.__dict__.update( kwargs ) |
3fcef5ba |
41 | |
8817bb2e |
42 | class Groveshark: |
973b4c6c |
43 | |
8817bb2e |
44 | albumImg = xbmc.translatePath(os.path.join(imgDir, 'album.png')) |
45 | artistImg = xbmc.translatePath(os.path.join(imgDir, 'artist.png')) |
46 | favoritesImg = xbmc.translatePath(os.path.join(imgDir, 'favorites.png')) |
47 | playlistImg = xbmc.translatePath(os.path.join(imgDir, 'playlist.png')) |
36cc00d7 |
48 | popularSongsImg = xbmc.translatePath(os.path.join(imgDir, 'popularSongs.png')) |
8817bb2e |
49 | songImg = xbmc.translatePath(os.path.join(imgDir, 'song.png')) |
6ae708d0 |
50 | defImg = xbmc.translatePath(os.path.join(imgDir, 'default.tbn')) |
2254a6b5 |
51 | fanImg = xbmc.translatePath(os.path.join(baseDir, 'fanart.png')) |
8817bb2e |
52 | |
2254a6b5 |
53 | settings = xbmcaddon.Addon(id='plugin.audio.groove') |
973b4c6c |
54 | songsearchlimit = settings.getSetting('songsearchlimit') |
55 | albumsearchlimit = settings.getSetting('albumsearchlimit') |
56 | artistsearchlimit = settings.getSetting('artistsearchlimit') |
2254a6b5 |
57 | username = settings.getSetting('username') |
58 | password = settings.getSetting('password') |
4be42357 |
59 | |
8817bb2e |
60 | def __init__( self ): |
61 | self._handle = int(sys.argv[1]) |
b738088f |
62 | |
8817bb2e |
63 | def categories(self): |
2254a6b5 |
64 | |
65 | xbmc.log(self.username + ", limits: " + str(self.songsearchlimit) + ", " + str(self.albumsearchlimit) + ", " + str(self.artistsearchlimit)) |
b738088f |
66 | |
8817bb2e |
67 | userid = self._get_login() |
b738088f |
68 | |
69 | # Setup |
6ae708d0 |
70 | xbmcplugin.setPluginFanart(int(sys.argv[1]), self.fanImg) |
71 | if os.path.isdir(thumbDir) == False: |
72 | os.makedirs(thumbDir) |
73 | |
3fcef5ba |
74 | self._add_dir('Search songs', '', MODE_SEARCH_SONGS, self.songImg, 0) |
75 | self._add_dir('Search albums', '', MODE_SEARCH_ALBUMS, self.albumImg, 0) |
76 | self._add_dir('Search artists', '', MODE_SEARCH_ARTISTS, self.artistImg, 0) |
36cc00d7 |
77 | self._add_dir('Popular songs', '', MODE_POPULAR_SONGS, self.popularSongsImg, 0) |
8817bb2e |
78 | if (userid != 0): |
3fcef5ba |
79 | self._add_dir('Favorites', '', MODE_FAVORITES, self.favoritesImg, 0) |
80 | self._add_dir('Playlists', '', MODE_PLAYLISTS, self.playlistImg, 0) |
81 | |
8817bb2e |
82 | def searchSongs(self): |
83 | query = self._get_keyboard(default="", heading="Search songs") |
4be42357 |
84 | if (query): |
85 | groovesharkApi.setRemoveDuplicates(True) |
2254a6b5 |
86 | songs = groovesharkApi.searchSongs(query, limit = self.songsearchlimit) |
8817bb2e |
87 | if (len(songs) > 0): |
6ae708d0 |
88 | self._add_songs_directory(songs) |
8817bb2e |
89 | else: |
90 | dialog = xbmcgui.Dialog() |
91 | dialog.ok('Grooveshark', 'No matching songs.') |
92 | self.categories() |
93 | |
94 | def searchAlbums(self): |
95 | query = self._get_keyboard(default="", heading="Search albums") |
96 | if (query): |
2254a6b5 |
97 | albums = groovesharkApi.searchAlbums(query, limit = self.albumsearchlimit) |
8817bb2e |
98 | if (len(albums) > 0): |
6ae708d0 |
99 | self._add_albums_directory(albums) |
8817bb2e |
100 | else: |
101 | dialog = xbmcgui.Dialog() |
102 | dialog.ok('Grooveshark', 'No matching albums.') |
103 | self.categories() |
104 | |
105 | def searchArtists(self): |
106 | query = self._get_keyboard(default="", heading="Search artists") |
107 | if (query): |
2254a6b5 |
108 | artists = groovesharkApi.searchArtists(query, limit = self.artistsearchlimit) |
8817bb2e |
109 | if (len(artists) > 0): |
6ae708d0 |
110 | self._add_artists_directory(artists) |
8817bb2e |
111 | else: |
112 | dialog = xbmcgui.Dialog() |
113 | dialog.ok('Grooveshark', 'No matching artists.') |
114 | self.categories() |
115 | |
116 | def favorites(self): |
36cc00d7 |
117 | userid = self._get_login() |
118 | if (userid != 0): |
119 | favorites = groovesharkApi.userGetFavoriteSongs(userid) |
120 | if (len(favorites) > 0): |
4be42357 |
121 | self._add_songs_directory(favorites, isFavorites=True) |
36cc00d7 |
122 | else: |
123 | dialog = xbmcgui.Dialog() |
124 | dialog.ok('Grooveshark', 'You have no favorites.') |
125 | self.categories() |
8817bb2e |
126 | |
36cc00d7 |
127 | def popularSongs(self): |
2254a6b5 |
128 | popular = groovesharkApi.popularGetSongs(limit = self.songsearchlimit) |
8817bb2e |
129 | if (len(popular) > 0): |
6ae708d0 |
130 | self._add_songs_directory(popular) |
8817bb2e |
131 | else: |
132 | dialog = xbmcgui.Dialog() |
133 | dialog.ok('Grooveshark', 'No popular songs.') |
134 | self.categories() |
36cc00d7 |
135 | |
136 | def popularAlbums(self): |
137 | popular = groovesharkApi.popularGetAlbums(limit = self.albumsearchlimit) |
138 | if (len(popular) > 0): |
139 | self._add_albums_directory(popular) |
140 | else: |
141 | dialog = xbmcgui.Dialog() |
142 | dialog.ok('Grooveshark', 'No popular albums.') |
143 | self.categories() |
144 | |
145 | def popularArtists(self): |
146 | popular = groovesharkApi.popularGetArtists(limit = self.artistsearchlimit) |
147 | if (len(popular) > 0): |
148 | self._add_artists_directory(popular) |
149 | else: |
150 | dialog = xbmcgui.Dialog() |
151 | dialog.ok('Grooveshark', 'No popular artists.') |
152 | self.categories() |
8817bb2e |
153 | |
154 | def playlists(self): |
155 | userid = self._get_login() |
156 | if (userid != 0): |
157 | playlists = groovesharkApi.userGetPlaylists() |
158 | if (len(playlists) > 0): |
6ae708d0 |
159 | self._add_playlists_directory(playlists) |
8817bb2e |
160 | else: |
161 | dialog = xbmcgui.Dialog() |
162 | dialog.ok('Grooveshark', 'You have no playlists.') |
163 | self.categories() |
164 | |
165 | def favorite(self, songid): |
166 | userid = self._get_login() |
167 | if (userid != 0): |
406ab447 |
168 | xbmc.log("Favorite song: " + str(songid)) |
8817bb2e |
169 | groovesharkApi.favoriteSong(songID = songid) |
3cfead3c |
170 | xbmc.executebuiltin('XBMC.Notification(Grooveshark, Added to favorites, 1000, ' + thumbDef + ')') |
8817bb2e |
171 | else: |
172 | dialog = xbmcgui.Dialog() |
173 | dialog.ok('Grooveshark', 'You must be logged in', 'to add favorites.') |
174 | |
4be42357 |
175 | def unfavorite(self, songid, prevMode): |
8817bb2e |
176 | userid = self._get_login() |
177 | if (userid != 0): |
4be42357 |
178 | xbmc.log("Unfavorite song: " + str(songid) + ', previous mode was ' + str(prevMode)) |
8817bb2e |
179 | groovesharkApi.unfavoriteSong(songID = songid) |
3cfead3c |
180 | xbmc.executebuiltin('XBMC.Notification(Grooveshark, Removed from favorites, 1000, ' + thumbDef + ')') |
4be42357 |
181 | if (int(prevMode) == MODE_FAVORITES): |
182 | xbmc.executebuiltin("Container.Update(" + favoritesUrl + ",replace)") |
8817bb2e |
183 | else: |
184 | dialog = xbmcgui.Dialog() |
185 | dialog.ok('Grooveshark', 'You must be logged in', 'to remove favorites.') |
3fcef5ba |
186 | |
187 | def frown(self, songid): |
188 | userid = self._get_login() |
189 | if (userid != 0): |
406ab447 |
190 | xbmc.log("Frown song: " + str(songid)) |
3fcef5ba |
191 | if groovesharkApi.radioFrown(songId = songid) != True: |
192 | xbmc.log("Unable to frown song " + str(songid)) |
3cfead3c |
193 | else: |
194 | xbmc.executebuiltin('XBMC.Notification(Grooveshark, Frowned, 1000, ' + thumbDef + ')') |
3fcef5ba |
195 | else: |
196 | dialog = xbmcgui.Dialog() |
197 | dialog.ok('Grooveshark', 'You must be logged in', 'to frown a song.') |
198 | |
406ab447 |
199 | def similarArtists(self, artistId): |
200 | similar = groovesharkApi.artistGetSimilar(artistId, limit = self.artistsearchlimit) |
201 | if (len(similar) > 0): |
202 | self._add_artists_directory(similar) |
3fcef5ba |
203 | else: |
204 | dialog = xbmcgui.Dialog() |
406ab447 |
205 | dialog.ok('Grooveshark', 'No similar artists.') |
206 | self.categories() |
36cc00d7 |
207 | |
208 | def makePlaylist(self, albumid, name): |
209 | userid = self._get_login() |
210 | if (userid != 0): |
3cfead3c |
211 | re.split(' - ',name,1) |
212 | nameTokens = re.split(' - ',name,1) |
213 | name = self._get_keyboard(default=nameTokens[0], heading="Playlist name") |
214 | if name != '': |
4be42357 |
215 | groovesharkApi.setRemoveDuplicates(True) |
3cfead3c |
216 | album = groovesharkApi.albumGetSongs(albumid, self.songsearchlimit) |
217 | songids = [] |
218 | for song in album: |
219 | songids.append(song[1]) |
220 | id = groovesharkApi.playlistCreateUnique(name, songids) |
221 | if id == 0: |
222 | dialog = xbmcgui.Dialog() |
223 | dialog.ok('Grooveshark', 'Cannot create playlist ', name) |
224 | else: |
225 | xbmc.executebuiltin('XBMC.Notification(Grooveshark, Playlist created, 1000, ' + thumbDef + ')') |
36cc00d7 |
226 | else: |
227 | dialog = xbmcgui.Dialog() |
228 | dialog.ok('Grooveshark', 'You must be logged in ', ' to create a playlist.') |
229 | |
230 | def removePlaylist(self, playlistid, name): |
231 | dialog = xbmcgui.Dialog() |
232 | if dialog.yesno('Grooveshark', name, 'Delete this playlist?') == True: |
233 | userid = self._get_login() |
234 | if (userid != 0): |
235 | groovesharkApi.playlistDelete(playlistid) |
4be42357 |
236 | xbmc.executebuiltin("Container.Update(" + playlistsUrl + ",replace)") |
36cc00d7 |
237 | else: |
238 | dialog = xbmcgui.Dialog() |
239 | dialog.ok('Grooveshark', 'You must be logged in ', ' to delete a playlist.') |
240 | |
4be42357 |
241 | def removePlaylistSong(self, playlistid, playlistname, songpos): |
242 | dialog = xbmcgui.Dialog() |
243 | if dialog.yesno('Grooveshark', 'Delete this song from the playlist?') == True: |
244 | userid = self._get_login() |
245 | if (userid != 0): |
246 | if groovesharkApi.playlistDeleteSong(playlistid, songpos) == 0: |
247 | dialog = xbmcgui.Dialog() |
248 | dialog.ok('Grooveshark', 'Failed to remove ', ' song from playlist.') |
249 | else: |
250 | xbmc.executebuiltin("Container.Update(" + playlistUrl + "&id="+str(playlistid) + "&name=" + playlistname + ",replace)") |
251 | else: |
252 | dialog = xbmcgui.Dialog() |
253 | dialog.ok('Grooveshark', 'You must be logged in ', ' to delete a song from a playlist.') |
254 | |
36cc00d7 |
255 | def renamePlaylist(self, playlistid, name): |
256 | userid = self._get_login() |
257 | if (userid != 0): |
258 | newname = self._get_keyboard(default=name, heading="Playlist name") |
3cfead3c |
259 | if newname == '': |
260 | return |
261 | elif groovesharkApi.playlistRename(playlistid, newname) == 0: |
36cc00d7 |
262 | dialog = xbmcgui.Dialog() |
263 | dialog.ok('Grooveshark', 'Cannot rename playlist ', name) |
264 | else: |
265 | xbmc.executebuiltin("Container.Refresh") |
266 | else: |
267 | dialog = xbmcgui.Dialog() |
268 | dialog.ok('Grooveshark', 'You must be logged in ', ' to rename a playlist.') |
8817bb2e |
269 | |
36cc00d7 |
270 | def album(self, albumid): |
4be42357 |
271 | groovesharkApi.setRemoveDuplicates(True) |
2254a6b5 |
272 | album = groovesharkApi.albumGetSongs(albumId = albumid, limit = self.songsearchlimit) |
6ae708d0 |
273 | self._add_songs_directory(album) |
8817bb2e |
274 | |
275 | def artist(self, artistid): |
2254a6b5 |
276 | albums = groovesharkApi.artistGetAlbums(artistId = artistid, limit = self.albumsearchlimit) |
406ab447 |
277 | self._add_albums_directory(albums, artistid) |
8817bb2e |
278 | |
4be42357 |
279 | def playlist(self, playlistid, playlistname): |
8817bb2e |
280 | userid = self._get_login() |
281 | if (userid != 0): |
2254a6b5 |
282 | songs = groovesharkApi.playlistGetSongs(playlistId = playlistid, limit = self.songsearchlimit) |
4be42357 |
283 | self._add_songs_directory(songs, playlistid, playlistname) |
8817bb2e |
284 | else: |
285 | dialog = xbmcgui.Dialog() |
286 | dialog.ok('Grooveshark', 'You must be logged in', 'to get playlists.') |
287 | |
6ae708d0 |
288 | def playSong(self, item): |
289 | url = item.getProperty('url') |
9b1d8c96 |
290 | xbmc.log("Playing: " + url) |
2254a6b5 |
291 | xbmcplugin.setResolvedUrl(handle=int(sys.argv[1]), succeeded=True, listitem=item) |
3fcef5ba |
292 | |
6ae708d0 |
293 | def songItem(self, id, name, album, artist, duration, thumb, image): |
294 | url = groovesharkApi.getStreamURL(id) |
e6ccfeca |
295 | # Only try to get the image |
296 | if image != "": |
297 | songImg = self._get_icon(image, 'song-' + str(id) + "-image") |
298 | songThm = songImg |
299 | else: |
300 | songThm = self._get_icon(thumb, 'song-' + str(id) + "-thumb") |
301 | songImg = songThm |
6ae708d0 |
302 | item = xbmcgui.ListItem(label = artist + " - " + album + " - " + name, path=url, thumbnailImage=songThm, iconImage=songImg) |
2254a6b5 |
303 | item.setInfo( type="music", infoLabels={ "title": name, "duration": duration, "album": album, "artist": artist} ) |
304 | item.setProperty('mimetype', 'audio/mpeg') |
305 | item.setProperty("IsPlayable", "true") |
6ae708d0 |
306 | item.setProperty('url', url) |
307 | item.setProperty('thumb', songThm) |
308 | item.setProperty('image', songImg) |
2254a6b5 |
309 | |
6ae708d0 |
310 | return item |
2254a6b5 |
311 | |
8817bb2e |
312 | def _get_keyboard(self, default="", heading="", hidden=False): |
3cfead3c |
313 | kb = xbmc.Keyboard(default, heading, hidden) |
314 | kb.doModal() |
315 | if (kb.isConfirmed()): |
316 | return unicode(kb.getText(), "utf-8") |
317 | return '' |
8817bb2e |
318 | |
319 | def _get_login(self): |
2254a6b5 |
320 | if (self.username == "" or self.password == ""): |
8817bb2e |
321 | dialog = xbmcgui.Dialog() |
322 | dialog.ok('Grooveshark', 'Unable to login.', 'Check username and password in settings.') |
323 | return 0 |
324 | else: |
325 | if groovesharkApi.loggedInStatus() == 1: |
326 | groovesharkApi.logout() |
2254a6b5 |
327 | userid = groovesharkApi.loginExt(self.username, self.password) |
8817bb2e |
328 | if (userid != 0): |
329 | xbmc.log("Logged in") |
330 | return userid |
331 | else: |
332 | dialog = xbmcgui.Dialog() |
333 | dialog.ok('Grooveshark', 'Unable to login.', 'Check username and password in settings.') |
334 | return 0 |
335 | |
6ae708d0 |
336 | def _get_song_item(self, song): |
337 | name = song[0] |
338 | id = song[1] |
339 | duration = song[2] |
340 | album = song[3] |
341 | artist = song[6] |
342 | thumb = song[8] |
343 | image = song[9] |
344 | return self.songItem(id, name, album, artist, duration, thumb, image) |
345 | |
346 | # File download |
347 | def _get_icon(self, url, id): |
6ae708d0 |
348 | localThumb = os.path.join(xbmc.translatePath(os.path.join(thumbDir, str(id)))) + '.tbn' |
349 | try: |
350 | if os.path.isfile(localThumb) == False: |
e6ccfeca |
351 | xbmc.log('Downloading ' + url + ' to ' + localThumb) |
6ae708d0 |
352 | loc = urllib.URLopener() |
353 | loc.retrieve(url, localThumb) |
354 | except: |
355 | xbmc.log('URL download failed of ' + url + ' to ' + localThumb) |
2254a6b5 |
356 | return(self.defImg) |
6ae708d0 |
357 | |
358 | return os.path.join(os.path.join(thumbDir, str(id))) + '.tbn' |
359 | |
4be42357 |
360 | def _add_songs_directory(self, songs, playlistid=0, playlistname='', isFavorites=False): |
6ae708d0 |
361 | n = len(songs) |
362 | xbmc.log("Found " + str(n) + " songs...") |
8817bb2e |
363 | i = 0 |
6ae708d0 |
364 | while i < n: |
8817bb2e |
365 | song = songs[i] |
6ae708d0 |
366 | item = self._get_song_item(song) |
367 | songurl = item.getProperty('url') |
368 | songthumb = item.getProperty('thumb') |
369 | songimage = item.getProperty('image') |
370 | songname = song[0] |
371 | songid = song[1] |
372 | songduration = song[2] |
373 | songalbum = song[3] |
374 | songartist = song[6] |
6ae708d0 |
375 | u=sys.argv[0]+"?url="+urllib.quote_plus(songurl) \ |
376 | +"&mode="+str(MODE_SONG)+"&name="+urllib.quote_plus(songname)+"&id=" \ |
377 | +str(songid) \ |
378 | +"&album="+urllib.quote_plus(songalbum) \ |
379 | +"&artist="+urllib.quote_plus(songartist) \ |
380 | +"&duration="+str(songduration) \ |
381 | +"&thumb="+urllib.quote_plus(songthumb) \ |
382 | +"&image="+urllib.quote_plus(songimage) |
383 | fav=sys.argv[0]+"?url="+urllib.quote_plus(songurl)+"&mode="+str(MODE_FAVORITE)+"&name="+urllib.quote_plus(songname)+"&id="+str(songid) |
4be42357 |
384 | unfav=sys.argv[0]+"?url="+urllib.quote_plus(songurl)+"&mode="+str(MODE_UNFAVORITE)+"&name="+urllib.quote_plus(songname)+"&id="+str(songid)+"&prevmode=" |
6ae708d0 |
385 | menuItems = [] |
4be42357 |
386 | if isFavorites == True: |
387 | unfav = unfav +str(MODE_FAVORITES) |
388 | else: |
389 | menuItems.append(("Grooveshark Favorite", "XBMC.RunPlugin("+fav+")")) |
6ae708d0 |
390 | menuItems.append(("Not Grooveshark Favorite", "XBMC.RunPlugin("+unfav+")")) |
4be42357 |
391 | if playlistid > 0: |
392 | rmplaylstsong=sys.argv[0]+"?playlistid="+str(playlistid)+"&id="+str(i+1)+"&mode="+str(MODE_REMOVE_PLAYLIST_SONG)+"&name="+playlistname |
393 | menuItems.append(("Remove from playlist", "XBMC.RunPlugin("+rmplaylstsong+")")) |
6ae708d0 |
394 | item.addContextMenuItems(menuItems, replaceItems=False) |
31731635 |
395 | xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]),url=u,listitem=item,isFolder=False,totalItems=n) |
8817bb2e |
396 | i = i + 1 |
397 | xbmcplugin.setContent(self._handle, 'songs') |
31731635 |
398 | xbmcplugin.setPluginFanart(int(sys.argv[1]), self.fanImg) |
8817bb2e |
399 | |
406ab447 |
400 | def _add_albums_directory(self, albums, artistid=0): |
31731635 |
401 | n = len(albums) |
402 | xbmc.log("Found " + str(n) + " albums...") |
8817bb2e |
403 | i = 0 |
31731635 |
404 | while i < n: |
8817bb2e |
405 | album = albums[i] |
406 | albumArtistName = album[0] |
407 | albumName = album[2] |
408 | albumID = album[3] |
2254a6b5 |
409 | albumImage = self._get_icon(album[4], 'album-' + str(albumID)) |
31731635 |
410 | self._add_dir(albumName + " - " + albumArtistName, '', MODE_ALBUM, albumImage, albumID, n) |
8817bb2e |
411 | i = i + 1 |
406ab447 |
412 | if artistid > 0: |
413 | self._add_dir('Similar artists...', '', MODE_SIMILAR_ARTISTS, self.artistImg, artistid) |
8817bb2e |
414 | xbmcplugin.setContent(self._handle, 'albums') |
415 | xbmcplugin.addSortMethod(self._handle, xbmcplugin.SORT_METHOD_ALBUM_IGNORE_THE) |
31731635 |
416 | xbmcplugin.setPluginFanart(int(sys.argv[1]), self.fanImg) |
8817bb2e |
417 | |
6ae708d0 |
418 | def _add_artists_directory(self, artists): |
31731635 |
419 | n = len(artists) |
420 | xbmc.log("Found " + str(n) + " artists...") |
8817bb2e |
421 | i = 0 |
31731635 |
422 | while i < n: |
8817bb2e |
423 | artist = artists[i] |
424 | artistName = artist[0] |
425 | artistID = artist[1] |
31731635 |
426 | self._add_dir(artistName, '', MODE_ARTIST, self.artistImg, artistID, n) |
8817bb2e |
427 | i = i + 1 |
428 | xbmcplugin.setContent(self._handle, 'artists') |
429 | xbmcplugin.addSortMethod(self._handle, xbmcplugin.SORT_METHOD_ARTIST_IGNORE_THE) |
31731635 |
430 | xbmcplugin.setPluginFanart(int(sys.argv[1]), self.fanImg) |
8817bb2e |
431 | |
6ae708d0 |
432 | def _add_playlists_directory(self, playlists): |
31731635 |
433 | n = len(playlists) |
434 | xbmc.log("Found " + str(n) + " playlists...") |
8817bb2e |
435 | i = 0 |
31731635 |
436 | while i < n: |
8817bb2e |
437 | playlist = playlists[i] |
438 | playlistName = playlist[0] |
439 | playlistID = playlist[1] |
31731635 |
440 | self._add_dir(playlistName, '', MODE_PLAYLIST, self.playlistImg, playlistID, n) |
8817bb2e |
441 | i = i + 1 |
442 | xbmcplugin.setContent(self._handle, 'files') |
443 | xbmcplugin.addSortMethod(self._handle, xbmcplugin.SORT_METHOD_PLAYLIST_ORDER) |
31731635 |
444 | xbmcplugin.setPluginFanart(int(sys.argv[1]), self.fanImg) |
3fcef5ba |
445 | |
31731635 |
446 | def _add_dir(self, name, url, mode, iconimage, id, items=1): |
8817bb2e |
447 | u=sys.argv[0]+"?url="+urllib.quote_plus(url)+"&mode="+str(mode)+"&name="+urllib.quote_plus(name)+"&id="+str(id) |
448 | dir=xbmcgui.ListItem(name, iconImage=iconimage, thumbnailImage=iconimage) |
b738088f |
449 | dir.setInfo( type="Music", infoLabels={ "title": name } ) |
8817bb2e |
450 | # Codes from http://xbmc-scripting.googlecode.com/svn/trunk/Script%20Templates/common/gui/codes.py |
451 | menuItems = [] |
452 | menuItems.append(("Select", "XBMC.executebuiltin(Action(7))")) |
36cc00d7 |
453 | if mode == MODE_ALBUM: |
454 | mkplaylst=sys.argv[0]+"?mode="+str(MODE_MAKE_PLAYLIST)+"&name="+name+"&id="+str(id) |
455 | menuItems.append(("Make Grooveshark playlist", "XBMC.RunPlugin("+mkplaylst+")")) |
456 | if mode == MODE_PLAYLIST: |
457 | rmplaylst=sys.argv[0]+"?mode="+str(MODE_REMOVE_PLAYLIST)+"&name="+urllib.quote_plus(name)+"&id="+str(id) |
458 | menuItems.append(("Delete playlist", "XBMC.RunPlugin("+rmplaylst+")")) |
459 | mvplaylst=sys.argv[0]+"?mode="+str(MODE_RENAME_PLAYLIST)+"&name="+urllib.quote_plus(name)+"&id="+str(id) |
460 | menuItems.append(("Rename playlist", "XBMC.RunPlugin("+mvplaylst+")")) |
8817bb2e |
461 | dir.addContextMenuItems(menuItems, replaceItems=True) |
31731635 |
462 | return xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]),url=u,listitem=dir,isFolder=True, totalItems=items) |
6ae708d0 |
463 | |
8817bb2e |
464 | |
465 | def get_params(): |
466 | param=[] |
467 | paramstring=sys.argv[2] |
468 | if len(paramstring)>=2: |
469 | params=sys.argv[2] |
470 | cleanedparams=params.replace('?','') |
471 | if (params[len(params)-1]=='/'): |
472 | params=params[0:len(params)-2] |
473 | pairsofparams=cleanedparams.split('&') |
474 | param={} |
475 | for i in range(len(pairsofparams)): |
476 | splitparams={} |
477 | splitparams=pairsofparams[i].split('=') |
478 | if (len(splitparams))==2: |
479 | param[splitparams[0]]=splitparams[1] |
480 | print param |
481 | return param |
482 | |
483 | grooveshark = Groveshark(); |
484 | |
485 | params=get_params() |
486 | url=None |
487 | name=None |
488 | mode=None |
489 | id=None |
490 | |
491 | try: url=urllib.unquote_plus(params["url"]) |
492 | except: pass |
493 | try: name=urllib.unquote_plus(params["name"]) |
494 | except: pass |
495 | try: mode=int(params["mode"]) |
496 | except: pass |
497 | try: id=int(params["id"]) |
498 | except: pass |
499 | |
8817bb2e |
500 | if mode==None: |
501 | grooveshark.categories() |
502 | |
503 | elif mode==MODE_SEARCH_SONGS: |
504 | grooveshark.searchSongs() |
505 | |
506 | elif mode==MODE_SEARCH_ALBUMS: |
507 | grooveshark.searchAlbums() |
508 | |
509 | elif mode==MODE_SEARCH_ARTISTS: |
510 | grooveshark.searchArtists() |
511 | |
36cc00d7 |
512 | elif mode==MODE_POPULAR_SONGS: |
513 | grooveshark.popularSongs() |
8817bb2e |
514 | |
515 | elif mode==MODE_PLAYLISTS: |
516 | grooveshark.playlists() |
517 | |
518 | elif mode==MODE_FAVORITES: |
519 | grooveshark.favorites() |
520 | |
521 | elif mode==MODE_SONG: |
8817bb2e |
522 | try: album=urllib.unquote_plus(params["album"]) |
523 | except: pass |
524 | try: artist=urllib.unquote_plus(params["artist"]) |
525 | except: pass |
526 | try: duration=int(params["duration"]) |
527 | except: pass |
b738088f |
528 | try: thumb=urllib.unquote_plus(params["thumb"]) |
529 | except: pass |
530 | try: image=urllib.unquote_plus(params["image"]) |
531 | except: pass |
6ae708d0 |
532 | song = grooveshark.songItem(id, name, album, artist, duration, thumb, image) |
3fcef5ba |
533 | grooveshark.playSong(song) |
8817bb2e |
534 | |
535 | elif mode==MODE_ARTIST: |
4be42357 |
536 | grooveshark.artist(id) |
8817bb2e |
537 | |
538 | elif mode==MODE_ALBUM: |
4be42357 |
539 | grooveshark.album(id) |
8817bb2e |
540 | |
541 | elif mode==MODE_PLAYLIST: |
4be42357 |
542 | grooveshark.playlist(id, name) |
8817bb2e |
543 | |
544 | elif mode==MODE_FAVORITE: |
4be42357 |
545 | grooveshark.favorite(id) |
8817bb2e |
546 | |
547 | elif mode==MODE_UNFAVORITE: |
4be42357 |
548 | try: prevMode=urllib.unquote_plus(params["prevmode"]) |
549 | except: pass |
550 | grooveshark.unfavorite(id, prevMode) |
3fcef5ba |
551 | |
406ab447 |
552 | elif mode==MODE_SIMILAR_ARTISTS: |
4be42357 |
553 | grooveshark.similarArtists(id) |
3fcef5ba |
554 | |
36cc00d7 |
555 | elif mode==MODE_MAKE_PLAYLIST: |
4be42357 |
556 | grooveshark.makePlaylist(id, name) |
36cc00d7 |
557 | |
558 | elif mode==MODE_REMOVE_PLAYLIST: |
4be42357 |
559 | grooveshark.removePlaylist(id, name) |
36cc00d7 |
560 | |
561 | elif mode==MODE_RENAME_PLAYLIST: |
4be42357 |
562 | grooveshark.renamePlaylist(id, name) |
36cc00d7 |
563 | |
4be42357 |
564 | elif mode==MODE_REMOVE_PLAYLIST_SONG: |
565 | try: playlistID=urllib.unquote_plus(params["playlistid"]) |
566 | except: pass |
567 | grooveshark.removePlaylistSong(playlistID, name, id) |
36cc00d7 |
568 | |
8817bb2e |
569 | if (mode < MODE_SONG): |
570 | xbmcplugin.endOfDirectory(int(sys.argv[1])) |