f0ckv1/src/trigger/ytdl.js
2016-09-08 15:46:14 +00:00

91 lines
4.3 KiB
JavaScript

var fs = require('fs-extra');
var ytdl = require('ytdl-core');
var uuid = require('uuid');
var probe = require('node-ffprobe');
module.exports = (bot, trigger, lib) => {
trigger.add({
name: 'ytdl',
call: /https?:\/\/(www\.)?(yotu\.be\/|youtube\.com\/)((.+\/)?(watch(\?v=|.+&v=))?(v=)?)([\w_-]{11})(&.+)?/gi,
level: 0,
active: 1,
func: (e) => {
if(e.channel.getName() == '#f0ck' || e.channel.getName() == '#kbot-dev') {
if(!e.message.match(/\!ignore/)) {
var tmp = e.message.match(/https?:\/\/(www\.)?(yotu\.be\/|youtube\.com\/)((.+\/)?(watch(\?v=|.+&v=))?(v=)?)([\w_-]{11})(&.+)?/gi); // get links
tmp.forEach((entry,i,a) => {
var dl = true;
var tmpdest = uuid.v1().split('-')[0];
lib.checkRepost(entry, (cbcr) => {
if(cbcr) {
ytdl.getInfo(entry, (err, info) => {
if(!err) {
var title = info.title;
var dat = fs.createWriteStream('./b/'+tmpdest+'.webm');
try {
ytdl.downloadFromInfo(info, { filter: (format) => { return format.container === 'webm'; } })
.on('response', (res) => {
if(res.headers['content-length'] > lib.cfg.maxFileSize) {
res.destroy();
dl = false;
e.reply('f0ck! your file is too big (~'+lib.formatSize(res.headers['content-length'])+'), max '+lib.formatSize(lib.cfg.maxFileSize)+' allowed');
}
})
.on('error', (err) => {
//e.reply(err);
})
.pipe( dat );
}
catch(ex) {
e.reply(ex);
}
dat.on('finish', () => {
if(dl) {
dat.close();
var stat = fs.statSync("./b/"+tmpdest+".webm");
bot.whois(e.user.getNick(), e.network, (err, cbgu) => {
lib.getCheckSum("./b/"+tmpdest+".webm", (cbcs) => {
lib.checkRepostCheckSum(cbcs, (cbcrcs) => {
if(cbcrcs === true) {
lib.sql.query("insert into `f0ck`.`items` (`src`,`dest`,`mime`,`size`,`checksum`,`username`,`userchannel`,`usernetwork`,`stamp`,`active`) values (?,?,?,?,?,?,?,?,?,?)", [
entry,
"./b/"+tmpdest+".webm",
"video/webm",
stat.size,
cbcs,
cbgu['nick'],
e.channel.getName(),
e.network,
Math.floor(new Date() / 1000),
0
]).on('result', (result) => {
lib.generateThumbs();
e.reply("https://f0ck.me/"+result.insertId+" - "+title+" (video/webm, ~"+lib.formatSize(stat.size)+") from "+cbgu['nick']+" ("+cbgu['username']+"@"+cbgu['hostname']+")");
}).on('error', (msg) => {
e.reply(msg);
});
}
else {
fs.unlink("./b/"+tmpdest+".webm"); // delete repost
e.reply("repost motherf0cker: https://f0ck.me/"+cbcrcs);
}
});
});
});
}
else
fs.unlink('./b/'+tmpdest+'.webm');
});
}
});
}
else
e.reply('repost motherf0cker');
});
});
}
}
},
desc: 'download youtubevideos'
});
};