UNPKG

notmebotz-tools

Version:

Sebuah Tools yang berfungsi untuk mendownload Video atau Foto dari media sosial, serta sebagai tools yang berguna untuk aplikasi kamu seperti untuk BOT

145 lines (126 loc) 4.14 kB
const axios = require('axios'); const FormData = require('form-data'); const rmvbg = { api: { base: 'https://removal.ai', remove: 'https://api.removal.ai', endpoint: { webtoken: '/wp-admin/admin-ajax.php', remove: '/3.0/remove', slug: '/upload/' } }, headers: { 'user-agent': 'Postify/1.0.0' }, isUrl: async (link) => { if (!link || !link.match(/^https?:\/\/.+\/.+$/)) { return { valid: false, code: 400, error: "Link imagenya mana? Input kagak boleh kosong yak bree.." }; } try { const response = await axios.get(link, { responseType: 'arraybuffer' }); const contentType = response.headers['content-type']; if (!contentType?.startsWith('image/')) { return { valid: false, code: 400, error: "Njirr, bukan link image ini mah" }; } const buffer = Buffer.from(response.data); if (buffer.length > 5 * 1024 * 1024) { return { valid: false, code: 400, error: "File imagenya kegedean bree, max 5mb yak.." }; } return { valid: true, buffer, fileName: link.split('/').pop().split('#')[0].split('?')[0], type: contentType }; } catch (err) { return { valid: false, code: err.response?.status || 400, error: "Linknya kagak ada gambarnya bree.. coba cek lagi dah.." }; } }, getSecurity: async () => { try { const response = await axios.get(`${rmvbg.api.base}${rmvbg.api.endpoint.slug}`); const sc = response.data.match(/ajax_upload_object = (.*?);/); if (!sc) { return { valid: false, code: 400, error: "Security tokennya kagak ada di responsenya bree" }; } return { valid: true, security: JSON.parse(sc[1]).security }; } catch (err) { return { valid: false, code: err.response?.status || 400, error: "Endpoint securitynya kagak ada bree" }; } }, getWebToken: async (security) => { if (!security) { return { valid: false, code: 400, error: "Securitynya mana?? Kagak ada inputnya nih" }; } try { const response = await axios.get(`${rmvbg.api.base}${rmvbg.api.endpoint.webtoken}`, { params: { action: 'ajax_get_webtoken', security }, headers: { ...rmvbg.headers, 'Referer': `${rmvbg.api.base}${rmvbg.api.endpoint.slug}`, 'X-Requested-With': 'XMLHttpRequest' } }); if (!response.data.success) { return { valid: false, code: 400, error: "Servernya nolak buat diakses bree" }; } return { valid: true, webtoken: response.data.data.webtoken }; } catch (err) { return { valid: false, code: err.response?.status || 400, error: "Endpoint Webtokennya kagak ada bree" }; } }, remove: async (link) => { const img = await rmvbg.isUrl(link); if (!img.valid) { return { author: "Herza", status: img.code, data: img.error }; } const res = await rmvbg.getSecurity(); if (!res.valid) { return { author: "Herza", status: res.code, data: res.error }; } const toket = await rmvbg.getWebToken(res.security); if (!toket.valid) { return { author: "Herza", status: toket.code, data: toket.error }; } try { const formData = new FormData(); formData.append('image_file', img.buffer, { filename: img.fileName, contentType: img.type }); const response = await axios.post(`${rmvbg.api.remove}${rmvbg.api.endpoint.remove}`, formData, { headers: { ...rmvbg.headers, 'authority': 'api.removal.ai', 'origin': rmvbg.api.base, 'web-token': toket.webtoken, ...formData.getHeaders() } }); const { status, ...resx } = response.data; return { author: "Herza", status: 200, data: resx }; } catch (err) { return { author: "Herza", status: err.response?.status || 400, data: "Server Removal AInya gagal memproses remove bgnya" }; } } }; module.exports = { rmvbg };