def find_albums_by_track(track_name): print("Finding tracks named:", track_name) fe = Attr("type").eq("track") response = pinehead_table.query( IndexName="name_title-index", KeyConditionExpression=Key("name_title").eq(track_name), FilterExpression=fe, ) data = response["Items"] while "LastEvaluatedKey" in response: response = pinehead_table.query( IndexName="name_title-index", KeyConditionExpression=Key("name_title").eq(track_name), FilterExpression=fe, ExclusiveStartKey=response["LastEvaluatedKey"], ) data.extend(response["Items"]) albums = [] for item in data: album = get_album_by_id(item["album_id"]) album.artist = get_artist_by_id(album.artist_id) albums.append(album) return albums
def find_album_by_artist_and_title(artist_name, title): print(f"Finding albums with artist={artist_name} and title={title}") # Uses LSI artist_name-title-index response = pinehead_table.query( IndexName="artist_name-title-index", KeyConditionExpression=Key("artist_name").eq(artist_name), FilterExpression=Attr("title").eq(title), ) data = response["Items"] albums = [] for item in data: album = album_from_item(item) print(json.dumps(item)) if "cover_art" in item: album.cover_art = item["cover_art"].value albums.append(album) return albums
def get_album_by_id(album_id): response = pinehead_table.query( KeyConditionExpression=Key("type").eq("album") & Key("id").eq(album_id), ) if len(response["Items"]) == 0: return None return album_from_item(response["Items"][0])
def get_tracks_by_album_id(album_id): print("Getting tracks where album_id =", album_id) response = pinehead_table.query( IndexName="type-album_id-index", KeyConditionExpression=Key("type").eq("track") & Key("album_id").eq(album_id), ) tracks = [] for item in response["Items"]: track = track_from_item(item) tracks.append(track) return tracks
def find_albums_by_artist_name(artist_name): print("Finding albums by artist name:", artist_name) # Step 1 - Get artist_id by artist name # Uses LSI artist_name-title-index response = pinehead_table.query( IndexName="name_title-index", KeyConditionExpression=Key("name_title").eq(artist_name), FilterExpression=Attr("type").eq("artist"), ) artist_id = response["Items"][0]["id"] # Step 2 - Get albums by artist_id albums = find_albums_by_artist_id(artist_id) return albums
def find_albums_by_artist_id(artist_id): print("Finding albums where artist_id =", artist_id) response = pinehead_table.query( IndexName="artist_id-type-index", KeyConditionExpression=Key("artist_id").eq(artist_id) & Key("type").eq("album"), ) albums = [] for item in response["Items"]: album = album_from_item(item) albums.append(album) # print(albums) return albums