This commit is contained in:
Urban Modig
2025-09-29 17:28:46 +02:00
commit a91fdf59c4
4 changed files with 90 additions and 0 deletions

67
server.js Normal file
View File

@ -0,0 +1,67 @@
const express = require('express')
const bodyParser = require('body-parser')
const app = express()
const port = 3000
const puppeteer = require('puppeteer');
app.use(bodyParser.text())
//app.use(bodyParser.urlencoded({ extended: false }))
async function retry(promiseFactory, retryCount) {
try {
return await promiseFactory();
} catch (error) {
if (retryCount <= 0) {
throw error;
}
return await retry(promiseFactory, retryCount - 1);
}
}
function run (url) {
return new Promise(async (resolve, reject) => {
try {
const browser = await puppeteer.launch({headless: 'new'});
const page = await browser.newPage();
// "https://sxyprn.com/post/6461f5ddbc5c9.html"
// await page.goto(url);
await retry(
() => page.goto(url),
5 // retry this 5 times
);
let urls = await page.evaluate(() => {
//let item = document.querySelector('video#player_el');
//return "https://sxyprn.com" + item.getAttribute('src');
let results = [];
let items = document.querySelectorAll('video#player_el');
items.forEach((item) => {
results.push(document.title);
results.push("https://sxyprn.com" + item.getAttribute('src'));
});
return results;
})
browser.close();
return resolve(JSON.stringify(urls));
} catch (e) {
return reject(e);
}
})
}
// example req. curl -X POST localhost:3000 -H "Content-Type: text/plain" --data 'https://sxyprn.com/post/653e2c6329e1c.html'
app.post('/', (req, res) => {
console.log(req.body);
run(req.body)
.then((r) => {
res.send(r)
});
})
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})