| const os = require('os') | |
| const bytes = require('bytes') | |
| const logger = require('morgan') | |
| const cheerio = require('cheerio') | |
| const express = require('express') | |
| const stealth = require('puppeteer-extra-plugin-stealth') | |
| const { chromium } = require('playwright-extra') | |
| chromium.use(stealth()) | |
| const app = express() | |
| app.set('json spaces', 4) | |
| app.use(logger('dev')) | |
| app.all('/', (req, res) => { | |
| const obj = {} | |
| const used = process.memoryUsage() | |
| for (let key in used) obj[key] = formatSize(used[key]) | |
| const totalmem = os.totalmem() | |
| const freemem = os.freemem() | |
| obj.memoryUsage = `${formatSize(totalmem - freemem)} / ${formatSize(totalmem)}` | |
| res.json({ | |
| creator: '@rippanteq7', | |
| message: 'Hello World', | |
| uptime: new Date(process.uptime() * 1000).toUTCString().split(' ')[4], | |
| status: obj | |
| }) | |
| }) | |
| app.get('/api', async (req, res) => { | |
| try { | |
| const { url, log } = req.query | |
| if (!url) return res.json({ success: false, message: 'Input parameter url' }) | |
| if (!/https?:\/\//.test(url)) return res.json({ success: false, message: 'Invalid url' }) | |
| const data = await xbuddy(url, log) | |
| data.message ? | |
| res.json({ success: false, message: data.message, result: data.response }) : | |
| res.json({ success: true, result: data.response }) | |
| } catch (e) { | |
| console.log(e) | |
| e = String(e) | |
| res.json({ error: true, message: e === '[object Object]' ? 'Internal Server Error' : e }) | |
| } | |
| }) | |
| const PORT = process.env.PORT || 7860 | |
| app.listen(PORT, () => console.log('App running on port', PORT)) | |
| function formatSize(num) { | |
| return bytes(+num || 0, { unitSeparator: ' ' }) | |
| } | |
| async function xbuddy(url) { | |
| const browser = await chromium.launch({ | |
| headless: true, | |
| executablePath: '/usr/bin/chromium', | |
| args: ['--no-sandbox'] | |
| }) | |
| const page = await browser.newPage() | |
| await page.goto(`https://9xbuddy.in/process?url=${url}`) | |
| const request = await page.waitForRequest(req => req.url().includes('extract')) | |
| const response = await page.waitForResponse(res => | |
| res.url().includes('extract') && res.request().method() === 'POST') | |
| const json = await response.json() | |
| if (json.status && json.response.type !== 'list') { | |
| const html = await page.content() | |
| const $ = cheerio.load(html) | |
| const result = $('.text-sm.tracking-wide.px-3.py-3').get().map(el => ({ | |
| type: $(el).find('.w-24').text(), | |
| q: $(el).find('.truncate').text(), | |
| size: $(el).find('.w-full div:nth-child(3)').text(), | |
| url: $(el).find('a').attr('href') | |
| })).filter(c => /^http/.test(c.url)) | |
| json.response.formats = result | |
| json.response.headers = request.headers() | |
| } | |
| await browser.close() | |
| return json | |
| } | |