Skip to content

Instantly share code, notes, and snippets.

@seresk
Last active April 6, 2021 22:07
Show Gist options
  • Save seresk/ef1d32ebfe3a096cb5020753fd952677 to your computer and use it in GitHub Desktop.
Save seresk/ef1d32ebfe3a096cb5020753fd952677 to your computer and use it in GitHub Desktop.
SpotifyEngine
export class SpotifyEngine extends EngineBase
implements IStreamEngine{
///...
private async getAlbums(): Promise<Array<AlbumModel>> {
const limitQueryString = '?limit=50';
const result = new Array<AlbumModel>();
let url = SpotifyURLResources.UsersSavedAlbums + limitQueryString;
do {
const response = await this.dataService.get<SpotifyApi.UsersSavedAlbumsResponse>(
url,
this.authorizationService.getAuthorizationHeaders()
);
response.items.forEach((a) => {
const tracks = new Array<TrackModel>();
a.album.tracks.items.forEach((t, index) => {
tracks.push(
new TrackModel({
id: t.id,
url: t.uri,
name: t.name,
artistName: t.artists[0].name,
albumName: a.album.name,
trackNumber: index + 1,
totalNumberOfTrackInPlaylist: a.album.tracks.total,
genre: a.album.genres[0],
artworkUrl: a.album.images[0].url,
durationMs: t.duration_ms,
isPlayable: t.is_playable,
})
);
});
const album = new AlbumModel({
id: a.album.id,
artist: a.album.artists[0].name,
genre: a.album.genres[0],
name: a.album.name,
imageUrl: a.album.images[0].url,
totalTracks: a.album.tracks.total,
tracks,
});
result.push(album);
});
url = response.next;
} while (url != null);
try {
result.sort(
this.getOrderFunction<AlbumModel>('name', OrderDirectionEnum.ascending)
);
} catch (e) {
console.log('SortError', e);
}
return result;
}
///...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment