Add albums to spotify playlist
This commit is contained in:
@ -4,6 +4,7 @@ import type { OauthResponse } from '$lib/mastodon/response';
|
|||||||
import type { SongInfo } from '$lib/odesliResponse';
|
import type { SongInfo } from '$lib/odesliResponse';
|
||||||
import { OauthPlaylistAdder } from './oauthPlaylistAdder';
|
import { OauthPlaylistAdder } from './oauthPlaylistAdder';
|
||||||
import type { PlaylistAdder } from './playlistAdder';
|
import type { PlaylistAdder } from './playlistAdder';
|
||||||
|
import type { SpotifyAlbumTracksResponse } from './spotifyResponse';
|
||||||
|
|
||||||
export class SpotifyPlaylistAdder extends OauthPlaylistAdder implements PlaylistAdder {
|
export class SpotifyPlaylistAdder extends OauthPlaylistAdder implements PlaylistAdder {
|
||||||
public constructor() {
|
public constructor() {
|
||||||
@ -94,12 +95,39 @@ export class SpotifyPlaylistAdder extends OauthPlaylistAdder implements Playlist
|
|||||||
return;
|
return;
|
||||||
}*/
|
}*/
|
||||||
|
|
||||||
//const searchParams = new URLSearchParams([['part', 'snippet']]);
|
let uris: string[] = [];
|
||||||
|
if (song.type === 'album') {
|
||||||
|
const albumId = song.spotifyUri.split(':').pop();
|
||||||
|
// This only fetches max. 50 tracks, otherwise we would have to implement paging
|
||||||
|
// Maybe in the future, for now it should be enough to get the first 50 tracks
|
||||||
|
const albumTracksUrl = new URL(`${this.apiBase}/albums/${albumId}/tracks?limit=50`);
|
||||||
|
const options: RequestInit = {
|
||||||
|
method: 'GET',
|
||||||
|
headers: { Authorization: `${token.token_type} ${token.access_token}` }
|
||||||
|
};
|
||||||
|
let trackResponse = await fetch(albumTracksUrl, options);
|
||||||
|
let tracks: SpotifyAlbumTracksResponse = await trackResponse.json();
|
||||||
|
if (tracks.error && tracks.error.status === 401) {
|
||||||
|
this.logger.info('Token expired, refreshing and retrying');
|
||||||
|
this.refreshToken(true);
|
||||||
|
trackResponse = await fetch(albumTracksUrl, options);
|
||||||
|
tracks = await trackResponse.json();
|
||||||
|
} else if (tracks.error) {
|
||||||
|
this.logger.error('Fetching album trackas failed', tracks.error);
|
||||||
|
}
|
||||||
|
if (tracks.items) {
|
||||||
|
uris = tracks.items.map((x) => x.uri);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
uris = [song.spotifyUri];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Maximum is 100 items, but we'Re requesting only max. 50 anyways
|
||||||
const options: RequestInit = {
|
const options: RequestInit = {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: { Authorization: `${token.token_type} ${token.access_token}` },
|
headers: { Authorization: `${token.token_type} ${token.access_token}` },
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
uris: [song.spotifyUri]
|
uris: uris
|
||||||
})
|
})
|
||||||
};
|
};
|
||||||
const apiUrl = new URL(`${this.apiBase}/playlists/${SPOTIFY_PLAYLIST_ID}/tracks`);
|
const apiUrl = new URL(`${this.apiBase}/playlists/${SPOTIFY_PLAYLIST_ID}/tracks`);
|
||||||
|
33
src/lib/server/playlist/spotifyResponse.ts
Normal file
33
src/lib/server/playlist/spotifyResponse.ts
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
export type SpotifyAlbumTracksResponse = {
|
||||||
|
href?: string;
|
||||||
|
limit?: number;
|
||||||
|
offset?: number;
|
||||||
|
total?: number;
|
||||||
|
next?: string | null;
|
||||||
|
previous?: string | null;
|
||||||
|
items?: SpotifyAlbumTrack[];
|
||||||
|
error?: SpotifyError;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type SpotifyAlbumTrack = {
|
||||||
|
artists: {
|
||||||
|
id: string;
|
||||||
|
uri: string;
|
||||||
|
name: string;
|
||||||
|
type: 'artist';
|
||||||
|
}[];
|
||||||
|
available_markets: string[];
|
||||||
|
external_urls: {
|
||||||
|
spotify: string;
|
||||||
|
};
|
||||||
|
href: string;
|
||||||
|
name: string;
|
||||||
|
type: 'track';
|
||||||
|
id: string;
|
||||||
|
uri: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type SpotifyError = {
|
||||||
|
status: number;
|
||||||
|
message: string;
|
||||||
|
};
|
Reference in New Issue
Block a user