spotify-url-info
Advanced tools
Comparing version 1.3.1 to 1.4.0
77
index.js
@@ -5,4 +5,13 @@ const spotifyURI = require("spotify-uri"); | ||
const SUPPORTED_TYPES = [ | ||
"album", | ||
"artist", | ||
"episode", | ||
"playlist", | ||
"track" | ||
]; | ||
function getData(url) { | ||
let parsedURL = {}; | ||
try { | ||
@@ -13,6 +22,9 @@ parsedURL = spotifyURI.parse(url); | ||
} | ||
if (!parsedURL.type) { | ||
return Promise.reject(new Error(`Failed to parse ${url} as Spotify URL`)); | ||
} | ||
const embedURL = spotifyURI.formatEmbedURL(parsedURL); | ||
return fetch(embedURL) | ||
@@ -36,42 +48,21 @@ .then(res => res.text()) | ||
function getPreview(url) { | ||
return getData(url).then(parseIntoPreview); | ||
} | ||
function parseIntoPreview(data) { | ||
const firstTrack = getFirstTrack(data); | ||
const track = getFirstTrack(data); | ||
const images = data.type === "track" ? data.album.images : data.images; | ||
const date = data.album ? data.album.release_date : data.release_date | ||
return Promise.resolve({ | ||
date, | ||
title: data.name, | ||
type: data.type, | ||
track: firstTrack.name, | ||
artist: firstTrack.artists.map(a => a.name).join(" & "), | ||
track: track.name, | ||
description: data.description || undefined, | ||
artist: track.artists.map(a => a.name).join(" & "), | ||
image: images.reduce((a, b) => (a.width > b.width ? a : b)).url, | ||
audio: firstTrack.preview_url, | ||
audio: track.preview_url, | ||
link: data.external_urls.spotify, | ||
embed: "https://embed.spotify.com/?uri=" + data.uri | ||
embed: `https://embed.spotify.com/?uri=${data.uri}` | ||
}); | ||
} | ||
function sanityCheck(data) { | ||
if (!data || !data.type || !data.name) { | ||
return Promise.reject( | ||
new Error("Data doesn't seem to be of the right shape to parse") | ||
); | ||
} | ||
if ( | ||
data.type != "track" && | ||
data.type != "album" && | ||
data.type != "playlist" && | ||
data.type != "artist" | ||
) { | ||
return Promise.reject( | ||
new Error( | ||
"Not an album, artist, track or playlist. Only these types can be parsed" | ||
) | ||
); | ||
} | ||
return Promise.resolve(data); | ||
} | ||
function getFirstTrack(data) { | ||
@@ -87,2 +78,8 @@ switch (data.type) { | ||
return data.tracks[0]; | ||
case "episode": | ||
return { | ||
artists: data.show.publisher.split(' and ').map(name => ({ name })), | ||
name: data.show.name, | ||
preview_url: data.audio_preview_url | ||
}; | ||
default: | ||
@@ -93,3 +90,21 @@ return data; | ||
function sanityCheck(data) { | ||
if (!data || !data.type || !data.name) { | ||
return Promise.reject( | ||
new Error("Data doesn't seem to be of the right shape to parse") | ||
); | ||
} | ||
if (!SUPPORTED_TYPES.includes(data.type)) { | ||
return Promise.reject( | ||
new Error( | ||
`Not an ${SUPPORTED_TYPES.join(', ')}. Only these types can be parsed` | ||
) | ||
); | ||
} | ||
return Promise.resolve(data); | ||
} | ||
module.exports.getData = getData; | ||
module.exports.getPreview = getPreview; | ||
module.exports.getPreview = url => getData(url).then(parseIntoPreview); |
{ | ||
"name": "spotify-url-info", | ||
"version": "1.3.1", | ||
"description": "Get metadata for a spotify url without spotify API access", | ||
"author": "Karl Sander <kall@kall.ws>", | ||
"homepage": "https://github.com/karlsander/spotify-url-info#readme", | ||
"version": "1.4.0", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "jest" | ||
"author": { | ||
"email": "kall@kall.ws", | ||
"name": "Karl Sander" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/karlsander/spotify-url-info.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/karlsander/spotify-url-info/issues" | ||
}, | ||
"keywords": [ | ||
"embed", | ||
"metadata", | ||
"music", | ||
"spotify" | ||
], | ||
"dependencies": { | ||
"cross-fetch": "^3.0.4", | ||
"cross-fetch": "^3.0.5", | ||
"himalaya": "^1.1.0", | ||
"spotify-uri": "^2.0.0" | ||
"spotify-uri": "^2.1.0" | ||
}, | ||
"devDependencies": { | ||
"jest": "^25.2.7" | ||
"jest": "^26.1.0" | ||
}, | ||
@@ -21,16 +35,9 @@ "engines": { | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/karlsander/spotify-url-info/issues" | ||
"files": [ | ||
"index.js" | ||
], | ||
"scripts": { | ||
"test": "jest" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/karlsander/spotify-url-info" | ||
}, | ||
"keywords": [ | ||
"spotify", | ||
"metadata", | ||
"music", | ||
"embed" | ||
], | ||
"license": "ISC" | ||
} |
@@ -125,6 +125,10 @@ # spotify-url-info | ||
"link": "https://open.spotify.com/track/5nTtCOCds6I0PHMNtqelas", | ||
"embed": "https://embed.spotify.com/?uri=spotify:track:5nTtCOCds6I0PHMNtqelas" | ||
"embed": "https://embed.spotify.com/?uri=spotify:track:5nTtCOCds6I0PHMNtqelas", | ||
"date": "2018-06-15", | ||
"description": "description of a podcast episode" | ||
} | ||
``` | ||
The fields `description` and `date` will be undefined for some types of media that don't have this information. | ||
## Caveats | ||
@@ -136,2 +140,7 @@ | ||
### 1.4.0 | ||
- Support for podcast episodes on spotify (contributed by @kikobeats) | ||
- new `description` and `date` fields in the preview object (contributed by @kikobeats) | ||
### 1.3.1 | ||
@@ -138,0 +147,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
0
168
9090
3
92
2
Updatedcross-fetch@^3.0.5
Updatedspotify-uri@^2.1.0