68 lines
1.9 KiB
JavaScript
68 lines
1.9 KiB
JavaScript
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}`)
|
|
})
|