-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathYoutubeSearchServer.js
More file actions
44 lines (38 loc) · 1.09 KB
/
Copy pathYoutubeSearchServer.js
File metadata and controls
44 lines (38 loc) · 1.09 KB
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
var request = require('request');
//add string format function
//use like "Hello, {0}!".format("world")
if (!String.prototype.format) {
String.prototype.format = function() {
var args = arguments;
return this.replace(/\{(\d+)\}/g, function(match, number) {
return typeof args[number] != 'undefined'
? args[number]
: match
;
});
};
}
/**
YoutubeSearchServer simply searches youtube for the provied query,
and returns the results to the client.
*/
function YoutubeSearchServer(io)
{
var self = this;
this.io = io;
this.searchURL = "https://gdata.youtube.com/feeds/api/videos?q={0}&start-index={1}&max-results=10&v=2&alt=jsonc";
this.io.of('/search').on('connection', function(socket){
socket.on('disconnect', function(){});
socket.on('searchYoutube', function(data, callback)
{
var searchURL = self.searchURL.format(data.query, data.startIndex);
request(searchURL, function(error, response, body){
callback({
status : "success",
results : (JSON.parse(body))
});
});
});
});
}
exports.YoutubeSearchServer = YoutubeSearchServer;