File size: 2,605 Bytes
5c7c2cf
 
 
 
 
22ae60e
 
 
5c7c2cf
 
 
 
 
 
 
 
 
 
 
 
d3ef760
5c7c2cf
 
 
 
 
 
 
 
 
 
 
add4bda
5c7c2cf
 
 
add4bda
5c7c2cf
 
add4bda
6571f35
5c7c2cf
 
 
 
 
 
 
 
 
 
 
 
 
 
af3268b
e8fb8e9
 
 
 
 
11f0336
fc0cabd
e8fb8e9
22ae60e
af3268b
e8fb8e9
 
 
 
976218e
e8fb8e9
 
5c7c2cf
e8fb8e9
 
 
6745188
e8fb8e9
 
65c7696
add4bda
af3268b
5c7c2cf
e8fb8e9
 
 
fc0cabd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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
}