1 # Copyright 2011 Stephen Denham
3 # This file is part of xbmc-groove.
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.
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.
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/>.
19 import urllib
, sys
, os
, shutil
, re
, pickle
, time
, tempfile
, xbmcaddon
, xbmcplugin
, xbmcgui
, xbmc
21 __addon__
= xbmcaddon
.Addon('plugin.audio.groove')
22 __addonname__
= __addon__
.getAddonInfo('name')
23 __cwd__
= __addon__
.getAddonInfo('path')
24 __author__
= __addon__
.getAddonInfo('author')
25 __version__
= __addon__
.getAddonInfo('version')
26 __language__
= __addon__
.getLocalizedString
27 __debuggui__
= __addon__
.getSetting('debug')
30 MODE_SEARCH_ALBUMS
= 2
31 MODE_SEARCH_ARTISTS
= 3
32 MODE_SEARCH_ARTISTS_ALBUMS
= 4
33 MODE_SEARCH_PLAYLISTS
= 5
34 MODE_ARTIST_POPULAR
= 6
35 MODE_POPULAR_SONGS
= 7
42 MODE_SIMILAR_ARTISTS
= 14
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
57 ACTION_SELECT_ITEM
= 7
58 ACTION_PREVIOUS_MENU
= 10
60 # Formats for track labels
61 ARTIST_ALBUM_NAME_LABEL
= 0
62 NAME_ALBUM_ARTIST_LABEL
= 1
64 # Stream marking time (seconds)
65 STREAM_MARKING_TIME
= 30
74 player
= xbmc
.Player()
78 resDir
= xbmc
.translatePath(os
.path
.join(baseDir
, 'resources'))
79 libDir
= xbmc
.translatePath(os
.path
.join(resDir
, 'lib'))
80 imgDir
= xbmc
.translatePath(os
.path
.join(resDir
, 'img'))
81 cacheDir
= os
.path
.join(xbmc
.translatePath('special://masterprofile/addon_data/'), os
.path
.basename(baseDir
))
82 thumbDirName
= 'thumb'
83 thumbDir
= os
.path
.join('special://masterprofile/addon_data/', os
.path
.basename(baseDir
), thumbDirName
)
85 baseModeUrl
= 'plugin://plugin.audio.groove/'
86 playlistUrl
= baseModeUrl
+ '?mode=' + str(MODE_PLAYLIST
)
87 playlistsUrl
= baseModeUrl
+ '?mode=' + str(MODE_PLAYLISTS
)
88 favoritesUrl
= baseModeUrl
+ '?mode=' + str(MODE_FAVORITES
)
90 searchArtistsAlbumsName
= __language__(30006)
92 thumbDef
= os
.path
.join(imgDir
, 'default.tbn')
93 listBackground
= os
.path
.join(imgDir
, 'listbackground.png')
95 sys
.path
.append (libDir
)
96 from GroovesharkAPI
import GrooveAPI
97 from threading
import Event
, Thread
100 groovesharkApi
= GrooveAPI()
101 groovesharkApi
.setDebug(__debuggui__
)
102 if groovesharkApi
.pingService() != True:
103 raise StandardError(__language__(30007))
105 dialog
= xbmcgui
.Dialog(__language__(30008),__language__(30009),__language__(30010))
109 # Mark song as playing or played
110 def markSong(songid
, duration
, streamKey
, streamServerID
):
114 if player
.isPlayingAudio():
115 tNow
= player
.getTime()
116 if tNow
>= STREAM_MARKING_TIME
and songMarkTime
== 0:
117 groovesharkApi
.markStreamKeyOver30Secs(streamKey
, streamServerID
)
119 elif duration
> tNow
and duration
- tNow
< 2 and songMarkTime
>= STREAM_MARKING_TIME
:
122 groovesharkApi
.markSongComplete(songid
, streamKey
, streamServerID
)
128 def __init__( self
, *args
, **kwargs
):
129 self
.__dict
__.update( kwargs
)
131 # Window dialog to select a grooveshark playlist
132 class GroovesharkPlaylistSelect(xbmcgui
.WindowDialog
):
134 def __init__(self
, items
=[]):
135 gap
= int(self
.getHeight()/100)
136 w
= int(self
.getWidth()*0.5)
137 h
= self
.getHeight()-30*gap
139 rh
= self
.getHeight()
143 self
.imgBg
= xbmcgui
.ControlImage(x
+gap
, 5*gap
+y
, w
-2*gap
, h
-5*gap
, listBackground
)
144 self
.addControl(self
.imgBg
)
146 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)
147 self
.addControl(self
.playlistControl
)
150 self
.isSelecting
= False
153 for playlist
in items
:
154 listitems
.append(xbmcgui
.ListItem(playlist
[0]))
155 listitems
.append(xbmcgui
.ListItem(__language__(30011)))
156 self
.playlistControl
.addItems(listitems
)
157 self
.setFocus(self
.playlistControl
)
158 self
.playlistControl
.selectItem(0)
159 item
= self
.playlistControl
.getListItem(self
.lastPos
)
162 # Highlight selected item
163 def setHighlight(self
):
167 self
.isSelecting
= True
169 pos
= self
.playlistControl
.getSelectedPosition()
171 item
= self
.playlistControl
.getListItem(self
.lastPos
)
173 item
= self
.playlistControl
.getListItem(pos
)
176 self
.isSelecting
= False
179 def onControl(self
, control
):
180 if control
== self
.playlistControl
:
181 self
.selected
= self
.playlistControl
.getSelectedPosition()
184 # Action - close or up/down
185 def onAction(self
, action
):
186 if action
== ACTION_PREVIOUS_MENU
:
189 elif action
== ACTION_MOVE_UP
or action
== ACTION_MOVE_DOWN
or action
== ACTION_PAGE_UP
or action
== ACTION_PAGE_DOWN
== 6:
190 self
.setFocus(self
.playlistControl
)
194 class PlayTimer(Thread
):
195 # interval -- floating point number specifying the number of seconds to wait before executing function
196 # function -- the function (or callable object) to be executed
198 # iterations -- integer specifying the number of iterations to perform
199 # args -- list of positional arguments passed to function
200 # kwargs -- dictionary of keyword arguments passed to function
202 def __init__(self
, interval
, function
, iterations
=0, args
=[], kwargs
={}):
203 Thread
.__init
__(self
)
204 self
.interval
= interval
205 self
.function
= function
206 self
.iterations
= iterations
209 self
.finished
= Event()
213 while not self
.finished
.isSet() and (self
.iterations
<= 0 or count
< self
.iterations
):
214 self
.finished
.wait(self
.interval
)
215 if not self
.finished
.isSet():
216 self
.function(*self
.args
, **self
.kwargs
)
222 def setIterations(self
, iterations
):
223 self
.iterations
= iterations
227 return self
.iterations
* self
.interval
232 albumImg
= xbmc
.translatePath(os
.path
.join(imgDir
, 'album.png'))
233 artistImg
= xbmc
.translatePath(os
.path
.join(imgDir
, 'artist.png'))
234 artistsAlbumsImg
= xbmc
.translatePath(os
.path
.join(imgDir
, 'artistsalbums.png'))
235 favoritesImg
= xbmc
.translatePath(os
.path
.join(imgDir
, 'favorites.png'))
236 playlistImg
= xbmc
.translatePath(os
.path
.join(imgDir
, 'playlist.png'))
237 usersplaylistsImg
= xbmc
.translatePath(os
.path
.join(imgDir
, 'usersplaylists.png'))
238 popularSongsImg
= xbmc
.translatePath(os
.path
.join(imgDir
, 'popularSongs.png'))
239 popularSongsArtistImg
= xbmc
.translatePath(os
.path
.join(imgDir
, 'popularSongsArtist.png'))
240 songImg
= xbmc
.translatePath(os
.path
.join(imgDir
, 'song.png'))
241 defImg
= xbmc
.translatePath(os
.path
.join(imgDir
, 'default.tbn'))
242 fanImg
= xbmc
.translatePath(os
.path
.join(baseDir
, 'fanart.png'))
244 settings
= xbmcaddon
.Addon(id='plugin.audio.groove')
245 songsearchlimit
= int(settings
.getSetting('songsearchlimit'))
246 albumsearchlimit
= int(settings
.getSetting('albumsearchlimit'))
247 artistsearchlimit
= int(settings
.getSetting('artistsearchlimit'))
248 songspagelimit
= int(settings
.getSetting('songspagelimit'))
249 username
= settings
.getSetting('username')
250 password
= settings
.getSetting('password')
254 def __init__( self
):
255 self
._handle
= int(sys
.argv
[1])
256 if os
.path
.isdir(cacheDir
) == False:
257 os
.makedirs(cacheDir
)
259 xbmc
.log(__language__(30012) + " " + cacheDir
)
260 artDir
= xbmc
.translatePath(thumbDir
)
261 if os
.path
.isdir(artDir
) == False:
264 xbmc
.log(__language__(30012) + " " + artDir
)
267 def categories(self
):
269 self
.userid
= self
._get
_login
()
272 xbmcplugin
.setPluginFanart(int(sys
.argv
[1]), self
.fanImg
)
274 self
._add
_dir
(__language__(30013), '', MODE_SEARCH_SONGS
, self
.songImg
, 0)
275 self
._add
_dir
(__language__(30014), '', MODE_SEARCH_ALBUMS
, self
.albumImg
, 0)
276 self
._add
_dir
(__language__(30015), '', MODE_SEARCH_ARTISTS
, self
.artistImg
, 0)
277 self
._add
_dir
(searchArtistsAlbumsName
, '', MODE_SEARCH_ARTISTS_ALBUMS
, self
.artistsAlbumsImg
, 0)
278 # Not supported by key
279 #self._add_dir("Search for user's playlists...", '', MODE_SEARCH_PLAYLISTS, self.usersplaylistsImg, 0)
280 self
._add
_dir
(__language__(30016), '', MODE_ARTIST_POPULAR
, self
.popularSongsArtistImg
, 0)
281 self
._add
_dir
(__language__(30017), '', MODE_POPULAR_SONGS
, self
.popularSongsImg
, 0)
282 if (self
.userid
!= 0):
283 self
._add
_dir
(__language__(30018), '', MODE_FAVORITES
, self
.favoritesImg
, 0)
284 self
._add
_dir
(__language__(30019), '', MODE_PLAYLISTS
, self
.playlistImg
, 0)
287 def searchSongs(self
):
288 query
= self
._get
_keyboard
(default
="", heading
=__language__(30020))
290 songs
= groovesharkApi
.getSongSearchResults(query
, limit
= self
.songsearchlimit
)
292 self
._add
_songs
_directory
(songs
)
294 dialog
= xbmcgui
.Dialog()
295 dialog
.ok(__language__(30008), __language__(30021))
301 def searchAlbums(self
):
302 query
= self
._get
_keyboard
(default
="", heading
=__language__(30022))
304 albums
= groovesharkApi
.getAlbumSearchResults(query
, limit
= self
.albumsearchlimit
)
305 if (len(albums
) > 0):
306 self
._add
_albums
_directory
(albums
)
308 dialog
= xbmcgui
.Dialog()
309 dialog
.ok(__language__(30008), __language__(30023))
315 def searchArtists(self
):
316 query
= self
._get
_keyboard
(default
="", heading
=__language__(30024))
318 artists
= groovesharkApi
.getArtistSearchResults(query
, limit
= self
.artistsearchlimit
)
319 if (len(artists
) > 0):
320 self
._add
_artists
_directory
(artists
)
322 dialog
= xbmcgui
.Dialog()
323 dialog
.ok(__language__(30008), __language__(30025))
328 # Search for playlists
329 def searchPlaylists(self
):
330 query
= self
._get
_keyboard
(default
="", heading
=__language__(30026))
332 playlists
= groovesharkApi
.getUserPlaylistsByUsername(query
)
333 if (len(playlists
) > 0):
334 self
._add
_playlists
_directory
(playlists
)
336 dialog
= xbmcgui
.Dialog()
337 dialog
.ok(__language__(30008), __language__(30027))
342 # Search for artists albums
343 def searchArtistsAlbums(self
, artistName
= None):
344 if artistName
== None or artistName
== searchArtistsAlbumsName
:
345 query
= self
._get
_keyboard
(default
="", heading
=__language__(30028))
349 artists
= groovesharkApi
.getArtistSearchResults(query
, limit
= self
.artistsearchlimit
)
350 if (len(artists
) > 0):
354 xbmc
.log("Found " + artist
[0] + "...")
355 albums
= groovesharkApi
.getArtistAlbums(artistID
, limit
= self
.albumsearchlimit
)
356 if (len(albums
) > 0):
357 self
._add
_albums
_directory
(albums
, artistID
)
359 dialog
= xbmcgui
.Dialog()
360 dialog
.ok(__language__(30008), __language__(30029))
363 dialog
= xbmcgui
.Dialog()
364 dialog
.ok(__language__(30008), __language__(30030))
371 userid
= self
._get
_login
()
373 favorites
= groovesharkApi
.getUserFavoriteSongs()
374 if (len(favorites
) > 0):
375 self
._add
_songs
_directory
(favorites
, isFavorites
=True)
377 dialog
= xbmcgui
.Dialog()
378 dialog
.ok(__language__(30008), __language__(30031))
382 def popularSongs(self
):
383 popular
= groovesharkApi
.getPopularSongsToday(limit
= self
.songsearchlimit
)
384 if (len(popular
) > 0):
385 self
._add
_songs
_directory
(popular
)
387 dialog
= xbmcgui
.Dialog()
388 dialog
.ok(__language__(30008), __language__(30032))
393 userid
= self
._get
_login
()
395 playlists
= groovesharkApi
.getUserPlaylists()
396 if (len(playlists
) > 0):
397 self
._add
_playlists
_directory
(playlists
)
399 dialog
= xbmcgui
.Dialog()
400 dialog
.ok(__language__(30008), __language__(30033))
403 dialog
= xbmcgui
.Dialog()
404 dialog
.ok(__language__(30008), __language__(30034), __language__(30035))
406 # Make songs a favorite
407 def favorite(self
, songid
):
408 userid
= self
._get
_login
()
411 xbmc
.log("Favorite song: " + str(songid
))
412 groovesharkApi
.addUserFavoriteSong(songID
= songid
)
413 xbmc
.executebuiltin('XBMC.Notification(' + __language__(30008) + ', ' + __language__(30036) + ', 1000, ' + thumbDef
+ ')')
415 dialog
= xbmcgui
.Dialog()
416 dialog
.ok(__language__(30008), __language__(30034), __language__(30037))
418 # Remove song from favorites
419 def unfavorite(self
, songid
, prevMode
=0):
420 userid
= self
._get
_login
()
423 xbmc
.log("Unfavorite song: " + str(songid
) + ', previous mode was ' + str(prevMode
))
424 groovesharkApi
.removeUserFavoriteSongs(songIDs
= songid
)
425 xbmc
.executebuiltin('XBMC.Notification(' + __language__(30008) + ', ' + __language__(30038) + ', 1000, ' + thumbDef
+ ')')
426 # Refresh to remove item from directory
427 if (int(prevMode
) == MODE_FAVORITES
):
428 xbmc
.executebuiltin("Container.Refresh(" + favoritesUrl
+ ")")
430 dialog
= xbmcgui
.Dialog()
431 dialog
.ok(__language__(30008), __language__(30034), __language__(30039))
434 # Show selected album
435 def album(self
, albumid
):
436 album
= groovesharkApi
.getAlbumSongs(albumid
, limit
= self
.songsearchlimit
)
437 self
._add
_songs
_directory
(album
, trackLabelFormat
=NAME_ALBUM_ARTIST_LABEL
)
439 # Show selected artist
440 def artist(self
, artistid
):
441 albums
= groovesharkApi
.getArtistAlbums(artistid
, limit
= self
.albumsearchlimit
)
442 self
._add
_albums
_directory
(albums
, artistid
)
444 # Show selected playlist
445 def playlist(self
, playlistid
, playlistname
):
446 userid
= self
._get
_login
()
448 songs
= groovesharkApi
.getPlaylistSongs(playlistid
)
449 self
._add
_songs
_directory
(songs
, trackLabelFormat
=NAME_ALBUM_ARTIST_LABEL
, playlistid
=playlistid
, playlistname
=playlistname
)
451 dialog
= xbmcgui
.Dialog()
452 dialog
.ok(__language__(30008), __language__(30034), __language__(30040))
454 # Show popular songs of the artist
455 def artistPopularSongs(self
):
456 query
= self
._get
_keyboard
(default
="", heading
=__language__(30041))
458 artists
= groovesharkApi
.getArtistSearchResults(query
, limit
= self
.artistsearchlimit
)
459 if (len(artists
) > 0):
463 xbmc
.log("Found " + artist
[0] + "...")
464 songs
= groovesharkApi
.getArtistPopularSongs(artistID
, limit
= self
.songsearchlimit
)
466 self
._add
_songs
_directory
(songs
, trackLabelFormat
=NAME_ALBUM_ARTIST_LABEL
)
468 dialog
= xbmcgui
.Dialog()
469 dialog
.ok(__language__(30008), __language__(30042))
472 dialog
= xbmcgui
.Dialog()
473 dialog
.ok(__language__(30008), __language__(30043))
479 def playSong(self
, item
):
483 songid
= self
._setItem
(item
)
484 # Wait for play then start timer
486 while seconds
< STREAM_TIMEOUT
:
488 if player
.isPlayingAudio() == True:
489 if playTimer
!= None:
492 stream
= self
._getSongStream
(songid
)
493 duration
= stream
['duration']
494 playTimer
= PlayTimer(1, markSong
, duration
, [songid
, duration
, stream
['streamKey'], stream
['streamServerID']])
499 seconds
= seconds
+ 1
501 # If not playing after a few seconds try to refresh the URL
502 if (player
.isPlayingAudio() == False and seconds
== STREAM_RETRY
):
504 xbmc
.log("Refreshing URL")
505 item
.setProperty('url', '')
508 xbmc
.executebuiltin('XBMC.Notification(' + __language__(30008) + ', ' + __language__(30044) + ', 1000, ' + thumbDef
+ ')')
510 # Make a song directory item
511 def songItem(self
, songid
, name
, album
, artist
, coverart
, trackLabelFormat
=ARTIST_ALBUM_NAME_LABEL
):
512 songImg
= self
._get
_icon
(coverart
, 'song-' + str(songid
) + "-image")
513 if int(trackLabelFormat
) == NAME_ALBUM_ARTIST_LABEL
:
514 trackLabel
= name
+ " - " + album
+ " - " + artist
516 trackLabel
= artist
+ " - " + album
+ " - " + name
517 stream
= self
._getSongStream
(songid
)
518 duration
= stream
['duration']
520 item
= xbmcgui
.ListItem(label
= trackLabel
, thumbnailImage
=songImg
, iconImage
=songImg
)
521 item
.setInfo( type="music", infoLabels
={ "title": name
, "album": album
, "artist": artist
, "duration": duration
} )
522 item
.setProperty('mimetype', 'audio/mpeg')
523 item
.setProperty("IsPlayable", "true")
524 item
.setProperty('songid', str(songid
))
525 item
.setProperty('coverart', songImg
)
526 item
.setProperty('title', name
)
527 item
.setProperty('album', album
)
528 item
.setProperty('artist', artist
)
529 item
.setProperty('duration', str(duration
))
530 item
.setProperty('url', str(url
))
535 def songPage(self
, offset
, trackLabelFormat
, playlistid
= 0, playlistname
= ''):
536 self
._add
_songs
_directory
([], trackLabelFormat
, offset
, playlistid
= playlistid
, playlistname
= playlistname
)
538 # Make a playlist from an album
539 def makePlaylist(self
, albumid
, name
):
540 userid
= self
._get
_login
()
542 re
.split(' - ',name
,1)
543 nameTokens
= re
.split(' - ',name
,1) # suggested name
544 name
= self
._get
_keyboard
(default
=nameTokens
[0], heading
=__language__(30045))
546 album
= groovesharkApi
.getAlbumSongs(albumid
, limit
= self
.songsearchlimit
)
549 songids
.append(song
[1])
550 if groovesharkApi
.createPlaylist(name
, songids
) == 0:
551 dialog
= xbmcgui
.Dialog()
552 dialog
.ok(__language__(30008), __language__(30046), name
)
554 xbmc
.executebuiltin('XBMC.Notification(' + __language__(30008) + ',' + __language__(30047)+ ', 1000, ' + thumbDef
+ ')')
556 dialog
= xbmcgui
.Dialog()
557 dialog
.ok(__language__(30008), __language__(30034), __language__(30048))
560 def renamePlaylist(self
, playlistid
, name
):
561 userid
= self
._get
_login
()
563 newname
= self
._get
_keyboard
(default
=name
, heading
=__language__(30049))
566 elif groovesharkApi
.playlistRename(playlistid
, newname
) == 0:
567 dialog
= xbmcgui
.Dialog()
568 dialog
.ok(__language__(30008), __language__(30050), name
)
570 # Refresh to show new item name
571 xbmc
.executebuiltin("Container.Refresh")
573 dialog
= xbmcgui
.Dialog()
574 dialog
.ok(__language__(30008), __language__(30034), __language__(30051))
577 def removePlaylist(self
, playlistid
, name
):
578 dialog
= xbmcgui
.Dialog()
579 if dialog
.yesno(__language__(30008), name
, __language__(30052)) == True:
580 userid
= self
._get
_login
()
582 if groovesharkApi
.playlistDelete(playlistid
) == 0:
583 dialog
= xbmcgui
.Dialog()
584 dialog
.ok(__language__(30008), __language__(30053), name
)
586 # Refresh to remove item from directory
587 xbmc
.executebuiltin("Container.Refresh(" + playlistsUrl
+ ")")
589 dialog
= xbmcgui
.Dialog()
590 dialog
.ok(__language__(30008), __language__(30034), __language__(30054))
592 # Add song to playlist
593 def addPlaylistSong(self
, songid
):
594 userid
= self
._get
_login
()
596 playlists
= groovesharkApi
.getUserPlaylists()
597 if (len(playlists
) > 0):
599 # Select the playlist
600 playlistSelect
= GroovesharkPlaylistSelect(items
=playlists
)
601 playlistSelect
.setFocus(playlistSelect
.playlistControl
)
602 playlistSelect
.doModal()
603 i
= playlistSelect
.selected
607 if i
>= len(playlists
):
608 name
= self
._get
_keyboard
(default
='', heading
=__language__(30055))
611 songIds
.append(songid
)
612 if groovesharkApi
.createPlaylist(name
, songIds
) == 0:
613 dialog
= xbmcgui
.Dialog()
614 dialog
.ok(__language__(30008), __language__(30056), name
)
616 xbmc
.executebuiltin('XBMC.Notification(' + __language__(30008) + ',' + __language__(30057) + ', 1000, ' + thumbDef
+ ')')
619 playlist
= playlists
[i
]
620 playlistid
= playlist
[1]
622 xbmc
.log("Add song " + str(songid
) + " to playlist " + str(playlistid
))
624 songs
= groovesharkApi
.getPlaylistSongs(playlistid
)
626 songIDs
.append(song
[1])
627 songIDs
.append(songid
)
628 ret
= groovesharkApi
.setPlaylistSongs(playlistid
, songIDs
)
630 dialog
= xbmcgui
.Dialog()
631 dialog
.ok(__language__(30008), __language__(30058))
633 xbmc
.executebuiltin('XBMC.Notification(' + __language__(30008) + ',' + __language__(30059) + ', 1000, ' + thumbDef
+ ')')
635 dialog
= xbmcgui
.Dialog()
636 dialog
.ok(__language__(30008), __language__(30060))
639 dialog
= xbmcgui
.Dialog()
640 dialog
.ok(__language__(30008), __language__(30034), __language__(30061))
642 # Remove song from playlist
643 def removePlaylistSong(self
, playlistid
, playlistname
, songid
):
644 dialog
= xbmcgui
.Dialog()
645 if dialog
.yesno(__language__(30008), __language__(30062), __language__(30063)) == True:
646 userid
= self
._get
_login
()
648 songs
= groovesharkApi
.getPlaylistSongs(playlistID
)
651 if (song
[1] != songid
):
652 songIDs
.append(song
[1])
653 ret
= groovesharkApi
.setPlaylistSongs(playlistID
, songIDs
)
655 dialog
= xbmcgui
.Dialog()
656 dialog
.ok(__language__(30008), __language__(30064), __language__(30065))
658 # Refresh to remove item from directory
659 xbmc
.executebuiltin('XBMC.Notification(' + __language__(30008) + ',' + __language__(30066)+ ', 1000, ' + thumbDef
+ ')')
660 xbmc
.executebuiltin("Container.Update(" + playlistUrl
+ "&id="+str(playlistid
) + "&name=" + playlistname
+ ")")
662 dialog
= xbmcgui
.Dialog()
663 dialog
.ok(__language__(30008), __language__(30034), __language__(30067))
665 # Find similar artists to searched artist
666 def similarArtists(self
, artistId
):
667 similar
= groovesharkApi
.getSimilarArtists(artistId
, limit
= self
.artistsearchlimit
)
668 if (len(similar
) > 0):
669 self
._add
_artists
_directory
(similar
)
671 dialog
= xbmcgui
.Dialog()
672 dialog
.ok(__language__(30008), __language__(30068))
676 def _get_keyboard(self
, default
="", heading
="", hidden
=False):
677 kb
= xbmc
.Keyboard(default
, heading
, hidden
)
679 if (kb
.isConfirmed()):
680 return unicode(kb
.getText(), "utf-8")
683 # Login to grooveshark
684 def _get_login(self
):
685 if (self
.username
== "" or self
.password
== ""):
686 dialog
= xbmcgui
.Dialog()
687 dialog
.ok(__language__(30008), __language__(30069), __language__(30070))
691 uid
= groovesharkApi
.login(self
.username
, self
.password
)
695 dialog
= xbmcgui
.Dialog()
696 dialog
.ok(__language__(30008), __language__(30069), __language__(30070))
699 # Get a song directory item
700 def _get_song_item(self
, song
, trackLabelFormat
):
706 return self
.songItem(songid
, name
, album
, artist
, coverart
, trackLabelFormat
)
709 def _get_icon(self
, url
, songid
):
711 localThumb
= os
.path
.join(xbmc
.translatePath(os
.path
.join(thumbDir
, str(songid
)))) + '.tbn'
713 if os
.path
.isfile(localThumb
) == False:
714 loc
= urllib
.URLopener()
715 loc
.retrieve(url
, localThumb
)
717 shutil
.copy2(thumbDef
, localThumb
)
718 return os
.path
.join(os
.path
.join(thumbDir
, str(songid
))) + '.tbn'
723 def _setItem(self
, item
):
724 url
= item
.getProperty('url')
725 songid
= item
.getProperty('songid')
727 stream
= groovesharkApi
.getSubscriberStreamKey(songid
)
730 self
._setSongStream
(songid
, stream
)
732 xbmcplugin
.setResolvedUrl(handle
=int(sys
.argv
[1]), succeeded
=True, listitem
=item
)
734 xbmc
.log("Grooveshark playing: " + songid
)
737 # Add songs to directory
738 def _add_songs_directory(self
, songs
, trackLabelFormat
=ARTIST_ALBUM_NAME_LABEL
, offset
=0, playlistid
=0, playlistname
='', isFavorites
=False):
740 totalSongs
= len(songs
)
746 if offset
== 0 and totalSongs
<= self
.songspagelimit
:
748 xbmc
.log("Found " + str(totalSongs
) + " songs...")
751 # Cache all next pages songs
753 self
._setSavedSongs
(songs
)
755 songs
= self
._getSavedSongs
()
756 totalSongs
= len(songs
)
760 end
= min(start
+ self
.songspagelimit
,totalSongs
)
768 stream
= self
._getSongStream
(songid
)
769 if stream
['url'] != '':
770 item
= self
._get
_song
_item
(song
, trackLabelFormat
)
771 coverart
= item
.getProperty('coverart')
775 u
=sys
.argv
[0]+"?mode="+str(MODE_SONG
)+"&name="+urllib
.quote_plus(songname
)+"&id="+str(songid
) \
776 +"&album="+urllib
.quote_plus(songalbum
) \
777 +"&artist="+urllib
.quote_plus(songartist
) \
778 +"&coverart="+urllib
.quote_plus(coverart
)
779 fav
=sys
.argv
[0]+"?mode="+str(MODE_FAVORITE
)+"&name="+urllib
.quote_plus(songname
)+"&id="+str(songid
)
780 unfav
=sys
.argv
[0]+"?mode="+str(MODE_UNFAVORITE
)+"&name="+urllib
.quote_plus(songname
)+"&id="+str(songid
)+"&prevmode="
782 if isFavorites
== True:
783 unfav
= unfav
+str(MODE_FAVORITES
)
785 menuItems
.append((__language__(30071), "XBMC.RunPlugin("+fav
+")"))
786 menuItems
.append((__language__(30072), "XBMC.RunPlugin("+unfav
+")"))
788 rmplaylstsong
=sys
.argv
[0]+"?playlistid="+str(playlistid
)+"&id="+str(songid
)+"&mode="+str(MODE_REMOVE_PLAYLIST_SONG
)+"&name="+playlistname
789 menuItems
.append((__language__(30073), "XBMC.RunPlugin("+rmplaylstsong
+")"))
791 addplaylstsong
=sys
.argv
[0]+"?id="+str(songid
)+"&mode="+str(MODE_ADD_PLAYLIST_SONG
)
792 menuItems
.append((__language__(30074), "XBMC.RunPlugin("+addplaylstsong
+")"))
793 item
.addContextMenuItems(menuItems
, replaceItems
=False)
794 xbmcplugin
.addDirectoryItem(handle
=int(sys
.argv
[1]),url
=u
,listitem
=item
,isFolder
=False, totalItems
=items
)
797 end
= min(end
+ 1,totalSongs
)
799 xbmc
.log(song
[0] + " does not exist.")
803 u
=sys
.argv
[0]+"?mode="+str(MODE_SONG_PAGE
)+"&id=playlistid"+"&offset="+str(end
)+"&label="+str(trackLabelFormat
)+"&name="+playlistname
804 self
._add
_dir
(__language__(30075) + '...', u
, MODE_SONG_PAGE
, self
.songImg
, 0, totalSongs
- end
)
806 xbmcplugin
.setContent(self
._handle
, 'songs')
807 xbmcplugin
.setPluginFanart(int(sys
.argv
[1]), self
.fanImg
)
809 # Add albums to directory
810 def _add_albums_directory(self
, albums
, artistid
=0):
814 xbmc
.log("Found " + str(n
) + " albums...")
819 if groovesharkApi
.getDoesAlbumExist(albumID
):
820 albumArtistName
= album
[0]
822 albumImage
= self
._get
_icon
(album
[4], 'album-' + str(albumID
))
823 self
._add
_dir
(albumName
+ " - " + albumArtistName
, '', MODE_ALBUM
, albumImage
, albumID
, itemsExisting
)
825 itemsExisting
= itemsExisting
- 1
827 # Not supported by key
829 # self._add_dir('Similar artists...', '', MODE_SIMILAR_ARTISTS, self.artistImg, artistid)
830 xbmcplugin
.setContent(self
._handle
, 'albums')
831 xbmcplugin
.addSortMethod(self
._handle
, xbmcplugin
.SORT_METHOD_ALBUM_IGNORE_THE
)
832 xbmcplugin
.setPluginFanart(int(sys
.argv
[1]), self
.fanImg
)
834 # Add artists to directory
835 def _add_artists_directory(self
, artists
):
839 xbmc
.log("Found " + str(n
) + " artists...")
844 if groovesharkApi
.getDoesArtistExist(artistID
):
845 artistName
= artist
[0]
846 self
._add
_dir
(artistName
, '', MODE_ARTIST
, self
.artistImg
, artistID
, itemsExisting
)
848 itemsExisting
= itemsExisting
- 1
850 xbmcplugin
.setContent(self
._handle
, 'artists')
851 xbmcplugin
.addSortMethod(self
._handle
, xbmcplugin
.SORT_METHOD_ARTIST_IGNORE_THE
)
852 xbmcplugin
.setPluginFanart(int(sys
.argv
[1]), self
.fanImg
)
854 # Add playlists to directory
855 def _add_playlists_directory(self
, playlists
):
858 xbmc
.log("Found " + str(n
) + " playlists...")
861 playlist
= playlists
[i
]
862 playlistName
= playlist
[0]
863 playlistID
= playlist
[1]
864 dir = self
._add
_dir
(playlistName
, '', MODE_PLAYLIST
, self
.playlistImg
, playlistID
, n
)
866 xbmcplugin
.setContent(self
._handle
, 'files')
867 xbmcplugin
.addSortMethod(self
._handle
, xbmcplugin
.SORT_METHOD_LABEL
)
868 xbmcplugin
.setPluginFanart(int(sys
.argv
[1]), self
.fanImg
)
870 # Add whatever directory
871 def _add_dir(self
, name
, url
, mode
, iconimage
, id, items
=1):
874 u
=sys
.argv
[0]+"?mode="+str(mode
)+"&name="+urllib
.quote_plus(name
)+"&id="+str(id)
877 dir=xbmcgui
.ListItem(name
, iconImage
=iconimage
, thumbnailImage
=iconimage
)
878 dir.setInfo( type="Music", infoLabels
={ "title": name
} )
882 if mode
== MODE_ALBUM
:
883 mkplaylst
=sys
.argv
[0]+"?mode="+str(MODE_MAKE_PLAYLIST
)+"&name="+name
+"&id="+str(id)
884 menuItems
.append((__language__(30076), "XBMC.RunPlugin("+mkplaylst
+")"))
885 if mode
== MODE_PLAYLIST
:
886 rmplaylst
=sys
.argv
[0]+"?mode="+str(MODE_REMOVE_PLAYLIST
)+"&name="+urllib
.quote_plus(name
)+"&id="+str(id)
887 menuItems
.append((__language__(30077), "XBMC.RunPlugin("+rmplaylst
+")"))
888 mvplaylst
=sys
.argv
[0]+"?mode="+str(MODE_RENAME_PLAYLIST
)+"&name="+urllib
.quote_plus(name
)+"&id="+str(id)
889 menuItems
.append((__language__(30078), "XBMC.RunPlugin("+mvplaylst
+")"))
891 dir.addContextMenuItems(menuItems
, replaceItems
=False)
893 return xbmcplugin
.addDirectoryItem(handle
=int(sys
.argv
[1]),url
=u
,listitem
=dir,isFolder
=True, totalItems
=items
)
895 def _getSavedSongs(self
):
896 path
= os
.path
.join(cacheDir
, 'songs.dmp')
899 songs
= pickle
.load(f
)
906 def _setSavedSongs(self
, songs
):
908 # Create the 'data' directory if it doesn't exist.
909 if not os
.path
.exists(cacheDir
):
910 os
.makedirs(cacheDir
)
911 path
= os
.path
.join(cacheDir
, 'songs.dmp')
913 pickle
.dump(songs
, f
, protocol
=pickle
.HIGHEST_PROTOCOL
)
916 xbmc
.log("An error occurred saving songs")
919 def _getSongStream(self
, songid
):
924 path
= os
.path
.join(cacheDir
, 'streams.dmp')
927 streams
= pickle
.load(f
)
933 streamServerID
= song
[4]
939 # Not in cache but exists
940 if duration
< 0 and groovesharkApi
.getDoesSongExist(songid
):
941 stream
= groovesharkApi
.getSubscriberStreamKey(songid
)
942 usecs
= stream
['uSecs']
944 streamKey
= stream
['StreamKey']
945 streamServerID
= stream
['StreamServerID']
947 duration
= self
._setDuration
(usecs
)
948 song
= [id, duration
, url
, streamKey
, streamServerID
]
950 self
._setSongStreams
(streams
)
952 return {'duration':duration
, 'url':url
, 'streamKey':streamKey
, 'streamServerID':streamServerID
}
954 def _setSongStream(self
, songid
, stream
):
955 path
= os
.path
.join(cacheDir
, 'streams.dmp')
958 streams
= pickle
.load(f
)
960 if song
[0] == songid
:
961 song
[1] = self
._setDuration
(stream
['uSecs'])
962 song
[2] = stream
['url']
963 song
[3] = stream
['streamKey']
964 song
[4] = stream
['streamServerID']
965 self
._setSongStreams
(streams
)
969 xbmc
.log("An error occurred saving stream")
972 def _setSongStreams(self
, streams
):
974 # Create the cache directory if it doesn't exist.
975 if not os
.path
.exists(cacheDir
):
976 os
.makedirs(cacheDir
)
977 path
= os
.path
.join(cacheDir
, 'streams.dmp')
979 pickle
.dump(streams
, f
, protocol
=pickle
.HIGHEST_PROTOCOL
)
982 xbmc
.log("An error occurred saving streams")
985 def _setDuration(self
, usecs
):
987 usecs
= usecs
* 10 # Some durations are 10x to small
988 return usecs
/ 1000000
990 # Parse URL parameters
993 paramstring
=sys
.argv
[2]
995 xbmc
.log(paramstring
)
996 if len(paramstring
)>=2:
998 cleanedparams
=params
.replace('?','')
999 if (params
[len(params
)-1]=='/'):
1000 params
=params
[0:len(params
)-2]
1001 pairsofparams
=cleanedparams
.split('&')
1003 for i
in range(len(pairsofparams
)):
1005 splitparams
=pairsofparams
[i
].split('=')
1006 if (len(splitparams
))==2:
1007 param
[splitparams
[0]]=splitparams
[1]
1011 grooveshark
= Grooveshark();
1015 try: mode
=int(params
["mode"])
1018 try: id=int(params
["id"])
1021 try: name
=urllib
.unquote_plus(params
["name"])
1024 # Call function for URL
1026 grooveshark
.categories()
1028 elif mode
==MODE_SEARCH_SONGS
:
1029 grooveshark
.searchSongs()
1031 elif mode
==MODE_SEARCH_ALBUMS
:
1032 grooveshark
.searchAlbums()
1034 elif mode
==MODE_SEARCH_ARTISTS
:
1035 grooveshark
.searchArtists()
1037 elif mode
==MODE_SEARCH_ARTISTS_ALBUMS
:
1038 grooveshark
.searchArtistsAlbums(name
)
1040 elif mode
==MODE_SEARCH_PLAYLISTS
:
1041 grooveshark
.searchPlaylists()
1043 elif mode
==MODE_POPULAR_SONGS
:
1044 grooveshark
.popularSongs()
1046 elif mode
==MODE_ARTIST_POPULAR
:
1047 grooveshark
.artistPopularSongs()
1049 elif mode
==MODE_FAVORITES
:
1050 grooveshark
.favorites()
1052 elif mode
==MODE_PLAYLISTS
:
1053 grooveshark
.playlists()
1055 elif mode
==MODE_SONG_PAGE
:
1056 try: offset
=urllib
.unquote_plus(params
["offset"])
1058 try: label
=urllib
.unquote_plus(params
["label"])
1060 grooveshark
.songPage(offset
, label
, id, name
)
1062 elif mode
==MODE_SONG
:
1063 try: album
=urllib
.unquote_plus(params
["album"])
1065 try: artist
=urllib
.unquote_plus(params
["artist"])
1067 try: coverart
=urllib
.unquote_plus(params
["coverart"])
1069 song
= grooveshark
.songItem(id, name
, album
, artist
, coverart
)
1070 grooveshark
.playSong(song
)
1072 elif mode
==MODE_ARTIST
:
1073 grooveshark
.artist(id)
1075 elif mode
==MODE_ALBUM
:
1076 grooveshark
.album(id)
1078 elif mode
==MODE_PLAYLIST
:
1079 grooveshark
.playlist(id, name
)
1081 elif mode
==MODE_FAVORITE
:
1082 grooveshark
.favorite(id)
1084 elif mode
==MODE_UNFAVORITE
:
1085 try: prevMode
=int(urllib
.unquote_plus(params
["prevmode"]))
1088 grooveshark
.unfavorite(id, prevMode
)
1090 elif mode
==MODE_SIMILAR_ARTISTS
:
1091 grooveshark
.similarArtists(id)
1093 elif mode
==MODE_MAKE_PLAYLIST
:
1094 grooveshark
.makePlaylist(id, name
)
1096 elif mode
==MODE_REMOVE_PLAYLIST
:
1097 grooveshark
.removePlaylist(id, name
)
1099 elif mode
==MODE_RENAME_PLAYLIST
:
1100 grooveshark
.renamePlaylist(id, name
)
1102 elif mode
==MODE_REMOVE_PLAYLIST_SONG
:
1103 try: playlistID
=urllib
.unquote_plus(params
["playlistid"])
1105 grooveshark
.removePlaylistSong(playlistID
, name
, id)
1107 elif mode
==MODE_ADD_PLAYLIST_SONG
:
1108 grooveshark
.addPlaylistSong(id)
1110 if mode
< MODE_SONG
:
1111 xbmcplugin
.endOfDirectory(int(sys
.argv
[1]))