Skip to content

Instantly share code, notes, and snippets.

@wdfsinap
Created May 8, 2016 07:06
Show Gist options
  • Save wdfsinap/41fa93d5560d21aa6a8ebdc90e791826 to your computer and use it in GitHub Desktop.
Save wdfsinap/41fa93d5560d21aa6a8ebdc90e791826 to your computer and use it in GitHub Desktop.
var path = require('path');
var express = require('express');
var nunjucks = require('nunjucks');
var app = express();
var ytdl = require("youtube-dl");
var request = require("request");
var utils = require('utility');
// view engine setup
nunjucks.configure('views', {
autoescape: true,
express: app
});
// uncomment after placing your favicon in /public
app.use(express.static(path.join(__dirname, 'public')));
// catch 404 and forward to error handler
function mapInfo(item) {
'use strict';
return {itag: item.format_id,
filetype: item.ext,
resolution: item.resolution||(item.width +'x'+item.height),
url:item.url,
c_url:utils.base64encode(item.url),
filesize:item.size,
};
}
app.get('/',function(req,res){
res.render('index.html');
})
app.get('/download', function (req, res) {
var url = req.query.url;
ytdl.getInfo(url, function(err, info) {
'use strict';
if (err) { res.send(err); }
else {
var formats = info.formats
var streams = []
var audio_streams =[]
if (typeof formats === 'undefined') {
streams_info = [{url:info.url,filetype:info.ext,resolution:'unknown'}]
audio_info = [{url:info.url,filetype:info.ext,resolution:'unknown'}]
}
else {
for(var i=0;i<formats.length;i++){
//streams with video and audio
if (formats[i].vcodec !="none" && formats[i].acodec !="none") {
streams.push(formats[i]);
//console.log(formats[i]);
}
else if(formats[i].vcodec ="none" && formats[i].acodec !="none"){
audio_streams.push(formats[i]);
}
}
var streams_info = streams.map(mapInfo);
var audio_info = audio_streams.map(mapInfo);
}
var v ={ url:url,
extractor:info.extractor,
title:info.title,
thumbnail:info.thumbnail,
duration:info.duration,
description:info.description,
published:info.upload_date,
uploader:info.uploader,
view_count:info.view_count,
video:streams_info,
audio:audio_info};
//console.log(formats);
res.render('download.html',v);
}
});
});
// development error handler
// will print stacktrace
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
module.exports = app;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment