mirror of
https://github.com/eRgo35/spotify-parser.git
synced 2025-12-16 12:56:10 +01:00
playlist and album support
This commit is contained in:
12
README.md
12
README.md
@@ -1 +1,11 @@
|
|||||||
# spotify
|
# Spotify URL Parser
|
||||||
|
|
||||||
|
This tool allows to quickly ~~scrape~~ *fetch* Spotify track, album, or playlist metadada based on the provided url.
|
||||||
|
|
||||||
|
No Spotify API is required
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
```bash
|
||||||
|
$ node index.js "<url>"
|
||||||
|
```
|
||||||
|
|||||||
47
index.js
47
index.js
@@ -1,29 +1,32 @@
|
|||||||
const fetch = require('isomorphic-unfetch')
|
import fetch from 'isomorphic-unfetch'
|
||||||
const Spotify = require("spotify-url-info")
|
import Spotify from 'spotify-url-info'
|
||||||
|
|
||||||
const {getData, getPreview} = Spotify(fetch)
|
const {getData, getPreview} = Spotify(fetch)
|
||||||
|
const spotifyRegex = /https?:\/\/(?:embed\.|open\.)(?:spotify\.com\/)(?:track\/|\?uri=spotify:track:)((\w|-)+)(?:(?=\?)(?:[?&]foo=(\d*)(?=[&#]|$)|(?![?&]foo=)[^#])+)?(?=#|$)/
|
||||||
|
const spotifyPlaylistRegex = /https?:\/\/(?:embed\.|open\.)(?:spotify\.com\/)(?:(album|playlist)\/|\?uri=spotify:playlist:)((\w|-)+)(?:(?=\?)(?:[?&]foo=(\d*)(?=[&#]|$)|(?![?&]foo=)[^#])+)?(?=#|$)/
|
||||||
|
|
||||||
let spotufy_regex = /https?:\/\/(?:embed\.|open\.)(?:spotify\.com\/)(?:track\/|\?uri=spotify:track:)((\w|-)+)(?:(?=\?)(?:[?&]foo=(\d*)(?=[&#]|$)|(?![?&]foo=)[^#])+)?(?=#|$)/
|
const parse = async url => {
|
||||||
let isUrl = false
|
let isSpotifySong = spotifyRegex.test(url)
|
||||||
let url = ""
|
let isSpotifyPlaylist = spotifyPlaylistRegex.test(url)
|
||||||
|
|
||||||
|
if (isSpotifySong === isSpotifyPlaylist)
|
||||||
|
return
|
||||||
|
|
||||||
|
if (isSpotifyPlaylist) {
|
||||||
|
let spotifyResultData = await getData(url).catch(() => null)
|
||||||
|
if (!spotifyResultData || !['playlist', 'album'].includes(spotifyResultData.type))
|
||||||
|
return
|
||||||
|
|
||||||
|
for (const spotifyResult of spotifyResultData.trackList) {
|
||||||
|
console.log(`${spotifyResult.subtitle} - ${spotifyResult.title}`)
|
||||||
|
}
|
||||||
|
|
||||||
process.argv.forEach(val => {
|
return
|
||||||
if (isUrl) {
|
|
||||||
url = val
|
|
||||||
}
|
}
|
||||||
if (val == "--url") {
|
|
||||||
isUrl = true
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
let spotifyLink = spotufy_regex.test(url)
|
let spotifyResult = await getPreview(url)
|
||||||
if (!spotifyLink) {
|
console.log(`${spotifyResult.artist} - ${spotifyResult.title}`)
|
||||||
console.error("invalid url")
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const parser = async () => {
|
let url = process.argv[process.argv.length - 1]
|
||||||
let spotifyResult = await getPreview(url)
|
parse(url)
|
||||||
console.log(spotifyResult.artist, " - ", spotifyResult.title)
|
|
||||||
}
|
|
||||||
|
|
||||||
parser()
|
|
||||||
|
|||||||
4
package-lock.json
generated
4
package-lock.json
generated
@@ -1,11 +1,11 @@
|
|||||||
{
|
{
|
||||||
"name": "spotify",
|
"name": "spotify-parser",
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"lockfileVersion": 3,
|
"lockfileVersion": 3,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "spotify",
|
"name": "spotify-parser",
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
{
|
{
|
||||||
"name": "spotify-parser",
|
"name": "spotify-parser",
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
|
"type": "module",
|
||||||
"description": "parses provided spotify url to a query that can be searched in yt-dlp",
|
"description": "parses provided spotify url to a query that can be searched in yt-dlp",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|||||||
Reference in New Issue
Block a user