def __get_albums(self, name): """ Get albums for name @param name as str """ try: # Read album list formated = Lio.uri_escape_string(name, None, True).replace(' ', '+') s = Lio.File.new_for_uri("https://itunes.apple.com/search" "?entity=album&term=%s" % formated) (status, data, tag) = s.load_contents(self._cancel) if not status: raise decode = json.loads(data.decode('utf-8')) # For each album, get cover and tracks for item in decode['results']: album_item = SearchItem() album_item.name = album_item.album_name =\ item['collectionName'] album_item.is_track = False album_item.artists = [item['artistName']] album_item.cover = item['artworkUrl60'].replace( '60x60', '512x512') album_item.smallcover = item['artworkUrl100'] s = Lio.File.new_for_uri("https://itunes.apple.com/lookup" "?id=%s&entity=song" % item['collectionId']) (status, data, tag) = s.load_contents(self._cancel) if not status: raise decode = json.loads(data.decode('utf-8')) for item in decode['results']: if item['wrapperType'] == "collection": continue track_item = SearchItem() track_item.is_track = True try: track_item.year = decode['releaseDate'][:4] except: pass # May be missing track_item.name = item['trackName'] track_item.album = album_item.name track_item.tracknumber = int(item['trackNumber']) track_item.discnumber = int(item['discNumber']) track_item.duration = int(item['trackTimeMillis'])\ / 1000 track_item.artists.append(item['artistName']) album_item.subitems.append(track_item) self._items.append(album_item) GLib.idle_add(self.emit, 'item-found') except Exception as e: print("ItunesSearch::__get_albums(): %s" % e)
def __get_albums(self, name): """ Get albums for name @param name as str """ try: # Read album list formated = GLib.uri_escape_string(name, None, True).replace(" ", "+") s = Lio.File.new_for_uri("https://api.spotify.com/v1/search?q=%s" "&type=album" % formated) s.add_spotify_headers(self.__token) (status, data, tag) = s.load_contents(self._cancel) if status: decode = json.loads(data.decode("utf-8")) # For each album, get cover and tracks for item in decode["albums"]["items"]: album_item = SearchItem() album_item.name = item["name"] album_item.is_track = False album_item.cover = item["images"][0]["url"] album_item.smallcover = item["images"][2]["url"] s = Lio.File.new_for_uri("https://api.spotify.com/v1/" "albums/%s" % item["id"]) s.add_spotify_headers(self.__token) (status, data, tag) = s.load_contents(self._cancel) if status: decode = json.loads(data.decode("utf-8")) for item in decode["tracks"]["items"]: track_item = SearchItem() track_item.is_track = True try: track_item.year = decode["release_date"][:4] except: pass # May be missing track_item.name = item["name"] track_item.album = album_item track_item.tracknumber = int(item["track_number"]) track_item.discnumber = int(item["disc_number"]) track_item.duration = int(item["duration_ms"])\ / 1000 for artist in item["artists"]: track_item.artists.append(artist["name"]) if not album_item.artists: album_item.artists = track_item.artists album_item.subitems.append(track_item) self._items.append(album_item) GLib.idle_add(self.emit, "item-found") except Exception as e: print("SpotifySearch::albums(): %s" % e)
def __get_albums(self, name): """ Get albums for name @param name as str """ try: # Read album list formated = GLib.uri_escape_string(name, None, True).replace(' ', '+') s = Gio.File.new_for_uri("https://api.spotify.com/v1/search?q=%s" "&type=album" % formated) (status, data, tag) = s.load_contents(self._cancel) if status: decode = json.loads(data.decode('utf-8')) # For each album, get cover and tracks for item in decode['albums']['items']: album_item = SearchItem() album_item.name = album_item.album_name = item['name'] album_item.is_track = False album_item.cover = item['images'][0]['url'] album_item.smallcover = item['images'][2]['url'] s = Gio.File.new_for_uri("https://api.spotify.com/v1/" "albums/%s" % item['id']) (status, data, tag) = s.load_contents(self._cancel) if status: decode = json.loads(data.decode('utf-8')) for item in decode['tracks']['items']: track_item = SearchItem() track_item.is_track = True try: track_item.year = decode['release_date'][:4] except: pass # May be missing track_item.name = item['name'] track_item.album = album_item.name track_item.tracknumber = int(item['track_number']) track_item.discnumber = int(item['disc_number']) track_item.duration = int(item['duration_ms'])\ / 1000 for artist in item['artists']: track_item.artists.append(artist['name']) if not album_item.artists: album_item.artists = track_item.artists album_item.subitems.append(track_item) self._items.append(album_item) GLib.idle_add(self.emit, 'item-found') except Exception as e: print("SpotifySearch::albums(): %s" % e)
def tracks(self, name): """ Return tracks containing name @param name as str """ try: formated = GLib.uri_escape_string(name, None, True).replace(' ', '+') s = Gio.File.new_for_uri("https://api.spotify.com/v1/search?q=%s" "&type=track" % formated) (status, data, tag) = s.load_contents(self._cancel) if status: decode = json.loads(data.decode('utf-8')) tracks = [] for item in decode['tracks']['items']: if item['name'].lower() in tracks: continue search_item = SearchItem() search_item.is_track = True search_item.name = item['name'] tracks.append(search_item.name.lower()) search_item.album = item['album']['name'] search_item.tracknumber = int(item['track_number']) search_item.discnumber = int(item['disc_number']) search_item.duration = int(item['duration_ms']) / 1000 search_item.cover = item['album']['images'][0]['url'] search_item.smallcover = item['album']['images'][2]['url'] for artist in item['artists']: search_item.artists.append(artist['name']) self._items.append(search_item) GLib.idle_add(self.emit, 'item-found') except Exception as e: print("SpotifySearch::tracks(): %s" % e)
def tracks(self, name): """ Return tracks containing name @param name as str """ try: formated = GLib.uri_escape_string(name, None, True).replace( " ", "+") s = Lio.File.new_for_uri("https://api.spotify.com/v1/search?q=%s" "&type=track" % formated) (status, data, tag) = s.load_contents(self._cancel) if status: decode = json.loads(data.decode("utf-8")) tracks = [] for item in decode["tracks"]["items"]: if item["name"].lower() in tracks: continue album_item = SearchItem() search_item = SearchItem() search_item.is_track = True search_item.name = item["name"] album_item.name = item["name"] search_item.album = album_item tracks.append(search_item.name.lower()) search_item.tracknumber = int(item["track_number"]) search_item.discnumber = int(item["disc_number"]) search_item.duration = int(item["duration_ms"]) / 1000 search_item.cover = item["album"]["images"][0]["url"] search_item.smallcover = item["album"]["images"][2]["url"] for artist in item["artists"]: search_item.artists.append(artist["name"]) self._items.append(search_item) GLib.idle_add(self.emit, "item-found") except Exception as e: print("SpotifySearch::tracks(): %s" % e)
def get_track(self, track_id): """ Return spotify track as SearchItem @param track id as str @return SearchItem """ try: s = Lio.File.new_for_uri("https://api.spotify.com/v1/" "tracks/%s" % track_id) (status, data, tag) = s.load_contents(self._cancel) if status: decode = json.loads(data.decode("utf-8")) album_item = SearchItem() album_item.name = decode["album"]["name"] album_item.cover = decode["album"]["images"][0]["url"] album_item.smallcover = decode["album"]["images"][2]["url"] track_item = SearchItem() track_item.is_track = True track_item.name = decode["name"] track_item.album = album_item track_item.tracknumber = int( decode["track_number"]) track_item.discnumber = int( decode["disc_number"]) track_item.duration = int( decode["duration_ms"]) / 1000 for artist in decode["artists"]: track_item.artists.append(artist["name"]) album_item.artists = track_item.artists album_item.subitems.append(track_item) return album_item except Exception as e: print("SpotifySearch::get_track", e) return None
def tracks(self, name): """ Return tracks containing name @param name as str """ try: formated = Lio.uri_escape_string(name, None, True).replace(' ', '+') s = Lio.File.new_for_uri("https://itunes.apple.com/search" "?entity=musicTrack&term=%s" % formated) (status, data, tag) = s.load_contents(self._cancel) if status: decode = json.loads(data.decode('utf-8')) tracks = [] for item in decode['results']: if item['trackName'].lower() in tracks: continue search_item = SearchItem() search_item.is_track = True search_item.name = item['trackName'] tracks.append(search_item.name.lower()) search_item.album = item['collectionName'] search_item.tracknumber = int(item['trackNumber']) search_item.discnumber = int(item['discNumber']) search_item.duration = int(item['trackTimeMillis']) / 1000 search_item.cover = item['artworkUrl60'].replace( '60x60', '512x512') search_item.smallcover = item['artworkUrl100'] search_item.artists.append(item['artistName']) self._items.append(search_item) GLib.idle_add(self.emit, 'item-found') except Exception as e: print("SpotifySearch::tracks(): %s" % e)
def __get_album(self, itunes_id): """ Get itunes album items @param id as int @return SearchItem/None """ if not Gio.NetworkMonitor.get_default().get_network_available(): return language = getdefaultlocale()[0][0:2] try: debug("ItunesCharts::__get_album(): %s" % itunes_id) url = self.__INFO % (itunes_id, language) f = Gio.File.new_for_uri(url) (status, data, tag) = f.load_contents(self.__cancel) if not status or self.__stop: return decode = json.loads(data.decode('utf-8')) item = decode['results'][0] album_item = SearchItem() album_item.name = item['collectionName'] album_item.album_name = album_item.name album_item.artists.append(item['artistName']) album_item.cover = item['artworkUrl60'].replace( '60x60', '512x512') for item in decode['results'][1:]: track_item = SearchItem() track_item.is_track = True track_item.name = item['trackName'] track_item.album = album_item.name track_item.year = item['releaseDate'][:4] track_item.tracknumber = int( item['trackNumber']) track_item.discnumber = int( item['discNumber']) track_item.duration = int( item['trackTimeMillis']) / 1000 if album_item.artists[0] != item['artistName']: track_item.artists.append(album_item.artists[0]) track_item.artists.append(item['artistName']) album_item.subitems.append(track_item) return album_item except Exception as e: print("ItunesCharts::__get_album()", e) return None
def get_album(self, album_id): """ Return spotify album as SearchItem @param album id as str @return SearchItem """ try: s = Lio.File.new_for_uri("https://api.spotify.com/v1/" "albums/%s" % album_id) (status, data, tag) = s.load_contents(self._cancel) if status: decode = json.loads(data.decode("utf-8")) album_item = SearchItem() album_item.name = decode["name"] album_item.cover = decode["images"][0]["url"] album_item.smallcover = decode["images"][2]["url"] album_item.ex_id = album_id for item in decode["tracks"]["items"]: track_item = SearchItem() track_item.is_track = True track_item.name = item["name"] track_item.album = album_item.name try: track_item.year = decode[ "release_date"][:4] except: pass # May be missing track_item.tracknumber = int( item["track_number"]) track_item.discnumber = int( item["disc_number"]) track_item.duration = int( item["duration_ms"]) / 1000 for artist in item["artists"]: track_item.artists.append(artist["name"]) if not album_item.artists: album_item.artists = track_item.artists album_item.subitems.append(track_item) return album_item except Exception as e: print("SpotifySearch::get_album:", e) return None
def __get_album(self, itunes_id): """ Get itunes album items @param id as int @return SearchItem/None """ if not get_network_available(): return country = getdefaultlocale()[0][3:] try: debug("ItunesCharts::__get_album(): %s" % itunes_id) url = self.__INFO % (itunes_id, country) f = Lio.File.new_for_uri(url) (status, data, tag) = f.load_contents(self.__cancel) if not status or self.__stop: return decode = json.loads(data.decode("utf-8")) item = decode["results"][0] album_item = SearchItem() album_item.name = item["collectionName"] album_item.artists.append(item["artistName"]) album_item.cover = item["artworkUrl60"].replace("60x60", "512x512") for item in decode["results"][1:]: track_item = SearchItem() track_item.is_track = True track_item.name = item["trackName"] track_item.album = album_item track_item.year = item["releaseDate"][:4] track_item.tracknumber = int(item["trackNumber"]) track_item.discnumber = int(item["discNumber"]) track_item.duration = int(item["trackTimeMillis"]) / 1000 if album_item.artists[0] != item["artistName"]: track_item.artists.append(album_item.artists[0]) track_item.artists.append(item["artistName"]) album_item.subitems.append(track_item) return album_item except Exception as e: print("ItunesCharts::__get_album()", e) return None
def do(self, search_items): """ Return tracks containing name @param search items as [str] @return tracks as [SearchItem] """ self.__stop = False # Local search added_album_ids = [] added_track_ids = [] for item in search_items: if self.__stop: return albums = [] tracks_non_album_artist = [] # Get all albums for all artists and non album_artist tracks for artist_id in Lp().artists.search(item): if self.__stop: return for album_id in Lp().albums.get_ids([artist_id], []): if (album_id, artist_id) not in albums: albums.append((album_id, artist_id)) for track_id, track_name in Lp( ).tracks.get_as_non_album_artist(artist_id): tracks_non_album_artist.append((track_id, track_name)) for album_id, artist_id in albums: if self.__stop: return if album_id in added_album_ids: continue search_item = SearchItem() search_item.id = album_id added_album_ids.append(album_id) search_item.is_track = False search_item.artist_ids = [artist_id] self._items.append(search_item) GLib.idle_add(self.emit, 'item-found') albums = Lp().albums.search(item) for album_id in albums: if self.__stop: return if album_id in added_album_ids: continue search_item = SearchItem() search_item.id = album_id added_album_ids.append(album_id) search_item.is_track = False search_item.artist_ids = Lp().albums.get_artist_ids(album_id) self._items.append(search_item) GLib.idle_add(self.emit, 'item-found') for track_id, track_name in Lp().tracks.search( item) + tracks_non_album_artist: if self.__stop: return if track_id in added_track_ids: continue search_item = SearchItem() search_item.id = track_id added_track_ids.append(track_id) search_item.is_track = True search_item.artist_ids = Lp().tracks.get_artist_ids(track_id) self._items.append(search_item) GLib.idle_add(self.emit, 'item-found') self._finished = True GLib.idle_add(self.emit, 'item-found')
def __populate(self): """ Populate searching items in db based on text entry current text """ GLib.idle_add(self.__clear) # Network Search t = Thread(target=self.__network_search) t.daemon = True t.start() # Local search results = [] added_album_ids = [] added_track_ids = [] search_items = [self.__current_search] # search_items += self.__current_search.split() for item in search_items: albums = [] tracks_non_album_artist = [] # Get all albums for all artists and non album_artist tracks for artist_id in Lp().artists.search(item): for album_id in Lp().albums.get_ids([artist_id], []): if (album_id, artist_id) not in albums: albums.append((album_id, artist_id)) for track_id, track_name in Lp( ).tracks.get_as_non_album_artist(artist_id): tracks_non_album_artist.append((track_id, track_name)) for album_id, artist_id in albums: if album_id in added_album_ids: continue search_item = SearchItem() search_item.id = album_id added_album_ids.append(album_id) search_item.is_track = False search_item.artist_ids = [artist_id] results.append(search_item) albums = Lp().albums.search(item) for album_id in albums: if album_id in added_album_ids: continue search_item = SearchItem() search_item.id = album_id added_album_ids.append(album_id) search_item.is_track = False search_item.artist_ids = Lp().albums.get_artist_ids(album_id) results.append(search_item) for track_id, track_name in Lp().tracks.search( item) + tracks_non_album_artist: if track_id in added_track_ids: continue search_item = SearchItem() search_item.id = track_id added_track_ids.append(track_id) search_item.is_track = True search_item.artist_ids = Lp().tracks.get_artist_ids(track_id) results.append(search_item) if not self.__stop_thread: GLib.idle_add(self.__add_rows_internal, results) else: self.__in_thread = False self.__stop_thread = False if not self.__need_network_search(): self.__stack.set_visible_child(self.__new_btn) self.__spinner.stop()