__author__ = __addon__.getAddonInfo('author')
__version__ = __addon__.getAddonInfo('version')
__language__ = __addon__.getLocalizedString
-
+__debugging__ = __addon__.getSetting('debug')
MODE_SEARCH_SONGS = 1
MODE_SEARCH_ALBUMS = 2
# Stream marking time (seconds)
STREAM_MARKING_TIME = 30
+# Timeout
+STREAM_TIMEOUT = 30
+
songMarkTime = 0
player = xbmc.Player()
playTimer = None
imgDir = xbmc.translatePath(os.path.join(resDir, 'img'))
cacheDir = os.path.join(xbmc.translatePath('special://masterprofile/addon_data/'), os.path.basename(baseDir))
thumbDirName = 'thumb'
-thumbDir = os.path.join('special://masterprofile/addon_data/', os.path.basename(baseDir), thumbDirName)
+thumbDir = os.path.join(xbmc.translatePath('special://masterprofile/addon_data/'), os.path.basename(baseDir), thumbDirName)
baseModeUrl = 'plugin://plugin.audio.groove/'
playlistUrl = baseModeUrl + '?mode=' + str(MODE_PLAYLIST)
from GroovesharkAPI import GrooveAPI
from threading import Event, Thread
+if __debugging__ == 'true':
+ __debugging__ = True
+else:
+ __debugging__ = False
+
try:
- groovesharkApi = GrooveAPI()
+ groovesharkApi = GrooveAPI(__debugging__)
if groovesharkApi.pingService() != True:
raise StandardError(__language__(30007))
except:
sys.exit(-1)
# Mark song as playing or played
-def markSong(songid, duration):
+def markSong(songid, duration, streamKey, streamServerID):
global songMarkTime
global playTimer
global player
if player.isPlayingAudio():
tNow = player.getTime()
if tNow >= STREAM_MARKING_TIME and songMarkTime == 0:
- groovesharkApi.markStreamKeyOver30Secs()
+ groovesharkApi.markStreamKeyOver30Secs(streamKey, streamServerID)
songMarkTime = tNow
elif duration > tNow and duration - tNow < 2 and songMarkTime >= STREAM_MARKING_TIME:
playTimer.cancel()
songMarkTime = 0
- groovesharkApi.markSongComplete(songid)
+ groovesharkApi.markSongComplete(songid, streamKey, streamServerID)
else:
playTimer.cancel()
songMarkTime = 0
popularSongsArtistImg = xbmc.translatePath(os.path.join(imgDir, 'popularSongsArtist.png'))
songImg = xbmc.translatePath(os.path.join(imgDir, 'song.png'))
defImg = xbmc.translatePath(os.path.join(imgDir, 'default.tbn'))
- fanImg = xbmc.translatePath(os.path.join(baseDir, 'fanart.png'))
+ fanImg = xbmc.translatePath(os.path.join(baseDir, 'fanart.jpg'))
settings = xbmcaddon.Addon(id='plugin.audio.groove')
songsearchlimit = int(settings.getSetting('songsearchlimit'))
def __init__( self ):
self._handle = int(sys.argv[1])
- self.setDebug()
if os.path.isdir(cacheDir) == False:
os.makedirs(cacheDir)
- if self.debug:
+ if __debugging__ :
xbmc.log(__language__(30012) + " " + cacheDir)
artDir = xbmc.translatePath(thumbDir)
if os.path.isdir(artDir) == False:
os.makedirs(artDir)
- if self.debug:
+ if __debugging__ :
xbmc.log(__language__(30012) + " " + artDir)
# Top-level menu
if (len(artists) > 0):
artist = artists[0]
artistID = artist[1]
- if self.debug:
+ if __debugging__ :
xbmc.log("Found " + artist[0] + "...")
- albums = groovesharkApi.getArtistAlbums(artistID, limit = self.albumsearchlimit)
+ albums = groovesharkApi.getArtistAlbums(artistID, self.albumsearchlimit)
if (len(albums) > 0):
self._add_albums_directory(albums, artistID)
else:
def favorite(self, songid):
userid = self._get_login()
if (userid != 0):
- if self.debug:
+ if __debugging__ :
xbmc.log("Favorite song: " + str(songid))
groovesharkApi.addUserFavoriteSong(songID = songid)
xbmc.executebuiltin('XBMC.Notification(' + __language__(30008) + ', ' + __language__(30036) + ', 1000, ' + thumbDef + ')')
def unfavorite(self, songid, prevMode=0):
userid = self._get_login()
if (userid != 0):
- if self.debug:
+ if __debugging__ :
xbmc.log("Unfavorite song: " + str(songid) + ', previous mode was ' + str(prevMode))
groovesharkApi.removeUserFavoriteSongs(songIDs = songid)
xbmc.executebuiltin('XBMC.Notification(' + __language__(30008) + ', ' + __language__(30038) + ', 1000, ' + thumbDef + ')')
# Show selected artist
def artist(self, artistid):
albums = groovesharkApi.getArtistAlbums(artistid, limit = self.albumsearchlimit)
- self._add_albums_directory(albums, artistid)
+ self._add_albums_directory(albums, artistid, True)
# Show selected playlist
def playlist(self, playlistid, playlistname):
if (len(artists) > 0):
artist = artists[0]
artistID = artist[1]
- if self.debug:
+ if __debugging__ :
xbmc.log("Found " + artist[0] + "...")
songs = groovesharkApi.getArtistPopularSongs(artistID, limit = self.songsearchlimit)
if (len(songs) > 0):
def playSong(self, item):
global playTimer
global player
+ player.stop()
if item != None:
+ url = ''
songid = item.getProperty('songid')
- url = item.getProperty('url')
- if url == '':
- stream = groovesharkApi.getSubscriberStreamKey(songid)
+ duration = int(self._getSongDuration(songid))
+ stream = groovesharkApi.getSubscriberStreamKey(songid)
+ if stream != False:
url = stream['url']
- item.setPath(url)
- if self.debug:
- xbmc.log("Grooveshark playing: " + url)
- xbmcplugin.setResolvedUrl(handle=int(sys.argv[1]), succeeded=True, listitem=item)
- # Wait for play then start time
- seconds = 0
- while seconds < STREAM_MARKING_TIME:
- try:
- if player.isPlayingAudio() == True:
- if playTimer != None:
- playTimer.cancel()
- songMarkTime = 0
- duration = int(item.getProperty('duration'))
- playTimer = PlayTimer(1, markSong, duration, [songid, duration])
- playTimer.start()
- break
- except: pass
- time.sleep(1)
- seconds = seconds + 1
+ key = stream['StreamKey']
+ server = stream['StreamServerID']
+ duration = self._setDuration(stream['uSecs'])
+ if url != '':
+ item.setPath(url)
+ xbmcplugin.setResolvedUrl(handle=int(sys.argv[1]), succeeded=True, listitem=item)
+ if __debugging__ :
+ xbmc.log("Grooveshark playing: " + url)
+ # Wait for play then start timer
+ seconds = 0
+ while seconds < STREAM_TIMEOUT:
+ try:
+ if player.isPlayingAudio() == True:
+ if playTimer != None:
+ playTimer.cancel()
+ songMarkTime = 0
+ playTimer = PlayTimer(1, markSong, duration, [songid, duration, key, server])
+ playTimer.start()
+ break
+ except: pass
+ time.sleep(1)
+ seconds = seconds + 1
+ else:
+ xbmc.log("No song URL")
else:
xbmc.executebuiltin('XBMC.Notification(' + __language__(30008) + ', ' + __language__(30044) + ', 1000, ' + thumbDef + ')')
trackLabel = name + " - " + album + " - " + artist
else:
trackLabel = artist + " - " + album + " - " + name
- stream = self._getSongStream(songid)
- duration = stream['duration']
- url = stream['url']
+ duration = self._getSongDuration(songid)
item = xbmcgui.ListItem(label = trackLabel, thumbnailImage=songImg, iconImage=songImg)
item.setInfo( type="music", infoLabels={ "title": name, "album": album, "artist": artist, "duration": duration} )
item.setProperty('mimetype', 'audio/mpeg')
item.setProperty('album', album)
item.setProperty('artist', artist)
item.setProperty('duration', str(duration))
- item.setProperty('url', str(url))
return item
else:
playlist = playlists[i]
playlistid = playlist[1]
- if self.debug:
+ if __debugging__ :
xbmc.log("Add song " + str(songid) + " to playlist " + str(playlistid))
songIDs=[]
songs = groovesharkApi.getPlaylistSongs(playlistid)
dialog.ok(__language__(30008), __language__(30068))
self.categories()
- # Debug
- def setDebug(self):
- self.debug = self.settings.getSetting('debug')
- if self.debug:
- xbmc.log("Debug is on")
-
- # Debug
- def getDebug(self):
- return self.debug
-
# Get keyboard input
def _get_keyboard(self, default="", heading="", hidden=False):
kb = xbmc.Keyboard(default, heading, hidden)
# No pages needed
if offset == 0 and totalSongs <= self.songspagelimit:
- if self.debug:
+ if __debugging__ :
xbmc.log("Found " + str(totalSongs) + " songs...")
# Pages
else:
while n < end:
song = songs[n]
songid = song[1]
- stream = self._getSongStream(songid)
- if stream['url'] != '':
+ duration = self._getSongDuration(songid)
+ if duration != -1:
item = self._get_song_item(song, trackLabelFormat)
coverart = item.getProperty('coverart')
songname = song[0]
id = id + 1
else:
end = min(end + 1,totalSongs)
- if self.debug:
+ if __debugging__ :
xbmc.log(song[0] + " does not exist.")
n = n + 1
xbmcplugin.setPluginFanart(int(sys.argv[1]), self.fanImg)
# Add albums to directory
- def _add_albums_directory(self, albums, artistid=0):
+ def _add_albums_directory(self, albums, artistid=0, isverified=False):
n = len(albums)
itemsExisting = n
- if self.debug:
+ if __debugging__ :
xbmc.log("Found " + str(n) + " albums...")
i = 0
while i < n:
album = albums[i]
albumID = album[3]
- if groovesharkApi.getDoesAlbumExist(albumID):
+ if isverified or groovesharkApi.getDoesAlbumExist(albumID):
albumArtistName = album[0]
albumName = album[2]
albumImage = self._get_icon(album[4], 'album-' + str(albumID))
def _add_artists_directory(self, artists):
n = len(artists)
itemsExisting = n
- if self.debug:
+ if __debugging__ :
xbmc.log("Found " + str(n) + " artists...")
i = 0
while i < n:
# Add playlists to directory
def _add_playlists_directory(self, playlists):
n = len(playlists)
- if self.debug:
+ if __debugging__ :
xbmc.log("Found " + str(n) + " playlists...")
i = 0
while i < n:
xbmc.log("An error occurred saving songs")
pass
- def _getSongStream(self, songid):
+ def _getSongDuration(self, songid):
id = int(songid)
duration = -1
- streams = []
- url = ''
- path = os.path.join(cacheDir, 'streams.dmp')
+ durations = []
+ path = os.path.join(cacheDir, 'duration.dmp')
try:
f = open(path, 'rb')
- streams = pickle.load(f)
- for song in streams:
+ durations = pickle.load(f)
+ for song in durations:
if song[0] == id:
duration = song[1]
- url = song[2]
break;
f.close()
except:
pass
- if duration < 0 and groovesharkApi.getDoesSongExist(songid):
+ # Not in cache
+ if duration < 0:
stream = groovesharkApi.getSubscriberStreamKey(songid)
- url = stream['url']
- usecs = stream['uSecs']
- if usecs < 60000000:
- usecs = usecs * 10 # Some durations are 10x to small
- duration = usecs / 1000000
- song = [id, duration, url]
- streams.append(song)
- self._setSongStream(streams)
-
- return {'duration':duration, 'url':url}
+ if stream != False and stream['url'] != '':
+ duration = self._setDuration(stream['uSecs'])
+ song = [id, duration]
+ self._setSongDuration(song, durations)
+
+ return duration
- def _setSongStream(self, streams):
+ def _setSongDuration(self, song, durations):
try:
- # Create the 'data' directory if it doesn't exist.
+ durations.append(song)
+ # Create the cache directory if it doesn't exist.
if not os.path.exists(cacheDir):
os.makedirs(cacheDir)
- path = os.path.join(cacheDir, 'streams.dmp')
+ path = os.path.join(cacheDir, 'duration.dmp')
f = open(path, 'wb')
- pickle.dump(streams, f, protocol=pickle.HIGHEST_PROTOCOL)
+ pickle.dump(durations, f, protocol=pickle.HIGHEST_PROTOCOL)
f.close()
except:
- xbmc.log("An error occurred saving streams")
+ xbmc.log("An error occurred saving duration")
pass
+ # Duration to seconds
+ def _setDuration(self, usecs):
+ if usecs < 60000000:
+ usecs = usecs * 10 # Some durations are 10x to small
+ return usecs / 1000000
# Parse URL parameters
def get_params():
param=[]
paramstring=sys.argv[2]
- if grooveshark.getDebug():
+ if __debugging__ :
xbmc.log(paramstring)
if len(paramstring)>=2:
params=sys.argv[2]
# Main
grooveshark = Grooveshark();
-grooveshark.setDebug()
-groovesharkApi.setDebug(grooveshark.getDebug())
params=get_params()
mode=None