aa0c1263 |
1 | import urllib, urllib2, re, xbmcplugin, xbmcgui, xbmc, sys, os, time, 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 |
7ea6f166 |
20 | MODE_ADD_PLAYLIST_SONG = 18 |
21 | |
38df1fa5 |
22 | ACTION_MOVE_LEFT = 1 |
7ea6f166 |
23 | ACTION_MOVE_UP = 3 |
24 | ACTION_MOVE_DOWN = 4 |
25 | ACTION_PAGE_UP = 5 |
26 | ACTION_PAGE_DOWN = 6 |
27 | ACTION_SELECT_ITEM = 7 |
28 | ACTION_PREVIOUS_MENU = 10 |
8817bb2e |
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')) |
973b4c6c |
34 | thumbDir = os.path.join('special://masterprofile/addon_data/', os.path.join(os.path.basename(os.getcwd()), 'thumb')) |
4be42357 |
35 | |
36 | baseModeUrl = 'plugin://plugin.audio.groove/' |
37 | playlistUrl = baseModeUrl + '?url=&mode=' + str(MODE_PLAYLIST) |
38 | playlistsUrl = baseModeUrl + '?mode=' + str(MODE_PLAYLISTS) |
39 | favoritesUrl = baseModeUrl + '?mode=' + str(MODE_FAVORITES) |
40 | |
3cfead3c |
41 | thumbDef = os.path.join(os.path.basename(os.getcwd()), 'default.tbn') |
7ea6f166 |
42 | listBackground = os.path.join(imgDir, 'listbackground.png') |
8817bb2e |
43 | |
44 | sys.path.append (libDir) |
45 | from GrooveAPI import * |
46 | groovesharkApi = GrooveAPI() |
47 | |
48 | class _Info: |
49 | def __init__( self, *args, **kwargs ): |
50 | self.__dict__.update( kwargs ) |
3fcef5ba |
51 | |
7ea6f166 |
52 | class GroovesharkPlaylistSelect(xbmcgui.WindowDialog): |
53 | |
38df1fa5 |
54 | def __init__(self, items=[]): |
38df1fa5 |
55 | gap = int(self.getHeight()/100) |
aa0c1263 |
56 | w = int(self.getWidth()*0.5) |
38df1fa5 |
57 | h = self.getHeight()-30*gap |
7ea6f166 |
58 | rw = self.getWidth() |
59 | rh = self.getHeight() |
60 | x = rw/2 - w/2 |
61 | y = rh/2 -h/2 |
62 | |
38df1fa5 |
63 | self.imgBg = xbmcgui.ControlImage(x+gap, 5*gap+y, w-2*gap, h-5*gap, listBackground) |
7ea6f166 |
64 | self.addControl(self.imgBg) |
aa0c1263 |
65 | |
cb06c186 |
66 | self.playlistControl = xbmcgui.ControlList(2*gap+x, y+3*gap+30, w-4*gap, h-10*gap, textColor='0xFFFFFFFF', selectedColor='0xFFFF4242', itemTextYOffset=0, itemHeight=50, alignmentY = 0) |
38df1fa5 |
67 | self.addControl(self.playlistControl) |
68 | |
7ea6f166 |
69 | self.lastPos = 0 |
70 | self.isSelecting = False |
38df1fa5 |
71 | self.selected = -1 |
7ea6f166 |
72 | listitems = [] |
7ea6f166 |
73 | for playlist in items: |
7ea6f166 |
74 | listitems.append(xbmcgui.ListItem(playlist[0])) |
38df1fa5 |
75 | listitems.append(xbmcgui.ListItem('New...')) |
76 | self.playlistControl.addItems(listitems) |
77 | self.setFocus(self.playlistControl) |
aa0c1263 |
78 | self.playlistControl.selectItem(0) |
38df1fa5 |
79 | item = self.playlistControl.getListItem(self.lastPos) |
7ea6f166 |
80 | item.select(True) |
81 | |
82 | # Highlight selected item |
83 | def setHighlight(self): |
84 | if self.isSelecting: |
85 | return |
86 | else: |
87 | self.isSelecting = True |
88 | |
38df1fa5 |
89 | pos = self.playlistControl.getSelectedPosition() |
7ea6f166 |
90 | if pos >= 0: |
38df1fa5 |
91 | item = self.playlistControl.getListItem(self.lastPos) |
7ea6f166 |
92 | item.select(False) |
38df1fa5 |
93 | item = self.playlistControl.getListItem(pos) |
7ea6f166 |
94 | item.select(True) |
95 | self.lastPos = pos |
96 | self.isSelecting = False |
97 | |
aa0c1263 |
98 | # Control |
99 | def onControl(self, control): |
100 | if control == self.playlistControl: |
101 | self.selected = self.playlistControl.getSelectedPosition() |
102 | self.close() |
103 | |
7ea6f166 |
104 | def onAction(self, action): |
aa0c1263 |
105 | if action == ACTION_PREVIOUS_MENU: |
106 | self.selected = -1 |
107 | self.close() |
108 | elif action == ACTION_MOVE_UP or action == ACTION_MOVE_DOWN or action == ACTION_PAGE_UP or action == ACTION_PAGE_DOWN == 6: |
109 | self.setFocus(self.playlistControl) |
110 | self.setHighlight() |
7ea6f166 |
111 | |
8817bb2e |
112 | class Groveshark: |
973b4c6c |
113 | |
7ea6f166 |
114 | albumImg = xbmc.translatePath(os.path.join(imgDir, 'album.png')) |
115 | artistImg = xbmc.translatePath(os.path.join(imgDir, 'artist.png')) |
116 | favoritesImg = xbmc.translatePath(os.path.join(imgDir, 'favorites.png')) |
117 | playlistImg = xbmc.translatePath(os.path.join(imgDir, 'playlist.png')) |
118 | popularSongsImg = xbmc.translatePath(os.path.join(imgDir, 'popularSongs.png')) |
119 | songImg = xbmc.translatePath(os.path.join(imgDir, 'song.png')) |
120 | defImg = xbmc.translatePath(os.path.join(imgDir, 'default.tbn')) |
2254a6b5 |
121 | fanImg = xbmc.translatePath(os.path.join(baseDir, 'fanart.png')) |
8817bb2e |
122 | |
2254a6b5 |
123 | settings = xbmcaddon.Addon(id='plugin.audio.groove') |
973b4c6c |
124 | songsearchlimit = settings.getSetting('songsearchlimit') |
125 | albumsearchlimit = settings.getSetting('albumsearchlimit') |
126 | artistsearchlimit = settings.getSetting('artistsearchlimit') |
2254a6b5 |
127 | username = settings.getSetting('username') |
128 | password = settings.getSetting('password') |
38df1fa5 |
129 | userid = 0 |
4be42357 |
130 | |
8817bb2e |
131 | def __init__( self ): |
132 | self._handle = int(sys.argv[1]) |
b738088f |
133 | |
8817bb2e |
134 | def categories(self): |
2254a6b5 |
135 | |
136 | xbmc.log(self.username + ", limits: " + str(self.songsearchlimit) + ", " + str(self.albumsearchlimit) + ", " + str(self.artistsearchlimit)) |
b738088f |
137 | |
38df1fa5 |
138 | self.userid = self._get_login() |
b738088f |
139 | |
140 | # Setup |
6ae708d0 |
141 | xbmcplugin.setPluginFanart(int(sys.argv[1]), self.fanImg) |
142 | if os.path.isdir(thumbDir) == False: |
143 | os.makedirs(thumbDir) |
144 | |
3fcef5ba |
145 | self._add_dir('Search songs', '', MODE_SEARCH_SONGS, self.songImg, 0) |
146 | self._add_dir('Search albums', '', MODE_SEARCH_ALBUMS, self.albumImg, 0) |
147 | self._add_dir('Search artists', '', MODE_SEARCH_ARTISTS, self.artistImg, 0) |
36cc00d7 |
148 | self._add_dir('Popular songs', '', MODE_POPULAR_SONGS, self.popularSongsImg, 0) |
38df1fa5 |
149 | if (self.userid != 0): |
3fcef5ba |
150 | self._add_dir('Favorites', '', MODE_FAVORITES, self.favoritesImg, 0) |
151 | self._add_dir('Playlists', '', MODE_PLAYLISTS, self.playlistImg, 0) |
152 | |
8817bb2e |
153 | def searchSongs(self): |
154 | query = self._get_keyboard(default="", heading="Search songs") |
7ea6f166 |
155 | if (query != ''): |
4be42357 |
156 | groovesharkApi.setRemoveDuplicates(True) |
2254a6b5 |
157 | songs = groovesharkApi.searchSongs(query, limit = self.songsearchlimit) |
8817bb2e |
158 | if (len(songs) > 0): |
6ae708d0 |
159 | self._add_songs_directory(songs) |
8817bb2e |
160 | else: |
161 | dialog = xbmcgui.Dialog() |
38df1fa5 |
162 | dialog.ok('Grooveshark XBMC', 'No matching songs.') |
8817bb2e |
163 | self.categories() |
7ea6f166 |
164 | else: |
165 | self.categories() |
8817bb2e |
166 | |
167 | def searchAlbums(self): |
168 | query = self._get_keyboard(default="", heading="Search albums") |
7ea6f166 |
169 | if (query != ''): |
2254a6b5 |
170 | albums = groovesharkApi.searchAlbums(query, limit = self.albumsearchlimit) |
8817bb2e |
171 | if (len(albums) > 0): |
6ae708d0 |
172 | self._add_albums_directory(albums) |
8817bb2e |
173 | else: |
174 | dialog = xbmcgui.Dialog() |
38df1fa5 |
175 | dialog.ok('Grooveshark XBMC', 'No matching albums.') |
8817bb2e |
176 | self.categories() |
7ea6f166 |
177 | else: |
178 | self.categories() |
8817bb2e |
179 | |
180 | def searchArtists(self): |
181 | query = self._get_keyboard(default="", heading="Search artists") |
7ea6f166 |
182 | if (query != ''): |
2254a6b5 |
183 | artists = groovesharkApi.searchArtists(query, limit = self.artistsearchlimit) |
8817bb2e |
184 | if (len(artists) > 0): |
6ae708d0 |
185 | self._add_artists_directory(artists) |
8817bb2e |
186 | else: |
187 | dialog = xbmcgui.Dialog() |
38df1fa5 |
188 | dialog.ok('Grooveshark XBMC', 'No matching artists.') |
8817bb2e |
189 | self.categories() |
7ea6f166 |
190 | else: |
191 | self.categories() |
8817bb2e |
192 | |
193 | def favorites(self): |
36cc00d7 |
194 | userid = self._get_login() |
195 | if (userid != 0): |
196 | favorites = groovesharkApi.userGetFavoriteSongs(userid) |
197 | if (len(favorites) > 0): |
4be42357 |
198 | self._add_songs_directory(favorites, isFavorites=True) |
36cc00d7 |
199 | else: |
200 | dialog = xbmcgui.Dialog() |
38df1fa5 |
201 | dialog.ok('Grooveshark XBMC', 'You have no favorites.') |
36cc00d7 |
202 | self.categories() |
8817bb2e |
203 | |
36cc00d7 |
204 | def popularSongs(self): |
2254a6b5 |
205 | popular = groovesharkApi.popularGetSongs(limit = self.songsearchlimit) |
8817bb2e |
206 | if (len(popular) > 0): |
6ae708d0 |
207 | self._add_songs_directory(popular) |
8817bb2e |
208 | else: |
209 | dialog = xbmcgui.Dialog() |
38df1fa5 |
210 | dialog.ok('Grooveshark XBMC', 'No popular songs.') |
8817bb2e |
211 | self.categories() |
36cc00d7 |
212 | |
213 | def popularAlbums(self): |
214 | popular = groovesharkApi.popularGetAlbums(limit = self.albumsearchlimit) |
215 | if (len(popular) > 0): |
216 | self._add_albums_directory(popular) |
217 | else: |
218 | dialog = xbmcgui.Dialog() |
38df1fa5 |
219 | dialog.ok('Grooveshark XBMC', 'No popular albums.') |
36cc00d7 |
220 | self.categories() |
221 | |
222 | def popularArtists(self): |
223 | popular = groovesharkApi.popularGetArtists(limit = self.artistsearchlimit) |
224 | if (len(popular) > 0): |
225 | self._add_artists_directory(popular) |
226 | else: |
227 | dialog = xbmcgui.Dialog() |
38df1fa5 |
228 | dialog.ok('Grooveshark XBMC', 'No popular artists.') |
36cc00d7 |
229 | self.categories() |
8817bb2e |
230 | |
231 | def playlists(self): |
232 | userid = self._get_login() |
233 | if (userid != 0): |
234 | playlists = groovesharkApi.userGetPlaylists() |
235 | if (len(playlists) > 0): |
6ae708d0 |
236 | self._add_playlists_directory(playlists) |
8817bb2e |
237 | else: |
238 | dialog = xbmcgui.Dialog() |
38df1fa5 |
239 | dialog.ok('Grooveshark XBMC', 'You have no Grooveshark playlists.') |
8817bb2e |
240 | self.categories() |
7ea6f166 |
241 | else: |
242 | dialog = xbmcgui.Dialog() |
38df1fa5 |
243 | dialog.ok('Grooveshark XBMC', 'You must be logged in ', ' to see your Grooveshark playlists.') |
7ea6f166 |
244 | |
8817bb2e |
245 | |
246 | def favorite(self, songid): |
247 | userid = self._get_login() |
248 | if (userid != 0): |
406ab447 |
249 | xbmc.log("Favorite song: " + str(songid)) |
8817bb2e |
250 | groovesharkApi.favoriteSong(songID = songid) |
38df1fa5 |
251 | xbmc.executebuiltin('XBMC.Notification(Grooveshark XBMC, Added to favorites, 1000, ' + thumbDef + ')') |
8817bb2e |
252 | else: |
253 | dialog = xbmcgui.Dialog() |
38df1fa5 |
254 | dialog.ok('Grooveshark XBMC', 'You must be logged in', 'to add favorites.') |
8817bb2e |
255 | |
4be42357 |
256 | def unfavorite(self, songid, prevMode): |
8817bb2e |
257 | userid = self._get_login() |
258 | if (userid != 0): |
4be42357 |
259 | xbmc.log("Unfavorite song: " + str(songid) + ', previous mode was ' + str(prevMode)) |
8817bb2e |
260 | groovesharkApi.unfavoriteSong(songID = songid) |
aa0c1263 |
261 | xbmc.executebuiltin('XBMC.Notification(Grooveshark XBMC, Removed from Grooveshark favorites, 1000, ' + thumbDef + ')') |
4be42357 |
262 | if (int(prevMode) == MODE_FAVORITES): |
cb06c186 |
263 | xbmc.executebuiltin("Container.Refresh(" + favoritesUrl + ")") |
8817bb2e |
264 | else: |
265 | dialog = xbmcgui.Dialog() |
aa0c1263 |
266 | dialog.ok('Grooveshark XBMC', 'You must be logged in', 'to remove Grooveshark favorites.') |
3fcef5ba |
267 | |
268 | def frown(self, songid): |
269 | userid = self._get_login() |
270 | if (userid != 0): |
406ab447 |
271 | xbmc.log("Frown song: " + str(songid)) |
3fcef5ba |
272 | if groovesharkApi.radioFrown(songId = songid) != True: |
273 | xbmc.log("Unable to frown song " + str(songid)) |
3cfead3c |
274 | else: |
38df1fa5 |
275 | xbmc.executebuiltin('XBMC.Notification(Grooveshark XBMC, Frowned, 1000, ' + thumbDef + ')') |
3fcef5ba |
276 | else: |
277 | dialog = xbmcgui.Dialog() |
aa0c1263 |
278 | dialog.ok('Grooveshark XBMC', 'You must be logged in', 'to frown a Grooveshark song.') |
3fcef5ba |
279 | |
406ab447 |
280 | def similarArtists(self, artistId): |
281 | similar = groovesharkApi.artistGetSimilar(artistId, limit = self.artistsearchlimit) |
282 | if (len(similar) > 0): |
283 | self._add_artists_directory(similar) |
3fcef5ba |
284 | else: |
285 | dialog = xbmcgui.Dialog() |
38df1fa5 |
286 | dialog.ok('Grooveshark XBMC', 'No similar artists.') |
406ab447 |
287 | self.categories() |
36cc00d7 |
288 | |
289 | def makePlaylist(self, albumid, name): |
290 | userid = self._get_login() |
291 | if (userid != 0): |
3cfead3c |
292 | re.split(' - ',name,1) |
293 | nameTokens = re.split(' - ',name,1) |
38df1fa5 |
294 | name = self._get_keyboard(default=nameTokens[0], heading="Grooveshark playlist name") |
3cfead3c |
295 | if name != '': |
4be42357 |
296 | groovesharkApi.setRemoveDuplicates(True) |
3cfead3c |
297 | album = groovesharkApi.albumGetSongs(albumid, self.songsearchlimit) |
298 | songids = [] |
299 | for song in album: |
300 | songids.append(song[1]) |
38df1fa5 |
301 | if groovesharkApi.playlistCreateUnique(name, songids) == 0: |
3cfead3c |
302 | dialog = xbmcgui.Dialog() |
38df1fa5 |
303 | dialog.ok('Grooveshark XBMC', 'Cannot create Grooveshark playlist ', name) |
3cfead3c |
304 | else: |
38df1fa5 |
305 | xbmc.executebuiltin('XBMC.Notification(Grooveshark XBMC, Grooveshark playlist created, 1000, ' + thumbDef + ')') |
36cc00d7 |
306 | else: |
307 | dialog = xbmcgui.Dialog() |
38df1fa5 |
308 | dialog.ok('Grooveshark XBMC', 'You must be logged in ', ' to create a Grooveshark playlist.') |
36cc00d7 |
309 | |
310 | def removePlaylist(self, playlistid, name): |
311 | dialog = xbmcgui.Dialog() |
38df1fa5 |
312 | if dialog.yesno('Grooveshark XBMC', name, 'Delete this Grooveshark playlist?') == True: |
36cc00d7 |
313 | userid = self._get_login() |
314 | if (userid != 0): |
315 | groovesharkApi.playlistDelete(playlistid) |
7ea6f166 |
316 | xbmc.executebuiltin("Container.Refresh(" + playlistsUrl + ")") |
36cc00d7 |
317 | else: |
318 | dialog = xbmcgui.Dialog() |
38df1fa5 |
319 | dialog.ok('Grooveshark XBMC', 'You must be logged in ', ' to delete a Grooveshark playlist.') |
36cc00d7 |
320 | |
4be42357 |
321 | def removePlaylistSong(self, playlistid, playlistname, songpos): |
322 | dialog = xbmcgui.Dialog() |
38df1fa5 |
323 | if dialog.yesno('Grooveshark XBMC', 'Delete this song from the Grooveshark playlist?') == True: |
4be42357 |
324 | userid = self._get_login() |
325 | if (userid != 0): |
326 | if groovesharkApi.playlistDeleteSong(playlistid, songpos) == 0: |
327 | dialog = xbmcgui.Dialog() |
38df1fa5 |
328 | dialog.ok('Grooveshark XBMC', 'Failed to remove ', ' song from Grooveshark playlist.') |
4be42357 |
329 | else: |
7ea6f166 |
330 | xbmc.executebuiltin("Container.Refresh(" + playlistUrl + "&id="+str(playlistid) + "&name=" + playlistname + ",replace)") |
331 | else: |
332 | dialog = xbmcgui.Dialog() |
38df1fa5 |
333 | dialog.ok('Grooveshark XBMC', 'You must be logged in ', ' to delete a song from a Grooveshark playlist.') |
7ea6f166 |
334 | |
335 | def addPlaylistSong(self, songid): |
336 | userid = self._get_login() |
337 | if (userid != 0): |
338 | playlists = groovesharkApi.userGetPlaylists() |
339 | if (len(playlists) > 0): |
38df1fa5 |
340 | ret = 0 |
341 | playlistSelect = GroovesharkPlaylistSelect(items=playlists) |
342 | playlistSelect.setFocus(playlistSelect.playlistControl) |
7ea6f166 |
343 | playlistSelect.doModal() |
38df1fa5 |
344 | i = playlistSelect.selected |
7ea6f166 |
345 | del playlistSelect |
38df1fa5 |
346 | if i > -1: |
347 | # New playlist |
348 | if i >= len(playlists): |
349 | name = self._get_keyboard(default='', heading="Grooveshark playlist name") |
350 | if name != '': |
351 | songIds = [] |
352 | songIds.append(songid) |
353 | if groovesharkApi.playlistCreateUnique(name, songIds) == 0: |
354 | dialog = xbmcgui.Dialog() |
355 | dialog.ok('Grooveshark XBMC', 'Cannot create Grooveshark playlist ', name) |
356 | else: |
357 | xbmc.executebuiltin('XBMC.Notification(Grooveshark XBMC, Grooveshark playlist created, 1000, ' + thumbDef + ')') |
358 | # Existing playlist |
359 | else: |
360 | playlist = playlists[i] |
361 | playlistid = playlist[1] |
362 | xbmc.log("Add song " + str(songid) + " to playlist " + str(playlistid)) |
363 | ret = groovesharkApi.playlistAddSong(playlistid, songid, 0) |
364 | if ret == 0: |
365 | dialog = xbmcgui.Dialog() |
366 | dialog.ok('Grooveshark XBMC', 'Cannot add to playlist ') |
367 | else: |
368 | xbmc.executebuiltin('XBMC.Notification(Grooveshark XBMC, Added song to Grooveshark playlist, 1000, ' + thumbDef + ')') |
4be42357 |
369 | else: |
370 | dialog = xbmcgui.Dialog() |
38df1fa5 |
371 | dialog.ok('Grooveshark XBMC', 'You have no Grooveshark playlists.') |
7ea6f166 |
372 | self.categories() |
373 | else: |
374 | dialog = xbmcgui.Dialog() |
38df1fa5 |
375 | dialog.ok('Grooveshark XBMC', 'You must be logged in ', ' to add a song to a Grooveshark playlist.') |
4be42357 |
376 | |
36cc00d7 |
377 | def renamePlaylist(self, playlistid, name): |
378 | userid = self._get_login() |
379 | if (userid != 0): |
aa0c1263 |
380 | newname = self._get_keyboard(default=name, heading="Grooveshark playlist name") |
3cfead3c |
381 | if newname == '': |
382 | return |
383 | elif groovesharkApi.playlistRename(playlistid, newname) == 0: |
36cc00d7 |
384 | dialog = xbmcgui.Dialog() |
38df1fa5 |
385 | dialog.ok('Grooveshark XBMC', 'Cannot rename Grooveshark playlist ', name) |
36cc00d7 |
386 | else: |
387 | xbmc.executebuiltin("Container.Refresh") |
388 | else: |
389 | dialog = xbmcgui.Dialog() |
38df1fa5 |
390 | dialog.ok('Grooveshark XBMC', 'You must be logged in ', ' to rename a Grooveshark playlist.') |
8817bb2e |
391 | |
36cc00d7 |
392 | def album(self, albumid): |
4be42357 |
393 | groovesharkApi.setRemoveDuplicates(True) |
2254a6b5 |
394 | album = groovesharkApi.albumGetSongs(albumId = albumid, limit = self.songsearchlimit) |
6ae708d0 |
395 | self._add_songs_directory(album) |
8817bb2e |
396 | |
397 | def artist(self, artistid): |
2254a6b5 |
398 | albums = groovesharkApi.artistGetAlbums(artistId = artistid, limit = self.albumsearchlimit) |
406ab447 |
399 | self._add_albums_directory(albums, artistid) |
8817bb2e |
400 | |
4be42357 |
401 | def playlist(self, playlistid, playlistname): |
8817bb2e |
402 | userid = self._get_login() |
403 | if (userid != 0): |
2254a6b5 |
404 | songs = groovesharkApi.playlistGetSongs(playlistId = playlistid, limit = self.songsearchlimit) |
4be42357 |
405 | self._add_songs_directory(songs, playlistid, playlistname) |
8817bb2e |
406 | else: |
407 | dialog = xbmcgui.Dialog() |
38df1fa5 |
408 | dialog.ok('Grooveshark XBMC', 'You must be logged in', 'to get Grooveshark playlists.') |
8817bb2e |
409 | |
6ae708d0 |
410 | def playSong(self, item): |
411 | url = item.getProperty('url') |
9b1d8c96 |
412 | xbmc.log("Playing: " + url) |
2254a6b5 |
413 | xbmcplugin.setResolvedUrl(handle=int(sys.argv[1]), succeeded=True, listitem=item) |
3fcef5ba |
414 | |
6ae708d0 |
415 | def songItem(self, id, name, album, artist, duration, thumb, image): |
416 | url = groovesharkApi.getStreamURL(id) |
e6ccfeca |
417 | # Only try to get the image |
418 | if image != "": |
419 | songImg = self._get_icon(image, 'song-' + str(id) + "-image") |
420 | songThm = songImg |
421 | else: |
422 | songThm = self._get_icon(thumb, 'song-' + str(id) + "-thumb") |
423 | songImg = songThm |
6ae708d0 |
424 | item = xbmcgui.ListItem(label = artist + " - " + album + " - " + name, path=url, thumbnailImage=songThm, iconImage=songImg) |
2254a6b5 |
425 | item.setInfo( type="music", infoLabels={ "title": name, "duration": duration, "album": album, "artist": artist} ) |
426 | item.setProperty('mimetype', 'audio/mpeg') |
427 | item.setProperty("IsPlayable", "true") |
6ae708d0 |
428 | item.setProperty('url', url) |
429 | item.setProperty('thumb', songThm) |
430 | item.setProperty('image', songImg) |
2254a6b5 |
431 | |
6ae708d0 |
432 | return item |
2254a6b5 |
433 | |
8817bb2e |
434 | def _get_keyboard(self, default="", heading="", hidden=False): |
3cfead3c |
435 | kb = xbmc.Keyboard(default, heading, hidden) |
436 | kb.doModal() |
437 | if (kb.isConfirmed()): |
438 | return unicode(kb.getText(), "utf-8") |
439 | return '' |
8817bb2e |
440 | |
441 | def _get_login(self): |
2254a6b5 |
442 | if (self.username == "" or self.password == ""): |
8817bb2e |
443 | dialog = xbmcgui.Dialog() |
38df1fa5 |
444 | dialog.ok('Grooveshark XBMC', 'Unable to login.', 'Check username and password in settings.') |
8817bb2e |
445 | return 0 |
446 | else: |
38df1fa5 |
447 | if self.userid == 0: |
448 | uid = groovesharkApi.loginExt(self.username, self.password) |
449 | if (uid != 0): |
8817bb2e |
450 | xbmc.log("Logged in") |
38df1fa5 |
451 | return uid |
8817bb2e |
452 | else: |
453 | dialog = xbmcgui.Dialog() |
38df1fa5 |
454 | dialog.ok('Grooveshark XBMC', 'Unable to login.', 'Check username and password in settings.') |
8817bb2e |
455 | return 0 |
456 | |
6ae708d0 |
457 | def _get_song_item(self, song): |
458 | name = song[0] |
459 | id = song[1] |
460 | duration = song[2] |
461 | album = song[3] |
462 | artist = song[6] |
463 | thumb = song[8] |
464 | image = song[9] |
465 | return self.songItem(id, name, album, artist, duration, thumb, image) |
466 | |
467 | # File download |
468 | def _get_icon(self, url, id): |
6ae708d0 |
469 | localThumb = os.path.join(xbmc.translatePath(os.path.join(thumbDir, str(id)))) + '.tbn' |
470 | try: |
471 | if os.path.isfile(localThumb) == False: |
e6ccfeca |
472 | xbmc.log('Downloading ' + url + ' to ' + localThumb) |
6ae708d0 |
473 | loc = urllib.URLopener() |
474 | loc.retrieve(url, localThumb) |
475 | except: |
cd371e5d |
476 | shutil.copy2(self.defImg, localThumb) |
6ae708d0 |
477 | |
478 | return os.path.join(os.path.join(thumbDir, str(id))) + '.tbn' |
479 | |
4be42357 |
480 | def _add_songs_directory(self, songs, playlistid=0, playlistname='', isFavorites=False): |
6ae708d0 |
481 | n = len(songs) |
482 | xbmc.log("Found " + str(n) + " songs...") |
cb06c186 |
483 | progressDialog = xbmcgui.DialogProgress() |
5ea2ed41 |
484 | progressDialog.create('Grooveshark XBMC', 'Fetching...', '0 of ' + str(n)) |
aa0c1263 |
485 | dirItems = [] |
8817bb2e |
486 | i = 0 |
6ae708d0 |
487 | while i < n: |
8817bb2e |
488 | song = songs[i] |
6ae708d0 |
489 | item = self._get_song_item(song) |
490 | songurl = item.getProperty('url') |
491 | songthumb = item.getProperty('thumb') |
492 | songimage = item.getProperty('image') |
493 | songname = song[0] |
494 | songid = song[1] |
495 | songduration = song[2] |
496 | songalbum = song[3] |
497 | songartist = song[6] |
6ae708d0 |
498 | u=sys.argv[0]+"?url="+urllib.quote_plus(songurl) \ |
499 | +"&mode="+str(MODE_SONG)+"&name="+urllib.quote_plus(songname)+"&id=" \ |
500 | +str(songid) \ |
501 | +"&album="+urllib.quote_plus(songalbum) \ |
502 | +"&artist="+urllib.quote_plus(songartist) \ |
503 | +"&duration="+str(songduration) \ |
504 | +"&thumb="+urllib.quote_plus(songthumb) \ |
505 | +"&image="+urllib.quote_plus(songimage) |
506 | fav=sys.argv[0]+"?url="+urllib.quote_plus(songurl)+"&mode="+str(MODE_FAVORITE)+"&name="+urllib.quote_plus(songname)+"&id="+str(songid) |
4be42357 |
507 | unfav=sys.argv[0]+"?url="+urllib.quote_plus(songurl)+"&mode="+str(MODE_UNFAVORITE)+"&name="+urllib.quote_plus(songname)+"&id="+str(songid)+"&prevmode=" |
6ae708d0 |
508 | menuItems = [] |
4be42357 |
509 | if isFavorites == True: |
510 | unfav = unfav +str(MODE_FAVORITES) |
511 | else: |
7ea6f166 |
512 | menuItems.append(("Grooveshark favorite", "XBMC.RunPlugin("+fav+")")) |
513 | menuItems.append(("Not Grooveshark favorite", "XBMC.RunPlugin("+unfav+")")) |
4be42357 |
514 | if playlistid > 0: |
515 | rmplaylstsong=sys.argv[0]+"?playlistid="+str(playlistid)+"&id="+str(i+1)+"&mode="+str(MODE_REMOVE_PLAYLIST_SONG)+"&name="+playlistname |
aa0c1263 |
516 | menuItems.append(("Remove from Grooveshark playlist", "XBMC.RunPlugin("+rmplaylstsong+")")) |
7ea6f166 |
517 | else: |
518 | addplaylstsong=sys.argv[0]+"?id="+str(songid)+"&mode="+str(MODE_ADD_PLAYLIST_SONG) |
519 | menuItems.append(("Add to Grooveshark playlist", "XBMC.RunPlugin("+addplaylstsong+")")) |
6ae708d0 |
520 | item.addContextMenuItems(menuItems, replaceItems=False) |
aa0c1263 |
521 | dirItem = (u, item, False) |
522 | dirItems.append(dirItem) |
8817bb2e |
523 | i = i + 1 |
5ea2ed41 |
524 | progressDialog.update(int((i*100)/n), 'Fetching...', str(i) + ' of ' + str(n)) |
cb06c186 |
525 | if progressDialog.iscanceled(): |
526 | break |
527 | |
528 | progressDialog.close() |
aa0c1263 |
529 | xbmcplugin.addDirectoryItems(int(sys.argv[1]), dirItems ,n) |
8817bb2e |
530 | xbmcplugin.setContent(self._handle, 'songs') |
31731635 |
531 | xbmcplugin.setPluginFanart(int(sys.argv[1]), self.fanImg) |
8817bb2e |
532 | |
406ab447 |
533 | def _add_albums_directory(self, albums, artistid=0): |
31731635 |
534 | n = len(albums) |
535 | xbmc.log("Found " + str(n) + " albums...") |
8817bb2e |
536 | i = 0 |
31731635 |
537 | while i < n: |
8817bb2e |
538 | album = albums[i] |
539 | albumArtistName = album[0] |
540 | albumName = album[2] |
541 | albumID = album[3] |
2254a6b5 |
542 | albumImage = self._get_icon(album[4], 'album-' + str(albumID)) |
31731635 |
543 | self._add_dir(albumName + " - " + albumArtistName, '', MODE_ALBUM, albumImage, albumID, n) |
8817bb2e |
544 | i = i + 1 |
406ab447 |
545 | if artistid > 0: |
546 | self._add_dir('Similar artists...', '', MODE_SIMILAR_ARTISTS, self.artistImg, artistid) |
8817bb2e |
547 | xbmcplugin.setContent(self._handle, 'albums') |
548 | xbmcplugin.addSortMethod(self._handle, xbmcplugin.SORT_METHOD_ALBUM_IGNORE_THE) |
31731635 |
549 | xbmcplugin.setPluginFanart(int(sys.argv[1]), self.fanImg) |
8817bb2e |
550 | |
6ae708d0 |
551 | def _add_artists_directory(self, artists): |
31731635 |
552 | n = len(artists) |
553 | xbmc.log("Found " + str(n) + " artists...") |
8817bb2e |
554 | i = 0 |
31731635 |
555 | while i < n: |
8817bb2e |
556 | artist = artists[i] |
557 | artistName = artist[0] |
558 | artistID = artist[1] |
31731635 |
559 | self._add_dir(artistName, '', MODE_ARTIST, self.artistImg, artistID, n) |
8817bb2e |
560 | i = i + 1 |
561 | xbmcplugin.setContent(self._handle, 'artists') |
562 | xbmcplugin.addSortMethod(self._handle, xbmcplugin.SORT_METHOD_ARTIST_IGNORE_THE) |
31731635 |
563 | xbmcplugin.setPluginFanart(int(sys.argv[1]), self.fanImg) |
8817bb2e |
564 | |
6ae708d0 |
565 | def _add_playlists_directory(self, playlists): |
31731635 |
566 | n = len(playlists) |
567 | xbmc.log("Found " + str(n) + " playlists...") |
8817bb2e |
568 | i = 0 |
31731635 |
569 | while i < n: |
8817bb2e |
570 | playlist = playlists[i] |
571 | playlistName = playlist[0] |
572 | playlistID = playlist[1] |
31731635 |
573 | self._add_dir(playlistName, '', MODE_PLAYLIST, self.playlistImg, playlistID, n) |
8817bb2e |
574 | i = i + 1 |
575 | xbmcplugin.setContent(self._handle, 'files') |
576 | xbmcplugin.addSortMethod(self._handle, xbmcplugin.SORT_METHOD_PLAYLIST_ORDER) |
31731635 |
577 | xbmcplugin.setPluginFanart(int(sys.argv[1]), self.fanImg) |
3fcef5ba |
578 | |
31731635 |
579 | def _add_dir(self, name, url, mode, iconimage, id, items=1): |
8817bb2e |
580 | u=sys.argv[0]+"?url="+urllib.quote_plus(url)+"&mode="+str(mode)+"&name="+urllib.quote_plus(name)+"&id="+str(id) |
581 | dir=xbmcgui.ListItem(name, iconImage=iconimage, thumbnailImage=iconimage) |
b738088f |
582 | dir.setInfo( type="Music", infoLabels={ "title": name } ) |
8817bb2e |
583 | menuItems = [] |
36cc00d7 |
584 | if mode == MODE_ALBUM: |
585 | mkplaylst=sys.argv[0]+"?mode="+str(MODE_MAKE_PLAYLIST)+"&name="+name+"&id="+str(id) |
586 | menuItems.append(("Make Grooveshark playlist", "XBMC.RunPlugin("+mkplaylst+")")) |
587 | if mode == MODE_PLAYLIST: |
588 | rmplaylst=sys.argv[0]+"?mode="+str(MODE_REMOVE_PLAYLIST)+"&name="+urllib.quote_plus(name)+"&id="+str(id) |
aa0c1263 |
589 | menuItems.append(("Delete Grooveshark playlist", "XBMC.RunPlugin("+rmplaylst+")")) |
36cc00d7 |
590 | mvplaylst=sys.argv[0]+"?mode="+str(MODE_RENAME_PLAYLIST)+"&name="+urllib.quote_plus(name)+"&id="+str(id) |
aa0c1263 |
591 | menuItems.append(("Rename Grooveshark playlist", "XBMC.RunPlugin("+mvplaylst+")")) |
7ea6f166 |
592 | dir.addContextMenuItems(menuItems, replaceItems=False) |
31731635 |
593 | return xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]),url=u,listitem=dir,isFolder=True, totalItems=items) |
6ae708d0 |
594 | |
8817bb2e |
595 | |
596 | def get_params(): |
597 | param=[] |
598 | paramstring=sys.argv[2] |
599 | if len(paramstring)>=2: |
600 | params=sys.argv[2] |
601 | cleanedparams=params.replace('?','') |
602 | if (params[len(params)-1]=='/'): |
603 | params=params[0:len(params)-2] |
604 | pairsofparams=cleanedparams.split('&') |
605 | param={} |
606 | for i in range(len(pairsofparams)): |
607 | splitparams={} |
608 | splitparams=pairsofparams[i].split('=') |
609 | if (len(splitparams))==2: |
610 | param[splitparams[0]]=splitparams[1] |
611 | print param |
612 | return param |
613 | |
614 | grooveshark = Groveshark(); |
615 | |
616 | params=get_params() |
617 | url=None |
618 | name=None |
619 | mode=None |
620 | id=None |
621 | |
622 | try: url=urllib.unquote_plus(params["url"]) |
623 | except: pass |
624 | try: name=urllib.unquote_plus(params["name"]) |
625 | except: pass |
626 | try: mode=int(params["mode"]) |
627 | except: pass |
628 | try: id=int(params["id"]) |
629 | except: pass |
630 | |
8817bb2e |
631 | if mode==None: |
632 | grooveshark.categories() |
633 | |
634 | elif mode==MODE_SEARCH_SONGS: |
635 | grooveshark.searchSongs() |
636 | |
637 | elif mode==MODE_SEARCH_ALBUMS: |
638 | grooveshark.searchAlbums() |
639 | |
640 | elif mode==MODE_SEARCH_ARTISTS: |
641 | grooveshark.searchArtists() |
642 | |
36cc00d7 |
643 | elif mode==MODE_POPULAR_SONGS: |
644 | grooveshark.popularSongs() |
8817bb2e |
645 | |
646 | elif mode==MODE_PLAYLISTS: |
647 | grooveshark.playlists() |
648 | |
649 | elif mode==MODE_FAVORITES: |
650 | grooveshark.favorites() |
651 | |
652 | elif mode==MODE_SONG: |
8817bb2e |
653 | try: album=urllib.unquote_plus(params["album"]) |
654 | except: pass |
655 | try: artist=urllib.unquote_plus(params["artist"]) |
656 | except: pass |
657 | try: duration=int(params["duration"]) |
658 | except: pass |
b738088f |
659 | try: thumb=urllib.unquote_plus(params["thumb"]) |
660 | except: pass |
661 | try: image=urllib.unquote_plus(params["image"]) |
662 | except: pass |
6ae708d0 |
663 | song = grooveshark.songItem(id, name, album, artist, duration, thumb, image) |
3fcef5ba |
664 | grooveshark.playSong(song) |
8817bb2e |
665 | |
666 | elif mode==MODE_ARTIST: |
4be42357 |
667 | grooveshark.artist(id) |
8817bb2e |
668 | |
669 | elif mode==MODE_ALBUM: |
4be42357 |
670 | grooveshark.album(id) |
8817bb2e |
671 | |
672 | elif mode==MODE_PLAYLIST: |
4be42357 |
673 | grooveshark.playlist(id, name) |
8817bb2e |
674 | |
675 | elif mode==MODE_FAVORITE: |
4be42357 |
676 | grooveshark.favorite(id) |
8817bb2e |
677 | |
678 | elif mode==MODE_UNFAVORITE: |
4be42357 |
679 | try: prevMode=urllib.unquote_plus(params["prevmode"]) |
680 | except: pass |
681 | grooveshark.unfavorite(id, prevMode) |
3fcef5ba |
682 | |
406ab447 |
683 | elif mode==MODE_SIMILAR_ARTISTS: |
4be42357 |
684 | grooveshark.similarArtists(id) |
3fcef5ba |
685 | |
36cc00d7 |
686 | elif mode==MODE_MAKE_PLAYLIST: |
4be42357 |
687 | grooveshark.makePlaylist(id, name) |
36cc00d7 |
688 | |
689 | elif mode==MODE_REMOVE_PLAYLIST: |
4be42357 |
690 | grooveshark.removePlaylist(id, name) |
36cc00d7 |
691 | |
692 | elif mode==MODE_RENAME_PLAYLIST: |
4be42357 |
693 | grooveshark.renamePlaylist(id, name) |
36cc00d7 |
694 | |
4be42357 |
695 | elif mode==MODE_REMOVE_PLAYLIST_SONG: |
696 | try: playlistID=urllib.unquote_plus(params["playlistid"]) |
697 | except: pass |
698 | grooveshark.removePlaylistSong(playlistID, name, id) |
36cc00d7 |
699 | |
7ea6f166 |
700 | elif mode==MODE_ADD_PLAYLIST_SONG: |
701 | grooveshark.addPlaylistSong(id) |
702 | |
8817bb2e |
703 | if (mode < MODE_SONG): |
704 | xbmcplugin.endOfDirectory(int(sys.argv[1])) |