aktueller Stand lol

This commit is contained in:
Flummi
2021-11-19 11:14:22 +00:00
parent c5ab2a3118
commit 9e4079e3f4
14 changed files with 1130 additions and 60 deletions

View File

@@ -1,6 +1,5 @@
import fetch from "flumm-fetch-cookies";
import crypto from "crypto";
//import config from "../../../../cfg/config.json";
export default class cleverbot {
constructor() {
@@ -18,51 +17,12 @@ export default class cleverbot {
let body = `stimulus=${escape(stimulus)}&cb_settings_scripting=no&islearning=1&icognoid=ws&icognocheck=`;
body += crypto.createHash("md5").update(body.substring(7, 33)).digest("hex");
return decodeURIComponent((await fetch("https://www.cleverbot.com/webservicemin?uc=UseOfficialCleverbotAPI", {
return decodeURIComponent((await (await fetch("https://www.cleverbot.com/webservicemin?uc=UseOfficialCleverbotAPI", {
method: "POST",
headers: {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.85 Safari/537.36"
},
body: body
})).headers["cboutput"]);
})).text()).split("\r")[0]);
}
};
/*export default new class cleverbot {
constructor() {
this.api = "https://cleverbot.io/1.0";
this.nick = "";
this.init();
}
init() {
const options = {
method: "POST",
body: {
...config.apis.cleverbot,
...{ nick: "uwibot" }
}
};
fetch(`${this.api}/create`, options)
.then(res => res.json())
.then(res => this.nick = res.status === "success" ? res.nick: "uwibot")
.catch(err => console.log("cleverbot is offline or whatever lol"));
}
ask(msg) {
return new Promise((resolve, reject) => {
const options = {
method: "POST",
body: {
...config.apis.cleverbot,
...{
nick: this.nick,
text: msg
}
}
};
fetch(`${this.api}/ask`, options)
.then(res => res.json())
.then(res => res.status === "success"?resolve(res):reject(res.status))
.catch(err => reject("cleverbot is offline or whatever lol"));
});
}
};*/

View File

@@ -0,0 +1,50 @@
import http from "http";
import https from "https";
import url from "url";
import querystring from "querystring";
const redirectStatus = new Set([ 301, 302, 303, 307, 308 ]);
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(data); } break;
case "buffer": resolve(new Buffer.from(data)); break;
default: reject("lol no D:"); break;
}
}));
const genResolve = res => ({
body: res,
headers: res.headers,
text: () => readdata(res, "text"),
json: () => readdata(res, "json"),
buffer: () => readdata(res, "buffer")
});
const request = (a, options, resolve, reject, link = url.parse(a), body = "") => {
options = {...{ hostname: link.hostname, path: link.path, method: "GET", redirect: "follow" }, ...options};
if(options.method === "POST") {
body = typeof options.body === "object" ? querystring.stringify(options.body) : options.body;
delete options.body;
options.headers = {...options.headers, ...{
"Content-Type": "application/x-www-form-urlencoded",
"Content-Length": Buffer.byteLength(body)
}};
}
(link.protocol === "https:"?https:http).request(options, res => {
if(redirectStatus.has(res.statusCode) || options.redirect === "manual") {
switch(options.redirect) {
case "follow":
delete options.hostname;
delete options.path;
request(res.headers.location.startsWith("http") ? res.headers.location : `${link.protocol}//${link.host}${res.headers.location}`, options, resolve, reject);
break;
case "error": return reject("error lol");
}
}
else return resolve(genResolve(res));
}).on("error", err => err).end(body);
};
export default (_url, options = {}) => new Promise((resolve, reject) => request(_url, options, resolve, reject));