-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspotify-favorite-genres.js
62 lines (49 loc) · 1.48 KB
/
spotify-favorite-genres.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
var request = require('request');
var MongoClient = require('mongodb').MongoClient;
function saveGenre(genre, url, callback) {
var doc = {
genre: genre
};
var increment = {
$inc: {
count: 1
}
};
var options = {
upsert: true
};
MongoClient.connect(url, function (error, database) {
if(error) return callback(error);
database
.collection('genres')
.updateOne(doc, increment, options, function (error) {
if(error) return callback(error);
callback(null);
});
});
}
function requestGenre(title, artist, callback) {
var apiKey = 'YOUR LAST.FM API KEY';
var baseUrl = 'http://ws.audioscrobbler.com/2.0/?method=track.getInfo&format=json&';
var url = baseUrl + 'api_key=' + apiKey + '&artist=' + artist + '&track=' + title;
request(url, function(error, response, body) {
if(error) callback(error, null);
var json = JSON.parse(body);
var genre = json.track.toptags.tag[0].name;
callback(null, genre);
});
}
module.exports = function (context, done) {
var title = context.data.title;
var artist = context.data.artist;
var mongoUrl = context.data.MONGO_URL;
requestGenre(title, artist, (error, genre) => {
if(error) return done(error);
saveGenre(genre, mongoUrl, function(error) {
if(error) return done(error);
var successMessage = 'Updated genre ' + genre + ' for ' + artist + ', ' + title;
console.log(successMessage);
done(null, successMessage);
});
});
}