spotify api

This commit is contained in:
Mateusz Pstrucha committed 2021-09-08 07:44:54 +02:00
1 parent cf10bc6c29
commit 33468a8e9d
7 files changed
+98 -5

No files matched your search

+1 -1
View File
@@ -20,7 +20,7 @@ Get track metadata
art: String,
year: Number,
},
duration: Number,
duration: Number, // milliseconds
explicit: Boolean,
banned: Boolean,
votes: {
+27
View File
@@ -0,0 +1,27 @@
class Track {
constructor(data) {
console.log(data)
this.id = data.id
this.title = data.name
this.artists = data.artists.map((a) => a.name)
this.album = {
name: data.album.name,
art: data.album.images[0].url,
year: data.album.release_date.slice(0, 4),
}
this.duration = data.duration_ms
this.explicit = data.explicit
this.banned = false
this.votes = {
up: 100,
down: 50,
rank: 1,
}
}
}
export default Track
+2
View File
@@ -4,7 +4,9 @@ import { join } from 'path'
import cookieParser from 'cookie-parser'
import { createServer } from 'http'
import fs from 'fs'
import db from './db.js'
import { byId } from './spotify.js'
import track from './routes/track.js'
import status from './routes/status.js'
+2 -2
View File
@@ -29,6 +29,6 @@
"dotenv": "^8.2.0",
"express": "^4.17.1",
"mongoose": "^5.10.7",
"socket.io": "^2.4.1"
"spotify-web-api-node": "^5.0.2"
}
}
}
+8 -1
View File
@@ -1,5 +1,7 @@
import express from 'express'
import { byQuery } from '../spotify.js'
const router = express.Router()
router.get('/', (req, res) => {
@@ -7,7 +9,12 @@ router.get('/', (req, res) => {
})
router.get('/track/:query', (req, res) => {
res.send('tracks')
byQuery(req.params.query).then(({ tracks, total }) => {
res.send({
tracks,
total,
})
})
})
export default router
+3 -1
View File
@@ -1,5 +1,7 @@
import express from 'express'
import { byId } from '../spotify.js'
const router = express.Router()
router.get('/', (req, res) => {
@@ -7,7 +9,7 @@ router.get('/', (req, res) => {
})
router.get('/:id', (req, res) => {
res.send('track data')
byId(req.params.id).then((track) => res.send(track))
})
export default router
+55
View File
@@ -0,0 +1,55 @@
import SpotifyWebApi from 'spotify-web-api-node'
import Track from './Track.js'
const spotify = new SpotifyWebApi({
clientId: 'c9cd1ede71914999a75fbea35313c1a4',
clientSecret: '13858e98eb264cf78251072f2ea5e96d',
})
spotify.clientCredentialsGrant().then(
function (data) {
console.log('Spotify: access token granted')
// Save the access token so that it's used in future calls
spotify.setAccessToken(data.body['access_token'])
},
function (err) {
throw err
}
)
function byId(id) {
return new Promise((resolve, reject) => {
if (typeof id != 'string')
return reject(new Error('Incorrect id, must by type String'))
spotify
.getTrack(id)
.then((data) => {
resolve(new Track(data.body))
})
.catch((err) => {
reject(err)
})
})
}
function byQuery(query) {
return new Promise((resolve, reject) => {
spotify
.searchTracks(query)
.then((response) => {
resolve({
tracks: response.body.tracks.items.map((tdata) => tdata.id),
total: response.body.tracks.total,
})
})
.catch((err) => {
reject(err)
})
})
}
export default spotify
export { byId, byQuery }