diff --git a/__tests__/converter/spotify.ts b/__tests__/converter/spotify.ts new file mode 100644 index 0000000..1e417dd --- /dev/null +++ b/__tests__/converter/spotify.ts @@ -0,0 +1,28 @@ +import test from 'ava'; +import {spotify} from '../../src'; + +const PLAYLIST_TITLE = 'This Is Eminem'; +const PLAYLIST_ID = '37i9dQZF1DX1clOuib1KtQ'; +const ARTIST_ID = '7dGJo4pcD2V6oG8kP0tJRR'; + +test.serial('SET ANONYMOUS TOKEN', async (t) => { + const response = await spotify.setSpotifyAnonymousToken(); + + t.truthy(response.accessToken, 'string'); + t.true(response.isAnonymous); +}); + +test('GET ARTIST TO DEEZER TRACKS', async (t) => { + const tracks = await spotify.artist2Deezer(ARTIST_ID); + + t.is(tracks.length, 10); +}); + +if (process.env.CI) { + test('GET PLAYLIST TO DEEZER TRACKS', async (t) => { + const [playlist, tracks] = await spotify.playlist2Deezer(PLAYLIST_ID); + + t.is(playlist.TITLE, PLAYLIST_TITLE); + t.true(tracks.length > 50); + }); +} diff --git a/src/converter/index.ts b/src/converter/index.ts index 8756c68..4e65706 100644 --- a/src/converter/index.ts +++ b/src/converter/index.ts @@ -1,2 +1,3 @@ export * from './deezer'; export * as tidal from './tidal'; +export * as spotify from './spotify'; diff --git a/src/converter/spotify.ts b/src/converter/spotify.ts index 7a74f67..c6acfd4 100644 --- a/src/converter/spotify.ts +++ b/src/converter/spotify.ts @@ -24,9 +24,17 @@ const getOffset = (next: null | string): number => { const queue = new PQueue({concurrency: 25}); -export const spotifyApi = new SpotifyWebApi(); +const spotifyApi = new SpotifyWebApi(); -export const spotifyPlaylist2Deezer = async ( +export const setSpotifyAnonymousToken = async () => { + const {data}: {data: tokensType} = await axios.get( + 'https://open.spotify.com/get_access_token?reason=transport&productType=embed', + ); + spotifyApi.setAccessToken(data.accessToken); + return data; +}; + +export const playlist2Deezer = async ( id: string, onError?: (item: SpotifyApi.PlaylistTrackObject, index: number, err: Error) => void, ): Promise<[playlistInfo, trackType[]]> => { @@ -86,7 +94,7 @@ export const spotifyPlaylist2Deezer = async ( return [playlistInfoData, tracks]; }; -export const spotifyArtist2Deezer = async ( +export const artist2Deezer = async ( id: string, onError?: (item: SpotifyApi.TrackObjectFull, index: number, err: Error) => void, ): Promise => { @@ -112,10 +120,3 @@ export const spotifyArtist2Deezer = async ( return tracks; }; - -export const setSpotifyAnonymousToken = async () => { - const {data}: {data: tokensType} = await axios.get( - 'https://open.spotify.com/get_access_token?reason=transport&productType=embed', - ); - spotifyApi.setAccessToken(data.accessToken); -};