97289139 |
1 | import urllib, sys, os, shutil, re, time, xbmcaddon, xbmcplugin, xbmcgui, xbmc, pickle |
8817bb2e |
2 | MODE_SEARCH_SONGS = 1 |
3 | MODE_SEARCH_ALBUMS = 2 |
4 | MODE_SEARCH_ARTISTS = 3 |
86f629ea |
5 | MODE_SEARCH_ARTISTS_ALBUMS = 4 |
6 | MODE_SEARCH_PLAYLISTS = 5 |
97289139 |
7 | MODE_ARTIST_POPULAR = 6 |
8 | MODE_POPULAR_SONGS = 7 |
9 | MODE_FAVORITES = 8 |
10 | MODE_PLAYLISTS = 9 |
11 | MODE_ALBUM = 10 |
12 | MODE_ARTIST = 11 |
13 | MODE_PLAYLIST = 12 |
14 | MODE_SONG_PAGE = 13 |
15 | MODE_SONG = 14 |
16 | MODE_FAVORITE = 15 |
7ea6f166 |
17 | |
38df1fa5 |
18 | ACTION_MOVE_LEFT = 1 |
7ea6f166 |
19 | ACTION_MOVE_UP = 3 |
20 | ACTION_MOVE_DOWN = 4 |
21 | ACTION_PAGE_UP = 5 |
22 | ACTION_PAGE_DOWN = 6 |
23 | ACTION_SELECT_ITEM = 7 |
24 | ACTION_PREVIOUS_MENU = 10 |
8817bb2e |
25 | |
86f629ea |
26 | # Formats for track labels |
27 | ARTIST_ALBUM_NAME_LABEL = 0 |
28 | NAME_ALBUM_ARTIST_LABEL = 1 |
29 | |
6ae708d0 |
30 | baseDir = os.getcwd() |
31 | resDir = xbmc.translatePath(os.path.join(baseDir, 'resources')) |
8817bb2e |
32 | libDir = xbmc.translatePath(os.path.join(resDir, 'lib')) |
33 | imgDir = xbmc.translatePath(os.path.join(resDir, 'img')) |
7ce01be6 |
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) |
4be42357 |
37 | |
38 | baseModeUrl = 'plugin://plugin.audio.groove/' |
e278f474 |
39 | playlistUrl = baseModeUrl + '?mode=' + str(MODE_PLAYLIST) |
4be42357 |
40 | playlistsUrl = baseModeUrl + '?mode=' + str(MODE_PLAYLISTS) |
41 | favoritesUrl = baseModeUrl + '?mode=' + str(MODE_FAVORITES) |
42 | |
7ce01be6 |
43 | thumbDef = os.path.join(imgDir, 'default.tbn') |
8817bb2e |
44 | |
45 | sys.path.append (libDir) |
7ce01be6 |
46 | from GroovesharkAPI import GrooveAPI |
47 | |
a3ad8f73 |
48 | try: |
49 | groovesharkApi = GrooveAPI() |
7ce01be6 |
50 | if groovesharkApi.pingService() != True: |
51 | raise StandardError('No Grooveshark service') |
a3ad8f73 |
52 | except: |
53 | dialog = xbmcgui.Dialog() |
54 | dialog.ok('Grooveshark XBMC', 'Unable to connect with Grooveshark.', 'Please try again later') |
55 | sys.exit(-1) |
56 | |
8817bb2e |
57 | |
58 | class _Info: |
59 | def __init__( self, *args, **kwargs ): |
60 | self.__dict__.update( kwargs ) |
e278f474 |
61 | |
7ce01be6 |
62 | |
8817bb2e |
63 | class Groveshark: |
973b4c6c |
64 | |
7ea6f166 |
65 | albumImg = xbmc.translatePath(os.path.join(imgDir, 'album.png')) |
66 | artistImg = xbmc.translatePath(os.path.join(imgDir, 'artist.png')) |
86f629ea |
67 | artistsAlbumsImg = xbmc.translatePath(os.path.join(imgDir, 'artistsalbums.png')) |
7ea6f166 |
68 | favoritesImg = xbmc.translatePath(os.path.join(imgDir, 'favorites.png')) |
69 | playlistImg = xbmc.translatePath(os.path.join(imgDir, 'playlist.png')) |
86f629ea |
70 | usersplaylistsImg = xbmc.translatePath(os.path.join(imgDir, 'usersplaylists.png')) |
7ea6f166 |
71 | popularSongsImg = xbmc.translatePath(os.path.join(imgDir, 'popularSongs.png')) |
97289139 |
72 | popularSongsArtistImg = xbmc.translatePath(os.path.join(imgDir, 'popularSongsArtist.png')) |
7ea6f166 |
73 | songImg = xbmc.translatePath(os.path.join(imgDir, 'song.png')) |
74 | defImg = xbmc.translatePath(os.path.join(imgDir, 'default.tbn')) |
2254a6b5 |
75 | fanImg = xbmc.translatePath(os.path.join(baseDir, 'fanart.png')) |
8817bb2e |
76 | |
2254a6b5 |
77 | settings = xbmcaddon.Addon(id='plugin.audio.groove') |
7ce01be6 |
78 | songsearchlimit = int(settings.getSetting('songsearchlimit')) |
79 | albumsearchlimit = int(settings.getSetting('albumsearchlimit')) |
80 | artistsearchlimit = int(settings.getSetting('artistsearchlimit')) |
97289139 |
81 | songspagelimit = int(settings.getSetting('songspagelimit')) |
2254a6b5 |
82 | username = settings.getSetting('username') |
83 | password = settings.getSetting('password') |
38df1fa5 |
84 | userid = 0 |
4be42357 |
85 | |
8817bb2e |
86 | def __init__( self ): |
87 | self._handle = int(sys.argv[1]) |
7ce01be6 |
88 | if os.path.isdir(cacheDir) == False: |
89 | os.makedirs(cacheDir) |
90 | xbmc.log("Made " + cacheDir) |
164e42d8 |
91 | artDir = xbmc.translatePath(thumbDir) |
92 | if os.path.isdir(artDir) == False: |
3a794693 |
93 | os.makedirs(artDir) |
94 | xbmc.log("Made " + artDir) |
b738088f |
95 | |
e278f474 |
96 | # Top-level menu |
8817bb2e |
97 | def categories(self): |
2254a6b5 |
98 | |
38df1fa5 |
99 | self.userid = self._get_login() |
b738088f |
100 | |
101 | # Setup |
6ae708d0 |
102 | xbmcplugin.setPluginFanart(int(sys.argv[1]), self.fanImg) |
6ae708d0 |
103 | |
86f629ea |
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) |
97289139 |
109 | self._add_dir('Popular songs for artist...', '', MODE_ARTIST_POPULAR, self.popularSongsArtistImg, 0) |
36cc00d7 |
110 | self._add_dir('Popular songs', '', MODE_POPULAR_SONGS, self.popularSongsImg, 0) |
38df1fa5 |
111 | if (self.userid != 0): |
86f629ea |
112 | self._add_dir('My favorites', '', MODE_FAVORITES, self.favoritesImg, 0) |
113 | self._add_dir('My playlists', '', MODE_PLAYLISTS, self.playlistImg, 0) |
e278f474 |
114 | |
115 | # Search for songs |
8817bb2e |
116 | def searchSongs(self): |
86f629ea |
117 | query = self._get_keyboard(default="", heading="Search for songs") |
7ea6f166 |
118 | if (query != ''): |
7ce01be6 |
119 | songs = groovesharkApi.getSongSearchResults(query, limit = self.songsearchlimit) |
8817bb2e |
120 | if (len(songs) > 0): |
6ae708d0 |
121 | self._add_songs_directory(songs) |
8817bb2e |
122 | else: |
123 | dialog = xbmcgui.Dialog() |
38df1fa5 |
124 | dialog.ok('Grooveshark XBMC', 'No matching songs.') |
8817bb2e |
125 | self.categories() |
7ea6f166 |
126 | else: |
127 | self.categories() |
8817bb2e |
128 | |
e278f474 |
129 | # Search for albums |
8817bb2e |
130 | def searchAlbums(self): |
86f629ea |
131 | query = self._get_keyboard(default="", heading="Search for albums") |
7ea6f166 |
132 | if (query != ''): |
7ce01be6 |
133 | albums = groovesharkApi.getAlbumSearchResults(query, limit = self.albumsearchlimit) |
8817bb2e |
134 | if (len(albums) > 0): |
6ae708d0 |
135 | self._add_albums_directory(albums) |
8817bb2e |
136 | else: |
137 | dialog = xbmcgui.Dialog() |
38df1fa5 |
138 | dialog.ok('Grooveshark XBMC', 'No matching albums.') |
8817bb2e |
139 | self.categories() |
7ea6f166 |
140 | else: |
141 | self.categories() |
8817bb2e |
142 | |
e278f474 |
143 | # Search for artists |
8817bb2e |
144 | def searchArtists(self): |
86f629ea |
145 | query = self._get_keyboard(default="", heading="Search for artists") |
7ea6f166 |
146 | if (query != ''): |
7ce01be6 |
147 | artists = groovesharkApi.getArtistSearchResults(query, limit = self.artistsearchlimit) |
8817bb2e |
148 | if (len(artists) > 0): |
6ae708d0 |
149 | self._add_artists_directory(artists) |
8817bb2e |
150 | else: |
151 | dialog = xbmcgui.Dialog() |
38df1fa5 |
152 | dialog.ok('Grooveshark XBMC', 'No matching artists.') |
8817bb2e |
153 | self.categories() |
7ea6f166 |
154 | else: |
155 | self.categories() |
86f629ea |
156 | |
157 | # Search for playlists |
158 | def searchPlaylists(self): |
97289139 |
159 | query = self._get_keyboard(default="", heading="Username") |
86f629ea |
160 | if (query != ''): |
161 | playlists = groovesharkApi.getUserPlaylistsEx(query) |
162 | if (len(playlists) > 0): |
163 | self._add_playlists_directory(playlists) |
164 | else: |
165 | dialog = xbmcgui.Dialog() |
166 | dialog.ok('Grooveshark XBMC', 'No Grooveshark playlists found.') |
167 | self.categories() |
168 | else: |
169 | self.categories() |
170 | |
171 | # Search for artists albums |
99f72740 |
172 | def searchArtistsAlbums(self, artistName = ""): |
173 | if artistName == "": |
174 | query = self._get_keyboard(default="", heading="Search for artist's albums") |
175 | else: |
176 | query = artistName |
86f629ea |
177 | if (query != ''): |
178 | artists = groovesharkApi.getArtistSearchResults(query, limit = self.artistsearchlimit) |
179 | if (len(artists) > 0): |
180 | artist = artists[0] |
181 | artistID = artist[1] |
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) |
186 | else: |
187 | dialog = xbmcgui.Dialog() |
188 | dialog.ok('Grooveshark XBMC', 'No matching albums.') |
189 | self.categories() |
190 | else: |
191 | dialog = xbmcgui.Dialog() |
192 | dialog.ok('Grooveshark XBMC', 'No matching artists.') |
193 | self.categories() |
194 | else: |
195 | self.categories() |
196 | |
e278f474 |
197 | # Get my favorites |
8817bb2e |
198 | def favorites(self): |
36cc00d7 |
199 | userid = self._get_login() |
200 | if (userid != 0): |
7ce01be6 |
201 | favorites = groovesharkApi.getUserFavoriteSongs() |
36cc00d7 |
202 | if (len(favorites) > 0): |
7ce01be6 |
203 | self._add_songs_directory(favorites) |
36cc00d7 |
204 | else: |
205 | dialog = xbmcgui.Dialog() |
38df1fa5 |
206 | dialog.ok('Grooveshark XBMC', 'You have no favorites.') |
36cc00d7 |
207 | self.categories() |
8817bb2e |
208 | |
e278f474 |
209 | # Get popular songs |
36cc00d7 |
210 | def popularSongs(self): |
7ce01be6 |
211 | popular = groovesharkApi.getPopularSongsToday(limit = self.songsearchlimit) |
8817bb2e |
212 | if (len(popular) > 0): |
6ae708d0 |
213 | self._add_songs_directory(popular) |
8817bb2e |
214 | else: |
215 | dialog = xbmcgui.Dialog() |
38df1fa5 |
216 | dialog.ok('Grooveshark XBMC', 'No popular songs.') |
8817bb2e |
217 | self.categories() |
36cc00d7 |
218 | |
e278f474 |
219 | # Get my playlists |
8817bb2e |
220 | def playlists(self): |
221 | userid = self._get_login() |
222 | if (userid != 0): |
7ce01be6 |
223 | playlists = groovesharkApi.getUserPlaylists() |
8817bb2e |
224 | if (len(playlists) > 0): |
6ae708d0 |
225 | self._add_playlists_directory(playlists) |
8817bb2e |
226 | else: |
227 | dialog = xbmcgui.Dialog() |
38df1fa5 |
228 | dialog.ok('Grooveshark XBMC', 'You have no Grooveshark playlists.') |
8817bb2e |
229 | self.categories() |
7ea6f166 |
230 | else: |
231 | dialog = xbmcgui.Dialog() |
38df1fa5 |
232 | dialog.ok('Grooveshark XBMC', 'You must be logged in ', ' to see your Grooveshark playlists.') |
7ea6f166 |
233 | |
e278f474 |
234 | # Make songs a favorite |
8817bb2e |
235 | def favorite(self, songid): |
236 | userid = self._get_login() |
237 | if (userid != 0): |
406ab447 |
238 | xbmc.log("Favorite song: " + str(songid)) |
7ce01be6 |
239 | groovesharkApi.addUserFavoriteSong(songID = songid) |
38df1fa5 |
240 | xbmc.executebuiltin('XBMC.Notification(Grooveshark XBMC, Added to favorites, 1000, ' + thumbDef + ')') |
8817bb2e |
241 | else: |
242 | dialog = xbmcgui.Dialog() |
38df1fa5 |
243 | dialog.ok('Grooveshark XBMC', 'You must be logged in', 'to add favorites.') |
e278f474 |
244 | |
245 | # Show selected album |
36cc00d7 |
246 | def album(self, albumid): |
7ce01be6 |
247 | album = groovesharkApi.getAlbumSongs(albumid, limit = self.songsearchlimit) |
86f629ea |
248 | self._add_songs_directory(album, trackLabelFormat=NAME_ALBUM_ARTIST_LABEL) |
e278f474 |
249 | |
250 | # Show selected artist |
8817bb2e |
251 | def artist(self, artistid): |
7ce01be6 |
252 | albums = groovesharkApi.getArtistAlbums(artistid, limit = self.albumsearchlimit) |
7fda21f0 |
253 | self._add_albums_directory(albums) |
8817bb2e |
254 | |
e278f474 |
255 | # Show selected playlist |
7ce01be6 |
256 | def playlist(self, playlistid): |
8817bb2e |
257 | userid = self._get_login() |
258 | if (userid != 0): |
7ce01be6 |
259 | songs = groovesharkApi.getPlaylistSongs(playlistid) |
86f629ea |
260 | self._add_songs_directory(songs, trackLabelFormat=NAME_ALBUM_ARTIST_LABEL) |
8817bb2e |
261 | else: |
262 | dialog = xbmcgui.Dialog() |
38df1fa5 |
263 | dialog.ok('Grooveshark XBMC', 'You must be logged in', 'to get Grooveshark playlists.') |
8817bb2e |
264 | |
97289139 |
265 | # Show popular songs of the artist |
266 | def artistPopularSongs(self): |
267 | query = self._get_keyboard(default="", heading="Artist") |
268 | if (query != ''): |
269 | artists = groovesharkApi.getArtistSearchResults(query, limit = self.artistsearchlimit) |
270 | if (len(artists) > 0): |
271 | artist = artists[0] |
272 | artistID = artist[1] |
273 | xbmc.log("Found " + artist[0] + "...") |
274 | songs = groovesharkApi.getArtistPopularSongs(artistID, limit = self.songsearchlimit) |
275 | if (len(songs) > 0): |
276 | self._add_songs_directory(songs, trackLabelFormat=NAME_ALBUM_ARTIST_LABEL) |
277 | else: |
278 | dialog = xbmcgui.Dialog() |
279 | dialog.ok('Grooveshark XBMC', 'No popular songs.') |
280 | self.categories() |
281 | else: |
282 | dialog = xbmcgui.Dialog() |
283 | dialog.ok('Grooveshark XBMC', 'No matching artists.') |
284 | self.categories() |
285 | else: |
286 | self.categories() |
287 | |
e278f474 |
288 | # Play a song |
6ae708d0 |
289 | def playSong(self, item): |
7ce01be6 |
290 | songid = item.getProperty('songid') |
291 | song = groovesharkApi.getSongURLFromSongID(songid) |
cbb0985e |
292 | if song != '': |
7ce01be6 |
293 | item.setPath(song) |
294 | xbmc.log("Playing: " + song) |
295 | xbmcplugin.setResolvedUrl(handle=int(sys.argv[1]), succeeded=True, listitem=item) |
296 | else: |
cbb0985e |
297 | xbmc.executebuiltin('XBMC.Notification(Grooveshark XBMC, Unable to play song, 1000, ' + thumbDef + ')') |
3fcef5ba |
298 | |
e278f474 |
299 | # Make a song directory item |
86f629ea |
300 | def songItem(self, songid, name, album, artist, coverart, trackLabelFormat=ARTIST_ALBUM_NAME_LABEL): |
7ce01be6 |
301 | songImg = self._get_icon(coverart, 'song-' + str(songid) + "-image") |
86f629ea |
302 | if trackLabelFormat == NAME_ALBUM_ARTIST_LABEL: |
303 | trackLabel = name + " - " + album + " - " + artist |
304 | else: |
305 | trackLabel = artist + " - " + album + " - " + name |
306 | item = xbmcgui.ListItem(label = trackLabel, thumbnailImage=songImg, iconImage=songImg) |
7ce01be6 |
307 | item.setInfo( type="music", infoLabels={ "title": name, "album": album, "artist": artist} ) |
2254a6b5 |
308 | item.setProperty('mimetype', 'audio/mpeg') |
309 | item.setProperty("IsPlayable", "true") |
7ce01be6 |
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) |
315 | |
6ae708d0 |
316 | return item |
2254a6b5 |
317 | |
97289139 |
318 | # Next page of songs |
319 | def songPage(self, page, trackLabelFormat): |
320 | self._add_songs_directory([], trackLabelFormat, page) |
321 | |
e278f474 |
322 | # Get keyboard input |
8817bb2e |
323 | def _get_keyboard(self, default="", heading="", hidden=False): |
3cfead3c |
324 | kb = xbmc.Keyboard(default, heading, hidden) |
325 | kb.doModal() |
326 | if (kb.isConfirmed()): |
327 | return unicode(kb.getText(), "utf-8") |
328 | return '' |
8817bb2e |
329 | |
e278f474 |
330 | # Login to grooveshark |
8817bb2e |
331 | def _get_login(self): |
2254a6b5 |
332 | if (self.username == "" or self.password == ""): |
8817bb2e |
333 | dialog = xbmcgui.Dialog() |
38df1fa5 |
334 | dialog.ok('Grooveshark XBMC', 'Unable to login.', 'Check username and password in settings.') |
8817bb2e |
335 | return 0 |
336 | else: |
38df1fa5 |
337 | if self.userid == 0: |
7ce01be6 |
338 | uid = groovesharkApi.login(self.username, self.password) |
38df1fa5 |
339 | if (uid != 0): |
38df1fa5 |
340 | return uid |
8817bb2e |
341 | else: |
342 | dialog = xbmcgui.Dialog() |
38df1fa5 |
343 | dialog.ok('Grooveshark XBMC', 'Unable to login.', 'Check username and password in settings.') |
8817bb2e |
344 | return 0 |
345 | |
e278f474 |
346 | # Get a song directory item |
86f629ea |
347 | def _get_song_item(self, song, trackLabelFormat): |
6ae708d0 |
348 | name = song[0] |
7ce01be6 |
349 | songid = song[1] |
350 | album = song[2] |
351 | artist = song[4] |
352 | coverart = song[6] |
86f629ea |
353 | return self.songItem(songid, name, album, artist, coverart, trackLabelFormat) |
6ae708d0 |
354 | |
355 | # File download |
7ce01be6 |
356 | def _get_icon(self, url, songid): |
357 | if url != 'None': |
358 | localThumb = os.path.join(xbmc.translatePath(os.path.join(thumbDir, str(songid)))) + '.tbn' |
359 | try: |
360 | if os.path.isfile(localThumb) == False: |
361 | loc = urllib.URLopener() |
362 | loc.retrieve(url, localThumb) |
363 | except: |
364 | shutil.copy2(thumbDef, localThumb) |
365 | return os.path.join(os.path.join(thumbDir, str(songid))) + '.tbn' |
366 | else: |
367 | return thumbDef |
e278f474 |
368 | |
369 | # Add songs to directory |
97289139 |
370 | def _add_songs_directory(self, songs, trackLabelFormat=ARTIST_ALBUM_NAME_LABEL, page=0): |
371 | |
372 | totalSongs = len(songs) |
373 | page = int(page) |
374 | |
375 | # No pages needed |
376 | if page == 0 and totalSongs <= self.songspagelimit: |
377 | xbmc.log("Found " + str(totalSongs) + " songs...") |
378 | # Pages |
379 | else: |
380 | # Cache all songs |
381 | if page == 0: |
382 | self._setSavedSongs(songs) |
383 | else: |
384 | songs = self._getSavedSongs() |
385 | totalSongs = len(songs) |
386 | |
387 | if totalSongs > 0: |
388 | start = page * self.songspagelimit |
389 | end = start + self.songspagelimit |
390 | songs = songs[start:end] |
391 | |
392 | for song in songs: |
86f629ea |
393 | item = self._get_song_item(song, trackLabelFormat) |
7ce01be6 |
394 | coverart = item.getProperty('coverart') |
6ae708d0 |
395 | songname = song[0] |
396 | songid = song[1] |
7ce01be6 |
397 | songalbum = song[2] |
398 | songartist = song[4] |
e278f474 |
399 | u=sys.argv[0]+"?mode="+str(MODE_SONG)+"&name="+urllib.quote_plus(songname)+"&id="+str(songid) \ |
6ae708d0 |
400 | +"&album="+urllib.quote_plus(songalbum) \ |
401 | +"&artist="+urllib.quote_plus(songartist) \ |
7ce01be6 |
402 | +"&coverart="+urllib.quote_plus(coverart) |
e278f474 |
403 | fav=sys.argv[0]+"?mode="+str(MODE_FAVORITE)+"&name="+urllib.quote_plus(songname)+"&id="+str(songid) |
6ae708d0 |
404 | menuItems = [] |
7ce01be6 |
405 | menuItems.append(("Grooveshark favorite", "XBMC.RunPlugin("+fav+")")) |
6ae708d0 |
406 | item.addContextMenuItems(menuItems, replaceItems=False) |
97289139 |
407 | xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]),url=u,listitem=item,isFolder=False, totalItems=len(songs)) |
408 | |
409 | page = page + 1 |
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)) |
cb06c186 |
413 | |
8817bb2e |
414 | xbmcplugin.setContent(self._handle, 'songs') |
31731635 |
415 | xbmcplugin.setPluginFanart(int(sys.argv[1]), self.fanImg) |
e278f474 |
416 | |
417 | # Add albums to directory |
7ce01be6 |
418 | def _add_albums_directory(self, albums): |
31731635 |
419 | n = len(albums) |
420 | xbmc.log("Found " + str(n) + " albums...") |
8817bb2e |
421 | i = 0 |
31731635 |
422 | while i < n: |
8817bb2e |
423 | album = albums[i] |
424 | albumArtistName = album[0] |
425 | albumName = album[2] |
426 | albumID = album[3] |
2254a6b5 |
427 | albumImage = self._get_icon(album[4], 'album-' + str(albumID)) |
31731635 |
428 | self._add_dir(albumName + " - " + albumArtistName, '', MODE_ALBUM, albumImage, albumID, n) |
8817bb2e |
429 | i = i + 1 |
430 | xbmcplugin.setContent(self._handle, 'albums') |
431 | xbmcplugin.addSortMethod(self._handle, xbmcplugin.SORT_METHOD_ALBUM_IGNORE_THE) |
31731635 |
432 | xbmcplugin.setPluginFanart(int(sys.argv[1]), self.fanImg) |
e278f474 |
433 | |
434 | # Add artists to directory |
6ae708d0 |
435 | def _add_artists_directory(self, artists): |
31731635 |
436 | n = len(artists) |
437 | xbmc.log("Found " + str(n) + " artists...") |
8817bb2e |
438 | i = 0 |
31731635 |
439 | while i < n: |
8817bb2e |
440 | artist = artists[i] |
441 | artistName = artist[0] |
442 | artistID = artist[1] |
31731635 |
443 | self._add_dir(artistName, '', MODE_ARTIST, self.artistImg, artistID, n) |
8817bb2e |
444 | i = i + 1 |
445 | xbmcplugin.setContent(self._handle, 'artists') |
446 | xbmcplugin.addSortMethod(self._handle, xbmcplugin.SORT_METHOD_ARTIST_IGNORE_THE) |
31731635 |
447 | xbmcplugin.setPluginFanart(int(sys.argv[1]), self.fanImg) |
e278f474 |
448 | |
449 | # Add playlists to directory |
6ae708d0 |
450 | def _add_playlists_directory(self, playlists): |
31731635 |
451 | n = len(playlists) |
452 | xbmc.log("Found " + str(n) + " playlists...") |
8817bb2e |
453 | i = 0 |
31731635 |
454 | while i < n: |
8817bb2e |
455 | playlist = playlists[i] |
456 | playlistName = playlist[0] |
457 | playlistID = playlist[1] |
86f629ea |
458 | dir = self._add_dir(playlistName, '', MODE_PLAYLIST, self.playlistImg, playlistID, n) |
8817bb2e |
459 | i = i + 1 |
460 | xbmcplugin.setContent(self._handle, 'files') |
86f629ea |
461 | xbmcplugin.addSortMethod(self._handle, xbmcplugin.SORT_METHOD_LABEL) |
31731635 |
462 | xbmcplugin.setPluginFanart(int(sys.argv[1]), self.fanImg) |
3fcef5ba |
463 | |
e278f474 |
464 | # Add whatever directory |
31731635 |
465 | def _add_dir(self, name, url, mode, iconimage, id, items=1): |
97289139 |
466 | if url == '': |
467 | u=sys.argv[0]+"?mode="+str(mode)+"&name="+urllib.quote_plus(name)+"&id="+str(id) |
468 | else: |
469 | u = url |
8817bb2e |
470 | dir=xbmcgui.ListItem(name, iconImage=iconimage, thumbnailImage=iconimage) |
b738088f |
471 | dir.setInfo( type="Music", infoLabels={ "title": name } ) |
31731635 |
472 | return xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]),url=u,listitem=dir,isFolder=True, totalItems=items) |
97289139 |
473 | |
474 | def _getSavedSongs(self): |
475 | path = os.path.join(cacheDir, 'songs.dmp') |
476 | try: |
477 | f = open(path, 'rb') |
478 | songs = pickle.load(f) |
479 | f.close() |
480 | except: |
481 | songs = [] |
482 | pass |
483 | return songs |
484 | |
485 | def _setSavedSongs(self, songs): |
486 | try: |
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') |
491 | f = open(path, 'wb') |
492 | pickle.dump(songs, f, protocol=pickle.HIGHEST_PROTOCOL) |
493 | f.close() |
494 | except: |
495 | xbmc.log("An error occurred saving songs") |
496 | pass |
497 | |
6ae708d0 |
498 | |
e278f474 |
499 | |
500 | # Parse URL parameters |
8817bb2e |
501 | def get_params(): |
502 | param=[] |
503 | paramstring=sys.argv[2] |
e278f474 |
504 | xbmc.log(paramstring) |
8817bb2e |
505 | if len(paramstring)>=2: |
506 | params=sys.argv[2] |
507 | cleanedparams=params.replace('?','') |
508 | if (params[len(params)-1]=='/'): |
509 | params=params[0:len(params)-2] |
510 | pairsofparams=cleanedparams.split('&') |
511 | param={} |
512 | for i in range(len(pairsofparams)): |
513 | splitparams={} |
514 | splitparams=pairsofparams[i].split('=') |
515 | if (len(splitparams))==2: |
516 | param[splitparams[0]]=splitparams[1] |
8817bb2e |
517 | return param |
518 | |
e278f474 |
519 | # Main |
8817bb2e |
520 | grooveshark = Groveshark(); |
e278f474 |
521 | |
8817bb2e |
522 | params=get_params() |
8817bb2e |
523 | mode=None |
8817bb2e |
524 | try: mode=int(params["mode"]) |
525 | except: pass |
7ce01be6 |
526 | id=0 |
527 | try: id=int(params["id"]) |
528 | except: pass |
e278f474 |
529 | |
530 | # Call function for URL |
8817bb2e |
531 | if mode==None: |
532 | grooveshark.categories() |
533 | |
534 | elif mode==MODE_SEARCH_SONGS: |
535 | grooveshark.searchSongs() |
536 | |
537 | elif mode==MODE_SEARCH_ALBUMS: |
538 | grooveshark.searchAlbums() |
539 | |
540 | elif mode==MODE_SEARCH_ARTISTS: |
541 | grooveshark.searchArtists() |
86f629ea |
542 | |
543 | elif mode==MODE_SEARCH_ARTISTS_ALBUMS: |
544 | grooveshark.searchArtistsAlbums() |
545 | |
546 | elif mode==MODE_SEARCH_PLAYLISTS: |
547 | grooveshark.searchPlaylists() |
8817bb2e |
548 | |
36cc00d7 |
549 | elif mode==MODE_POPULAR_SONGS: |
550 | grooveshark.popularSongs() |
97289139 |
551 | |
552 | elif mode==MODE_ARTIST_POPULAR: |
99f72740 |
553 | try: name=urllib.unquote_plus(params["name"]) |
554 | except: |
555 | name = "" |
556 | pass |
557 | grooveshark.artistPopularSongs(name) |
8817bb2e |
558 | |
8817bb2e |
559 | elif mode==MODE_FAVORITES: |
560 | grooveshark.favorites() |
561 | |
e278f474 |
562 | elif mode==MODE_PLAYLISTS: |
563 | grooveshark.playlists() |
97289139 |
564 | |
565 | elif mode==MODE_SONG_PAGE: |
566 | try: page=urllib.unquote_plus(params["page"]) |
567 | except: pass |
568 | try: label=urllib.unquote_plus(params["label"]) |
569 | except: pass |
570 | grooveshark.songPage(page, label) |
e278f474 |
571 | |
8817bb2e |
572 | elif mode==MODE_SONG: |
7ce01be6 |
573 | try: name=urllib.unquote_plus(params["name"]) |
574 | except: pass |
8817bb2e |
575 | try: album=urllib.unquote_plus(params["album"]) |
576 | except: pass |
577 | try: artist=urllib.unquote_plus(params["artist"]) |
578 | except: pass |
7ce01be6 |
579 | try: coverart=urllib.unquote_plus(params["coverart"]) |
8817bb2e |
580 | except: pass |
7ce01be6 |
581 | song = grooveshark.songItem(id, name, album, artist, coverart) |
3fcef5ba |
582 | grooveshark.playSong(song) |
8817bb2e |
583 | |
584 | elif mode==MODE_ARTIST: |
4be42357 |
585 | grooveshark.artist(id) |
8817bb2e |
586 | |
587 | elif mode==MODE_ALBUM: |
4be42357 |
588 | grooveshark.album(id) |
8817bb2e |
589 | |
590 | elif mode==MODE_PLAYLIST: |
7ce01be6 |
591 | grooveshark.playlist(id) |
8817bb2e |
592 | |
593 | elif mode==MODE_FAVORITE: |
4be42357 |
594 | grooveshark.favorite(id) |
97289139 |
595 | |
e278f474 |
596 | if mode < MODE_SONG: |
8817bb2e |
597 | xbmcplugin.endOfDirectory(int(sys.argv[1])) |