spotify-url-info
Advanced tools
Comparing version 2.0.0 to 2.1.0
58
index.js
@@ -5,9 +5,3 @@ const spotifyURI = require("spotify-uri"); | ||
const SUPPORTED_TYPES = [ | ||
"album", | ||
"artist", | ||
"episode", | ||
"playlist", | ||
"track" | ||
]; | ||
const SUPPORTED_TYPES = ["album", "artist", "episode", "playlist", "track"]; | ||
@@ -28,18 +22,36 @@ function getData(url) { | ||
const embedURL = spotifyURI.formatEmbedURL(parsedURL); | ||
return fetch(embedURL) | ||
.then(res => res.text()) | ||
.then(parse) | ||
.then(embed => | ||
JSON.parse( | ||
embed | ||
.filter(e => e.tagName === "html")[0] | ||
.children.filter(e => e.tagName === "body")[0] | ||
.children.filter( | ||
e => | ||
e.tagName === "script" && | ||
e.attributes.findIndex(a => a.value === "resource") !== -1 | ||
)[0].children[0].content | ||
) | ||
) | ||
.then(embed => { | ||
const scripts = embed | ||
.filter(e => e.tagName === "html")[0] | ||
.children.filter(e => e.tagName === "body")[0] | ||
.children.filter(e => e.tagName === "script"); | ||
const resourceScript = scripts.filter( | ||
e => e.attributes.findIndex(a => a.value === "resource") !== -1 | ||
); | ||
const hydrateScript = scripts.filter( | ||
e => e.children[0] && e.children[0].content.includes(`"data":`) | ||
); | ||
if (resourceScript.length > 0) { | ||
// found data in the older embed style | ||
return JSON.parse( | ||
decodeURIComponent(resourceScript[0].children[0].content) | ||
); | ||
} else if (hydrateScript.length > 0) { | ||
// found hydration data | ||
// parsing via looking for { to be a little bit resistant to code changes | ||
const scriptContent = hydrateScript[0].children[0].content; | ||
const dataString = | ||
"{" + scriptContent.split("{").slice(1).join("{").trim(); | ||
return JSON.parse(dataString).data; | ||
} else { | ||
return Promise.reject( | ||
new Error( | ||
"Couldn't find any data in embed page that we know how to parse" | ||
) | ||
); | ||
} | ||
}) | ||
.then(sanityCheck); | ||
@@ -51,3 +63,3 @@ } | ||
const images = data.type === "track" ? data.album.images : data.images; | ||
const date = data.album ? data.album.release_date : data.release_date | ||
const date = data.album ? data.album.release_date : data.release_date; | ||
@@ -80,3 +92,3 @@ return Promise.resolve({ | ||
return { | ||
artists: data.show.publisher.split(' and ').map(name => ({ name })), | ||
artists: data.show.publisher.split(" and ").map(name => ({ name })), | ||
name: data.show.name, | ||
@@ -100,3 +112,3 @@ preview_url: data.audio_preview_url | ||
new Error( | ||
`Not an ${SUPPORTED_TYPES.join(', ')}. Only these types can be parsed` | ||
`Not an ${SUPPORTED_TYPES.join(", ")}. Only these types can be parsed` | ||
) | ||
@@ -103,0 +115,0 @@ ); |
@@ -5,3 +5,3 @@ { | ||
"homepage": "https://github.com/karlsander/spotify-url-info#readme", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"main": "index.js", | ||
@@ -8,0 +8,0 @@ "author": { |
105
readme.md
@@ -8,3 +8,3 @@ # spotify-url-info | ||
__Warning:__ This library will not work directly in the browser, because the spotify page that it scrapes does not allow cross-origion requests. You can use it in node.js, in serverless functions and in react native. Alternatively use a CORS proxy. | ||
**Warning:** This library will not work directly in the browser, because the spotify page that it scrapes does not allow cross-origion requests. You can use it in node.js, in serverless functions and in react native. Alternatively use a CORS proxy. | ||
@@ -24,92 +24,2 @@ ## Usage | ||
```javascript | ||
await getData("https://open.spotify.com/track/5nTtCOCds6I0PHMNtqelas"); | ||
``` | ||
<details> | ||
<summary>returns a long json response, look at [the web API object model](https://developer.spotify.com/documentation/web-api/reference/object-model/)) </summary> | ||
```json | ||
{ | ||
"album": { | ||
"album_type": "album", | ||
"artists": [ | ||
{ | ||
"external_urls": { | ||
"spotify": "https://open.spotify.com/artist/5a2w2tgpLwv26BYJf2qYwu" | ||
}, | ||
"href": "https://api.spotify.com/v1/artists/5a2w2tgpLwv26BYJf2qYwu", | ||
"id": "5a2w2tgpLwv26BYJf2qYwu", | ||
"name": "SOPHIE", | ||
"type": "artist", | ||
"uri": "spotify:artist:5a2w2tgpLwv26BYJf2qYwu" | ||
} | ||
], | ||
"external_urls": { | ||
"spotify": "https://open.spotify.com/album/6ukR0pBrFXIXdQgLWAhK7J" | ||
}, | ||
"href": "https://api.spotify.com/v1/albums/6ukR0pBrFXIXdQgLWAhK7J", | ||
"id": "6ukR0pBrFXIXdQgLWAhK7J", | ||
"images": [ | ||
{ | ||
"height": 640, | ||
"url": | ||
"https://i.scdn.co/image/d6f496a6708d22a2f867e5acb84afb0eb0b07bc1", | ||
"width": 640 | ||
}, | ||
{ | ||
"height": 300, | ||
"url": | ||
"https://i.scdn.co/image/838e785a58d2f93791b623a4b1ff4ca8f61bf99c", | ||
"width": 300 | ||
}, | ||
{ | ||
"height": 64, | ||
"url": | ||
"https://i.scdn.co/image/d942cc566126ce31de1c16b12ce49a47c097da43", | ||
"width": 64 | ||
} | ||
], | ||
"name": "OIL OF EVERY PEARL'S UN-INSIDES", | ||
"release_date": "2018-06-15", | ||
"release_date_precision": "day", | ||
"type": "album", | ||
"uri": "spotify:album:6ukR0pBrFXIXdQgLWAhK7J" | ||
}, | ||
"artists": [ | ||
{ | ||
"external_urls": { | ||
"spotify": "https://open.spotify.com/artist/5a2w2tgpLwv26BYJf2qYwu" | ||
}, | ||
"href": "https://api.spotify.com/v1/artists/5a2w2tgpLwv26BYJf2qYwu", | ||
"id": "5a2w2tgpLwv26BYJf2qYwu", | ||
"name": "SOPHIE", | ||
"type": "artist", | ||
"uri": "spotify:artist:5a2w2tgpLwv26BYJf2qYwu" | ||
} | ||
], | ||
"disc_number": 1, | ||
"duration_ms": 232806, | ||
"explicit": false, | ||
"external_ids": { | ||
"isrc": "AUFF01800039" | ||
}, | ||
"external_urls": { | ||
"spotify": "https://open.spotify.com/track/5nTtCOCds6I0PHMNtqelas" | ||
}, | ||
"href": "https://api.spotify.com/v1/tracks/5nTtCOCds6I0PHMNtqelas", | ||
"id": "5nTtCOCds6I0PHMNtqelas", | ||
"is_local": false, | ||
"is_playable": true, | ||
"name": "Immaterial", | ||
"popularity": 50, | ||
"preview_url": | ||
"https://p.scdn.co/mp3-preview/6be8eb12ff18ae09b7a6d38ff1e5327fd128a74e?cid=162b7dc01f3a4a2ca32ed3cec83d1e02", | ||
"track_number": 8, | ||
"type": "track", | ||
"uri": "spotify:track:5nTtCOCds6I0PHMNtqelas", | ||
"dominantColor": "#87707f" | ||
} | ||
``` | ||
</details> | ||
```javascript | ||
await getPreview("https://open.spotify.com/track/5nTtCOCds6I0PHMNtqelas"); | ||
@@ -137,2 +47,8 @@ ``` | ||
```javascript | ||
await getData("https://open.spotify.com/track/5nTtCOCds6I0PHMNtqelas"); | ||
``` | ||
returns any raw data we can scrape from spotify. There are no guarantees about the shape of this data, because it varies with different media and scraping methods. Handle it carefully. | ||
## Caveats | ||
@@ -144,2 +60,9 @@ | ||
### 2.1.0 | ||
Warning: The data returned from `getData` can change at any time. For example, the newer podcast embed does not provide `dominantColor` anymore. I do not consider that a breaking change for this library. The only guarantee is that you get the data spotify makes available. You need to add safety checks in your application code. Only the data shape returned by `getPreview` is guaranteed. | ||
- fixes an issue with encoded data in the parsed html page (issue #55) | ||
- add support for scraping a different type of embed page, currently used in podcast episodes (fixes issue #54) | ||
### 2.0.0 | ||
@@ -146,0 +69,0 @@ |
105
8506
97