summaryrefslogtreecommitdiff
path: root/modules/ytdl/ytdl.js
blob: fdda7f432c38ac6cd8fc2a5eb0e4ee2d528d252f (plain)
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
var cmn=require("../$common.js"),
    child_process=require("child_process");

module.exports=function(app,io,moddir){
	app.param("ytdl_id",function(req,res,next,link){
		link=unescape(link);
		req.params.ytdl_id=link;
		if(/['"\x00-\x1f]/.test(link)){
			res.status(400).end("Invalid youtube link or id");
			return;
		}
		next();
	});
	app.get("/ytdl",function(req,res){
		res.sendFile(moddir+"/ytdl.html");
	});
	app.get("/ytdl/:ytdl_id",function(req,res){
		var link=req.params.ytdl_id;
		var cp=child_process.execFile("/usr/bin/env",["youtube-dl","-gf","best","--",link],{},function(err,stdout,stderr){
			if(err){
				//console.log(err);
				res.status(404);
				res.set("Content-Type","text/plain");
				res.end("Youtube video id not found\n\n"+stdout+"\n\n"+stderr);
				return;
			}
			res.set("Content-Type","text/plain");
			res.send(stdout);
		});
	});
};