0.0.1 - test
This commit is contained in:
59
controller/helper/uploadWithPayload.js
Normal file
59
controller/helper/uploadWithPayload.js
Normal file
@@ -0,0 +1,59 @@
|
||||
import axios from "axios";
|
||||
import FormData from "form-data";
|
||||
import fs from "fs";
|
||||
import { encode, decode } from "base64-arraybuffer";
|
||||
|
||||
import { Button, Embed, MessageBuilder, Webhook } from "discord-webhooks-node";
|
||||
import Config from "../../config/config.js";
|
||||
// https://github.com/gaurishhs/discord-webhooks-node
|
||||
// The 'data' parameter contains the usual webhook json stuff, like embeds etc.
|
||||
export const sendFileWithPayload = async (url, fData, filename, api_resp) => {
|
||||
//const form = new FormData();
|
||||
//form.append("file0", fData, "myfilename.png");
|
||||
//form.append("payload_json", JSON.stringify(data));
|
||||
//await axios.post(url, form);
|
||||
//fs.unlinkSync(path);
|
||||
try {
|
||||
const webhook = new Webhook({
|
||||
url: url,
|
||||
});
|
||||
let e = {};
|
||||
if(Config.showTags){
|
||||
let text = "";
|
||||
for(var i = 0;i<api_resp.tags.length;i++){
|
||||
text += `${api_resp.tags[i].names[0]} - `;
|
||||
}
|
||||
text = text.substring(0, 1023);
|
||||
e.fields = [
|
||||
{
|
||||
"name": "Tags",
|
||||
"value": `${text}`
|
||||
}
|
||||
];
|
||||
}
|
||||
webhook.execute({
|
||||
content: 'Hello world!',
|
||||
embeds: [
|
||||
|
||||
{
|
||||
"title": "We got a new Upload",
|
||||
"description": `New Upload - [URL](${Config.booru_url}/post/${api_resp.id}) - ${api_resp.safety}`,
|
||||
"color": 5814783,
|
||||
"image": {
|
||||
"url": `attachment://${filename}`
|
||||
},
|
||||
"flags": 4096,
|
||||
...e
|
||||
}
|
||||
],
|
||||
files: [
|
||||
{
|
||||
name: filename,
|
||||
file: Buffer.from(fData),
|
||||
},
|
||||
],
|
||||
}).then(() => console.log('Sent!')).catch((err) => console.error('Failed! ', err));
|
||||
} catch (e) {
|
||||
|
||||
}
|
||||
}
|
||||
70
controller/modules/type.post.js
Normal file
70
controller/modules/type.post.js
Normal file
@@ -0,0 +1,70 @@
|
||||
import Config from "../../config/config.js";
|
||||
import { sendFileWithPayload } from "../helper/uploadWithPayload.js";
|
||||
|
||||
import fetch from "node-fetch"
|
||||
|
||||
export default class Post {
|
||||
|
||||
static operationSelect(body) {
|
||||
switch (body.operation) {
|
||||
case "created":
|
||||
Post.created(body);
|
||||
break;
|
||||
case "modified":
|
||||
Post.modified(body);
|
||||
|
||||
default:
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static async created(body) {
|
||||
let x = await fetch(`${Config.booru_url}/api/post/${body.id}`, {
|
||||
"headers": {
|
||||
"Content-Type": "application/json",
|
||||
"Accept": "application/json",
|
||||
"Accept-Encoding": "multipart/form-data",
|
||||
"Authorization": `Token ${Config.booru_token}`
|
||||
//"cookie": "auth={%22user%22:%22Test%22%2C%22token%22:%229b0bd51b-61cd-4ce5-a6b7-5fe215edc46f%22}",
|
||||
},
|
||||
"body": null,
|
||||
"method": "GET"
|
||||
});
|
||||
let api_resp = await x.json();
|
||||
console.log(api_resp)
|
||||
console.log(`${Config.booru_url}/${api_resp.thumbnailUrl}`)
|
||||
let imgData = null;
|
||||
let type = "";
|
||||
switch(api_resp.mimeType){
|
||||
case "image/jpeg":
|
||||
case "image/jpg":
|
||||
case "image/png":
|
||||
case "image/gif":
|
||||
if(api_resp.fileSize>13107200){
|
||||
imgData = await fetch(`${Config.booru_url}/${api_resp.thumbnailUrl}`);
|
||||
let tmp = api_resp.thumbnailUrl.split(".")
|
||||
type = tmp[tmp.length-1]
|
||||
}else{
|
||||
imgData = await fetch(`${Config.booru_url}/${api_resp.contentUrl}`);
|
||||
let tmp = api_resp.contentUrl.split(".")
|
||||
type = tmp[tmp.length-1]
|
||||
}
|
||||
break;
|
||||
default:
|
||||
imgData = await fetch(`${Config.booru_url}/${api_resp.thumbnailUrl}`);
|
||||
let tmp = api_resp.thumbnailUrl.split(".")
|
||||
type = tmp[tmp.length-1]
|
||||
break;
|
||||
}
|
||||
console.log(imgData)
|
||||
let imgD = await imgData.arrayBuffer();
|
||||
let filename = `${api_resp.id}.${type}`;
|
||||
sendFileWithPayload(Config.webhook_url, imgD,filename,api_resp)
|
||||
}
|
||||
static modified(body) {
|
||||
Post.created(body);
|
||||
}
|
||||
|
||||
}
|
||||
31
controller/router.js
Normal file
31
controller/router.js
Normal file
@@ -0,0 +1,31 @@
|
||||
import express from "express";
|
||||
import bodyParser from "body-parser";
|
||||
import Post from "./modules/type.post.js";
|
||||
|
||||
export default class Router{
|
||||
constructor(){
|
||||
const app = express();
|
||||
app.use(bodyParser.urlencoded({ extended: false }))
|
||||
|
||||
// parse application/json
|
||||
app.use(bodyParser.json())
|
||||
app.get("*",async (req,res)=>{
|
||||
console.log(JSON.stringify(req.body));
|
||||
res.send();
|
||||
})
|
||||
app.post("*",async (req,res)=>{
|
||||
switch(req.body.type){
|
||||
case "post":
|
||||
Post.operationSelect(req.body)
|
||||
break;
|
||||
default:
|
||||
|
||||
break;
|
||||
}
|
||||
console.log(JSON.stringify(req.body));
|
||||
res.send();
|
||||
})
|
||||
|
||||
app.listen(7598);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user