-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathartistfinder.node.js
59 lines (53 loc) · 2.01 KB
/
artistfinder.node.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
var url = require("url");
var http = require("http");
exports.execute = function(request, response) {
var reqURL = url.parse(request.url);
console.log("url search: " + reqURL.search);
var artist = reqURL.search.substring(7);
console.log("artist: " + artist);
var dataString = "";
var options = {
host: 'ws.audioscrobbler.com',
path: '/2.0/?method=artist.getsimilar&autocorrect=1&limit=3&artist=' + artist + '&format=json&api_key=028a648a94b5ca3a256ed241d7952a20',
port: 80
}
http.get(options, function(result){
result.on('error', function(e) {
console.log("Got error: " + e.message);
});
result.setEncoding('utf8');
result.on('data', function (data) {
dataString = dataString + data;
});
result.on('end', function() {
var artistData=JSON.parse(dataString);
console.log("response: "+artistData.statusCode);
console.log("response: "+JSON.stringify(artistData));
if(artistData.error > 1)
{
response.writeHead(200, {'Content-Type': 'application/json'});
var reply = new Object();
reply.error = artistData.error;
}
else
{
var artist1url = artistData.similarartists.artist[0].url;
var artist2url = artistData.similarartists.artist[1].url;
var artist3url = artistData.similarartists.artist[2].url;
var artist1name = artistData.similarartists.artist[0].name;
var artist2name = artistData.similarartists.artist[1].name;
var artist3name = artistData.similarartists.artist[2].name;
response.writeHead(200, {'Content-Type': 'application/json'});
var reply = new Object();
reply.error = 0;
reply.artist1name = artist1name;
reply.artist2name = artist2name;
reply.artist3name = artist3name;
reply.artist1url = artist1url;
reply.artist2url = artist2url;
reply.artist3url = artist3url;
}
response.end(JSON.stringify(reply));
});
});
}