Update index.js
Browse files
index.js
CHANGED
|
@@ -1,60 +1,56 @@
|
|
| 1 |
-
const express = require('express');
|
| 2 |
-
const puppeteer = require('puppeteer');
|
| 3 |
-
const path = require('path');
|
| 4 |
-
|
| 5 |
-
const app = express();
|
| 6 |
-
const port =
|
| 7 |
-
|
| 8 |
-
// Sử dụng middleware express.static để phục vụ các tệp tĩnh từ thư mục 'public'
|
| 9 |
-
app.use(express.static(path.join(__dirname, 'public')));
|
| 10 |
-
|
| 11 |
-
// Endpoint '/reel' để trích xuất cả video và hình ảnh từ một URL được cung cấp
|
| 12 |
-
app.get('/reel', async (req, res) => {
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
await page.goto(reelUrl, { waitUntil: 'networkidle2' });
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
const
|
| 27 |
-
const
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
res.
|
| 50 |
-
}
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
});
|
| 56 |
-
|
| 57 |
-
// Lắng nghe các kết nối trên cổng đã được chỉ định
|
| 58 |
-
app.listen(port, () => {
|
| 59 |
-
console.log(`Server is running at http://localhost:${port}`);
|
| 60 |
-
});
|
|
|
|
| 1 |
+
const express = require('express');
|
| 2 |
+
const puppeteer = require('puppeteer');
|
| 3 |
+
const path = require('path');
|
| 4 |
+
|
| 5 |
+
const app = express();
|
| 6 |
+
const port = 8000;
|
| 7 |
+
|
| 8 |
+
// Sử dụng middleware express.static để phục vụ các tệp tĩnh từ thư mục 'public'
|
| 9 |
+
app.use(express.static(path.join(__dirname, 'public')));
|
| 10 |
+
|
| 11 |
+
// Endpoint '/reel' để trích xuất cả video và hình ảnh từ một URL được cung cấp
|
| 12 |
+
app.get('/reel', async (req, res) => {
|
| 13 |
+
const reelUrl = req.query.url;
|
| 14 |
+
|
| 15 |
+
try {
|
| 16 |
+
// Khởi tạo một trình duyệt Puppeteer với tùy chọn '--no-sandbox'
|
| 17 |
+
const browser = await puppeteer.launch({
|
| 18 |
+
headless: true,
|
| 19 |
+
args: ['--no-sandbox', '--disable-setuid-sandbox']
|
| 20 |
+
});
|
| 21 |
+
const page = await browser.newPage();
|
| 22 |
+
await page.goto(reelUrl, { waitUntil: 'networkidle2' });
|
| 23 |
+
|
| 24 |
+
const mediaUrls = await page.evaluate(() => {
|
| 25 |
+
const videoElements = document.querySelectorAll('video');
|
| 26 |
+
const imageElements = document.querySelectorAll('img');
|
| 27 |
+
const urls = [];
|
| 28 |
+
|
| 29 |
+
// Trích xuất các URL của video
|
| 30 |
+
videoElements.forEach(video => {
|
| 31 |
+
urls.push({ type: 'video', url: video.src });
|
| 32 |
+
});
|
| 33 |
+
|
| 34 |
+
// Trích xuất các URL của hình ảnh và loại bỏ các hình ảnh tỷ lệ 1:1
|
| 35 |
+
imageElements.forEach(image => {
|
| 36 |
+
if (image.naturalWidth / image.naturalHeight !== 1) {
|
| 37 |
+
urls.push({ type: 'image', url: image.src });
|
| 38 |
+
}
|
| 39 |
+
});
|
| 40 |
+
|
| 41 |
+
return urls;
|
| 42 |
+
});
|
| 43 |
+
|
| 44 |
+
await browser.close();
|
| 45 |
+
|
| 46 |
+
res.json(mediaUrls);
|
| 47 |
+
} catch (error) {
|
| 48 |
+
console.error('Error:', error);
|
| 49 |
+
res.status(500).send('Internal Server Error');
|
| 50 |
+
}
|
| 51 |
+
});
|
| 52 |
+
|
| 53 |
+
// Lắng nghe các kết nối trên cổng đã được chỉ định
|
| 54 |
+
app.listen(port, () => {
|
| 55 |
+
console.log(`Server is running at http://localhost:${port}`);
|
| 56 |
+
});
|
|
|
|
|
|
|
|
|
|
|
|