wudysoft commited on
Commit
83eaffc
·
verified ·
1 Parent(s): 86e6732

Update app.js

Browse files
Files changed (1) hide show
  1. app.js +66 -13
app.js CHANGED
@@ -2,6 +2,8 @@ import express from 'express';
2
  import { chromium } from 'playwright';
3
  import cors from 'cors';
4
  import axios from 'axios';
 
 
5
  import dotenv from 'dotenv';
6
  import os from 'os';
7
  import { io } from "socket.io-client";
@@ -120,23 +122,74 @@ app.get('/ytdl/v1', async (req, res) => {
120
  const id = req.query.id;
121
 
122
  if (!id) {
123
- return res.status(400).send('Parameter "id" is required.');
124
  }
125
-
126
  try {
127
- const response = await fetch(`https://api.allorigins.win/raw?url=https://ytdlp.online/stream?command=https://www.youtube.com/watch?v=${id} -j`, {
128
- timeout: 1000,
129
- cache: 'no-store'
130
- });
131
- if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
132
- const responseText = await response.text();
133
- const urls = responseText.split('\n')
134
- .filter(line => line.trim().startsWith('data:'))
135
- .map(line => line.substring(5).trim());
136
- res.json(urls);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
137
  } catch (error) {
138
  console.error(error);
139
- res.status(500).send('Something went wrong while processing the request.');
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
140
  }
141
  });
142
 
 
2
  import { chromium } from 'playwright';
3
  import cors from 'cors';
4
  import axios from 'axios';
5
+ import fetch from 'node-fetch';
6
+ import * as cheerio from 'cheerio';
7
  import dotenv from 'dotenv';
8
  import os from 'os';
9
  import { io } from "socket.io-client";
 
122
  const id = req.query.id;
123
 
124
  if (!id) {
125
+ return res.status(400).json({ error: 'Parameter "id" is required.' });
126
  }
127
+
128
  try {
129
+ const response = await fetch(
130
+ `https://api.allorigins.win/raw?url=https://ytdlp.online/stream?command=https://www.youtube.com/watch?v=${id} -j`,
131
+ {
132
+ timeout: 1000,
133
+ cache: 'no-store'
134
+ }
135
+ );
136
+
137
+ if (!response.ok) {
138
+ throw new Error(`HTTP error! Status: ${response.status}`);
139
+ }
140
+
141
+ const responseText = await response.text();
142
+ const parsedData = responseText
143
+ .split('\n')
144
+ .map(line => line.trim().substring(5).trim())
145
+ .filter(line => {
146
+ try {
147
+ JSON.parse(line);
148
+ return true;
149
+ } catch {
150
+ return false;
151
+ }
152
+ })
153
+ .map(line => JSON.parse(line));
154
+
155
+ if (parsedData.length === 0) {
156
+ return res.status(404).json({ error: 'No valid data found.' });
157
+ }
158
+
159
+ res.json(parsedData);
160
  } catch (error) {
161
  console.error(error);
162
+ res.status(500).json({ error: 'Something went wrong while processing the request.' });
163
+ }
164
+ });
165
+
166
+ app.get('/ytdl/v2', async (req, res) => {
167
+ const id = req.query.id;
168
+ if (!id) return res.status(400).json({ error: 'Parameter "id" is required.' });
169
+
170
+ try {
171
+ const response = await fetch(`https://api.allorigins.win/raw?url=https://ytdlp.online/stream?command=https://www.youtube.com/watch?v=${id}`);
172
+ if (!response.ok) throw new Error('Failed to fetch data');
173
+
174
+ const lastLine = response
175
+ .text()
176
+ .split('\n')
177
+ .map(line => line.trim().substring(5).trim())
178
+ .filter(Boolean)
179
+ .pop();
180
+
181
+ if (!lastLine) return res.status(404).json({ error: 'No valid data found.' });
182
+
183
+ const $ = cheerio.load(lastLine);
184
+ const downloadLink = $('a[href]')
185
+ .filter((_, el) => $(el).text().includes('Download File'))
186
+ .attr('href');
187
+
188
+ if (!downloadLink) return res.status(404).json({ error: 'No download link found.' });
189
+
190
+ res.json({ downloadLink: downloadLink.startsWith('/') ? `https://ytdlp.online${downloadLink}` : downloadLink });
191
+ } catch (error) {
192
+ res.status(500).json({ error: 'Something went wrong.' });
193
  }
194
  });
195