Spaces:
Paused
Paused
Update index.mjs
Browse files
index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
const express = require('express');
|
| 2 |
-
const
|
| 3 |
const bodyParser = require('body-parser');
|
| 4 |
const cors = require('cors');
|
| 5 |
|
|
@@ -14,7 +14,7 @@ const html = `
|
|
| 14 |
<head>
|
| 15 |
<meta charset="UTF-8">
|
| 16 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 17 |
-
<title>YouTube Transcript Generator</title>
|
| 18 |
<style>
|
| 19 |
body {
|
| 20 |
font-family: Arial, sans-serif;
|
|
@@ -39,7 +39,7 @@ const html = `
|
|
| 39 |
</style>
|
| 40 |
</head>
|
| 41 |
<body>
|
| 42 |
-
<h1>YouTube Transcript Generator</h1>
|
| 43 |
<form id="transcriptForm">
|
| 44 |
<input type="text" id="videoUrl" name="videoUrl" placeholder="YouTube Video URL" required>
|
| 45 |
<input type="text" id="videoTitle" name="videoTitle" placeholder="Video Title" required>
|
|
@@ -90,35 +90,43 @@ app.post('/extract-transcript', async (req, res) => {
|
|
| 90 |
if (!videoUrl || !videoTitle) {
|
| 91 |
return res.status(400).send('videoUrl and videoTitle are required');
|
| 92 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
try {
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
await page.
|
| 98 |
|
| 99 |
-
|
| 100 |
await page.click('tp-yt-paper-button#expand');
|
| 101 |
|
| 102 |
-
|
| 103 |
await page.click('button[aria-label="Show transcript"]');
|
| 104 |
|
| 105 |
-
|
|
|
|
| 106 |
|
|
|
|
| 107 |
const transcript = await page.evaluate(() => {
|
| 108 |
const elements = Array.from(document.querySelectorAll('ytd-transcript-segment-renderer .segment-text'));
|
| 109 |
return elements.map(element => element.innerText).join('\n');
|
| 110 |
});
|
| 111 |
|
| 112 |
-
await browser.close();
|
| 113 |
res.json({ transcript });
|
| 114 |
|
| 115 |
} catch (error) {
|
| 116 |
console.error('Error extracting transcript:', error);
|
| 117 |
res.status(500).send('Error extracting transcript');
|
|
|
|
|
|
|
| 118 |
}
|
| 119 |
});
|
| 120 |
|
| 121 |
-
const PORT =
|
| 122 |
app.listen(PORT, () => {
|
| 123 |
console.log(`Server is running on port ${PORT}`);
|
| 124 |
});
|
|
|
|
| 1 |
const express = require('express');
|
| 2 |
+
const { chromium } = require('playwright');
|
| 3 |
const bodyParser = require('body-parser');
|
| 4 |
const cors = require('cors');
|
| 5 |
|
|
|
|
| 14 |
<head>
|
| 15 |
<meta charset="UTF-8">
|
| 16 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 17 |
+
<title>YouTube Transcript Generator (Playwright)</title>
|
| 18 |
<style>
|
| 19 |
body {
|
| 20 |
font-family: Arial, sans-serif;
|
|
|
|
| 39 |
</style>
|
| 40 |
</head>
|
| 41 |
<body>
|
| 42 |
+
<h1>YouTube Transcript Generator (Playwright)</h1>
|
| 43 |
<form id="transcriptForm">
|
| 44 |
<input type="text" id="videoUrl" name="videoUrl" placeholder="YouTube Video URL" required>
|
| 45 |
<input type="text" id="videoTitle" name="videoTitle" placeholder="Video Title" required>
|
|
|
|
| 90 |
if (!videoUrl || !videoTitle) {
|
| 91 |
return res.status(400).send('videoUrl and videoTitle are required');
|
| 92 |
}
|
| 93 |
+
|
| 94 |
+
const browser = await chromium.launch();
|
| 95 |
+
const context = await browser.newContext();
|
| 96 |
+
const page = await context.newPage();
|
| 97 |
+
|
| 98 |
try {
|
| 99 |
+
await page.goto(videoUrl, { waitUntil: 'networkidle' });
|
| 100 |
+
|
| 101 |
+
// Set viewport size
|
| 102 |
+
await page.setViewportSize({ width: 1920, height: 1080 });
|
| 103 |
|
| 104 |
+
// Click the "Expand" button to expand the video description
|
| 105 |
await page.click('tp-yt-paper-button#expand');
|
| 106 |
|
| 107 |
+
// Wait for the "Show transcript" button and click it
|
| 108 |
await page.click('button[aria-label="Show transcript"]');
|
| 109 |
|
| 110 |
+
// Wait for the transcript container to appear
|
| 111 |
+
await page.waitForSelector('ytd-transcript-segment-list-renderer');
|
| 112 |
|
| 113 |
+
// Extract the transcript text
|
| 114 |
const transcript = await page.evaluate(() => {
|
| 115 |
const elements = Array.from(document.querySelectorAll('ytd-transcript-segment-renderer .segment-text'));
|
| 116 |
return elements.map(element => element.innerText).join('\n');
|
| 117 |
});
|
| 118 |
|
|
|
|
| 119 |
res.json({ transcript });
|
| 120 |
|
| 121 |
} catch (error) {
|
| 122 |
console.error('Error extracting transcript:', error);
|
| 123 |
res.status(500).send('Error extracting transcript');
|
| 124 |
+
} finally {
|
| 125 |
+
await browser.close();
|
| 126 |
}
|
| 127 |
});
|
| 128 |
|
| 129 |
+
const PORT = 7860;
|
| 130 |
app.listen(PORT, () => {
|
| 131 |
console.log(`Server is running on port ${PORT}`);
|
| 132 |
});
|