Skip to content

Commit

Permalink
fix: use official api
Browse files Browse the repository at this point in the history
  • Loading branch information
tatsumara committed Feb 12, 2023
1 parent 993fe98 commit 9846c46
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 196 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
desktop.ini
node_modules
node_modules
.env
25 changes: 18 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
const morgan = require('morgan');
const express = require('express');
const app = express();
const fetch = require('isomorphic-unfetch');
const spot = require('spotify-url-info')(fetch);
const Express = require('express');
const app = Express();
require('dotenv').config();

const Spotify = require('spotify-api.js');
const spot = new Spotify.Client({
token: {
clientID: process.env.clientID,
clientSecret: process.env.clientSecret,
},
refreshToken: true,
onRefresh() {
console.log('Refreshed token.')
}
});

app.use(morgan('[:date[web]] :remote-addr :method :url :status'));
app.set('trust proxy', true);
app.use(express.static('static'));
app.use(Express.static('static'));

let songArray;
let songArrayAge = 0;
app.get('/top4', async (req, res) => {
if (Date.now() - songArrayAge > 86400000) {
let data = await spot.getTracks('https://open.spotify.com/playlist/37i9dQZF1EpwA0Eb3mMXw4');
songArray = data.sort(() => Math.random() - 0.5).slice(0, 4).map(song => { return song.uri.slice(14) });
let data = await spot.playlists.getTracks('37i9dQZF1EpwA0Eb3mMXw4');
songArray = data.sort(() => Math.random() - 0.5).slice(0, 4).map(song => { return song.track.id });
songArrayAge = Date.now();
}
res.send(songArray);
Expand Down
Loading

0 comments on commit 9846c46

Please sign in to comment.