-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyoutube.js
46 lines (43 loc) · 1.35 KB
/
youtube.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
var http = require("http");
var url = require("url");
var youtube = module.exports = function(query, callback, errorcallback) {
var data = "";
var request = http.get({
host: "gdata.youtube.com",
port: 80,
path: url.format({
pathname: "/feeds/api/videos",
query: {
q: query,
format: 5,
"max-results": 1,
alt: "json",
v: 2
}
})
}, function(res) {
if (res.statusCode == 200)
{
res.on("data", function(chunk) { data += chunk; });
res.on("end", function() {
try
{
var results = JSON.parse(data);
var title = results.feed.entry[0].title["$t"];
var id = results.feed.entry[0]["media$group"]["yt$videoid"]["$t"];
var length = Number(results.feed.entry[0]["media$group"]["yt$duration"].seconds);
var thumbnail = results.feed.entry[0]["media$group"]["media$thumbnail"][0]["url"];
callback(id, title, length, thumbnail);
}
catch (e)
{
errorcallback();
}
});
}
else
{
errorcallback();
}
});
};