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 |
172 | def searchArtistsAlbums(self): |
173 | query = self._get_keyboard(default="", heading="Search for artist's albums") |
174 | if (query != ''): |
175 | artists = groovesharkApi.getArtistSearchResults(query, limit = self.artistsearchlimit) |
176 | if (len(artists) > 0): |
177 | artist = artists[0] |
178 | artistID = artist[1] |
179 | xbmc.log("Found " + artist[0] + "...") |
180 | albums = groovesharkApi.getArtistAlbums(artistID, limit = self.albumsearchlimit) |
181 | if (len(albums) > 0): |
182 | self._add_albums_directory(albums) |
183 | else: |
184 | dialog = xbmcgui.Dialog() |
185 | dialog.ok('Grooveshark XBMC', 'No matching albums.') |
186 | self.categories() |
187 | else: |
188 | dialog = xbmcgui.Dialog() |
189 | dialog.ok('Grooveshark XBMC', 'No matching artists.') |
190 | self.categories() |
191 | else: |
192 | self.categories() |
193 | |
e278f474 |
194 | # Get my favorites |
8817bb2e |
195 | def favorites(self): |
36cc00d7 |
196 | userid = self._get_login() |
197 | if (userid != 0): |
7ce01be6 |
198 | favorites = groovesharkApi.getUserFavoriteSongs() |
36cc00d7 |
199 | if (len(favorites) > 0): |
7ce01be6 |
200 | self._add_songs_directory(favorites) |
36cc00d7 |
201 | else: |
202 | dialog = xbmcgui.Dialog() |
38df1fa5 |
203 | dialog.ok('Grooveshark XBMC', 'You have no favorites.') |
36cc00d7 |
204 | self.categories() |
8817bb2e |
205 | |
e278f474 |
206 | # Get popular songs |
36cc00d7 |
207 | def popularSongs(self): |
7ce01be6 |
208 | popular = groovesharkApi.getPopularSongsToday(limit = self.songsearchlimit) |
8817bb2e |
209 | if (len(popular) > 0): |
6ae708d0 |
210 | self._add_songs_directory(popular) |
8817bb2e |
211 | else: |
212 | dialog = xbmcgui.Dialog() |
38df1fa5 |
213 | dialog.ok('Grooveshark XBMC', 'No popular songs.') |
8817bb2e |
214 | self.categories() |
36cc00d7 |
215 | |
e278f474 |
216 | # Get my playlists |
8817bb2e |
217 | def playlists(self): |
218 | userid = self._get_login() |
219 | if (userid != 0): |
7ce01be6 |
220 | playlists = groovesharkApi.getUserPlaylists() |
8817bb2e |
221 | if (len(playlists) > 0): |
6ae708d0 |
222 | self._add_playlists_directory(playlists) |
8817bb2e |
223 | else: |
224 | dialog = xbmcgui.Dialog() |
38df1fa5 |
225 | dialog.ok('Grooveshark XBMC', 'You have no Grooveshark playlists.') |
8817bb2e |
226 | self.categories() |
7ea6f166 |
227 | else: |
228 | dialog = xbmcgui.Dialog() |
38df1fa5 |
229 | dialog.ok('Grooveshark XBMC', 'You must be logged in ', ' to see your Grooveshark playlists.') |
7ea6f166 |
230 | |
e278f474 |
231 | # Make songs a favorite |
8817bb2e |
232 | def favorite(self, songid): |
233 | userid = self._get_login() |
234 | if (userid != 0): |
406ab447 |
235 | xbmc.log("Favorite song: " + str(songid)) |
7ce01be6 |
236 | groovesharkApi.addUserFavoriteSong(songID = songid) |
38df1fa5 |
237 | xbmc.executebuiltin('XBMC.Notification(Grooveshark XBMC, Added to favorites, 1000, ' + thumbDef + ')') |
8817bb2e |
238 | else: |
239 | dialog = xbmcgui.Dialog() |
38df1fa5 |
240 | dialog.ok('Grooveshark XBMC', 'You must be logged in', 'to add favorites.') |
e278f474 |
241 | |
242 | # Show selected album |
36cc00d7 |
243 | def album(self, albumid): |
7ce01be6 |
244 | album = groovesharkApi.getAlbumSongs(albumid, limit = self.songsearchlimit) |
86f629ea |
245 | self._add_songs_directory(album, trackLabelFormat=NAME_ALBUM_ARTIST_LABEL) |
e278f474 |
246 | |
247 | # Show selected artist |
8817bb2e |
248 | def artist(self, artistid): |
7ce01be6 |
249 | albums = groovesharkApi.getArtistAlbums(artistid, limit = self.albumsearchlimit) |
7fda21f0 |
250 | self._add_albums_directory(albums) |
8817bb2e |
251 | |
e278f474 |
252 | # Show selected playlist |
7ce01be6 |
253 | def playlist(self, playlistid): |
8817bb2e |
254 | userid = self._get_login() |
255 | if (userid != 0): |
7ce01be6 |
256 | songs = groovesharkApi.getPlaylistSongs(playlistid) |
86f629ea |
257 | self._add_songs_directory(songs, trackLabelFormat=NAME_ALBUM_ARTIST_LABEL) |
8817bb2e |
258 | else: |
259 | dialog = xbmcgui.Dialog() |
38df1fa5 |
260 | dialog.ok('Grooveshark XBMC', 'You must be logged in', 'to get Grooveshark playlists.') |
8817bb2e |
261 | |
97289139 |
262 | # Show popular songs of the artist |
263 | def artistPopularSongs(self): |
264 | query = self._get_keyboard(default="", heading="Artist") |
265 | if (query != ''): |
266 | artists = groovesharkApi.getArtistSearchResults(query, limit = self.artistsearchlimit) |
267 | if (len(artists) > 0): |
268 | artist = artists[0] |
269 | artistID = artist[1] |
270 | xbmc.log("Found " + artist[0] + "...") |
271 | songs = groovesharkApi.getArtistPopularSongs(artistID, limit = self.songsearchlimit) |
272 | if (len(songs) > 0): |
273 | self._add_songs_directory(songs, trackLabelFormat=NAME_ALBUM_ARTIST_LABEL) |
274 | else: |
275 | dialog = xbmcgui.Dialog() |
276 | dialog.ok('Grooveshark XBMC', 'No popular songs.') |
277 | self.categories() |
278 | else: |
279 | dialog = xbmcgui.Dialog() |
280 | dialog.ok('Grooveshark XBMC', 'No matching artists.') |
281 | self.categories() |
282 | else: |
283 | self.categories() |
284 | |
e278f474 |
285 | # Play a song |
6ae708d0 |
286 | def playSong(self, item): |
7ce01be6 |
287 | songid = item.getProperty('songid') |
288 | song = groovesharkApi.getSongURLFromSongID(songid) |
cbb0985e |
289 | if song != '': |
7ce01be6 |
290 | item.setPath(song) |
291 | xbmc.log("Playing: " + song) |
292 | xbmcplugin.setResolvedUrl(handle=int(sys.argv[1]), succeeded=True, listitem=item) |
293 | else: |
cbb0985e |
294 | xbmc.executebuiltin('XBMC.Notification(Grooveshark XBMC, Unable to play song, 1000, ' + thumbDef + ')') |
3fcef5ba |
295 | |
e278f474 |
296 | # Make a song directory item |
86f629ea |
297 | def songItem(self, songid, name, album, artist, coverart, trackLabelFormat=ARTIST_ALBUM_NAME_LABEL): |
7ce01be6 |
298 | songImg = self._get_icon(coverart, 'song-' + str(songid) + "-image") |
86f629ea |
299 | if trackLabelFormat == NAME_ALBUM_ARTIST_LABEL: |
300 | trackLabel = name + " - " + album + " - " + artist |
301 | else: |
302 | trackLabel = artist + " - " + album + " - " + name |
303 | item = xbmcgui.ListItem(label = trackLabel, thumbnailImage=songImg, iconImage=songImg) |
7ce01be6 |
304 | item.setInfo( type="music", infoLabels={ "title": name, "album": album, "artist": artist} ) |
2254a6b5 |
305 | item.setProperty('mimetype', 'audio/mpeg') |
306 | item.setProperty("IsPlayable", "true") |
7ce01be6 |
307 | item.setProperty('songid', str(songid)) |
308 | item.setProperty('coverart', songImg) |
309 | item.setProperty('title', name) |
310 | item.setProperty('album', album) |
311 | item.setProperty('artist', artist) |
312 | |
6ae708d0 |
313 | return item |
2254a6b5 |
314 | |
97289139 |
315 | # Next page of songs |
316 | def songPage(self, page, trackLabelFormat): |
317 | self._add_songs_directory([], trackLabelFormat, page) |
318 | |
e278f474 |
319 | # Get keyboard input |
8817bb2e |
320 | def _get_keyboard(self, default="", heading="", hidden=False): |
3cfead3c |
321 | kb = xbmc.Keyboard(default, heading, hidden) |
322 | kb.doModal() |
323 | if (kb.isConfirmed()): |
324 | return unicode(kb.getText(), "utf-8") |
325 | return '' |
8817bb2e |
326 | |
e278f474 |
327 | # Login to grooveshark |
8817bb2e |
328 | def _get_login(self): |
2254a6b5 |
329 | if (self.username == "" or self.password == ""): |
8817bb2e |
330 | dialog = xbmcgui.Dialog() |
38df1fa5 |
331 | dialog.ok('Grooveshark XBMC', 'Unable to login.', 'Check username and password in settings.') |
8817bb2e |
332 | return 0 |
333 | else: |
38df1fa5 |
334 | if self.userid == 0: |
7ce01be6 |
335 | uid = groovesharkApi.login(self.username, self.password) |
38df1fa5 |
336 | if (uid != 0): |
38df1fa5 |
337 | return uid |
8817bb2e |
338 | else: |
339 | dialog = xbmcgui.Dialog() |
38df1fa5 |
340 | dialog.ok('Grooveshark XBMC', 'Unable to login.', 'Check username and password in settings.') |
8817bb2e |
341 | return 0 |
342 | |
e278f474 |
343 | # Get a song directory item |
86f629ea |
344 | def _get_song_item(self, song, trackLabelFormat): |
6ae708d0 |
345 | name = song[0] |
7ce01be6 |
346 | songid = song[1] |
347 | album = song[2] |
348 | artist = song[4] |
349 | coverart = song[6] |
86f629ea |
350 | return self.songItem(songid, name, album, artist, coverart, trackLabelFormat) |
6ae708d0 |
351 | |
352 | # File download |
7ce01be6 |
353 | def _get_icon(self, url, songid): |
354 | if url != 'None': |
355 | localThumb = os.path.join(xbmc.translatePath(os.path.join(thumbDir, str(songid)))) + '.tbn' |
356 | try: |
357 | if os.path.isfile(localThumb) == False: |
358 | loc = urllib.URLopener() |
359 | loc.retrieve(url, localThumb) |
360 | except: |
361 | shutil.copy2(thumbDef, localThumb) |
362 | return os.path.join(os.path.join(thumbDir, str(songid))) + '.tbn' |
363 | else: |
364 | return thumbDef |
e278f474 |
365 | |
366 | # Add songs to directory |
97289139 |
367 | def _add_songs_directory(self, songs, trackLabelFormat=ARTIST_ALBUM_NAME_LABEL, page=0): |
368 | |
369 | totalSongs = len(songs) |
370 | page = int(page) |
371 | |
372 | # No pages needed |
373 | if page == 0 and totalSongs <= self.songspagelimit: |
374 | xbmc.log("Found " + str(totalSongs) + " songs...") |
375 | # Pages |
376 | else: |
377 | # Cache all songs |
378 | if page == 0: |
379 | self._setSavedSongs(songs) |
380 | else: |
381 | songs = self._getSavedSongs() |
382 | totalSongs = len(songs) |
383 | |
384 | if totalSongs > 0: |
385 | start = page * self.songspagelimit |
386 | end = start + self.songspagelimit |
387 | songs = songs[start:end] |
388 | |
389 | for song in songs: |
86f629ea |
390 | item = self._get_song_item(song, trackLabelFormat) |
7ce01be6 |
391 | coverart = item.getProperty('coverart') |
6ae708d0 |
392 | songname = song[0] |
393 | songid = song[1] |
7ce01be6 |
394 | songalbum = song[2] |
395 | songartist = song[4] |
e278f474 |
396 | u=sys.argv[0]+"?mode="+str(MODE_SONG)+"&name="+urllib.quote_plus(songname)+"&id="+str(songid) \ |
6ae708d0 |
397 | +"&album="+urllib.quote_plus(songalbum) \ |
398 | +"&artist="+urllib.quote_plus(songartist) \ |
7ce01be6 |
399 | +"&coverart="+urllib.quote_plus(coverart) |
e278f474 |
400 | fav=sys.argv[0]+"?mode="+str(MODE_FAVORITE)+"&name="+urllib.quote_plus(songname)+"&id="+str(songid) |
6ae708d0 |
401 | menuItems = [] |
7ce01be6 |
402 | menuItems.append(("Grooveshark favorite", "XBMC.RunPlugin("+fav+")")) |
6ae708d0 |
403 | item.addContextMenuItems(menuItems, replaceItems=False) |
97289139 |
404 | xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]),url=u,listitem=item,isFolder=False, totalItems=len(songs)) |
405 | |
406 | page = page + 1 |
407 | if totalSongs > page * self.songspagelimit: |
408 | u=sys.argv[0]+"?mode="+str(MODE_SONG_PAGE)+"&id=0"+"&page="+str(page)+"&label="+str(trackLabelFormat) |
409 | self._add_dir('More songs...', u, MODE_SONG_PAGE, self.songImg, 0, totalSongs - (page * self.songspagelimit)) |
cb06c186 |
410 | |
8817bb2e |
411 | xbmcplugin.setContent(self._handle, 'songs') |
31731635 |
412 | xbmcplugin.setPluginFanart(int(sys.argv[1]), self.fanImg) |
e278f474 |
413 | |
414 | # Add albums to directory |
7ce01be6 |
415 | def _add_albums_directory(self, albums): |
31731635 |
416 | n = len(albums) |
417 | xbmc.log("Found " + str(n) + " albums...") |
8817bb2e |
418 | i = 0 |
31731635 |
419 | while i < n: |
8817bb2e |
420 | album = albums[i] |
421 | albumArtistName = album[0] |
422 | albumName = album[2] |
423 | albumID = album[3] |
2254a6b5 |
424 | albumImage = self._get_icon(album[4], 'album-' + str(albumID)) |
31731635 |
425 | self._add_dir(albumName + " - " + albumArtistName, '', MODE_ALBUM, albumImage, albumID, n) |
8817bb2e |
426 | i = i + 1 |
427 | xbmcplugin.setContent(self._handle, 'albums') |
428 | xbmcplugin.addSortMethod(self._handle, xbmcplugin.SORT_METHOD_ALBUM_IGNORE_THE) |
31731635 |
429 | xbmcplugin.setPluginFanart(int(sys.argv[1]), self.fanImg) |
e278f474 |
430 | |
431 | # Add artists to directory |
6ae708d0 |
432 | def _add_artists_directory(self, artists): |
31731635 |
433 | n = len(artists) |
434 | xbmc.log("Found " + str(n) + " artists...") |
8817bb2e |
435 | i = 0 |
31731635 |
436 | while i < n: |
8817bb2e |
437 | artist = artists[i] |
438 | artistName = artist[0] |
439 | artistID = artist[1] |
31731635 |
440 | self._add_dir(artistName, '', MODE_ARTIST, self.artistImg, artistID, n) |
8817bb2e |
441 | i = i + 1 |
442 | xbmcplugin.setContent(self._handle, 'artists') |
443 | xbmcplugin.addSortMethod(self._handle, xbmcplugin.SORT_METHOD_ARTIST_IGNORE_THE) |
31731635 |
444 | xbmcplugin.setPluginFanart(int(sys.argv[1]), self.fanImg) |
e278f474 |
445 | |
446 | # Add playlists to directory |
6ae708d0 |
447 | def _add_playlists_directory(self, playlists): |
31731635 |
448 | n = len(playlists) |
449 | xbmc.log("Found " + str(n) + " playlists...") |
8817bb2e |
450 | i = 0 |
31731635 |
451 | while i < n: |
8817bb2e |
452 | playlist = playlists[i] |
453 | playlistName = playlist[0] |
454 | playlistID = playlist[1] |
86f629ea |
455 | dir = self._add_dir(playlistName, '', MODE_PLAYLIST, self.playlistImg, playlistID, n) |
8817bb2e |
456 | i = i + 1 |
457 | xbmcplugin.setContent(self._handle, 'files') |
86f629ea |
458 | xbmcplugin.addSortMethod(self._handle, xbmcplugin.SORT_METHOD_LABEL) |
31731635 |
459 | xbmcplugin.setPluginFanart(int(sys.argv[1]), self.fanImg) |
3fcef5ba |
460 | |
e278f474 |
461 | # Add whatever directory |
31731635 |
462 | def _add_dir(self, name, url, mode, iconimage, id, items=1): |
97289139 |
463 | if url == '': |
464 | u=sys.argv[0]+"?mode="+str(mode)+"&name="+urllib.quote_plus(name)+"&id="+str(id) |
465 | else: |
466 | u = url |
8817bb2e |
467 | dir=xbmcgui.ListItem(name, iconImage=iconimage, thumbnailImage=iconimage) |
b738088f |
468 | dir.setInfo( type="Music", infoLabels={ "title": name } ) |
31731635 |
469 | return xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]),url=u,listitem=dir,isFolder=True, totalItems=items) |
97289139 |
470 | |
471 | def _getSavedSongs(self): |
472 | path = os.path.join(cacheDir, 'songs.dmp') |
473 | try: |
474 | f = open(path, 'rb') |
475 | songs = pickle.load(f) |
476 | f.close() |
477 | except: |
478 | songs = [] |
479 | pass |
480 | return songs |
481 | |
482 | def _setSavedSongs(self, songs): |
483 | try: |
484 | # Create the 'data' directory if it doesn't exist. |
485 | if not os.path.exists(cacheDir): |
486 | os.makedirs(cacheDir) |
487 | path = os.path.join(cacheDir, 'songs.dmp') |
488 | f = open(path, 'wb') |
489 | pickle.dump(songs, f, protocol=pickle.HIGHEST_PROTOCOL) |
490 | f.close() |
491 | except: |
492 | xbmc.log("An error occurred saving songs") |
493 | pass |
494 | |
6ae708d0 |
495 | |
e278f474 |
496 | |
497 | # Parse URL parameters |
8817bb2e |
498 | def get_params(): |
499 | param=[] |
500 | paramstring=sys.argv[2] |
e278f474 |
501 | xbmc.log(paramstring) |
8817bb2e |
502 | if len(paramstring)>=2: |
503 | params=sys.argv[2] |
504 | cleanedparams=params.replace('?','') |
505 | if (params[len(params)-1]=='/'): |
506 | params=params[0:len(params)-2] |
507 | pairsofparams=cleanedparams.split('&') |
508 | param={} |
509 | for i in range(len(pairsofparams)): |
510 | splitparams={} |
511 | splitparams=pairsofparams[i].split('=') |
512 | if (len(splitparams))==2: |
513 | param[splitparams[0]]=splitparams[1] |
8817bb2e |
514 | return param |
515 | |
e278f474 |
516 | # Main |
8817bb2e |
517 | grooveshark = Groveshark(); |
e278f474 |
518 | |
8817bb2e |
519 | params=get_params() |
8817bb2e |
520 | mode=None |
8817bb2e |
521 | try: mode=int(params["mode"]) |
522 | except: pass |
7ce01be6 |
523 | id=0 |
524 | try: id=int(params["id"]) |
525 | except: pass |
e278f474 |
526 | |
527 | # Call function for URL |
8817bb2e |
528 | if mode==None: |
529 | grooveshark.categories() |
530 | |
531 | elif mode==MODE_SEARCH_SONGS: |
532 | grooveshark.searchSongs() |
533 | |
534 | elif mode==MODE_SEARCH_ALBUMS: |
535 | grooveshark.searchAlbums() |
536 | |
537 | elif mode==MODE_SEARCH_ARTISTS: |
538 | grooveshark.searchArtists() |
86f629ea |
539 | |
540 | elif mode==MODE_SEARCH_ARTISTS_ALBUMS: |
541 | grooveshark.searchArtistsAlbums() |
542 | |
543 | elif mode==MODE_SEARCH_PLAYLISTS: |
544 | grooveshark.searchPlaylists() |
8817bb2e |
545 | |
36cc00d7 |
546 | elif mode==MODE_POPULAR_SONGS: |
547 | grooveshark.popularSongs() |
97289139 |
548 | |
549 | elif mode==MODE_ARTIST_POPULAR: |
550 | grooveshark.artistPopularSongs() |
8817bb2e |
551 | |
8817bb2e |
552 | elif mode==MODE_FAVORITES: |
553 | grooveshark.favorites() |
554 | |
e278f474 |
555 | elif mode==MODE_PLAYLISTS: |
556 | grooveshark.playlists() |
97289139 |
557 | |
558 | elif mode==MODE_SONG_PAGE: |
559 | try: page=urllib.unquote_plus(params["page"]) |
560 | except: pass |
561 | try: label=urllib.unquote_plus(params["label"]) |
562 | except: pass |
563 | grooveshark.songPage(page, label) |
e278f474 |
564 | |
8817bb2e |
565 | elif mode==MODE_SONG: |
7ce01be6 |
566 | try: name=urllib.unquote_plus(params["name"]) |
567 | except: pass |
8817bb2e |
568 | try: album=urllib.unquote_plus(params["album"]) |
569 | except: pass |
570 | try: artist=urllib.unquote_plus(params["artist"]) |
571 | except: pass |
7ce01be6 |
572 | try: coverart=urllib.unquote_plus(params["coverart"]) |
8817bb2e |
573 | except: pass |
7ce01be6 |
574 | song = grooveshark.songItem(id, name, album, artist, coverart) |
3fcef5ba |
575 | grooveshark.playSong(song) |
8817bb2e |
576 | |
577 | elif mode==MODE_ARTIST: |
4be42357 |
578 | grooveshark.artist(id) |
8817bb2e |
579 | |
580 | elif mode==MODE_ALBUM: |
4be42357 |
581 | grooveshark.album(id) |
8817bb2e |
582 | |
583 | elif mode==MODE_PLAYLIST: |
7ce01be6 |
584 | grooveshark.playlist(id) |
8817bb2e |
585 | |
586 | elif mode==MODE_FAVORITE: |
4be42357 |
587 | grooveshark.favorite(id) |
97289139 |
588 | |
e278f474 |
589 | if mode < MODE_SONG: |
8817bb2e |
590 | xbmcplugin.endOfDirectory(int(sys.argv[1])) |