This commit is contained in:
Flummi
2018-09-02 18:17:30 +02:00
parent bfb9fa79de
commit e62de626b4
7 changed files with 84 additions and 96 deletions

View File

@ -0,0 +1,6 @@
const formatSize = (size, i = ~~(Math.log(size) / Math.log(1024))) => (size / Math.pow(1024, i)).toFixed(2) * 1 + ' ' + ['B', 'kB', 'MB', 'GB', 'TB'][i];
const getUUID = () => Math.random().toString(36).slice(-10) + (new Date()).getTime().toString(36).slice(-3);
export { formatSize, getUUID };

View File

@ -1,4 +1,4 @@
import debug from "./debug";
import parser from "./parser";
import parser from "./parser_new";
export default [ debug, parser ];

View File

@ -24,7 +24,7 @@ const checkRepostCheckSum = (cs, cbcrcs) => {
cbcrcs((rows[0].count == 0)?true:rows[0].id);
});
};
const formatSize = (size) => {
const formatSize = size => {
const i = ~~(Math.log(size) / Math.log(1024));
return (size / Math.pow(1024, i)).toFixed(2) * 1 + ' ' + ['B', 'kB', 'MB', 'GB', 'TB'][i];
};

View File

@ -0,0 +1,69 @@
import { cfg } from "../cfg";
import sql from "../sql";
import * as lib from "./inc/parser";
import fs from "fs";
import fetch from "node-fetch";
import { promisify } from "util";
import { execFile as ef } from "child_process";
const execFile = promisify(ef);
const bin = process.cwd() + "/bin/youtube-dl";
const b = process.cwd() + "/b";
const _args = [ "--dump-json" ];
export default bot => {
bot._trigger.set("parser", new bot.trigger({
call: /https?:\/\/[\w-]+(\.[\w-]+)+\.?(:\d+)?(\/\S*)?/gi,
f: e => {
if(e.channel !== "#kbot-dev" && !e.message.match(/(!|-)f0ck/i))
return;
if(e.message.match(/(!|-)ignore/))
return e.reply("ignored");
const tmp = e.message.match(/https?:\/\/[\w-]+(\.[\w-]+)+\.?(:\d+)?(\/\S*)?/gi); // get links
tmp.forEach(link => execFile(bin, [...[link], ..._args])
.then(({ stdout }) => JSON.parse(stdout))
.then(data => data.direct ? {
thumbnail: "",
link: data.url,
file: data.title,
format: [{
ext: data.ext,
note: "direct",
url: data.url,
size: 0,
rsize: 0
}]
} : {
thumbnail: data.thumbnail,
link: data.webpage_url,
file: data.id,
format: data.formats
.filter(f => (f.filesize <= cfg.main.maxFileSize.val || f.ext === "mp3") && ["webm","mp4","mp3"].includes(f.ext))
.splice(-1, 1)
.map(f => ({
ext: f.ext,
note: f.format_note,
url: f.url,
size: lib.formatSize(f.filesize),
rsize: f.filesize
}))
})
.then(data => {
if(data.format.length === 0)
return e.reply("no filters found, f0ck! D:");
fetch(data.format[0].url, { size: cfg.main.maxFileSize.val })
.then(res => {
const uuid = lib.getUUID();
const dest = fs.createWriteStream(b + "/" + uuid + "." + data.format[0].ext);
res.body.pipe(dest);
e.reply(`gef0ckt lol: ${data.file} -> ${uuid}.${data.format[0].ext}, ~${data.format[0].size}`);
})
.catch(err => {
console.error(err);
});
}));
}
}));
};