2d388879 |
1 | # Copyright 2011 Stephen Denham |
2 | |
3 | # This file is part of xbmc-groove. |
4 | # |
5 | # xbmc-groove is free software: you can redistribute it and/or modify |
6 | # it under the terms of the GNU General Public License as published by |
7 | # the Free Software Foundation, either version 3 of the License, or |
8 | # (at your option) any later version. |
9 | # |
10 | # xbmc-groove is distributed in the hope that it will be useful, |
11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | # GNU General Public License for more details. |
14 | # |
15 | # You should have received a copy of the GNU General Public License |
16 | # along with xbmc-groove. If not, see <http://www.gnu.org/licenses/>. |
17 | |
18 | |
f95afae7 |
19 | import urllib, sys, os, shutil, re, pickle, time, tempfile, xbmcaddon, xbmcplugin, xbmcgui, xbmc |
2d388879 |
20 | |
b26b96e6 |
21 | __addon__ = xbmcaddon.Addon('plugin.audio.groove') |
2d388879 |
22 | __addonname__ = __addon__.getAddonInfo('name') |
23 | __cwd__ = __addon__.getAddonInfo('path') |
24 | __author__ = __addon__.getAddonInfo('author') |
25 | __version__ = __addon__.getAddonInfo('version') |
26 | __language__ = __addon__.getLocalizedString |
9f9eadcd |
27 | __debugging__ = __addon__.getSetting('debug') |
5e72534b |
28 | |
8817bb2e |
29 | MODE_SEARCH_SONGS = 1 |
30 | MODE_SEARCH_ALBUMS = 2 |
31 | MODE_SEARCH_ARTISTS = 3 |
86f629ea |
32 | MODE_SEARCH_ARTISTS_ALBUMS = 4 |
33 | MODE_SEARCH_PLAYLISTS = 5 |
97289139 |
34 | MODE_ARTIST_POPULAR = 6 |
35 | MODE_POPULAR_SONGS = 7 |
36 | MODE_FAVORITES = 8 |
37 | MODE_PLAYLISTS = 9 |
38 | MODE_ALBUM = 10 |
39 | MODE_ARTIST = 11 |
40 | MODE_PLAYLIST = 12 |
41 | MODE_SONG_PAGE = 13 |
052028f1 |
42 | MODE_SIMILAR_ARTISTS = 14 |
43 | MODE_SONG = 15 |
44 | MODE_FAVORITE = 16 |
45 | MODE_UNFAVORITE = 17 |
46 | MODE_MAKE_PLAYLIST = 18 |
47 | MODE_REMOVE_PLAYLIST = 19 |
48 | MODE_RENAME_PLAYLIST = 20 |
49 | MODE_REMOVE_PLAYLIST_SONG = 21 |
50 | MODE_ADD_PLAYLIST_SONG = 22 |
7ea6f166 |
51 | |
38df1fa5 |
52 | ACTION_MOVE_LEFT = 1 |
7ea6f166 |
53 | ACTION_MOVE_UP = 3 |
54 | ACTION_MOVE_DOWN = 4 |
55 | ACTION_PAGE_UP = 5 |
56 | ACTION_PAGE_DOWN = 6 |
57 | ACTION_SELECT_ITEM = 7 |
58 | ACTION_PREVIOUS_MENU = 10 |
8817bb2e |
59 | |
86f629ea |
60 | # Formats for track labels |
61 | ARTIST_ALBUM_NAME_LABEL = 0 |
62 | NAME_ALBUM_ARTIST_LABEL = 1 |
63 | |
f95afae7 |
64 | # Stream marking time (seconds) |
65 | STREAM_MARKING_TIME = 30 |
cd937dea |
66 | |
67 | # Timeout |
68 | STREAM_TIMEOUT = 30 |
69 | |
f95afae7 |
70 | songMarkTime = 0 |
71 | player = xbmc.Player() |
72 | playTimer = None |
73 | |
2d388879 |
74 | baseDir = __cwd__ |
6ae708d0 |
75 | resDir = xbmc.translatePath(os.path.join(baseDir, 'resources')) |
8817bb2e |
76 | libDir = xbmc.translatePath(os.path.join(resDir, 'lib')) |
77 | imgDir = xbmc.translatePath(os.path.join(resDir, 'img')) |
2d388879 |
78 | cacheDir = os.path.join(xbmc.translatePath('special://masterprofile/addon_data/'), os.path.basename(baseDir)) |
7ce01be6 |
79 | thumbDirName = 'thumb' |
191f20f2 |
80 | thumbDir = os.path.join(xbmc.translatePath('special://masterprofile/addon_data/'), os.path.basename(baseDir), thumbDirName) |
4be42357 |
81 | |
82 | baseModeUrl = 'plugin://plugin.audio.groove/' |
e278f474 |
83 | playlistUrl = baseModeUrl + '?mode=' + str(MODE_PLAYLIST) |
4be42357 |
84 | playlistsUrl = baseModeUrl + '?mode=' + str(MODE_PLAYLISTS) |
85 | favoritesUrl = baseModeUrl + '?mode=' + str(MODE_FAVORITES) |
86 | |
b26b96e6 |
87 | searchArtistsAlbumsName = __language__(30006) |
3d95dfcb |
88 | |
7ce01be6 |
89 | thumbDef = os.path.join(imgDir, 'default.tbn') |
052028f1 |
90 | listBackground = os.path.join(imgDir, 'listbackground.png') |
8817bb2e |
91 | |
92 | sys.path.append (libDir) |
7ce01be6 |
93 | from GroovesharkAPI import GrooveAPI |
f95afae7 |
94 | from threading import Event, Thread |
7ce01be6 |
95 | |
9f9eadcd |
96 | if __debugging__ == 'true': |
97 | __debugging__ = True |
98 | else: |
99 | __debugging__ = False |
100 | |
a3ad8f73 |
101 | try: |
9f9eadcd |
102 | groovesharkApi = GrooveAPI(__debugging__) |
7ce01be6 |
103 | if groovesharkApi.pingService() != True: |
b26b96e6 |
104 | raise StandardError(__language__(30007)) |
a3ad8f73 |
105 | except: |
b26b96e6 |
106 | dialog = xbmcgui.Dialog(__language__(30008),__language__(30009),__language__(30010)) |
6d909a7d |
107 | dialog.ok(__language__(30008),__language__(30009)) |
a3ad8f73 |
108 | sys.exit(-1) |
109 | |
f95afae7 |
110 | # Mark song as playing or played |
cfad7202 |
111 | def markSong(songid, duration, streamKey, streamServerID): |
f95afae7 |
112 | global songMarkTime |
113 | global playTimer |
114 | global player |
115 | if player.isPlayingAudio(): |
116 | tNow = player.getTime() |
117 | if tNow >= STREAM_MARKING_TIME and songMarkTime == 0: |
cfad7202 |
118 | groovesharkApi.markStreamKeyOver30Secs(streamKey, streamServerID) |
f95afae7 |
119 | songMarkTime = tNow |
120 | elif duration > tNow and duration - tNow < 2 and songMarkTime >= STREAM_MARKING_TIME: |
121 | playTimer.cancel() |
122 | songMarkTime = 0 |
cfad7202 |
123 | groovesharkApi.markSongComplete(songid, streamKey, streamServerID) |
f95afae7 |
124 | else: |
125 | playTimer.cancel() |
126 | songMarkTime = 0 |
127 | |
8817bb2e |
128 | class _Info: |
129 | def __init__( self, *args, **kwargs ): |
130 | self.__dict__.update( kwargs ) |
e278f474 |
131 | |
052028f1 |
132 | # Window dialog to select a grooveshark playlist |
133 | class GroovesharkPlaylistSelect(xbmcgui.WindowDialog): |
134 | |
135 | def __init__(self, items=[]): |
136 | gap = int(self.getHeight()/100) |
137 | w = int(self.getWidth()*0.5) |
138 | h = self.getHeight()-30*gap |
139 | rw = self.getWidth() |
140 | rh = self.getHeight() |
141 | x = rw/2 - w/2 |
142 | y = rh/2 -h/2 |
143 | |
144 | self.imgBg = xbmcgui.ControlImage(x+gap, 5*gap+y, w-2*gap, h-5*gap, listBackground) |
145 | self.addControl(self.imgBg) |
146 | |
147 | 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) |
148 | self.addControl(self.playlistControl) |
149 | |
150 | self.lastPos = 0 |
151 | self.isSelecting = False |
152 | self.selected = -1 |
153 | listitems = [] |
154 | for playlist in items: |
155 | listitems.append(xbmcgui.ListItem(playlist[0])) |
b26b96e6 |
156 | listitems.append(xbmcgui.ListItem(__language__(30011))) |
052028f1 |
157 | self.playlistControl.addItems(listitems) |
158 | self.setFocus(self.playlistControl) |
159 | self.playlistControl.selectItem(0) |
160 | item = self.playlistControl.getListItem(self.lastPos) |
161 | item.select(True) |
162 | |
163 | # Highlight selected item |
164 | def setHighlight(self): |
165 | if self.isSelecting: |
166 | return |
167 | else: |
168 | self.isSelecting = True |
169 | |
170 | pos = self.playlistControl.getSelectedPosition() |
171 | if pos >= 0: |
172 | item = self.playlistControl.getListItem(self.lastPos) |
173 | item.select(False) |
174 | item = self.playlistControl.getListItem(pos) |
175 | item.select(True) |
176 | self.lastPos = pos |
177 | self.isSelecting = False |
178 | |
179 | # Control - select |
180 | def onControl(self, control): |
181 | if control == self.playlistControl: |
182 | self.selected = self.playlistControl.getSelectedPosition() |
183 | self.close() |
184 | |
185 | # Action - close or up/down |
186 | def onAction(self, action): |
187 | if action == ACTION_PREVIOUS_MENU: |
188 | self.selected = -1 |
189 | self.close() |
190 | elif action == ACTION_MOVE_UP or action == ACTION_MOVE_DOWN or action == ACTION_PAGE_UP or action == ACTION_PAGE_DOWN == 6: |
191 | self.setFocus(self.playlistControl) |
192 | self.setHighlight() |
f95afae7 |
193 | |
194 | |
195 | class PlayTimer(Thread): |
196 | # interval -- floating point number specifying the number of seconds to wait before executing function |
197 | # function -- the function (or callable object) to be executed |
198 | |
199 | # iterations -- integer specifying the number of iterations to perform |
200 | # args -- list of positional arguments passed to function |
201 | # kwargs -- dictionary of keyword arguments passed to function |
202 | |
203 | def __init__(self, interval, function, iterations=0, args=[], kwargs={}): |
204 | Thread.__init__(self) |
205 | self.interval = interval |
206 | self.function = function |
207 | self.iterations = iterations |
208 | self.args = args |
209 | self.kwargs = kwargs |
210 | self.finished = Event() |
211 | |
212 | def run(self): |
213 | count = 0 |
214 | while not self.finished.isSet() and (self.iterations <= 0 or count < self.iterations): |
215 | self.finished.wait(self.interval) |
216 | if not self.finished.isSet(): |
217 | self.function(*self.args, **self.kwargs) |
218 | count += 1 |
219 | |
220 | def cancel(self): |
221 | self.finished.set() |
222 | |
223 | def setIterations(self, iterations): |
224 | self.iterations = iterations |
225 | |
226 | |
227 | def getTime(self): |
228 | return self.iterations * self.interval |
229 | |
230 | |
5e72534b |
231 | class Grooveshark: |
973b4c6c |
232 | |
7ea6f166 |
233 | albumImg = xbmc.translatePath(os.path.join(imgDir, 'album.png')) |
234 | artistImg = xbmc.translatePath(os.path.join(imgDir, 'artist.png')) |
86f629ea |
235 | artistsAlbumsImg = xbmc.translatePath(os.path.join(imgDir, 'artistsalbums.png')) |
7ea6f166 |
236 | favoritesImg = xbmc.translatePath(os.path.join(imgDir, 'favorites.png')) |
237 | playlistImg = xbmc.translatePath(os.path.join(imgDir, 'playlist.png')) |
86f629ea |
238 | usersplaylistsImg = xbmc.translatePath(os.path.join(imgDir, 'usersplaylists.png')) |
7ea6f166 |
239 | popularSongsImg = xbmc.translatePath(os.path.join(imgDir, 'popularSongs.png')) |
97289139 |
240 | popularSongsArtistImg = xbmc.translatePath(os.path.join(imgDir, 'popularSongsArtist.png')) |
7ea6f166 |
241 | songImg = xbmc.translatePath(os.path.join(imgDir, 'song.png')) |
242 | defImg = xbmc.translatePath(os.path.join(imgDir, 'default.tbn')) |
57ffc218 |
243 | fanImg = xbmc.translatePath(os.path.join(baseDir, 'fanart.jpg')) |
8817bb2e |
244 | |
2254a6b5 |
245 | settings = xbmcaddon.Addon(id='plugin.audio.groove') |
7ce01be6 |
246 | songsearchlimit = int(settings.getSetting('songsearchlimit')) |
247 | albumsearchlimit = int(settings.getSetting('albumsearchlimit')) |
248 | artistsearchlimit = int(settings.getSetting('artistsearchlimit')) |
97289139 |
249 | songspagelimit = int(settings.getSetting('songspagelimit')) |
2254a6b5 |
250 | username = settings.getSetting('username') |
251 | password = settings.getSetting('password') |
5e72534b |
252 | |
38df1fa5 |
253 | userid = 0 |
4be42357 |
254 | |
8817bb2e |
255 | def __init__( self ): |
256 | self._handle = int(sys.argv[1]) |
7ce01be6 |
257 | if os.path.isdir(cacheDir) == False: |
258 | os.makedirs(cacheDir) |
9f9eadcd |
259 | if __debugging__ : |
5e72534b |
260 | xbmc.log(__language__(30012) + " " + cacheDir) |
164e42d8 |
261 | artDir = xbmc.translatePath(thumbDir) |
262 | if os.path.isdir(artDir) == False: |
3a794693 |
263 | os.makedirs(artDir) |
9f9eadcd |
264 | if __debugging__ : |
5e72534b |
265 | xbmc.log(__language__(30012) + " " + artDir) |
052028f1 |
266 | |
e278f474 |
267 | # Top-level menu |
8817bb2e |
268 | def categories(self): |
2254a6b5 |
269 | |
38df1fa5 |
270 | self.userid = self._get_login() |
b738088f |
271 | |
272 | # Setup |
6ae708d0 |
273 | xbmcplugin.setPluginFanart(int(sys.argv[1]), self.fanImg) |
6ae708d0 |
274 | |
b26b96e6 |
275 | self._add_dir(__language__(30013), '', MODE_SEARCH_SONGS, self.songImg, 0) |
276 | self._add_dir(__language__(30014), '', MODE_SEARCH_ALBUMS, self.albumImg, 0) |
277 | self._add_dir(__language__(30015), '', MODE_SEARCH_ARTISTS, self.artistImg, 0) |
3d95dfcb |
278 | self._add_dir(searchArtistsAlbumsName, '', MODE_SEARCH_ARTISTS_ALBUMS, self.artistsAlbumsImg, 0) |
f95afae7 |
279 | # Not supported by key |
280 | #self._add_dir("Search for user's playlists...", '', MODE_SEARCH_PLAYLISTS, self.usersplaylistsImg, 0) |
b26b96e6 |
281 | self._add_dir(__language__(30016), '', MODE_ARTIST_POPULAR, self.popularSongsArtistImg, 0) |
282 | self._add_dir(__language__(30017), '', MODE_POPULAR_SONGS, self.popularSongsImg, 0) |
38df1fa5 |
283 | if (self.userid != 0): |
b26b96e6 |
284 | self._add_dir(__language__(30018), '', MODE_FAVORITES, self.favoritesImg, 0) |
285 | self._add_dir(__language__(30019), '', MODE_PLAYLISTS, self.playlistImg, 0) |
e278f474 |
286 | |
287 | # Search for songs |
8817bb2e |
288 | def searchSongs(self): |
b26b96e6 |
289 | query = self._get_keyboard(default="", heading=__language__(30020)) |
7ea6f166 |
290 | if (query != ''): |
7ce01be6 |
291 | songs = groovesharkApi.getSongSearchResults(query, limit = self.songsearchlimit) |
8817bb2e |
292 | if (len(songs) > 0): |
6ae708d0 |
293 | self._add_songs_directory(songs) |
8817bb2e |
294 | else: |
295 | dialog = xbmcgui.Dialog() |
b26b96e6 |
296 | dialog.ok(__language__(30008), __language__(30021)) |
8817bb2e |
297 | self.categories() |
7ea6f166 |
298 | else: |
299 | self.categories() |
8817bb2e |
300 | |
e278f474 |
301 | # Search for albums |
8817bb2e |
302 | def searchAlbums(self): |
b26b96e6 |
303 | query = self._get_keyboard(default="", heading=__language__(30022)) |
7ea6f166 |
304 | if (query != ''): |
7ce01be6 |
305 | albums = groovesharkApi.getAlbumSearchResults(query, limit = self.albumsearchlimit) |
8817bb2e |
306 | if (len(albums) > 0): |
6ae708d0 |
307 | self._add_albums_directory(albums) |
8817bb2e |
308 | else: |
309 | dialog = xbmcgui.Dialog() |
b26b96e6 |
310 | dialog.ok(__language__(30008), __language__(30023)) |
8817bb2e |
311 | self.categories() |
7ea6f166 |
312 | else: |
313 | self.categories() |
8817bb2e |
314 | |
e278f474 |
315 | # Search for artists |
8817bb2e |
316 | def searchArtists(self): |
b26b96e6 |
317 | query = self._get_keyboard(default="", heading=__language__(30024)) |
7ea6f166 |
318 | if (query != ''): |
7ce01be6 |
319 | artists = groovesharkApi.getArtistSearchResults(query, limit = self.artistsearchlimit) |
8817bb2e |
320 | if (len(artists) > 0): |
6ae708d0 |
321 | self._add_artists_directory(artists) |
8817bb2e |
322 | else: |
323 | dialog = xbmcgui.Dialog() |
b26b96e6 |
324 | dialog.ok(__language__(30008), __language__(30025)) |
8817bb2e |
325 | self.categories() |
7ea6f166 |
326 | else: |
327 | self.categories() |
86f629ea |
328 | |
329 | # Search for playlists |
330 | def searchPlaylists(self): |
b26b96e6 |
331 | query = self._get_keyboard(default="", heading=__language__(30026)) |
86f629ea |
332 | if (query != ''): |
f95afae7 |
333 | playlists = groovesharkApi.getUserPlaylistsByUsername(query) |
86f629ea |
334 | if (len(playlists) > 0): |
335 | self._add_playlists_directory(playlists) |
336 | else: |
337 | dialog = xbmcgui.Dialog() |
b26b96e6 |
338 | dialog.ok(__language__(30008), __language__(30027)) |
86f629ea |
339 | self.categories() |
340 | else: |
341 | self.categories() |
342 | |
343 | # Search for artists albums |
3d95dfcb |
344 | def searchArtistsAlbums(self, artistName = None): |
345 | if artistName == None or artistName == searchArtistsAlbumsName: |
b26b96e6 |
346 | query = self._get_keyboard(default="", heading=__language__(30028)) |
99f72740 |
347 | else: |
348 | query = artistName |
86f629ea |
349 | if (query != ''): |
350 | artists = groovesharkApi.getArtistSearchResults(query, limit = self.artistsearchlimit) |
351 | if (len(artists) > 0): |
352 | artist = artists[0] |
353 | artistID = artist[1] |
9f9eadcd |
354 | if __debugging__ : |
5e72534b |
355 | xbmc.log("Found " + artist[0] + "...") |
8d7ba769 |
356 | albums = groovesharkApi.getArtistAlbums(artistID, self.albumsearchlimit) |
86f629ea |
357 | if (len(albums) > 0): |
052028f1 |
358 | self._add_albums_directory(albums, artistID) |
86f629ea |
359 | else: |
360 | dialog = xbmcgui.Dialog() |
b26b96e6 |
361 | dialog.ok(__language__(30008), __language__(30029)) |
86f629ea |
362 | self.categories() |
363 | else: |
364 | dialog = xbmcgui.Dialog() |
b26b96e6 |
365 | dialog.ok(__language__(30008), __language__(30030)) |
86f629ea |
366 | self.categories() |
367 | else: |
368 | self.categories() |
369 | |
e278f474 |
370 | # Get my favorites |
8817bb2e |
371 | def favorites(self): |
36cc00d7 |
372 | userid = self._get_login() |
373 | if (userid != 0): |
7ce01be6 |
374 | favorites = groovesharkApi.getUserFavoriteSongs() |
36cc00d7 |
375 | if (len(favorites) > 0): |
052028f1 |
376 | self._add_songs_directory(favorites, isFavorites=True) |
36cc00d7 |
377 | else: |
378 | dialog = xbmcgui.Dialog() |
b26b96e6 |
379 | dialog.ok(__language__(30008), __language__(30031)) |
36cc00d7 |
380 | self.categories() |
8817bb2e |
381 | |
e278f474 |
382 | # Get popular songs |
36cc00d7 |
383 | def popularSongs(self): |
7ce01be6 |
384 | popular = groovesharkApi.getPopularSongsToday(limit = self.songsearchlimit) |
8817bb2e |
385 | if (len(popular) > 0): |
6ae708d0 |
386 | self._add_songs_directory(popular) |
8817bb2e |
387 | else: |
388 | dialog = xbmcgui.Dialog() |
b26b96e6 |
389 | dialog.ok(__language__(30008), __language__(30032)) |
8817bb2e |
390 | self.categories() |
36cc00d7 |
391 | |
e278f474 |
392 | # Get my playlists |
8817bb2e |
393 | def playlists(self): |
394 | userid = self._get_login() |
395 | if (userid != 0): |
7ce01be6 |
396 | playlists = groovesharkApi.getUserPlaylists() |
8817bb2e |
397 | if (len(playlists) > 0): |
6ae708d0 |
398 | self._add_playlists_directory(playlists) |
8817bb2e |
399 | else: |
400 | dialog = xbmcgui.Dialog() |
b26b96e6 |
401 | dialog.ok(__language__(30008), __language__(30033)) |
8817bb2e |
402 | self.categories() |
7ea6f166 |
403 | else: |
404 | dialog = xbmcgui.Dialog() |
b26b96e6 |
405 | dialog.ok(__language__(30008), __language__(30034), __language__(30035)) |
7ea6f166 |
406 | |
e278f474 |
407 | # Make songs a favorite |
8817bb2e |
408 | def favorite(self, songid): |
409 | userid = self._get_login() |
410 | if (userid != 0): |
9f9eadcd |
411 | if __debugging__ : |
5e72534b |
412 | xbmc.log("Favorite song: " + str(songid)) |
7ce01be6 |
413 | groovesharkApi.addUserFavoriteSong(songID = songid) |
b26b96e6 |
414 | xbmc.executebuiltin('XBMC.Notification(' + __language__(30008) + ', ' + __language__(30036) + ', 1000, ' + thumbDef + ')') |
8817bb2e |
415 | else: |
416 | dialog = xbmcgui.Dialog() |
b26b96e6 |
417 | dialog.ok(__language__(30008), __language__(30034), __language__(30037)) |
052028f1 |
418 | |
419 | # Remove song from favorites |
420 | def unfavorite(self, songid, prevMode=0): |
f95afae7 |
421 | userid = self._get_login() |
052028f1 |
422 | if (userid != 0): |
9f9eadcd |
423 | if __debugging__ : |
5e72534b |
424 | xbmc.log("Unfavorite song: " + str(songid) + ', previous mode was ' + str(prevMode)) |
f95afae7 |
425 | groovesharkApi.removeUserFavoriteSongs(songIDs = songid) |
b26b96e6 |
426 | xbmc.executebuiltin('XBMC.Notification(' + __language__(30008) + ', ' + __language__(30038) + ', 1000, ' + thumbDef + ')') |
052028f1 |
427 | # Refresh to remove item from directory |
428 | if (int(prevMode) == MODE_FAVORITES): |
429 | xbmc.executebuiltin("Container.Refresh(" + favoritesUrl + ")") |
430 | else: |
431 | dialog = xbmcgui.Dialog() |
b26b96e6 |
432 | dialog.ok(__language__(30008), __language__(30034), __language__(30039)) |
052028f1 |
433 | |
e278f474 |
434 | |
435 | # Show selected album |
36cc00d7 |
436 | def album(self, albumid): |
7ce01be6 |
437 | album = groovesharkApi.getAlbumSongs(albumid, limit = self.songsearchlimit) |
86f629ea |
438 | self._add_songs_directory(album, trackLabelFormat=NAME_ALBUM_ARTIST_LABEL) |
e278f474 |
439 | |
440 | # Show selected artist |
8817bb2e |
441 | def artist(self, artistid): |
7ce01be6 |
442 | albums = groovesharkApi.getArtistAlbums(artistid, limit = self.albumsearchlimit) |
5d3b12f2 |
443 | self._add_albums_directory(albums, artistid, True) |
8817bb2e |
444 | |
e278f474 |
445 | # Show selected playlist |
e6f8730b |
446 | def playlist(self, playlistid, playlistname): |
8817bb2e |
447 | userid = self._get_login() |
448 | if (userid != 0): |
e6f8730b |
449 | songs = groovesharkApi.getPlaylistSongs(playlistid) |
052028f1 |
450 | self._add_songs_directory(songs, trackLabelFormat=NAME_ALBUM_ARTIST_LABEL, playlistid=playlistid, playlistname=playlistname) |
8817bb2e |
451 | else: |
452 | dialog = xbmcgui.Dialog() |
b26b96e6 |
453 | dialog.ok(__language__(30008), __language__(30034), __language__(30040)) |
8817bb2e |
454 | |
97289139 |
455 | # Show popular songs of the artist |
456 | def artistPopularSongs(self): |
b26b96e6 |
457 | query = self._get_keyboard(default="", heading=__language__(30041)) |
97289139 |
458 | if (query != ''): |
459 | artists = groovesharkApi.getArtistSearchResults(query, limit = self.artistsearchlimit) |
460 | if (len(artists) > 0): |
461 | artist = artists[0] |
462 | artistID = artist[1] |
9f9eadcd |
463 | if __debugging__ : |
5e72534b |
464 | xbmc.log("Found " + artist[0] + "...") |
97289139 |
465 | songs = groovesharkApi.getArtistPopularSongs(artistID, limit = self.songsearchlimit) |
466 | if (len(songs) > 0): |
467 | self._add_songs_directory(songs, trackLabelFormat=NAME_ALBUM_ARTIST_LABEL) |
468 | else: |
469 | dialog = xbmcgui.Dialog() |
b26b96e6 |
470 | dialog.ok(__language__(30008), __language__(30042)) |
97289139 |
471 | self.categories() |
472 | else: |
473 | dialog = xbmcgui.Dialog() |
b26b96e6 |
474 | dialog.ok(__language__(30008), __language__(30043)) |
97289139 |
475 | self.categories() |
476 | else: |
477 | self.categories() |
478 | |
e278f474 |
479 | # Play a song |
6ae708d0 |
480 | def playSong(self, item): |
f95afae7 |
481 | global playTimer |
482 | global player |
db319cef |
483 | player.stop() |
f95afae7 |
484 | if item != None: |
48e0c138 |
485 | url = '' |
9f9eadcd |
486 | songid = item.getProperty('songid') |
487 | duration = int(self._getSongDuration(songid)) |
48e0c138 |
488 | stream = groovesharkApi.getSubscriberStreamKey(songid) |
489 | if stream != False: |
490 | url = stream['url'] |
9f9eadcd |
491 | key = stream['StreamKey'] |
492 | server = stream['StreamServerID'] |
493 | duration = self._setDuration(stream['uSecs']) |
48e0c138 |
494 | if url != '': |
495 | item.setPath(url) |
496 | xbmcplugin.setResolvedUrl(handle=int(sys.argv[1]), succeeded=True, listitem=item) |
9f9eadcd |
497 | if __debugging__ : |
48e0c138 |
498 | xbmc.log("Grooveshark playing: " + url) |
499 | # Wait for play then start timer |
500 | seconds = 0 |
501 | while seconds < STREAM_TIMEOUT: |
502 | try: |
503 | if player.isPlayingAudio() == True: |
504 | if playTimer != None: |
505 | playTimer.cancel() |
506 | songMarkTime = 0 |
9f9eadcd |
507 | playTimer = PlayTimer(1, markSong, duration, [songid, duration, key, server]) |
48e0c138 |
508 | playTimer.start() |
509 | break |
510 | except: pass |
511 | time.sleep(1) |
512 | seconds = seconds + 1 |
513 | else: |
514 | xbmc.log("No song URL") |
7ce01be6 |
515 | else: |
b26b96e6 |
516 | xbmc.executebuiltin('XBMC.Notification(' + __language__(30008) + ', ' + __language__(30044) + ', 1000, ' + thumbDef + ')') |
f95afae7 |
517 | |
e278f474 |
518 | # Make a song directory item |
86f629ea |
519 | def songItem(self, songid, name, album, artist, coverart, trackLabelFormat=ARTIST_ALBUM_NAME_LABEL): |
7ce01be6 |
520 | songImg = self._get_icon(coverart, 'song-' + str(songid) + "-image") |
2cb26bea |
521 | if int(trackLabelFormat) == NAME_ALBUM_ARTIST_LABEL: |
86f629ea |
522 | trackLabel = name + " - " + album + " - " + artist |
523 | else: |
524 | trackLabel = artist + " - " + album + " - " + name |
48e0c138 |
525 | duration = self._getSongDuration(songid) |
86f629ea |
526 | item = xbmcgui.ListItem(label = trackLabel, thumbnailImage=songImg, iconImage=songImg) |
f95afae7 |
527 | item.setInfo( type="music", infoLabels={ "title": name, "album": album, "artist": artist, "duration": duration} ) |
2254a6b5 |
528 | item.setProperty('mimetype', 'audio/mpeg') |
529 | item.setProperty("IsPlayable", "true") |
7ce01be6 |
530 | item.setProperty('songid', str(songid)) |
531 | item.setProperty('coverart', songImg) |
532 | item.setProperty('title', name) |
533 | item.setProperty('album', album) |
534 | item.setProperty('artist', artist) |
f95afae7 |
535 | item.setProperty('duration', str(duration)) |
7ce01be6 |
536 | |
6ae708d0 |
537 | return item |
2254a6b5 |
538 | |
97289139 |
539 | # Next page of songs |
5e72534b |
540 | def songPage(self, offset, trackLabelFormat, playlistid = 0, playlistname = ''): |
541 | self._add_songs_directory([], trackLabelFormat, offset, playlistid = playlistid, playlistname = playlistname) |
052028f1 |
542 | |
543 | # Make a playlist from an album |
544 | def makePlaylist(self, albumid, name): |
f95afae7 |
545 | userid = self._get_login() |
052028f1 |
546 | if (userid != 0): |
547 | re.split(' - ',name,1) |
548 | nameTokens = re.split(' - ',name,1) # suggested name |
b26b96e6 |
549 | name = self._get_keyboard(default=nameTokens[0], heading=__language__(30045)) |
052028f1 |
550 | if name != '': |
551 | album = groovesharkApi.getAlbumSongs(albumid, limit = self.songsearchlimit) |
552 | songids = [] |
553 | for song in album: |
554 | songids.append(song[1]) |
f95afae7 |
555 | if groovesharkApi.createPlaylist(name, songids) == 0: |
052028f1 |
556 | dialog = xbmcgui.Dialog() |
b26b96e6 |
557 | dialog.ok(__language__(30008), __language__(30046), name) |
052028f1 |
558 | else: |
b26b96e6 |
559 | xbmc.executebuiltin('XBMC.Notification(' + __language__(30008) + ',' + __language__(30047)+ ', 1000, ' + thumbDef + ')') |
052028f1 |
560 | else: |
561 | dialog = xbmcgui.Dialog() |
b26b96e6 |
562 | dialog.ok(__language__(30008), __language__(30034), __language__(30048)) |
052028f1 |
563 | |
564 | # Rename a playlist |
565 | def renamePlaylist(self, playlistid, name): |
f95afae7 |
566 | userid = self._get_login() |
052028f1 |
567 | if (userid != 0): |
b26b96e6 |
568 | newname = self._get_keyboard(default=name, heading=__language__(30049)) |
052028f1 |
569 | if newname == '': |
570 | return |
f95afae7 |
571 | elif groovesharkApi.playlistRename(playlistid, newname) == 0: |
052028f1 |
572 | dialog = xbmcgui.Dialog() |
b26b96e6 |
573 | dialog.ok(__language__(30008), __language__(30050), name) |
052028f1 |
574 | else: |
575 | # Refresh to show new item name |
576 | xbmc.executebuiltin("Container.Refresh") |
577 | else: |
578 | dialog = xbmcgui.Dialog() |
b26b96e6 |
579 | dialog.ok(__language__(30008), __language__(30034), __language__(30051)) |
052028f1 |
580 | |
581 | # Remove a playlist |
582 | def removePlaylist(self, playlistid, name): |
583 | dialog = xbmcgui.Dialog() |
b26b96e6 |
584 | if dialog.yesno(__language__(30008), name, __language__(30052)) == True: |
f95afae7 |
585 | userid = self._get_login() |
052028f1 |
586 | if (userid != 0): |
f95afae7 |
587 | if groovesharkApi.playlistDelete(playlistid) == 0: |
052028f1 |
588 | dialog = xbmcgui.Dialog() |
b26b96e6 |
589 | dialog.ok(__language__(30008), __language__(30053), name) |
052028f1 |
590 | else: |
591 | # Refresh to remove item from directory |
592 | xbmc.executebuiltin("Container.Refresh(" + playlistsUrl + ")") |
593 | else: |
594 | dialog = xbmcgui.Dialog() |
b26b96e6 |
595 | dialog.ok(__language__(30008), __language__(30034), __language__(30054)) |
052028f1 |
596 | |
597 | # Add song to playlist |
598 | def addPlaylistSong(self, songid): |
f95afae7 |
599 | userid = self._get_login() |
052028f1 |
600 | if (userid != 0): |
601 | playlists = groovesharkApi.getUserPlaylists() |
602 | if (len(playlists) > 0): |
603 | ret = 0 |
604 | # Select the playlist |
605 | playlistSelect = GroovesharkPlaylistSelect(items=playlists) |
606 | playlistSelect.setFocus(playlistSelect.playlistControl) |
607 | playlistSelect.doModal() |
608 | i = playlistSelect.selected |
609 | del playlistSelect |
610 | if i > -1: |
611 | # Add a new playlist |
612 | if i >= len(playlists): |
b26b96e6 |
613 | name = self._get_keyboard(default='', heading=__language__(30055)) |
052028f1 |
614 | if name != '': |
615 | songIds = [] |
616 | songIds.append(songid) |
f95afae7 |
617 | if groovesharkApi.createPlaylist(name, songIds) == 0: |
052028f1 |
618 | dialog = xbmcgui.Dialog() |
b26b96e6 |
619 | dialog.ok(__language__(30008), __language__(30056), name) |
052028f1 |
620 | else: |
b26b96e6 |
621 | xbmc.executebuiltin('XBMC.Notification(' + __language__(30008) + ',' + __language__(30057) + ', 1000, ' + thumbDef + ')') |
052028f1 |
622 | # Existing playlist |
623 | else: |
624 | playlist = playlists[i] |
625 | playlistid = playlist[1] |
9f9eadcd |
626 | if __debugging__ : |
5e72534b |
627 | xbmc.log("Add song " + str(songid) + " to playlist " + str(playlistid)) |
f95afae7 |
628 | songIDs=[] |
629 | songs = groovesharkApi.getPlaylistSongs(playlistid) |
630 | for song in songs: |
631 | songIDs.append(song[1]) |
632 | songIDs.append(songid) |
633 | ret = groovesharkApi.setPlaylistSongs(playlistid, songIDs) |
634 | if ret == False: |
052028f1 |
635 | dialog = xbmcgui.Dialog() |
b26b96e6 |
636 | dialog.ok(__language__(30008), __language__(30058)) |
052028f1 |
637 | else: |
b26b96e6 |
638 | xbmc.executebuiltin('XBMC.Notification(' + __language__(30008) + ',' + __language__(30059) + ', 1000, ' + thumbDef + ')') |
052028f1 |
639 | else: |
640 | dialog = xbmcgui.Dialog() |
b26b96e6 |
641 | dialog.ok(__language__(30008), __language__(30060)) |
052028f1 |
642 | self.categories() |
643 | else: |
644 | dialog = xbmcgui.Dialog() |
b26b96e6 |
645 | dialog.ok(__language__(30008), __language__(30034), __language__(30061)) |
052028f1 |
646 | |
647 | # Remove song from playlist |
f95afae7 |
648 | def removePlaylistSong(self, playlistid, playlistname, songid): |
e6f8730b |
649 | dialog = xbmcgui.Dialog() |
b26b96e6 |
650 | if dialog.yesno(__language__(30008), __language__(30062), __language__(30063)) == True: |
f95afae7 |
651 | userid = self._get_login() |
052028f1 |
652 | if (userid != 0): |
f95afae7 |
653 | songs = groovesharkApi.getPlaylistSongs(playlistID) |
654 | songIDs=[] |
655 | for song in songs: |
656 | if (song[1] != songid): |
657 | songIDs.append(song[1]) |
658 | ret = groovesharkApi.setPlaylistSongs(playlistID, songIDs) |
659 | if ret == False: |
052028f1 |
660 | dialog = xbmcgui.Dialog() |
b26b96e6 |
661 | dialog.ok(__language__(30008), __language__(30064), __language__(30065)) |
052028f1 |
662 | else: |
663 | # Refresh to remove item from directory |
b26b96e6 |
664 | xbmc.executebuiltin('XBMC.Notification(' + __language__(30008) + ',' + __language__(30066)+ ', 1000, ' + thumbDef + ')') |
e6f8730b |
665 | xbmc.executebuiltin("Container.Update(" + playlistUrl + "&id="+str(playlistid) + "&name=" + playlistname + ")") |
052028f1 |
666 | else: |
667 | dialog = xbmcgui.Dialog() |
b26b96e6 |
668 | dialog.ok(__language__(30008), __language__(30034), __language__(30067)) |
052028f1 |
669 | |
670 | # Find similar artists to searched artist |
671 | def similarArtists(self, artistId): |
f95afae7 |
672 | similar = groovesharkApi.getSimilarArtists(artistId, limit = self.artistsearchlimit) |
052028f1 |
673 | if (len(similar) > 0): |
674 | self._add_artists_directory(similar) |
675 | else: |
676 | dialog = xbmcgui.Dialog() |
b26b96e6 |
677 | dialog.ok(__language__(30008), __language__(30068)) |
052028f1 |
678 | self.categories() |
5e72534b |
679 | |
e278f474 |
680 | # Get keyboard input |
8817bb2e |
681 | def _get_keyboard(self, default="", heading="", hidden=False): |
3cfead3c |
682 | kb = xbmc.Keyboard(default, heading, hidden) |
683 | kb.doModal() |
684 | if (kb.isConfirmed()): |
685 | return unicode(kb.getText(), "utf-8") |
686 | return '' |
8817bb2e |
687 | |
e278f474 |
688 | # Login to grooveshark |
f95afae7 |
689 | def _get_login(self): |
2254a6b5 |
690 | if (self.username == "" or self.password == ""): |
8817bb2e |
691 | dialog = xbmcgui.Dialog() |
b26b96e6 |
692 | dialog.ok(__language__(30008), __language__(30069), __language__(30070)) |
8817bb2e |
693 | return 0 |
694 | else: |
38df1fa5 |
695 | if self.userid == 0: |
f95afae7 |
696 | uid = groovesharkApi.login(self.username, self.password) |
38df1fa5 |
697 | if (uid != 0): |
38df1fa5 |
698 | return uid |
8817bb2e |
699 | else: |
700 | dialog = xbmcgui.Dialog() |
b26b96e6 |
701 | dialog.ok(__language__(30008), __language__(30069), __language__(30070)) |
8817bb2e |
702 | return 0 |
703 | |
e278f474 |
704 | # Get a song directory item |
86f629ea |
705 | def _get_song_item(self, song, trackLabelFormat): |
6ae708d0 |
706 | name = song[0] |
7ce01be6 |
707 | songid = song[1] |
708 | album = song[2] |
709 | artist = song[4] |
710 | coverart = song[6] |
86f629ea |
711 | return self.songItem(songid, name, album, artist, coverart, trackLabelFormat) |
6ae708d0 |
712 | |
713 | # File download |
7ce01be6 |
714 | def _get_icon(self, url, songid): |
715 | if url != 'None': |
716 | localThumb = os.path.join(xbmc.translatePath(os.path.join(thumbDir, str(songid)))) + '.tbn' |
717 | try: |
718 | if os.path.isfile(localThumb) == False: |
719 | loc = urllib.URLopener() |
720 | loc.retrieve(url, localThumb) |
721 | except: |
722 | shutil.copy2(thumbDef, localThumb) |
723 | return os.path.join(os.path.join(thumbDir, str(songid))) + '.tbn' |
724 | else: |
725 | return thumbDef |
e278f474 |
726 | |
727 | # Add songs to directory |
5e72534b |
728 | def _add_songs_directory(self, songs, trackLabelFormat=ARTIST_ALBUM_NAME_LABEL, offset=0, playlistid=0, playlistname='', isFavorites=False): |
97289139 |
729 | |
730 | totalSongs = len(songs) |
5e72534b |
731 | offset = int(offset) |
732 | start = 0 |
733 | end = totalSongs |
97289139 |
734 | |
735 | # No pages needed |
5e72534b |
736 | if offset == 0 and totalSongs <= self.songspagelimit: |
9f9eadcd |
737 | if __debugging__ : |
5e72534b |
738 | xbmc.log("Found " + str(totalSongs) + " songs...") |
97289139 |
739 | # Pages |
740 | else: |
5e72534b |
741 | # Cache all next pages songs |
742 | if offset == 0: |
97289139 |
743 | self._setSavedSongs(songs) |
744 | else: |
745 | songs = self._getSavedSongs() |
746 | totalSongs = len(songs) |
747 | |
748 | if totalSongs > 0: |
5e72534b |
749 | start = offset |
750 | end = min(start + self.songspagelimit,totalSongs) |
97289139 |
751 | |
052028f1 |
752 | id = 0 |
5e72534b |
753 | n = start |
754 | items = end - start |
755 | while n < end: |
756 | song = songs[n] |
6ae708d0 |
757 | songid = song[1] |
48e0c138 |
758 | duration = self._getSongDuration(songid) |
759 | if duration != -1: |
5e72534b |
760 | item = self._get_song_item(song, trackLabelFormat) |
761 | coverart = item.getProperty('coverart') |
762 | songname = song[0] |
763 | songalbum = song[2] |
764 | songartist = song[4] |
765 | u=sys.argv[0]+"?mode="+str(MODE_SONG)+"&name="+urllib.quote_plus(songname)+"&id="+str(songid) \ |
766 | +"&album="+urllib.quote_plus(songalbum) \ |
767 | +"&artist="+urllib.quote_plus(songartist) \ |
768 | +"&coverart="+urllib.quote_plus(coverart) |
769 | fav=sys.argv[0]+"?mode="+str(MODE_FAVORITE)+"&name="+urllib.quote_plus(songname)+"&id="+str(songid) |
770 | unfav=sys.argv[0]+"?mode="+str(MODE_UNFAVORITE)+"&name="+urllib.quote_plus(songname)+"&id="+str(songid)+"&prevmode=" |
771 | menuItems = [] |
772 | if isFavorites == True: |
773 | unfav = unfav +str(MODE_FAVORITES) |
774 | else: |
775 | menuItems.append((__language__(30071), "XBMC.RunPlugin("+fav+")")) |
776 | menuItems.append((__language__(30072), "XBMC.RunPlugin("+unfav+")")) |
777 | if playlistid > 0: |
778 | rmplaylstsong=sys.argv[0]+"?playlistid="+str(playlistid)+"&id="+str(songid)+"&mode="+str(MODE_REMOVE_PLAYLIST_SONG)+"&name="+playlistname |
779 | menuItems.append((__language__(30073), "XBMC.RunPlugin("+rmplaylstsong+")")) |
780 | else: |
781 | addplaylstsong=sys.argv[0]+"?id="+str(songid)+"&mode="+str(MODE_ADD_PLAYLIST_SONG) |
782 | menuItems.append((__language__(30074), "XBMC.RunPlugin("+addplaylstsong+")")) |
783 | item.addContextMenuItems(menuItems, replaceItems=False) |
784 | xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]),url=u,listitem=item,isFolder=False, totalItems=items) |
785 | id = id + 1 |
f95afae7 |
786 | else: |
5e72534b |
787 | end = min(end + 1,totalSongs) |
9f9eadcd |
788 | if __debugging__ : |
5e72534b |
789 | xbmc.log(song[0] + " does not exist.") |
790 | n = n + 1 |
97289139 |
791 | |
5e72534b |
792 | if totalSongs > end: |
793 | u=sys.argv[0]+"?mode="+str(MODE_SONG_PAGE)+"&id=playlistid"+"&offset="+str(end)+"&label="+str(trackLabelFormat)+"&name="+playlistname |
794 | self._add_dir(__language__(30075) + '...', u, MODE_SONG_PAGE, self.songImg, 0, totalSongs - end) |
cb06c186 |
795 | |
8817bb2e |
796 | xbmcplugin.setContent(self._handle, 'songs') |
31731635 |
797 | xbmcplugin.setPluginFanart(int(sys.argv[1]), self.fanImg) |
e278f474 |
798 | |
799 | # Add albums to directory |
5d3b12f2 |
800 | def _add_albums_directory(self, albums, artistid=0, isverified=False): |
31731635 |
801 | n = len(albums) |
5e72534b |
802 | itemsExisting = n |
9f9eadcd |
803 | if __debugging__ : |
5e72534b |
804 | xbmc.log("Found " + str(n) + " albums...") |
8817bb2e |
805 | i = 0 |
31731635 |
806 | while i < n: |
8817bb2e |
807 | album = albums[i] |
8817bb2e |
808 | albumID = album[3] |
5d3b12f2 |
809 | if isverified or groovesharkApi.getDoesAlbumExist(albumID): |
5e72534b |
810 | albumArtistName = album[0] |
811 | albumName = album[2] |
812 | albumImage = self._get_icon(album[4], 'album-' + str(albumID)) |
813 | self._add_dir(albumName + " - " + albumArtistName, '', MODE_ALBUM, albumImage, albumID, itemsExisting) |
814 | else: |
815 | itemsExisting = itemsExisting - 1 |
8817bb2e |
816 | i = i + 1 |
f95afae7 |
817 | # Not supported by key |
818 | #if artistid > 0: |
819 | # self._add_dir('Similar artists...', '', MODE_SIMILAR_ARTISTS, self.artistImg, artistid) |
8817bb2e |
820 | xbmcplugin.setContent(self._handle, 'albums') |
821 | xbmcplugin.addSortMethod(self._handle, xbmcplugin.SORT_METHOD_ALBUM_IGNORE_THE) |
31731635 |
822 | xbmcplugin.setPluginFanart(int(sys.argv[1]), self.fanImg) |
e278f474 |
823 | |
824 | # Add artists to directory |
6ae708d0 |
825 | def _add_artists_directory(self, artists): |
31731635 |
826 | n = len(artists) |
5e72534b |
827 | itemsExisting = n |
9f9eadcd |
828 | if __debugging__ : |
5e72534b |
829 | xbmc.log("Found " + str(n) + " artists...") |
8817bb2e |
830 | i = 0 |
31731635 |
831 | while i < n: |
8817bb2e |
832 | artist = artists[i] |
8817bb2e |
833 | artistID = artist[1] |
5e72534b |
834 | if groovesharkApi.getDoesArtistExist(artistID): |
835 | artistName = artist[0] |
836 | self._add_dir(artistName, '', MODE_ARTIST, self.artistImg, artistID, itemsExisting) |
837 | else: |
838 | itemsExisting = itemsExisting - 1 |
8817bb2e |
839 | i = i + 1 |
840 | xbmcplugin.setContent(self._handle, 'artists') |
841 | xbmcplugin.addSortMethod(self._handle, xbmcplugin.SORT_METHOD_ARTIST_IGNORE_THE) |
31731635 |
842 | xbmcplugin.setPluginFanart(int(sys.argv[1]), self.fanImg) |
e278f474 |
843 | |
844 | # Add playlists to directory |
6ae708d0 |
845 | def _add_playlists_directory(self, playlists): |
31731635 |
846 | n = len(playlists) |
9f9eadcd |
847 | if __debugging__ : |
5e72534b |
848 | xbmc.log("Found " + str(n) + " playlists...") |
8817bb2e |
849 | i = 0 |
31731635 |
850 | while i < n: |
8817bb2e |
851 | playlist = playlists[i] |
852 | playlistName = playlist[0] |
853 | playlistID = playlist[1] |
86f629ea |
854 | dir = self._add_dir(playlistName, '', MODE_PLAYLIST, self.playlistImg, playlistID, n) |
8817bb2e |
855 | i = i + 1 |
856 | xbmcplugin.setContent(self._handle, 'files') |
86f629ea |
857 | xbmcplugin.addSortMethod(self._handle, xbmcplugin.SORT_METHOD_LABEL) |
31731635 |
858 | xbmcplugin.setPluginFanart(int(sys.argv[1]), self.fanImg) |
3fcef5ba |
859 | |
e278f474 |
860 | # Add whatever directory |
31731635 |
861 | def _add_dir(self, name, url, mode, iconimage, id, items=1): |
052028f1 |
862 | |
97289139 |
863 | if url == '': |
864 | u=sys.argv[0]+"?mode="+str(mode)+"&name="+urllib.quote_plus(name)+"&id="+str(id) |
865 | else: |
866 | u = url |
8817bb2e |
867 | dir=xbmcgui.ListItem(name, iconImage=iconimage, thumbnailImage=iconimage) |
b738088f |
868 | dir.setInfo( type="Music", infoLabels={ "title": name } ) |
052028f1 |
869 | |
870 | # Custom menu items |
f95afae7 |
871 | menuItems = [] |
872 | if mode == MODE_ALBUM: |
873 | mkplaylst=sys.argv[0]+"?mode="+str(MODE_MAKE_PLAYLIST)+"&name="+name+"&id="+str(id) |
b26b96e6 |
874 | menuItems.append((__language__(30076), "XBMC.RunPlugin("+mkplaylst+")")) |
f95afae7 |
875 | if mode == MODE_PLAYLIST: |
876 | rmplaylst=sys.argv[0]+"?mode="+str(MODE_REMOVE_PLAYLIST)+"&name="+urllib.quote_plus(name)+"&id="+str(id) |
b26b96e6 |
877 | menuItems.append((__language__(30077), "XBMC.RunPlugin("+rmplaylst+")")) |
f95afae7 |
878 | mvplaylst=sys.argv[0]+"?mode="+str(MODE_RENAME_PLAYLIST)+"&name="+urllib.quote_plus(name)+"&id="+str(id) |
b26b96e6 |
879 | menuItems.append((__language__(30078), "XBMC.RunPlugin("+mvplaylst+")")) |
5e72534b |
880 | |
f95afae7 |
881 | dir.addContextMenuItems(menuItems, replaceItems=False) |
052028f1 |
882 | |
31731635 |
883 | return xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]),url=u,listitem=dir,isFolder=True, totalItems=items) |
97289139 |
884 | |
885 | def _getSavedSongs(self): |
886 | path = os.path.join(cacheDir, 'songs.dmp') |
887 | try: |
888 | f = open(path, 'rb') |
889 | songs = pickle.load(f) |
890 | f.close() |
891 | except: |
892 | songs = [] |
893 | pass |
894 | return songs |
895 | |
896 | def _setSavedSongs(self, songs): |
897 | try: |
898 | # Create the 'data' directory if it doesn't exist. |
899 | if not os.path.exists(cacheDir): |
900 | os.makedirs(cacheDir) |
901 | path = os.path.join(cacheDir, 'songs.dmp') |
902 | f = open(path, 'wb') |
903 | pickle.dump(songs, f, protocol=pickle.HIGHEST_PROTOCOL) |
904 | f.close() |
905 | except: |
906 | xbmc.log("An error occurred saving songs") |
907 | pass |
f95afae7 |
908 | |
48e0c138 |
909 | def _getSongDuration(self, songid): |
f95afae7 |
910 | id = int(songid) |
f95afae7 |
911 | duration = -1 |
48e0c138 |
912 | durations = [] |
5d3b12f2 |
913 | path = os.path.join(cacheDir, 'duration.dmp') |
f95afae7 |
914 | try: |
915 | f = open(path, 'rb') |
48e0c138 |
916 | durations = pickle.load(f) |
917 | for song in durations: |
f95afae7 |
918 | if song[0] == id: |
919 | duration = song[1] |
5e72534b |
920 | break; |
f95afae7 |
921 | f.close() |
922 | except: |
923 | pass |
924 | |
5d3b12f2 |
925 | # Not in cache |
926 | if duration < 0: |
f95afae7 |
927 | stream = groovesharkApi.getSubscriberStreamKey(songid) |
5d3b12f2 |
928 | if stream != False and stream['url'] != '': |
929 | duration = self._setDuration(stream['uSecs']) |
930 | song = [id, duration] |
931 | self._setSongDuration(song, durations) |
cd937dea |
932 | |
48e0c138 |
933 | return duration |
5e72534b |
934 | |
9f9eadcd |
935 | def _setSongDuration(self, song, durations): |
f95afae7 |
936 | try: |
9f9eadcd |
937 | durations.append(song) |
cd937dea |
938 | # Create the cache directory if it doesn't exist. |
f95afae7 |
939 | if not os.path.exists(cacheDir): |
940 | os.makedirs(cacheDir) |
5d3b12f2 |
941 | path = os.path.join(cacheDir, 'duration.dmp') |
f95afae7 |
942 | f = open(path, 'wb') |
48e0c138 |
943 | pickle.dump(durations, f, protocol=pickle.HIGHEST_PROTOCOL) |
f95afae7 |
944 | f.close() |
945 | except: |
48e0c138 |
946 | xbmc.log("An error occurred saving duration") |
f95afae7 |
947 | pass |
948 | |
9f9eadcd |
949 | # Duration to seconds |
cfad7202 |
950 | def _setDuration(self, usecs): |
951 | if usecs < 60000000: |
952 | usecs = usecs * 10 # Some durations are 10x to small |
953 | return usecs / 1000000 |
97289139 |
954 | |
e278f474 |
955 | # Parse URL parameters |
8817bb2e |
956 | def get_params(): |
957 | param=[] |
958 | paramstring=sys.argv[2] |
9f9eadcd |
959 | if __debugging__ : |
5e72534b |
960 | xbmc.log(paramstring) |
8817bb2e |
961 | if len(paramstring)>=2: |
962 | params=sys.argv[2] |
963 | cleanedparams=params.replace('?','') |
964 | if (params[len(params)-1]=='/'): |
965 | params=params[0:len(params)-2] |
966 | pairsofparams=cleanedparams.split('&') |
967 | param={} |
968 | for i in range(len(pairsofparams)): |
969 | splitparams={} |
970 | splitparams=pairsofparams[i].split('=') |
971 | if (len(splitparams))==2: |
972 | param[splitparams[0]]=splitparams[1] |
8817bb2e |
973 | return param |
974 | |
e278f474 |
975 | # Main |
5e72534b |
976 | grooveshark = Grooveshark(); |
e278f474 |
977 | |
8817bb2e |
978 | params=get_params() |
8817bb2e |
979 | mode=None |
8817bb2e |
980 | try: mode=int(params["mode"]) |
981 | except: pass |
7ce01be6 |
982 | id=0 |
983 | try: id=int(params["id"]) |
984 | except: pass |
052028f1 |
985 | name = None |
986 | try: name=urllib.unquote_plus(params["name"]) |
987 | except: pass |
e278f474 |
988 | |
989 | # Call function for URL |
8817bb2e |
990 | if mode==None: |
991 | grooveshark.categories() |
992 | |
993 | elif mode==MODE_SEARCH_SONGS: |
994 | grooveshark.searchSongs() |
995 | |
996 | elif mode==MODE_SEARCH_ALBUMS: |
997 | grooveshark.searchAlbums() |
998 | |
999 | elif mode==MODE_SEARCH_ARTISTS: |
1000 | grooveshark.searchArtists() |
86f629ea |
1001 | |
1002 | elif mode==MODE_SEARCH_ARTISTS_ALBUMS: |
a2e75b14 |
1003 | grooveshark.searchArtistsAlbums(name) |
86f629ea |
1004 | |
1005 | elif mode==MODE_SEARCH_PLAYLISTS: |
1006 | grooveshark.searchPlaylists() |
8817bb2e |
1007 | |
36cc00d7 |
1008 | elif mode==MODE_POPULAR_SONGS: |
1009 | grooveshark.popularSongs() |
97289139 |
1010 | |
1011 | elif mode==MODE_ARTIST_POPULAR: |
a2e75b14 |
1012 | grooveshark.artistPopularSongs() |
8817bb2e |
1013 | |
8817bb2e |
1014 | elif mode==MODE_FAVORITES: |
1015 | grooveshark.favorites() |
1016 | |
e278f474 |
1017 | elif mode==MODE_PLAYLISTS: |
1018 | grooveshark.playlists() |
97289139 |
1019 | |
1020 | elif mode==MODE_SONG_PAGE: |
5e72534b |
1021 | try: offset=urllib.unquote_plus(params["offset"]) |
97289139 |
1022 | except: pass |
1023 | try: label=urllib.unquote_plus(params["label"]) |
1024 | except: pass |
5e72534b |
1025 | grooveshark.songPage(offset, label, id, name) |
e278f474 |
1026 | |
8817bb2e |
1027 | elif mode==MODE_SONG: |
8817bb2e |
1028 | try: album=urllib.unquote_plus(params["album"]) |
1029 | except: pass |
1030 | try: artist=urllib.unquote_plus(params["artist"]) |
1031 | except: pass |
7ce01be6 |
1032 | try: coverart=urllib.unquote_plus(params["coverart"]) |
8817bb2e |
1033 | except: pass |
7ce01be6 |
1034 | song = grooveshark.songItem(id, name, album, artist, coverart) |
3fcef5ba |
1035 | grooveshark.playSong(song) |
8817bb2e |
1036 | |
1037 | elif mode==MODE_ARTIST: |
4be42357 |
1038 | grooveshark.artist(id) |
8817bb2e |
1039 | |
1040 | elif mode==MODE_ALBUM: |
4be42357 |
1041 | grooveshark.album(id) |
8817bb2e |
1042 | |
1043 | elif mode==MODE_PLAYLIST: |
e6f8730b |
1044 | grooveshark.playlist(id, name) |
8817bb2e |
1045 | |
1046 | elif mode==MODE_FAVORITE: |
4be42357 |
1047 | grooveshark.favorite(id) |
97289139 |
1048 | |
052028f1 |
1049 | elif mode==MODE_UNFAVORITE: |
1050 | try: prevMode=int(urllib.unquote_plus(params["prevmode"])) |
1051 | except: |
1052 | prevMode = 0 |
1053 | grooveshark.unfavorite(id, prevMode) |
1054 | |
1055 | elif mode==MODE_SIMILAR_ARTISTS: |
1056 | grooveshark.similarArtists(id) |
1057 | |
1058 | elif mode==MODE_MAKE_PLAYLIST: |
1059 | grooveshark.makePlaylist(id, name) |
1060 | |
1061 | elif mode==MODE_REMOVE_PLAYLIST: |
1062 | grooveshark.removePlaylist(id, name) |
1063 | |
1064 | elif mode==MODE_RENAME_PLAYLIST: |
1065 | grooveshark.renamePlaylist(id, name) |
1066 | |
1067 | elif mode==MODE_REMOVE_PLAYLIST_SONG: |
1068 | try: playlistID=urllib.unquote_plus(params["playlistid"]) |
1069 | except: pass |
1070 | grooveshark.removePlaylistSong(playlistID, name, id) |
1071 | |
1072 | elif mode==MODE_ADD_PLAYLIST_SONG: |
1073 | grooveshark.addPlaylistSong(id) |
1074 | |
e278f474 |
1075 | if mode < MODE_SONG: |
8817bb2e |
1076 | xbmcplugin.endOfDirectory(int(sys.argv[1])) |