Created
May 31, 2018 14:30
-
-
Save enjikaka/56f8c3bc55a3b8820eee6fef7e681794 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use latest'; | |
import express from 'express'; | |
import request from 'request'; | |
import { fromExpress } from 'webtask-tools'; | |
import bodyParser from 'body-parser'; | |
const app = express(); | |
app.use(bodyParser.json()); | |
function spotifyGet (url) { | |
return new Promise(resolve => { | |
var authOptions = { | |
url: 'https://accounts.spotify.com/api/token', | |
headers: { | |
'Authorization': '/* insert API crd here */' | |
}, | |
form: { | |
grant_type: 'client_credentials' | |
}, | |
json: true | |
}; | |
request.post(authOptions, function(error, response, body) { | |
if (!error && response.statusCode === 200) { | |
// use the access token to access the Spotify Web API | |
var token = body.access_token; | |
var options = { | |
url, | |
headers: { | |
'Authorization': 'Bearer ' + token | |
}, | |
json: true | |
}; | |
request.get(options, function(error, response, body) { | |
resolve(body); | |
}); | |
} | |
}); | |
}); | |
} | |
app.get('/:spotifyTrackId', (req, res) => { | |
const { spotifyTrackId } = req.params; | |
spotifyGet(`https://api.spotify.com/v1/tracks/${spotifyTrackId}`).then(json => { | |
res.redirect(json.album.images[0].url); | |
}); | |
}); | |
module.exports = fromExpress(app); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment