deepak191z commited on
Commit
85899ff
·
verified ·
1 Parent(s): 545de26

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +18 -15
index.js CHANGED
@@ -86,38 +86,42 @@ app.get('/', (req, res) => {
86
  });
87
 
88
  app.post('/extract-transcript', async (req, res) => {
89
- const { videoUrl, videoTitle } = req.body;
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');
@@ -125,7 +129,6 @@ app.post('/extract-transcript', async (req, res) => {
125
  await browser.close();
126
  }
127
  });
128
-
129
  const PORT = 7860;
130
  app.listen(PORT, () => {
131
  console.log(`Server is running on port ${PORT}`);
 
86
  });
87
 
88
  app.post('/extract-transcript', async (req, res) => {
89
+ const { videoUrl } = req.body;
90
+
91
+ if (!videoUrl) {
92
+ return res.status(400).send('videoUrl is required');
93
  }
94
+
95
  const browser = await chromium.launch();
96
  const context = await browser.newContext();
97
  const page = await context.newPage();
98
 
99
  try {
100
  await page.goto(videoUrl, { waitUntil: 'networkidle' });
101
+
102
  // Set viewport size
103
  await page.setViewportSize({ width: 1920, height: 1080 });
104
+
105
+ // Try to expand description if button is present (optional)
106
+ const expandButton = await page.$('tp-yt-paper-button#expand');
107
+ if (expandButton) {
108
+ await expandButton.click();
109
+ }
110
+
111
+ // Click the "Show transcript" button
112
  await page.click('button[aria-label="Show transcript"]');
113
+
114
  // Wait for the transcript container to appear
115
  await page.waitForSelector('ytd-transcript-segment-list-renderer');
116
+
117
  // Extract the transcript text
118
  const transcript = await page.evaluate(() => {
119
  const elements = Array.from(document.querySelectorAll('ytd-transcript-segment-renderer .segment-text'));
120
  return elements.map(element => element.innerText).join('\n');
121
  });
122
+
123
  res.json({ transcript });
124
+
125
  } catch (error) {
126
  console.error('Error extracting transcript:', error);
127
  res.status(500).send('Error extracting transcript');
 
129
  await browser.close();
130
  }
131
  });
 
132
  const PORT = 7860;
133
  app.listen(PORT, () => {
134
  console.log(`Server is running on port ${PORT}`);