ssyoutube / index.js
cruxx's picture
Update index.js
4d361e5
for (let e of ['unhandledRejection', 'uncaughtException']) process.on(e, console.error)
const os = require('os')
const bytes = require('bytes')
const morgan = require('morgan')
const express = require('express')
const puppeteer = require('puppeteer')
const { chromium } = require('playwright')
const app = express()
app.set('json spaces', 4)
app.use(morgan('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 } = 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 ssyoutube(url)
data.message ?
res.json({ success: false, message: data.message, result: data }) :
res.json({ success: true, result: data })
} 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.format(+num || 0, { unitSeparator: ' ' })
}
async function ssyoutube(url) {
const browser = await chromium.launch({
headless: true,
executablePath: '/usr/bin/chromium',
args: ['--no-sandbox']
})
const context = await browser.newContext()
const page = await context.newPage()
await page.goto('https://ssyoutube.com/')
await page.type('#id_url', url)
await page.click('#search')
const response = await page.waitForResponse(res =>
res.url().includes('ssyoutube.com/api') && res.request().resourceType() === 'xhr')
const json = await response.json()
await browser.close()
return json
}