Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: adapt to the new spotify data #114

Merged
merged 2 commits into from
Dec 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ jobs:
github-token: ${{ secrets.GITHUB_TOKEN }}
- name: Release
if: ${{ github.ref == 'refs/heads/master' && !startsWith(github.event.head_commit.message, 'chore(release):') && !startsWith(github.event.head_commit.message, 'docs:') }}
shell: 'script -q -e -c "bash {0}" > /dev/null'
env:
CONVENTIONAL_GITHUB_RELEASER_TOKEN: ${{ secrets.GH_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
Expand Down
87 changes: 18 additions & 69 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,25 +75,11 @@ function getParsedUrl (url) {
}
}

function getImages (data) {
switch (data.type) {
case TYPE.TRACK:
case TYPE.EPISODE:
return data.coverArt.sources
default:
return data.images
}
}
const getImages = data => data.coverArt?.sources || data.images

function getDate (data) {
switch (data.type) {
case TYPE.TRACK:
case TYPE.EPISODE:
return data.releaseDate.isoString
default:
return data.release_date
}
}
const getDate = data => data.releaseDate?.isoString || data.release_date

const getLink = data => spotifyURI.formatOpenURL(data.uri)

function getArtistTrack (track) {
return track.show
Expand All @@ -111,69 +97,34 @@ function getArtistTrack (track) {
)
}

function getLink (data) {
switch (data.type) {
case TYPE.EPISODE:
return spotifyURI.formatOpenURL(data.uri)
default:
return data.external_urls.spotify
}
}

function getPreview (data) {
const track = getFirstTrack(data)
const track = toTrack(data.trackList ? data.trackList[0] : data)
const date = getDate(data)

return {
date: date ? new Date(date).toISOString() : date,
title: data.name,
type: data.type,
track: track.name,
description: data.description || track.description,
artist: getArtistTrack(track) || track.artist,
description: data.description || data.subtitle || track.description,
artist: track.artist,
image: getImages(data).reduce((a, b) => (a.width > b.width ? a : b)).url,
audio:
track.audio_preview_url || track.audioPreview?.url || track.preview_url,
audio: track.previewUrl,
link: getLink(data),
embed: `https://embed.spotify.com/?uri=${data.uri}`
}
}

function getTracks (data) {
switch (data.type) {
case TYPE.PLAYLIST:
return data.tracks.items.map(({ track }) => track)
case TYPE.ARTIST:
return data.tracks
case TYPE.ALBUM:
return data.tracks.items
default:
return [data]
}
}
const toTrack = track => ({
artist: getArtistTrack(track) || track.subtitle,
duration: track.duration,
name: track.title,
previewUrl: track.isPlayable ? track.audioPreview.url : undefined,
uri: track.uri
})

function getFirstTrack (data) {
switch (data.type) {
case TYPE.TRACK:
return data
case TYPE.PLAYLIST:
return data.tracks.items[0].track
case TYPE.ALBUM:
return data.tracks.items[0]
case TYPE.ARTIST:
return data.tracks[0]
case TYPE.EPISODE: {
const { subtitle, audioPreview } = data
const [artist, description] = data.subtitle.split(' - ')
return {
artist,
description,
name: subtitle,
preview_url: audioPreview.url
}
}
}
}
const getTracks = data =>
data.trackList ? data.trackList.map(toTrack) : [toTrack(data)]

function normalizeData ({ data }) {
if (!data || !data.type || !data.name) {
Expand All @@ -186,9 +137,7 @@ function normalizeData ({ data }) {
)
}

if (data.type === TYPE.TRACK) {
data.external_urls = { spotify: spotifyURI.formatOpenURL(data.uri) }
}
data.type = data.uri.split(':')[1]

return data
}
Expand Down
37 changes: 12 additions & 25 deletions test/get-preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,11 @@ test('get preview for spotify artist', async t => {
)

t.is(preview.date, undefined)
t.is(preview.description, undefined)
t.is(preview.description, 'Top tracks')
t.is(preview.track, 'Immaterial')
t.is(preview.title, 'SOPHIE')
t.is(preview.type, 'artist')

t.is(
preview.embed,
'https://embed.spotify.com/?uri=spotify:artist:5a2w2tgpLwv26BYJf2qYwu'
Expand All @@ -94,8 +95,7 @@ test('get preview for spotify album', async t => {
'https://open.spotify.com/album/7vQKfsKKrI0xObMqojazHR'
)

t.is(preview.date, '2019-09-06T00:00:00.000Z')
t.is(preview.description, undefined)
t.is(preview.description, 'SOPHIE')
t.is(preview.title, "OIL OF EVERY PEARL'S UN-INSIDES NON-STOP REMIX ALBUM")
t.is(preview.type, 'album')
t.is(preview.artist.includes('SOPHIE'), true)
Expand All @@ -115,7 +115,7 @@ test('get preview for spotify playlist', async t => {
)

t.is(preview.date, undefined)
t.is(preview.description, undefined)
t.is(preview.description, 'sophiemsmsmsm')
t.is(preview.title, 'SOPHIE – PRODUCT')
t.is(preview.type, 'playlist')
t.is(preview.artist.includes('SOPHIE'), true)
Expand All @@ -135,10 +135,10 @@ test('get preview for spotify episode', async t => {
)

t.is(preview.title, 'Hasty Treat - Modules in Node')
t.is(preview.description, 'Tasty Web Development Treats')
t.is(preview.description, 'Syntax - Tasty Web Development Treats')
t.is(preview.type, 'episode')
t.is(preview.artist, 'Syntax')
t.is(preview.track, 'Syntax - Tasty Web Development Treats')
t.is(preview.artist, 'Syntax - Tasty Web Development Treats')
t.is(preview.track, 'Hasty Treat - Modules in Node')
t.is(preview.date, '2020-01-06T14:00:00.000Z')
t.is(preview.image.includes('://'), true)
t.is(preview.audio.includes('/mp3-preview/'), true)
Expand All @@ -155,13 +155,9 @@ test('get preview for spotify playlist with episode inside', async t => {
)

t.is(preview.date, undefined)
t.is(
preview.description,
'https://github.com/microlinkhq/spotify-url-info'
)
t.is(preview.title, 'spotify-url-with-episode')
t.is(preview.type, 'playlist')
t.is(preview.artist, 'DroidsAndDruids')
t.is(preview.artist, 'Droids And Druids')
t.is(preview.track, '#DDMAG9 Live: Novelettes con Celia Añó')
t.is(preview.image.includes('://'), true)
t.is(preview.audio.includes('/mp3-preview/'), true)
Expand All @@ -172,13 +168,12 @@ test('get preview for spotify playlist with episode inside', async t => {
)
})

test('get preview for spotify collaboratie playlist', async t => {
test('get preview for spotify collaborative playlist', async t => {
const preview = await getPreview(
'https://open.spotify.com/playlist/29n3VgifrVF9ZxFV9B6yRA'
)

t.is(preview.date, undefined)
t.is(preview.description, 'feat Joseba')
t.is(preview.title, '🤘 ROCK')
t.is(preview.type, 'playlist')
t.is(preview.artist, 'Metalocalypse: Dethklok')
Expand All @@ -195,8 +190,6 @@ test('get preview for spotify collaboratie playlist', async t => {
test('get preview for spotify album with constructed uri', async t => {
const preview = await getPreview('spotify:album:7vQKfsKKrI0xObMqojazHR')

t.is(preview.date, '2019-09-06T00:00:00.000Z')
t.is(preview.description, undefined)
t.is(preview.title, "OIL OF EVERY PEARL'S UN-INSIDES NON-STOP REMIX ALBUM")
t.is(preview.type, 'album')
t.is(preview.artist.includes('SOPHIE'), true)
Expand All @@ -215,14 +208,12 @@ test('get preview for spotify album with play url', async t => {
'https://play.spotify.com/album/4tDBsfbHRJ9OdcMO9bmnai'
)

t.is(preview.date, '2015-11-27T00:00:00.000Z')
t.is(preview.description, undefined)
t.is(preview.title, 'PRODUCT')
t.is(preview.type, 'album')
t.is(preview.artist.includes('SOPHIE'), true)
t.is(preview.track, 'BIPP')
t.is(preview.image.includes('://'), true)
t.is(preview.audio, null)
t.is(preview.audio, undefined)
t.is(preview.link.includes('open.spotify.com/album/'), true)
t.is(
preview.embed,
Expand All @@ -235,14 +226,12 @@ test('get preview for spotify Album with twitter embed url', async t => {
'https://open.spotify.com/embed/album/4tDBsfbHRJ9OdcMO9bmnai?utm_campaign=twitter-player&utm_source=open&utm_medium=twitter'
)

t.is(preview.date, '2015-11-27T00:00:00.000Z')
t.is(preview.description, undefined)
t.is(preview.title, 'PRODUCT')
t.is(preview.type, 'album')
t.is(preview.artist.includes('SOPHIE'), true)
t.is(preview.track, 'BIPP')
t.is(preview.image.includes('://'), true)
t.is(preview.audio, null)
t.is(preview.audio, undefined)
t.is(preview.link.includes('open.spotify.com/album/'), true)
t.is(
preview.embed,
Expand All @@ -255,14 +244,12 @@ test('get preview for spotify Track with constructed embed url', async t => {
'https://embed.spotify.com/?uri=spotify:album:4tDBsfbHRJ9OdcMO9bmnai'
)

t.is(preview.date, '2015-11-27T00:00:00.000Z')
t.is(preview.description, undefined)
t.is(preview.title, 'PRODUCT')
t.is(preview.type, 'album')
t.is(preview.artist.includes('SOPHIE'), true)
t.is(preview.track, 'BIPP')
t.is(preview.image.includes('://'), true)
t.is(preview.audio, null)
t.is(preview.audio, undefined)
t.is(preview.link.includes('open.spotify.com/album/'), true)
t.is(
preview.embed,
Expand Down
13 changes: 7 additions & 6 deletions test/get-tracks.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ test('get tracks for spotify track', async t => {
const tracks = await getTracks(url)
t.true(Array.isArray(tracks))
t.is(tracks[0].name, 'Immaterial')
t.true(tracks[0].external_urls.spotify?.includes('/track/'))
t.is(tracks[0].previewUrl.includes('/mp3-preview/'), true)
})

test('get tracks for spotify artist', async t => {
Expand All @@ -52,25 +52,25 @@ test('get tracks for spotify artist', async t => {
)
t.true(Array.isArray(tracks))
t.is(tracks[0].name, 'Immaterial')
t.true(tracks[0].external_urls.spotify?.includes('/track/'))
t.is(tracks[0].previewUrl.includes('/mp3-preview/'), true)
})

test('get tracks for spotify album', async t => {
const tracks = await getTracks(
'https://open.spotify.com/album/4tDBsfbHRJ9OdcMO9bmnai'
)
t.true(Array.isArray(tracks))
t.is(tracks[0].name, 'BIPP')
t.true(tracks[0].external_urls.spotify?.includes('/track/'))
t.is(tracks[1].name, 'ELLE')
t.is(tracks[1].previewUrl.includes('/mp3-preview/'), true)
})

test('get tracks for spotify playlist', async t => {
const tracks = await getTracks(
'https://open.spotify.com/user/sophiemsmsmsm/playlist/3Q4cPwMHY95ZHXtmcU2xvH'
)
t.true(Array.isArray(tracks))
t.is(tracks[0].name, 'BIPP')
t.true(tracks[0].external_urls.spotify?.includes('/track/'))
t.is(tracks[1].name, 'ELLE')
t.is(tracks[1].previewUrl.includes('/mp3-preview/'), true)
})

test('get tracks for spotify episode', async t => {
Expand All @@ -79,4 +79,5 @@ test('get tracks for spotify episode', async t => {
)
t.true(Array.isArray(tracks))
t.is(tracks[0].name, 'Hasty Treat - Modules in Node')
t.is(tracks[0].previewUrl.includes('/mp3-preview/'), true)
})