8817bb2e |
1 | import urllib, urllib2, re, xbmcplugin, xbmcgui, xbmc, sys, os |
2 | |
3 | # plugin constants |
4 | __plugin__ = "Grooveshark" |
5 | __author__ = "Stephen Denham" |
6 | __url__ = "" |
7 | __svn_url__ = "" |
8 | __version__ = "0.0.1" |
9 | __svn_revision__ = "" |
10 | __XBMC_Revision__ = "" |
11 | |
12 | MODE_SEARCH_SONGS = 1 |
13 | MODE_SEARCH_ALBUMS = 2 |
14 | MODE_SEARCH_ARTISTS = 3 |
15 | MODE_POPULAR = 4 |
16 | MODE_FAVORITES = 5 |
17 | MODE_PLAYLISTS = 6 |
18 | MODE_ALBUM = 7 |
19 | MODE_ARTIST = 8 |
20 | MODE_PLAYLIST = 9 |
21 | MODE_SONG = 10 |
22 | MODE_FAVORITE = 11 |
23 | MODE_UNFAVORITE = 12 |
24 | |
25 | songsearchlimit = 25 |
26 | albumsearchlimit = 15 |
27 | artistsearchlimit = 15 |
28 | |
29 | lastID = 0 |
30 | |
31 | rootDir = os.getcwd() |
32 | |
33 | resDir = xbmc.translatePath(os.path.join(rootDir, 'resources')) |
34 | libDir = xbmc.translatePath(os.path.join(resDir, 'lib')) |
35 | imgDir = xbmc.translatePath(os.path.join(resDir, 'img')) |
36 | |
37 | sys.path.append (libDir) |
38 | from GrooveAPI import * |
39 | groovesharkApi = GrooveAPI() |
40 | |
41 | class _Info: |
42 | def __init__( self, *args, **kwargs ): |
43 | self.__dict__.update( kwargs ) |
44 | |
45 | class Groveshark: |
46 | |
47 | albumImg = xbmc.translatePath(os.path.join(imgDir, 'album.png')) |
48 | artistImg = xbmc.translatePath(os.path.join(imgDir, 'artist.png')) |
49 | favoritesImg = xbmc.translatePath(os.path.join(imgDir, 'favorites.png')) |
50 | playlistImg = xbmc.translatePath(os.path.join(imgDir, 'playlist.png')) |
51 | popularImg = xbmc.translatePath(os.path.join(imgDir, 'popular.png')) |
52 | songImg = xbmc.translatePath(os.path.join(imgDir, 'song.png')) |
53 | |
54 | songsearchlimit = xbmcplugin.getSetting('songsearchlimit') |
55 | albumsearchlimit = xbmcplugin.getSetting('albumsearchlimit') |
56 | artistsearchlimit = xbmcplugin.getSetting('artistsearchlimit') |
57 | |
58 | def __init__( self ): |
59 | self._handle = int(sys.argv[1]) |
60 | |
61 | def categories(self): |
62 | userid = self._get_login() |
63 | self._addDir('Search songs', '', MODE_SEARCH_SONGS, self.songImg, 0) |
64 | self._addDir('Search albums', '', MODE_SEARCH_ALBUMS, self.albumImg, 0) |
65 | self._addDir('Search artists', '', MODE_SEARCH_ARTISTS, self.artistImg, 0) |
66 | self._addDir('Popular', '', MODE_POPULAR, self.popularImg, 0) |
67 | if (userid != 0): |
68 | self._addDir('Favorites', '', MODE_FAVORITES, self.favoritesImg, 0) |
69 | self._addDir('Playlists', '', MODE_PLAYLISTS, self.playlistImg, 0) |
70 | |
71 | def searchSongs(self): |
72 | query = self._get_keyboard(default="", heading="Search songs") |
73 | if (query): |
74 | songs = groovesharkApi.searchSongs(query, limit = songsearchlimit) |
75 | if (len(songs) > 0): |
76 | self._get_songs(songs) |
77 | else: |
78 | dialog = xbmcgui.Dialog() |
79 | dialog.ok('Grooveshark', 'No matching songs.') |
80 | self.categories() |
81 | |
82 | def searchAlbums(self): |
83 | query = self._get_keyboard(default="", heading="Search albums") |
84 | if (query): |
85 | albums = groovesharkApi.searchAlbums(query, limit = albumsearchlimit) |
86 | if (len(albums) > 0): |
87 | self._get_albums(albums) |
88 | else: |
89 | dialog = xbmcgui.Dialog() |
90 | dialog.ok('Grooveshark', 'No matching albums.') |
91 | self.categories() |
92 | |
93 | def searchArtists(self): |
94 | query = self._get_keyboard(default="", heading="Search artists") |
95 | if (query): |
96 | artists = groovesharkApi.searchArtists(query, limit = artistsearchlimit) |
97 | if (len(artists) > 0): |
98 | self._get_artists(artists) |
99 | else: |
100 | dialog = xbmcgui.Dialog() |
101 | dialog.ok('Grooveshark', 'No matching artists.') |
102 | self.categories() |
103 | |
104 | def favorites(self): |
105 | userid = self._get_login() |
106 | if (userid != 0): |
107 | favorites = groovesharkApi.userGetFavoriteSongs(userid) |
108 | if (len(favorites) > 0): |
109 | self._get_songs(favorites) |
110 | else: |
111 | dialog = xbmcgui.Dialog() |
112 | dialog.ok('Grooveshark', 'You have no favorites.') |
113 | self.categories() |
114 | |
115 | def popular(self): |
116 | popular = groovesharkApi.popularGetSongs(limit = songsearchlimit) |
117 | if (len(popular) > 0): |
118 | self._get_songs(popular) |
119 | else: |
120 | dialog = xbmcgui.Dialog() |
121 | dialog.ok('Grooveshark', 'No popular songs.') |
122 | self.categories() |
123 | |
124 | def playlists(self): |
125 | userid = self._get_login() |
126 | if (userid != 0): |
127 | playlists = groovesharkApi.userGetPlaylists() |
128 | if (len(playlists) > 0): |
129 | self._get_playlists(playlists) |
130 | else: |
131 | dialog = xbmcgui.Dialog() |
132 | dialog.ok('Grooveshark', 'You have no playlists.') |
133 | self.categories() |
134 | |
135 | def favorite(self, songid): |
136 | userid = self._get_login() |
137 | if (userid != 0): |
138 | xbmc.log("Favorite song: " + str(songid)) |
139 | groovesharkApi.favoriteSong(songID = songid) |
140 | else: |
141 | dialog = xbmcgui.Dialog() |
142 | dialog.ok('Grooveshark', 'You must be logged in', 'to add favorites.') |
143 | |
144 | def unfavorite(self, songid): |
145 | userid = self._get_login() |
146 | if (userid != 0): |
147 | xbmc.log("Unfavorite song: " + str(songid)) |
148 | groovesharkApi.unfavoriteSong(songID = songid) |
149 | else: |
150 | dialog = xbmcgui.Dialog() |
151 | dialog.ok('Grooveshark', 'You must be logged in', 'to remove favorites.') |
152 | |
153 | def album(self,albumid): |
154 | album = groovesharkApi.albumGetSongs(albumId = albumid, limit = songsearchlimit) |
155 | self._get_songs(album) |
156 | |
157 | def artist(self, artistid): |
158 | albums = groovesharkApi.artistGetAlbums(artistId = artistid, limit = albumsearchlimit) |
159 | self._get_albums(albums) |
160 | |
161 | def playlist(self, playlistid): |
162 | userid = self._get_login() |
163 | if (userid != 0): |
164 | songs = groovesharkApi.playlistGetSongs(playlistId = playlistid, limit = songsearchlimit) |
165 | self._get_songs(songs) |
166 | else: |
167 | dialog = xbmcgui.Dialog() |
168 | dialog.ok('Grooveshark', 'You must be logged in', 'to get playlists.') |
169 | |
170 | def song(self, url, name, album, artist, duration): |
171 | xbmc.log("Playing: " + url) |
172 | songItem = xbmcgui.ListItem(name + " - " + artist, path=url) |
173 | songItem.setInfo( type="Music", infoLabels={ "Title": name, "Duration": duration, "Album": album, "Artist": artist} ) |
174 | xbmc.Player().stop() |
175 | xbmc.Player(xbmc.PLAYER_CORE_PAPLAYER).play(url, songItem, False) |
176 | |
177 | def _get_keyboard(self, default="", heading="", hidden=False): |
178 | kb = xbmc.Keyboard(default, heading, hidden) |
179 | kb.doModal() |
180 | if (kb.isConfirmed()): |
181 | return unicode(kb.getText(), "utf-8") |
182 | return '' |
183 | |
184 | def _get_login(self): |
185 | username = xbmcplugin.getSetting('username') |
186 | password = xbmcplugin.getSetting('password') |
187 | if (username == "" or password == ""): |
188 | dialog = xbmcgui.Dialog() |
189 | dialog.ok('Grooveshark', 'Unable to login.', 'Check username and password in settings.') |
190 | return 0 |
191 | else: |
192 | if groovesharkApi.loggedInStatus() == 1: |
193 | groovesharkApi.logout() |
194 | try: |
195 | userid = groovesharkApi.loginExt(username, password) |
196 | except (LoginUnknownError): |
197 | userid = 0 |
198 | if (userid != 0): |
199 | xbmc.log("Logged in") |
200 | return userid |
201 | else: |
202 | dialog = xbmcgui.Dialog() |
203 | dialog.ok('Grooveshark', 'Unable to login.', 'Check username and password in settings.') |
204 | return 0 |
205 | |
206 | def _get_songs(self, songs): |
207 | xbmc.log("Found " + str(len(songs)) + " songs...") |
208 | i = 0 |
209 | while i < len(songs): |
210 | song = songs[i] |
211 | songName = song[0] |
212 | songID = song[1] |
213 | songDuration = song[2] |
214 | songAlbum = song[3] |
215 | songArtist = song[6] |
216 | songImage = song[9] |
217 | xbmc.log(songName) |
218 | self._addSong(songID, songName, groovesharkApi.getStreamURL(songID), songDuration, songAlbum, songArtist, songImage) |
219 | i = i + 1 |
220 | xbmcplugin.setContent(self._handle, 'songs') |
221 | xbmcplugin.addSortMethod(self._handle, xbmcplugin.SORT_METHOD_TITLE_IGNORE_THE) |
222 | |
223 | def _get_albums(self, albums): |
224 | xbmc.log("Found " + str(len(albums)) + " albums...") |
225 | i = 0 |
226 | while i < len(albums): |
227 | album = albums[i] |
228 | albumArtistName = album[0] |
229 | albumName = album[2] |
230 | albumID = album[3] |
231 | albumImage = album[4] |
232 | xbmc.log(albumName) |
233 | self._addDir(albumName + " - " + albumArtistName, '', MODE_ALBUM, albumImage, albumID) |
234 | i = i + 1 |
235 | xbmcplugin.setContent(self._handle, 'albums') |
236 | xbmcplugin.addSortMethod(self._handle, xbmcplugin.SORT_METHOD_ALBUM_IGNORE_THE) |
237 | |
238 | def _get_artists(self, artists): |
239 | xbmc.log("Found " + str(len(artists)) + " artists...") |
240 | i = 0 |
241 | while i < len(artists): |
242 | artist = artists[i] |
243 | artistName = artist[0] |
244 | artistID = artist[1] |
245 | xbmc.log(artistName) |
246 | self._addDir(artistName, '', MODE_ARTIST, self.artistImg, artistID) |
247 | i = i + 1 |
248 | xbmcplugin.setContent(self._handle, 'artists') |
249 | xbmcplugin.addSortMethod(self._handle, xbmcplugin.SORT_METHOD_ARTIST_IGNORE_THE) |
250 | |
251 | def _get_playlists(self, playlists): |
252 | xbmc.log("Found " + str(len(playlists)) + " playlists...") |
253 | i = 0 |
254 | while i < len(playlists): |
255 | playlist = playlists[i] |
256 | playlistName = playlist[0] |
257 | playlistID = playlist[1] |
258 | xbmc.log(playlistName) |
259 | self._addDir(playlistName, '', MODE_PLAYLIST, self.playlistImg, playlistID, ) |
260 | i = i + 1 |
261 | xbmcplugin.setContent(self._handle, 'files') |
262 | xbmcplugin.addSortMethod(self._handle, xbmcplugin.SORT_METHOD_PLAYLIST_ORDER) |
263 | |
264 | def _addSong(self, songid, songname, songurl, songduration, songalbum, songartist, songimage): |
265 | u=sys.argv[0]+"?url="+urllib.quote_plus(songurl)+"&mode="+str(MODE_SONG)+"&name="+urllib.quote_plus(songname)+"&id="+str(songid) \ |
266 | +"&album="+urllib.quote_plus(songalbum) \ |
267 | +"&artist="+urllib.quote_plus(songartist) \ |
268 | +"&duration="+str(songduration) |
269 | songItem = xbmcgui.ListItem(songname + " - " + songartist, iconImage=songimage, thumbnailImage=songimage, path=songurl) |
270 | songItem.setInfo( type="Music", infoLabels={ "Title": songname, "Duration": songduration, "Album": songalbum, "Artist": songartist} ) |
271 | fav=sys.argv[0]+"?url="+urllib.quote_plus(songurl)+"&mode="+str(MODE_FAVORITE)+"&name="+urllib.quote_plus(songname)+"&id="+str(songid) |
272 | unfav=sys.argv[0]+"?url="+urllib.quote_plus(songurl)+"&mode="+str(MODE_UNFAVORITE)+"&name="+urllib.quote_plus(songname)+"&id="+str(songid) |
273 | menuItems = [] |
274 | menuItems.append(("Grooveshark Favorite", "XBMC.RunPlugin("+fav+")")) |
275 | menuItems.append(("Not Grooveshark Favorite", "XBMC.RunPlugin("+unfav+")")) |
276 | songItem.addContextMenuItems(menuItems, replaceItems=False) |
277 | return xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]),url=u,listitem=songItem,isFolder=False) |
278 | |
279 | def _addDir(self, name, url, mode, iconimage, id): |
280 | u=sys.argv[0]+"?url="+urllib.quote_plus(url)+"&mode="+str(mode)+"&name="+urllib.quote_plus(name)+"&id="+str(id) |
281 | dir=xbmcgui.ListItem(name, iconImage=iconimage, thumbnailImage=iconimage) |
282 | dir.setInfo( type="Music", infoLabels={ "Title": name } ) |
283 | # Codes from http://xbmc-scripting.googlecode.com/svn/trunk/Script%20Templates/common/gui/codes.py |
284 | menuItems = [] |
285 | menuItems.append(("Select", "XBMC.executebuiltin(Action(7))")) |
286 | dir.addContextMenuItems(menuItems, replaceItems=True) |
287 | return xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]),url=u,listitem=dir,isFolder=True) |
288 | |
289 | def get_params(): |
290 | param=[] |
291 | paramstring=sys.argv[2] |
292 | if len(paramstring)>=2: |
293 | params=sys.argv[2] |
294 | cleanedparams=params.replace('?','') |
295 | if (params[len(params)-1]=='/'): |
296 | params=params[0:len(params)-2] |
297 | pairsofparams=cleanedparams.split('&') |
298 | param={} |
299 | for i in range(len(pairsofparams)): |
300 | splitparams={} |
301 | splitparams=pairsofparams[i].split('=') |
302 | if (len(splitparams))==2: |
303 | param[splitparams[0]]=splitparams[1] |
304 | print param |
305 | return param |
306 | |
307 | grooveshark = Groveshark(); |
308 | |
309 | params=get_params() |
310 | url=None |
311 | name=None |
312 | mode=None |
313 | id=None |
314 | |
315 | try: url=urllib.unquote_plus(params["url"]) |
316 | except: pass |
317 | try: name=urllib.unquote_plus(params["name"]) |
318 | except: pass |
319 | try: mode=int(params["mode"]) |
320 | except: pass |
321 | try: id=int(params["id"]) |
322 | except: pass |
323 | |
324 | if (id > 0): |
325 | lastID = id |
326 | |
327 | if mode==None: |
328 | grooveshark.categories() |
329 | |
330 | elif mode==MODE_SEARCH_SONGS: |
331 | grooveshark.searchSongs() |
332 | |
333 | elif mode==MODE_SEARCH_ALBUMS: |
334 | grooveshark.searchAlbums() |
335 | |
336 | elif mode==MODE_SEARCH_ARTISTS: |
337 | grooveshark.searchArtists() |
338 | |
339 | elif mode==MODE_POPULAR: |
340 | grooveshark.popular() |
341 | |
342 | elif mode==MODE_PLAYLISTS: |
343 | grooveshark.playlists() |
344 | |
345 | elif mode==MODE_FAVORITES: |
346 | grooveshark.favorites() |
347 | |
348 | elif mode==MODE_SONG: |
349 | try: name=urllib.unquote_plus(params["name"]) |
350 | except: pass |
351 | try: album=urllib.unquote_plus(params["album"]) |
352 | except: pass |
353 | try: artist=urllib.unquote_plus(params["artist"]) |
354 | except: pass |
355 | try: duration=int(params["duration"]) |
356 | except: pass |
357 | grooveshark.song(url, name, album, artist, duration) |
358 | |
359 | elif mode==MODE_ARTIST: |
360 | grooveshark.artist(lastID) |
361 | |
362 | elif mode==MODE_ALBUM: |
363 | grooveshark.album(lastID) |
364 | |
365 | elif mode==MODE_PLAYLIST: |
366 | grooveshark.playlist(lastID) |
367 | |
368 | elif mode==MODE_FAVORITE: |
369 | grooveshark.favorite(lastID) |
370 | |
371 | elif mode==MODE_UNFAVORITE: |
372 | grooveshark.unfavorite(lastID) |
373 | |
374 | if (mode < MODE_SONG): |
375 | xbmcplugin.endOfDirectory(int(sys.argv[1])) |