class GoogleMusicApi(): def __init__(self): self.xbmc = sys.modules["__main__"].xbmc self.xbmcgui = sys.modules["__main__"].xbmcgui self.xbmcplugin = sys.modules["__main__"].xbmcplugin self.settings = sys.modules["__main__"].settings self.language = sys.modules["__main__"].language self.dbg = sys.modules["__main__"].dbg self.common = sys.modules["__main__"].common self.storage = sys.modules["__main__"].storage self.gmusicapi = Api() self.login = GoogleMusicLogin.GoogleMusicLogin(self.gmusicapi) def getPlaylistSongs(self, playlist_id, forceRenew=False): if not self.storage.isPlaylistFetched(playlist_id) or forceRenew: self.updatePlaylistSongs(playlist_id) songs = self.storage.getPlaylistSongs(playlist_id) return songs def getPlaylistsByType(self, playlist_type, forceRenew=False): if forceRenew: self.updatePlaylists(playlist_type) playlists = self.storage.getPlaylistsByType(playlist_type) if len(playlists) == 0 and not forceRenew: self.updatePlaylists(playlist_type) playlists = self.storage.getPlaylistsByType(playlist_type) return playlists def getSong(self, song_id): return self.storage.getSong(song_id) def updatePlaylistSongs(self, playlist_id): api_songs = [] self.login.login() if playlist_id == 'all_songs': api_songs = self.gmusicapi.get_all_songs() else: api_songs = self.gmusicapi.get_playlist_songs(playlist_id) self.storage.storeApiSongs(api_songs, playlist_id) def updatePlaylists(self, playlist_type): self.login.login() playlists = self.gmusicapi.get_all_playlist_ids(playlist_type=="auto", playlist_type=="instant", playlist_type=="user", always_id_lists=True) self.storage.storePlaylists(playlists[playlist_type], playlist_type) def getSongStreamUrl(self, song_id): self.login.login() stream_url = self.gmusicapi.get_stream_url(song_id) #self.storage.updateSongStreamUrl(song_id, stream_url) return stream_url
class GoogleMusicApi(): def __init__(self): self.storage = sys.modules["__main__"].storage self.gmusicapi = Api(debug_logging=False) self.login = GoogleMusicLogin.GoogleMusicLogin(self.gmusicapi) def getPlaylistSongs(self, playlist_id, forceRenew=False): if playlist_id == 'thumbsup': return self.storage.getThumbsup() if playlist_id == 'lastadded': return self.storage.getLastadded() if playlist_id == 'mostplayed': return self.storage.getMostplayed() if playlist_id == 'freepurchased': return self.storage.getFreepurchased() if not self.storage.isPlaylistFetched(playlist_id) or forceRenew: self.updatePlaylistSongs(playlist_id) songs = self.storage.getPlaylistSongs(playlist_id) return songs def getPlaylistsByType(self, playlist_type, forceRenew=False): if playlist_type == 'auto': return [['thumbsup', 'Highly Rated'], ['lastadded', 'Last Added'], ['freepurchased', 'Free and Purchased'], ['mostplayed', 'Most Played']] if forceRenew: self.updatePlaylists(playlist_type) playlists = self.storage.getPlaylistsByType(playlist_type) if len(playlists) == 0 and not forceRenew: self.updatePlaylists(playlist_type) playlists = self.storage.getPlaylistsByType(playlist_type) return playlists def getSong(self, song_id): return self.storage.getSong(song_id) def updatePlaylistSongs(self, playlist_id): api_songs = [] self.login.login() if playlist_id == 'all_songs': api_songs = self.gmusicapi.get_all_songs() else: api_songs = self.gmusicapi.get_playlist_songs(playlist_id) if api_songs: self.storage.storeApiSongs(api_songs, playlist_id) def updatePlaylists(self, playlist_type): self.login.login() playlists = self.gmusicapi.get_all_playlist_ids(playlist_type) self.storage.storePlaylists(playlists[playlist_type], playlist_type) def getSongStreamUrl(self, song_id): self.login.login() stream_url = self.gmusicapi.get_stream_url(song_id) #self.storage.updateSongStreamUrl(song_id, stream_url) return stream_url def getFilterSongs(self, filter_type, filter_criteria): songs = self.storage.getFilterSongs(filter_type, filter_criteria) return songs def getCriteria(self, criteria): return self.storage.getCriteria(criteria) def getSearch(self, query): return self.storage.getSearch(query) def clearCache(self): self.storage.clearCache() self.login.clearCookie() def clearCookie(self): self.login.clearCookie()
class GoogleMusicApi(): def __init__(self): self.storage = sys.modules["__main__"].storage self.gmusicapi = Api(debug_logging=False) self.login = GoogleMusicLogin.GoogleMusicLogin(self.gmusicapi) def getPlaylistSongs(self, playlist_id, forceRenew=False): if playlist_id == 'thumbsup': return self.storage.getThumbsup() if playlist_id == 'lastadded': return self.storage.getLastadded() if playlist_id == 'mostplayed': return self.storage.getMostplayed() if playlist_id == 'freepurchased': return self.storage.getFreepurchased() if not self.storage.isPlaylistFetched(playlist_id) or forceRenew: self.updatePlaylistSongs(playlist_id) songs = self.storage.getPlaylistSongs(playlist_id) return songs def getPlaylistsByType(self, playlist_type, forceRenew=False): if playlist_type == 'auto': return [['thumbsup','Highly Rated'],['lastadded','Last Added'],['freepurchased','Free and Purchased'],['mostplayed','Most Played']] if forceRenew: self.updatePlaylists(playlist_type) playlists = self.storage.getPlaylistsByType(playlist_type) if len(playlists) == 0 and not forceRenew: self.updatePlaylists(playlist_type) playlists = self.storage.getPlaylistsByType(playlist_type) return playlists def getSong(self, song_id): return self.storage.getSong(song_id) def updatePlaylistSongs(self, playlist_id): api_songs = [] self.login.login() if playlist_id == 'all_songs': api_songs = self.gmusicapi.get_all_songs() else: api_songs = self.gmusicapi.get_playlist_songs(playlist_id) if api_songs: self.storage.storeApiSongs(api_songs, playlist_id) def updatePlaylists(self, playlist_type): self.login.login() playlists = self.gmusicapi.get_all_playlist_ids(playlist_type) self.storage.storePlaylists(playlists[playlist_type], playlist_type) def getSongStreamUrl(self, song_id): self.login.login() stream_url = self.gmusicapi.get_stream_url(song_id) #self.storage.updateSongStreamUrl(song_id, stream_url) return stream_url def getFilterSongs(self, filter_type, filter_criteria): songs = self.storage.getFilterSongs(filter_type, filter_criteria) return songs def getCriteria(self, criteria): return self.storage.getCriteria(criteria) def getSearch(self, query): return self.storage.getSearch(query) def clearCache(self): self.storage.clearCache() self.login.clearCookie() def clearCookie(self): self.login.clearCookie()
class GoogleMusic(): """This class handles the communication with Google Music.""" def __init__(self, email, password): if not email or not password: raise Exception("Username and password needs to be given") self._library = None self._artists = None self._albums = None self._playlists = None self._email = email self._password = password # Initialize the Google Music API self._api_init(self._email, self._password) if not self._api.is_authenticated(): raise Exception("Credentials were not accepted!") # Initial load of the library self.load_library() def _api_init(self, email, password): """Inits the API object and login to Google Music""" self._api = Api() self._api.login(email, password) def load_library(self): """ This function downloads the music library from Google Music and processes it. """ self._library = self._api.get_all_songs() # Generate artists and albums trees self._gen_trees() def _gen_trees(self): """ This function generates trees of artists and albums from the library. Parts of this function are taken from: https://github.com/mstill/thunner/blob/master/thunner => Thanks for the great code :) """ # Use defaultdict to group song dictionaries by artists artists_dict = collections.defaultdict(list) for i in self._library: artists_dict[i['artist']].append(i) artists = [] albums = [] for artist, songs_of_artist in artists_dict.iteritems(): albums_of_artists_dict = collections.defaultdict(list) for i in songs_of_artist: albums_of_artists_dict[i['album']].append(i) albums_of_artist = [] for album,tracks in albums_of_artists_dict.iteritems(): album_name = album if album == "": album_name = "Untitled album" albums_of_artist.append({ "name": album_name, "subtree": sorted(tracks, key=itemgetter('track')), "subtreeline": 0 }) albums = albums + albums_of_artist artists.append({ "name": artist, "subtree": sorted(albums_of_artist, key=lambda x: x['name'].lower()), "subtreeline": 0 }) self._artists = sorted(artists, key=lambda x: x['name'].lower()) self._albums = sorted(albums, key=lambda x: x['name'].lower()) def get_songs(self): """This function returns all songs from the music library.""" songs = [] for entry in self._library: song = { 'id': entry['id'], 'title': entry['title'], 'artist': entry['artist'], 'album': entry['album'] } songs.append(song) return songs def get_artists(self): pass def get_albums(self): pass def get_stream_url(self, id): return self._api.get_stream_url(id)
class GoogleMusic(): """This class handles the communication with Google Music.""" def __init__(self, email, password): if not email or not password: raise Exception("Username and password needs to be given") self._library = None self._artists = None self._albums = None self._playlists = None self._email = email self._password = password # Initialize the Google Music API self._api_init(self._email, self._password) if not self._api.is_authenticated(): raise Exception("Credentials were not accepted!") # Initial load of the library self.load_library() def _api_init(self, email, password): """Inits the API object and login to Google Music""" self._api = Api() self._api.login(email, password) def load_library(self): """ This function downloads the music library from Google Music and processes it. """ self._library = self._api.get_all_songs() # Generate artists and albums trees self._gen_trees() def _gen_trees(self): """ This function generates trees of artists and albums from the library. Parts of this function are taken from: https://github.com/mstill/thunner/blob/master/thunner => Thanks for the great code :) """ # Use defaultdict to group song dictionaries by artists artists_dict = collections.defaultdict(list) for i in self._library: artists_dict[i['artist']].append(i) artists = [] albums = [] for artist, songs_of_artist in artists_dict.iteritems(): albums_of_artists_dict = collections.defaultdict(list) for i in songs_of_artist: albums_of_artists_dict[i['album']].append(i) albums_of_artist = [] for album, tracks in albums_of_artists_dict.iteritems(): album_name = album if album == "": album_name = "Untitled album" albums_of_artist.append({ "name": album_name, "subtree": sorted(tracks, key=itemgetter('track')), "subtreeline": 0 }) albums = albums + albums_of_artist artists.append({ "name": artist, "subtree": sorted(albums_of_artist, key=lambda x: x['name'].lower()), "subtreeline": 0 }) self._artists = sorted(artists, key=lambda x: x['name'].lower()) self._albums = sorted(albums, key=lambda x: x['name'].lower()) def get_songs(self): """This function returns all songs from the music library.""" songs = [] for entry in self._library: song = { 'id': entry['id'], 'title': entry['title'], 'artist': entry['artist'], 'album': entry['album'] } songs.append(song) return songs def get_artists(self): pass def get_albums(self): pass def get_stream_url(self, id): return self._api.get_stream_url(id)
class GoogleMusicApi(): def __init__(self): self.xbmc = sys.modules["__main__"].xbmc self.xbmcgui = sys.modules["__main__"].xbmcgui self.xbmcplugin = sys.modules["__main__"].xbmcplugin self.settings = sys.modules["__main__"].settings self.language = sys.modules["__main__"].language self.dbg = sys.modules["__main__"].dbg self.common = sys.modules["__main__"].common self.storage = sys.modules["__main__"].storage self.gmusicapi = Api() self.login = GoogleMusicLogin.GoogleMusicLogin(self.gmusicapi) def getPlaylistSongs(self, playlist_id, forceRenew=False): if not self.storage.isPlaylistFetched(playlist_id) or forceRenew: self.updatePlaylistSongs(playlist_id) songs = self.storage.getPlaylistSongs(playlist_id) return songs def getPlaylistsByType(self, playlist_type, forceRenew=False): if forceRenew: self.updatePlaylists(playlist_type) playlists = self.storage.getPlaylistsByType(playlist_type) if len(playlists) == 0 and not forceRenew: self.updatePlaylists(playlist_type) playlists = self.storage.getPlaylistsByType(playlist_type) return playlists def getSong(self, song_id): return self.storage.getSong(song_id) def updatePlaylistSongs(self, playlist_id): api_songs = [] self.login.login() if playlist_id == 'all_songs': api_songs = self.gmusicapi.get_all_songs() else: api_songs = self.gmusicapi.get_playlist_songs(playlist_id) self.storage.storeApiSongs(api_songs, playlist_id) def updatePlaylists(self, playlist_type): self.login.login() playlists = self.gmusicapi.get_all_playlist_ids( playlist_type == "auto", playlist_type == "instant", playlist_type == "user", always_id_lists=True) self.storage.storePlaylists(playlists[playlist_type], playlist_type) def getSongStreamUrl(self, song_id): self.login.login() stream_url = self.gmusicapi.get_stream_url(song_id) #self.storage.updateSongStreamUrl(song_id, stream_url) return stream_url