thaldrin/utils/src/sourceFynnder.js

78 lines
2.0 KiB
JavaScript
Raw Normal View History

2019-11-10 08:42:09 +00:00
const axios = require('axios');
const db = require('quick.db');
const backend = new db.table('backend');
const Servers = new db.table('servers');
2019-10-14 11:19:41 +00:00
let md5 = new RegExp(
2019-11-10 08:42:09 +00:00
'((?:!)?https?://static[0-9]*.(?:e621|e926).net/data/(?:sample/|preview/|)[0-9a-f]{2}/[0-9a-f]{2}/([0-9a-f]{32}).([0-9a-z]+))',
'igm'
2019-10-14 11:19:41 +00:00
);
2019-11-10 08:42:09 +00:00
let search_md5 = 'https://e621.net/post/show.json?md5=';
let e6 = 'https://e621.net/post/show/';
let e9 = 'https://e926.net/post/show/';
2019-10-14 11:19:41 +00:00
2019-11-10 08:42:09 +00:00
const version = '0.1.0';
2019-10-14 11:19:41 +00:00
2020-01-18 14:45:03 +00:00
async function SourceFynnderBot(enabled, msg) {
2019-11-10 08:42:09 +00:00
if (!enabled || enabled === null || enabled === undefined) return;
res = msg.content.match(md5);
if (!res) return;
let Sources = [];
for (const m in res) {
let URL = res[m];
let hash = URL.split(md5)[2];
2020-01-18 14:45:03 +00:00
let {
data
} = await axios.get(search_md5 + hash, {
headers: {
'user-agent': `SourceFynnder/${version} (ry / codepupper)`
}
2019-11-10 08:42:09 +00:00
});
if (data.rating === 's') {
Source = e9 + data.id;
} else {
Source = e6 + data.id;
}
Sources.push(`:link::mag: ${Source}`);
}
msg.channel.send(Sources);
await backend.add('SourceFynnder.found', Sources.length);
2020-01-18 14:45:03 +00:00
await backend.add('SourceFynnder.foundBot', Sources.length);
2019-11-10 08:42:09 +00:00
await Servers.add(`${msg.guild.id}.foundSources`, Sources.length);
2019-10-14 11:19:41 +00:00
};
2020-01-18 14:45:03 +00:00
async function SourceFynnderAPI(url) {
url = url.toString().replace(/\,/g, ' ')
res = url.match(md5);
if (!res) throw new Error('Not a Valid e621/e926 URL');
2019-10-14 11:19:41 +00:00
2020-01-18 14:45:03 +00:00
let Sources = [];
for (const m in res) {
let URL = res[m];
let hash = URL.split(md5)[2];
2019-10-14 11:19:41 +00:00
2020-01-18 14:45:03 +00:00
let {
data
} = await axios.get(search_md5 + hash, {
headers: {
'user-agent': `SourceFynnder/${version} (ry / codepupper)`
}
});
if (data.rating === 's') {
Source = e9 + data.id;
} else {
Source = e6 + data.id;
}
Sources.push(`${Source}`);
}
await backend.add('SourceFynnder.found', Sources.length);
await backend.add('SourceFynnder.foundAPI', Sources.length);
return Sources
};
2019-10-14 11:19:41 +00:00
2020-01-18 14:45:03 +00:00
module.exports = {
SourceFynnder: SourceFynnderBot,
APIFind: SourceFynnderAPI
}