fast72 commited on
Commit
3c8c65c
·
verified ·
1 Parent(s): 32b7f9c

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +10 -12
server.js CHANGED
@@ -1,26 +1,24 @@
1
  const express = require('express');
2
  const ytdl = require('@distube/ytdl-core');
3
- //const fetch = require('node-fetch');
 
4
 
5
  const app = express();
6
  const PORT = 7860;
7
 
8
- async function getTranscript(videoUrl) {
9
- const info = await ytdl.getInfo(videoUrl);
10
- const captions = info.player_response.captions?.playerCaptionsTracklistRenderer.captionTracks;
11
- if (!captions) return null;
12
- const transcriptUrl = captions[0].baseUrl;
13
- const res = await fetch(transcriptUrl);
14
- const xml = await res.text();
15
- return xml.match(/<text[^>]*>(.*?)<\/text>/g).map(t => t.replace(/<[^>]+>/g, ''));
16
- }
17
 
18
  app.get('/transcript', async (req, res) => {
19
  const { url } = req.query;
20
  if (!url) return res.status(400).json({ error: 'Missing video URL' });
21
  try {
22
- const transcript = await getTranscript(url);
23
- res.json({ transcript });
24
  } catch (error) {
25
  console.log(error);
26
  res.status(500).json({ error: 'Failed to fetch transcript' });
 
1
  const express = require('express');
2
  const ytdl = require('@distube/ytdl-core');
3
+ const cheerio = require('cheerio');
4
+ const axios = require('axios');
5
 
6
  const app = express();
7
  const PORT = 7860;
8
 
9
+
10
+ async function transcript(url) {
11
+ const response = await axios.post( 'https://youtubetotranscript.com/transcript', new URLSearchParams({ youtube_url: url }), { headers: { 'Content-Type': 'application/x-www-form-urlencoded' } } );
12
+ const html = response.data;
13
+ const $ = cheerio.load(html); const texts = $('.transcript-segment').map((i, el) => $(el).text().trim()).get();
14
+ };
 
 
 
15
 
16
  app.get('/transcript', async (req, res) => {
17
  const { url } = req.query;
18
  if (!url) return res.status(400).json({ error: 'Missing video URL' });
19
  try {
20
+ const rttex = await transcript(url);
21
+ res.json(rttex);
22
  } catch (error) {
23
  console.log(error);
24
  res.status(500).json({ error: 'Failed to fetch transcript' });