Showing
2 changed files
with
181 additions
and
3 deletions
routes/api.js
0 → 100644
| 1 | +const SpotifyWebApi = require('spotify-web-api-node'); // spotify api | ||
| 2 | +//const { search } = require('.'); | ||
| 3 | +var spotifyApi = new SpotifyWebApi({ | ||
| 4 | + clientId: "your id", | ||
| 5 | + clientSecret: "your secret", | ||
| 6 | + redirectUri: 'redirectUri' | ||
| 7 | + }); | ||
| 8 | + | ||
| 9 | +const scopes = [ | ||
| 10 | + 'ugc-image-upload', | ||
| 11 | + 'user-read-playback-state', | ||
| 12 | + 'user-modify-playback-state', | ||
| 13 | + 'user-read-currently-playing', | ||
| 14 | + 'streaming', | ||
| 15 | + 'app-remote-control', | ||
| 16 | + 'user-read-email', | ||
| 17 | + 'user-read-private', | ||
| 18 | + 'playlist-read-collaborative', | ||
| 19 | + 'playlist-modify-public', | ||
| 20 | + 'playlist-read-private', | ||
| 21 | + 'playlist-modify-private', | ||
| 22 | + 'user-library-modify', | ||
| 23 | + 'user-library-read', | ||
| 24 | + 'user-top-read', | ||
| 25 | + 'user-read-playback-position', | ||
| 26 | + 'user-read-recently-played', | ||
| 27 | + 'user-follow-read', | ||
| 28 | + 'user-follow-modify' | ||
| 29 | + ]; | ||
| 30 | + | ||
| 31 | +var access_token; | ||
| 32 | + | ||
| 33 | +function redirect(res){ | ||
| 34 | + res.redirect(spotifyApi.createAuthorizeURL(scopes)); | ||
| 35 | +} | ||
| 36 | +module.exports.redirect = redirect; | ||
| 37 | + | ||
| 38 | + | ||
| 39 | +function callback(req, res) { | ||
| 40 | + const error = req.query.error; | ||
| 41 | + const code = req.query.code; | ||
| 42 | + const state = req.query.state; | ||
| 43 | + | ||
| 44 | + if (error) { | ||
| 45 | + console.error('Callback Error:', error); | ||
| 46 | + res.send(`Callback Error: ${error}`); | ||
| 47 | + return; | ||
| 48 | + } | ||
| 49 | + | ||
| 50 | + spotifyApi | ||
| 51 | + .authorizationCodeGrant(code) | ||
| 52 | + .then(data => { | ||
| 53 | + access_token = data.body['access_token']; | ||
| 54 | + const refresh_token = data.body['refresh_token']; | ||
| 55 | + const expires_in = data.body['expires_in']; | ||
| 56 | + | ||
| 57 | + spotifyApi.setAccessToken(access_token); | ||
| 58 | + spotifyApi.setRefreshToken(refresh_token); | ||
| 59 | + | ||
| 60 | + console.log('access_token:', access_token); | ||
| 61 | + console.log('refresh_token:', refresh_token); | ||
| 62 | + | ||
| 63 | + console.log( | ||
| 64 | + `Sucessfully retreived access token. Expires in ${expires_in} s.` | ||
| 65 | + ); | ||
| 66 | + | ||
| 67 | + setInterval(async () => { | ||
| 68 | + const data = await spotifyApi.refreshAccessToken(); | ||
| 69 | + const access_token = data.body['access_token']; | ||
| 70 | + | ||
| 71 | + console.log('The access token has been refreshed!'); | ||
| 72 | + console.log('access_token:', access_token); | ||
| 73 | + spotifyApi.setAccessToken(access_token); | ||
| 74 | + }, expires_in / 2 * 1000); | ||
| 75 | + }) | ||
| 76 | + .catch(error => { | ||
| 77 | + console.error('Error getting Tokens:', error); | ||
| 78 | + res.send(`Error getting Tokens: ${error}`); | ||
| 79 | + }); | ||
| 80 | + | ||
| 81 | +} | ||
| 82 | +module.exports.callback = callback; | ||
| 83 | + | ||
| 84 | + | ||
| 85 | +// Search artists whose name, album or artist contains 'hip hop' | ||
| 86 | +function searchArtists(genre, callback){ | ||
| 87 | + spotifyApi.searchArtists(genre) | ||
| 88 | + .then(function(data) { | ||
| 89 | + console.log('Search artists success', data.body.artists.items); | ||
| 90 | + callback(data.body.artists.items[19].id) | ||
| 91 | + }, function(err) { | ||
| 92 | + console.error(err); | ||
| 93 | + }); | ||
| 94 | +} | ||
| 95 | +module.exports.searchArtists = searchArtists; | ||
| 96 | + | ||
| 97 | +// Get an artist | ||
| 98 | +function getArtist(artistid,callback){ | ||
| 99 | + spotifyApi.getArtist(artistid) | ||
| 100 | + .then(function(data) { | ||
| 101 | + console.log('getArtist success', data.body); | ||
| 102 | + callback(data.body); | ||
| 103 | + }, function(err) { | ||
| 104 | + console.error(err); | ||
| 105 | + }); | ||
| 106 | +} | ||
| 107 | +module.exports.getArtist = getArtist; | ||
| 108 | + | ||
| 109 | +function getArtistAlbums(artistid,callback){ | ||
| 110 | + spotifyApi.getArtistAlbums(artistid) | ||
| 111 | + .then(function(data) { | ||
| 112 | + console.log('getArtistAlbums success', data.body.items[0].id); | ||
| 113 | + callback(data.body.items[0].id); | ||
| 114 | + }, function(err) { | ||
| 115 | + console.error(err); | ||
| 116 | + }); | ||
| 117 | +} | ||
| 118 | +module.exports.getArtistAlbums = getArtistAlbums; |
| 1 | const express = require("express"); | 1 | const express = require("express"); |
| 2 | +const { nextTick } = require("process"); | ||
| 2 | const router = express.Router(); | 3 | const router = express.Router(); |
| 4 | +var api = require('./api'); | ||
| 3 | 5 | ||
| 4 | -router.get('/', (req, res) => res.render('index')); | 6 | +router.get('/', (req, res) => { |
| 5 | -router.get("/login", (req, res) => res.render("login", {page: "login"})); | 7 | + api.redirect(res); |
| 6 | -router.get("/signup", (req, res) => res.render("signup", {page: "signup"})); | 8 | + }); |
| 9 | + | ||
| 10 | +router.get('/callback', (req, res) => { | ||
| 11 | + api.callback(req,res); | ||
| 12 | + res.render('index'); | ||
| 13 | +}); | ||
| 14 | + | ||
| 15 | +router.get("/select", (req, res) => { | ||
| 16 | + res.render("select", {page: "select"}) | ||
| 17 | +}); | ||
| 18 | + | ||
| 19 | +router.get("/rock", (req, res) => { | ||
| 20 | + var genre = 'rock'; | ||
| 21 | + api.searchArtists(genre,function(artistid){ | ||
| 22 | + api.getArtist(artistid,function(artistdata){ | ||
| 23 | + res.render("rock", {artistdata: artistdata}); | ||
| 24 | + }); | ||
| 25 | + }); | ||
| 26 | +}); | ||
| 27 | +router.get("/electronic", (req, res) => { | ||
| 28 | + var genre = 'electronic'; | ||
| 29 | + api.searchArtists(genre,function(artistid){ | ||
| 30 | + api.getArtist(artistid,function(artistdata){ | ||
| 31 | + res.render("electronic", {artistdata: artistdata}); | ||
| 32 | + }); | ||
| 33 | + }); | ||
| 34 | +}); | ||
| 35 | +router.get("/hiphop", (req, res) => { | ||
| 36 | + var genre = 'hiphop'; | ||
| 37 | + api.searchArtists(genre,function(artistid){ | ||
| 38 | + api.getArtist(artistid,function(artistdata){ | ||
| 39 | + res.render("hiphop", {artistdata: artistdata}); | ||
| 40 | + }); | ||
| 41 | + }); | ||
| 42 | +}); | ||
| 43 | +router.get("/acoustic", (req, res) => { | ||
| 44 | + var genre = 'acoustic'; | ||
| 45 | + api.searchArtists(genre,function(artistid){ | ||
| 46 | + api.getArtist(artistid,function(artistdata){ | ||
| 47 | + res.render("acoustic", {artistdata: artistdata}); | ||
| 48 | + }); | ||
| 49 | + }); | ||
| 50 | +}); | ||
| 51 | +router.get("/jazz", (req, res) => { | ||
| 52 | + var genre = 'jazz'; | ||
| 53 | + api.searchArtists(genre,function(artistid){ | ||
| 54 | + api.getArtist(artistid,function(artistdata){ | ||
| 55 | + res.render("jazz", {artistdata: artistdata}); | ||
| 56 | + }); | ||
| 57 | + }); | ||
| 58 | +}); | ||
| 59 | +router.get("/pop", (req, res) => { | ||
| 60 | + var genre = 'pop'; | ||
| 61 | + api.searchArtists(genre,function(artistid){ | ||
| 62 | + api.getArtist(artistid,function(artistdata){ | ||
| 63 | + res.render("pop", {artistdata: artistdata}); | ||
| 64 | + }); | ||
| 65 | + }); | ||
| 66 | +}); | ||
| 7 | 67 | ||
| 8 | module.exports = router; | 68 | module.exports = router; | ... | ... |
-
Please register or login to post a comment