Skip to content

Commit

Permalink
Get album fix (#441)
Browse files Browse the repository at this point in the history
get album doesn't need a parameter because we can search for albums
using the search action
  • Loading branch information
steveluc authored Nov 28, 2024
1 parent 21f8d24 commit 7407534
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 28 deletions.
6 changes: 1 addition & 5 deletions ts/packages/agents/player/src/agent/playerSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,14 +177,10 @@ export interface GetPlaylistAction {
};
}

// get album by name; if name is "", use the currently playing track
// get currently playing album
// set the current track list the tracks in the album
export interface GetAlbumAction {
actionName: "getAlbum";
parameters: {
// name of album to get
name: string;
};
}

// Set the current track list to the user's favorite tracks
Expand Down
30 changes: 9 additions & 21 deletions ts/packages/agents/player/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -858,23 +858,14 @@ export async function handleCall(
return createNotFoundActionResult(`playlist ${playlistName}`);
}
case "getAlbum": {
const getAlbumAction = action as unknown as GetPlaylistAction;
const name = getAlbumAction.parameters.name;
let album: SpotifyApi.AlbumObjectSimplified | undefined = undefined;
let status: SpotifyApi.CurrentPlaybackResponse | undefined =
undefined;
if (name.length > 0) {
actionIO.setDisplay(
chalk.magentaBright(`searching for album: ${name}`),
);
album = await searchAlbum(name, clientContext);
} else {
// get album of current playing track and load it as track collection
status = await getPlaybackState(clientContext.service);
if (status && status.item && status.item.type === "track") {
const track = status.item as SpotifyApi.TrackObjectFull;
album = track.album;
}
// get album of current playing track and load it as track collection
status = await getPlaybackState(clientContext.service);
if (status && status.item && status.item.type === "track") {
const track = status.item as SpotifyApi.TrackObjectFull;
album = track.album;
}
if (album !== undefined) {
const fullAlbumRsponse = await getAlbum(
Expand All @@ -900,16 +891,13 @@ export async function handleCall(
const collection = new AlbumTrackCollection(
fullAlbumRsponse,
);

actionIO.setDisplay(
chalk.magentaBright(
`${getAlbumAction.parameters.name}:`,
),
);
actionIO.setDisplay(chalk.magentaBright(`${album.name}:`));
await updateTrackListAndPrint(collection, clientContext);
return htmlTrackNames(collection);
}
return createNotFoundActionResult(`tracks from album ${name}`);
return createNotFoundActionResult(
`tracks from album ${album.name}`,
);
}
return createNotFoundActionResult("album");
}
Expand Down
4 changes: 2 additions & 2 deletions ts/packages/agents/player/src/endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -541,9 +541,9 @@ export async function getAlbum(
},
};

const artistsUrl = `https://api.spotify.com/v1/album/${encodeURIComponent(id)}`;
const albumUrl = `https://api.spotify.com/v1/albums/${encodeURIComponent(id)}`;
try {
const spotifyResult = await axios.get(artistsUrl, config);
const spotifyResult = await axios.get(albumUrl, config);
return spotifyResult.data as SpotifyApi.SingleAlbumResponse;
} catch (e) {
translateAxiosError(e);
Expand Down

0 comments on commit 7407534

Please sign in to comment.