X-Git-Url: http://git.hcoop.net/clinton/xbmc-groove.git/blobdiff_plain/9b1d8c96efe57fcf10c8290825b26fc302a7f666..3fcef5ba80eb63c54c4220ff91ebf79ac4390957:/resources/lib/GrooveAPI.py diff --git a/resources/lib/GrooveAPI.py b/resources/lib/GrooveAPI.py index 5cc37f7..63ab3db 100644 --- a/resources/lib/GrooveAPI.py +++ b/resources/lib/GrooveAPI.py @@ -27,13 +27,13 @@ class GrooveAPI: socket.setdefaulttimeout(timeout) self.enableDebug = enableDebug self.loggedIn = 0 - self.radioEnabled = 0 self.userId = 0 - self.seedArtists = [] - self.frowns = [] - self.songIDsAlreadySeen = [] - self.recentArtists = [] self.removeDuplicates = False + + self.radioRecentSongs = [] + self.radioRecentArtists = [] + self.radioEnabled = False + self.dataDir = 'plugin_data/music/' self.confDir = xbmc.translatePath(os.path.join('special://masterprofile/' + self.dataDir, os.path.basename(os.getcwd()))) self.sessionID = self.getSavedSession() @@ -152,7 +152,7 @@ class GrooveAPI: if 'fault' in result: return '' else: - return result['header']['sessionID'] + return result['result']['sessionID'] def sessionDestroy(self): return self.callRemote("session.destroy") @@ -328,127 +328,114 @@ class GrooveAPI: else: return 0 - def autoplayStartWithArtistIDs(self, artistIds): - result = self.callRemote("autoplay.startWithArtistIDs", {"artistIDs": artistIds}) - if 'fault' in result: - self.radioEnabled = 0 - return 0 - else: - self.radioEnabled = 1 - return 1 - - def autoplayStart(self, songIds): - result = self.callRemote("autoplay.start", {"songIDs": songIds}) + def radioStartArtists(self): + radio = self.getSavedRadio() + if radio == None: + return False + result = self.callRemote("autoplay.startWithArtistIDs", {"artistIDs": radio['seedArtists']}) if 'fault' in result: - self.radioEnabled = 0 - return 0 + self.radioEnabled = False else: - self.radioEnabled = 1 - return 1 + self.radioEnabled = True + return self.radioEnabled - def autoplayGetNextSongEx(self, seedArtists = [], frowns = [], songIDsAlreadySeen = [], recentArtists = []): - result = self.callRemote("autoplay.getNextSongEx", {"seedArtists": seedArtists, "frowns": frowns, "songIDsAlreadySeen": songIDsAlreadySeen, "recentArtists": recentArtists}) + def radioStartSongs(self): + radio = self.getSavedRadio() + if radio == None: + return False + result = self.callRemote("autoplay.start", {"songIDs": radio['seedSongs']}) if 'fault' in result: - return [] + self.radioEnabled = False else: - return result + self.radioEnabled = True + return self.radioEnabled - def radioGetNextSong(self): + def radioNextSong(self): radio = self.getSavedRadio() if radio == None: return None else: - seedArtists = [] - for song in radio['seedArtists']: - seedArtists.append(song[7]) - result = self.autoplayGetNextSongEx(seedArtists, radio['frowns'], radio['songIDsAlreadySeen'], radio['recentArtists']) + result = self.callRemote("autoplay.getNextSongEx", {"seedArtists": radio['seedArtists'], "frowns": radio['frowns'], "songIDsAlreadySeen": self.radioRecentSongs, "recentArtists": self.radioRecentArtists}) if 'fault' in result: return [] else: song = self.parseSongs(result) - self.radioSetAlreadyListenedSong(songId = song[0][1]) + self.radioRecentSongs.append(song[0][1]) + self.radioRecentArtists.append(song[0][7]) return song - def radioFrown(self, songId): - self.frown.append(songId) - - def radioAlreadySeen(self, songId): - self.songIDsAlreadySeen.append(songId) - - def radioAddArtist(self, song = None, radioName = None): - radio = self.getSavedRadio(name = radioName) - if radio != None and song != None: - radio['seedArtists'].append(song) + def radioFrown(self, songId = None): + radio = self.getSavedRadio() + if radio != None and songId != None: + try: + radio['frowns'].remove(songId) + except: pass + radio['frowns'].append(songId) return self.saveRadio(radio = radio) else: - return 0 + return False - def radioStart(self, artists = [], frowns = []): - for artist in artists: - self.seedArtists.append(artist) - for artist in frowns: - self.frowns.append(artist) - if self.autoplayStartWithArtistIDs(self.seedArtists) == 1: - self.radioEnabled = 1 - return 1 + def radioArtist(self, artistId = None): + radio = self.getSavedRadio() + if radio != None and artistId != None: + try: + radio['seedArtists'].remove(artistId) + except: pass + radio['seedArtists'].append(artistId) + return self.saveRadio(radio = radio) else: - self.radioEnabled = 0 - return 0 + return False - def radioStop(self): - self.seedArtists = [] - self.frowns = [] - self.songIDsAlreadySeen = [] - self.recentArtists = [] - self.radioEnabled = 0 + def radioSong(self, songId = None): + radio = self.getSavedRadio() + if radio != None and songId != None: + try: + radio['seedSongs'].remove(songId) + except: pass + radio['seedSongs'].append(songId) + return self.saveRadio(radio = radio) + else: + return False def radioTurnedOn(self): return self.radioEnabled - def radioSetAlreadyListenedSong(self, name = None, songId = ''): - radio = self.getSavedRadio(name = name) - if radio != None and songId != '': - radio['songIDsAlreadySeen'].append(songId) - while len(radio['songIDsAlreadySeen']) > 20: - radio['songIDsAlreadySeen'].pop(0) # Trim - return self.saveRadio(radio = radio) - else: - return 0 - - def getSavedRadio(self, name = None): - if name == None: - path = os.path.join(self.confDir, 'radio', 'default.txt') - else: - path = os.path.join(self.confDir, 'radio', 'saved', name) + def getSavedRadio(self): + path = os.path.join(self.confDir, 'radio', 'radio.dmp') try: f = open(path, 'rb') radio = pickle.load(f) f.close() + print radio except: - radio = None + radio = {} + radio['seedSongs'] = [] + radio['seedArtists'] = [] + radio['frowns'] = [] + if self.saveRadio(radio) == False: + return None return radio - def saveRadio(self, name = None, radio = {}): #blaher + def saveRadio(self, radio): #blaher + if radio == {}: + print 'Invalid radio' + return False try: dir = os.path.join(self.confDir, 'radio') # Create the 'data' directory if it doesn't exist. if not os.path.exists(dir): os.mkdir(dir) - os.mkdir(os.path.join(dir, 'saved')) - if name == None: - path = os.path.join(dir, 'default.txt') - else: - path = os.path.join(dir, 'saved', name) + path = os.path.join(dir, 'radio.dmp') f = open(path, 'wb') pickle.dump(radio, f, protocol=pickle.HIGHEST_PROTOCOL) f.close() - return 1 + return True except IOError, e: print 'There was an error while saving the radio pickle (%s)' % e - return 0 + return False except: - print "An unknown error occured during save radio: " + str(sys.exc_info()[0]) - return 0 + print "An unknown error occurred during save radio: " + str(sys.exc_info()[0]) + return False def favoriteSong(self, songID): return self.callRemote("song.favorite", {"songID": songID})