Skip to content

Commit 53a5401

Browse files
committed
fix: changes in Spotify embeds
1 parent b5973d1 commit 53a5401

File tree

1 file changed

+31
-7
lines changed

1 file changed

+31
-7
lines changed

src/index.js

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,36 @@ const createGetData = fetch => async (url, opts) => {
2626
.children.filter(e => e.tagName === 'body')[0]
2727
.children.filter(e => e.tagName === 'script')
2828

29-
const resourceScript = scripts.filter(
29+
const resourceScriptOld = scripts.filter(
3030
e => e.attributes.findIndex(a => a.value === 'resource') !== -1
3131
)
3232

33-
if (resourceScript.length > 0) {
33+
if (resourceScriptOld.length > 0) {
3434
// found data in the older embed style
35-
return JSON.parse(decodeURIComponent(resourceScript[0].children[0].content))
35+
return JSON.parse(
36+
decodeURIComponent(resourceScriptOld[0].children[0].content)
37+
)
38+
}
39+
40+
const resourceScriptNew = scripts.filter(
41+
e => e.attributes.findIndex(a => a.value === 'initial-state') !== -1
42+
)
43+
44+
if (resourceScriptNew.length > 0) {
45+
// found data in the new embed style
46+
const data = JSON.parse(
47+
Buffer.from(
48+
decodeURIComponent(resourceScriptNew[0].children[0].content),
49+
'base64'
50+
)
51+
).data.entity
52+
// they removed/renamed some things, which for backwards compatibility we need to add back
53+
data.external_urls = {
54+
spotify: 'https://open.spotify.com/track/' + data.uri
55+
}
56+
data.release_date = data.releaseDate.isoString
57+
data.audio = data.audioPreview.url
58+
return data
3659
}
3760

3861
const hydrateScript = scripts.filter(
@@ -77,7 +100,7 @@ function getParsedUrl (url) {
77100
function getImages (data) {
78101
switch (data.type) {
79102
case TYPE.TRACK:
80-
return data.album.images
103+
return data.album?.images || data.coverArt.sources
81104
case TYPE.EPISODE:
82105
return data.coverArt.sources
83106
default:
@@ -88,11 +111,11 @@ function getImages (data) {
88111
function getDate (data) {
89112
switch (data.type) {
90113
case TYPE.TRACK:
91-
return data.album.release_date
114+
return data.album?.release_date || data.releaseDate.isoString
92115
case TYPE.EPISODE:
93116
return data.releaseDate.isoString
94117
default:
95-
return data.release_date
118+
return data.release_date || data.releaseDate?.isoString
96119
}
97120
}
98121

@@ -127,7 +150,8 @@ function getPreview (data) {
127150
description: data.description || track.description,
128151
artist: getArtistTrack(track) || track.artist,
129152
image: getImages(data).reduce((a, b) => (a.width > b.width ? a : b)).url,
130-
audio: track.audio_preview_url || track.preview_url,
153+
audio:
154+
track.audio_preview_url || track.audioPreview?.url || track.preview_url,
131155
link: getLink(data),
132156
embed: `https://embed.spotify.com/?uri=${data.uri}`
133157
}

0 commit comments

Comments
 (0)