This commit is contained in:
Flummi
2018-09-09 17:42:59 +02:00
parent 9d440644bc
commit 0d39f68990
5 changed files with 47 additions and 359 deletions

35
src/inc/fetch.mjs Normal file
View File

@ -0,0 +1,35 @@
import http from "http";
import https from "https";
import url from "url";
import querystring from "querystring";
const readdata = (res, mode, data = "") => new Promise((resolve, reject) => res
.setEncoding("utf8")
.on("data", chunk => data += chunk)
.on("end", () => {
switch(mode) {
case "text": resolve(data); break;
case "json": try { resolve(JSON.parse(data)); } catch(err) { reject("no json D:"); } break;
case "buffer": resolve(new Buffer.from(data)); break;
default: reject("lol no"); break;
}
}));
export default (a, options = {}, link = url.parse(a), body = "") => new Promise((resolve, reject) => {
options = {...{ hostname: link.hostname, path: link.path, method: "GET" }, ...options};
if(options.method === "POST") {
body = querystring.stringify(options.body);
delete options.body;
options.headers = {
"Content-Type": "application/x-www-form-urlencoded",
"Content-Length": Buffer.byteLength(body)
};
}
(link.protocol === "https:"?https:http).request(options, res => resolve({
body: res,
headers: res.headers,
text: () => readdata(res, "text"),
json: () => readdata(res, "json"),
buffer: () => readdata(res, "buffer")
})).on("error", err => reject(err)).end(body);
});

View File

@ -15,6 +15,8 @@ export default bot => {
active: true,
f: e => {
const args = e.message.trim().substring(7);
if(args === "true" || args === "false")
return e.self.debug = !e.self.debug;
try {
context.admins = admins;
context.e = e;

View File

@ -3,15 +3,16 @@ import sql from "../sql";
import * as lib from "./inc/parser";
import fs from "fs";
import fetch from "node-fetch";
//import fetch from "node-fetch";
import fetch from "../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" ];
const regex = /https?:\/\/[\w-]+(\.[\w-]+)+\.?(:\d+)?(\/\S*)?/gi;
const execFile = promisify(ef)
, bin = process.cwd() + "/bin/youtube-dl"
, b = process.cwd() + "/b"
, _args = [ "--dump-json" ]
, regex = /https?:\/\/[\w-]+(\.[\w-]+)+\.?(:\d+)?(\/\S*)?/gi;
export default bot => {
bot._trigger.set("parser", new bot.trigger({
@ -69,6 +70,8 @@ export default bot => {
return e.reply("no filters found, f0ck! D:");
fetch(data.format[0].url, { size: cfg.main.maxFileSize.val })
.then(res => {
if(res.headers["content-length"] >= cfg.main.maxFileSize.val)
return e.reply(`file too large soos! D; (~${lib.formatSize(res.headers["content-length"])} / ~${lib.formatSize(cfg.main.maxFileSize.val)})`);
const uuid = lib.getUUID();
const file = `${b}/${uuid}.${data.format[0].ext}`;
const dest = fs.createWriteStream(file);