feat(WebEmbed) Custom API, Custom ShortenAPI, and open-source API

https://github.com/aiko-chan-ai/WebEmbed
This commit is contained in:
Cinnamon
2022-06-27 13:23:00 +07:00
parent 3b2d88caf9
commit 58b4179e81
8 changed files with 45 additions and 23 deletions

View File

@@ -208,7 +208,7 @@ class MessagePayload {
// Add hidden embed link
content += `\n${WebEmbed.hiddenEmbed} \n`;
if (webembeds.length > 1) {
console.warn('Multiple webembeds are not supported, this will be ignored.');
console.warn('[WARN] Multiple webembeds are not supported, this will be ignored.');
}
// Const embed = webembeds[0];
for (const webE of webembeds) {

View File

@@ -1,6 +1,6 @@
'use strict';
const axios = require('axios');
const baseURL = 'https://sagiri-fansub.tk/embed?';
const baseURL = 'https://sagiri-v3dot3.herokuapp.com/embed?';
const hiddenCharter =
'||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||';
const { RangeError } = require('../errors');
@@ -49,6 +49,20 @@ class WebEmbed {
* @type {?boolean}
*/
this.hidden = data.hidden ?? false;
/**
* Using Custom WebEmbed server ?
* @type {?string} https://sagiri-v3dot3.herokuapp.com/embed?
* @see https://github.com/aiko-chan-ai/WebEmbed
*/
this.baseURL = data.baseURL ?? baseURL;
/**
* Shorten API
* @type {?string} https://sagiri-v3dot3.herokuapp.com/short?url=
* @see https://github.com/aiko-chan-ai/WebEmbed
*/
this.shortenAPI = data.shortenAPI;
}
/**
* @private
@@ -353,9 +367,9 @@ class WebEmbed {
if (this.thumbnail?.url) {
arrayQuery.push(`image=${encodeURIComponent(this.thumbnail.url)}`);
}
const fullURL = `${baseURL}${arrayQuery.join('&')}`;
const fullURL = `${this.baseURL}${arrayQuery.join('&')}`;
if (this.shorten) {
const url = await getShorten(fullURL);
const url = await getShorten(fullURL, this);
if (!url) console.log('Cannot shorten URL in WebEmbed');
return this.hidden ? `${hiddenCharter} ${url || fullURL}` : url || fullURL;
} else {
@@ -365,14 +379,20 @@ class WebEmbed {
}
// Credit: https://www.npmjs.com/package/node-url-shortener + google :))
const getShorten = async url => {
const getShorten = async (url, embed) => {
const APIurl = [
'https://tinyurl.com/api-create.php?url=',
'https://sagiri-fansub.tk/api/v1/short?url=', // My api, pls don't ddos :(
// 'https://sagiri-fansub.tk/api/v1/short?url=', // My api, pls don't ddos :(
'https://sagiri-v3dot3.herokuapp.com/short?url=', // My api, pls don't ddos :(
// 'https://lazuee.ga/api/v1/shorten?url=',
];
const shorten = `${
embed.shortenAPI && typeof embed.shortenAPI == 'string'
? embed.shortenAPI
: APIurl[Math.floor(Math.random() * APIurl.length)]
}${encodeURIComponent(url)}`;
try {
const res = await axios.get(`${APIurl[Math.floor(Math.random() * APIurl.length)]}${encodeURIComponent(url)}`);
const res = await axios.get(`${shorten}`);
if (typeof res.data === 'string') return res.data;
else if (typeof res.data === 'object') return res.data.shorten;
else throw new Error('Unknown error');