Aichatbot12321 commited on
Commit
4d30c3c
·
verified ·
1 Parent(s): 732a612

Upload server.js

Browse files
Files changed (1) hide show
  1. server.js +73 -6
server.js CHANGED
@@ -7291,18 +7291,85 @@ function parseYouTubeId(input) {
7291
  return (id && /^[A-Za-z0-9_-]{11}$/.test(id)) ? id : null;
7292
  }
7293
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7294
  async function verifyYouTube(id) {
 
7295
  try {
7296
  const r = await fetch(`https://www.youtube.com/oembed?url=${encodeURIComponent('https://www.youtube.com/watch?v=' + id)}&format=json`, { signal: AbortSignal.timeout(6000) });
7297
  if (r.status === 200) {
7298
  const d = await r.json();
7299
- return { valid: true, verified: true, id, title: d.title || '', author: d.author_name || '', thumb: `https://img.youtube.com/vi/${id}/hqdefault.jpg` };
 
 
7300
  }
7301
- if (r.status === 404 || r.status === 400) return { valid: false, error: "This video does not exist, is private or was removed." };
7302
- return { valid: true, verified: false, id, title: '', author: '', thumb: `https://img.youtube.com/vi/${id}/hqdefault.jpg` }; // 401/403 etc: embedding off, still downloadable
7303
- } catch (e) {
7304
- return { valid: true, verified: false, id, title: '', author: '', thumb: `https://img.youtube.com/vi/${id}/hqdefault.jpg` }; // network hiccup: do not block the user
7305
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7306
  }
7307
 
7308
  async function handleVerifyYoutube(req, res) {
 
7291
  return (id && /^[A-Za-z0-9_-]{11}$/.test(id)) ? id : null;
7292
  }
7293
 
7294
+ const MAX_DL_DURATION_MIN = parseInt(process.env.MAX_DL_DURATION_MIN) || 90;
7295
+ const MAX_DL_DURATION_SEC = MAX_DL_DURATION_MIN * 60;
7296
+
7297
+ function parseIsoDurationToSeconds(iso) {
7298
+ const m = String(iso || '').match(/P(?:(\d+)D)?T?(?:(\d+)H)?(?:(\d+)M)?(?:(\d+)S)?/i);
7299
+ if (!m) return 0;
7300
+ const d = parseInt(m[1] || '0', 10);
7301
+ const h = parseInt(m[2] || '0', 10);
7302
+ const min = parseInt(m[3] || '0', 10);
7303
+ const s = parseInt(m[4] || '0', 10);
7304
+ return d * 86400 + h * 3600 + min * 60 + s;
7305
+ }
7306
+
7307
+ async function getYouTubeDurationInfo(id) {
7308
+ const YOUTUBE_API_KEY = process.env.YOUTUBE_API_KEY;
7309
+ if (YOUTUBE_API_KEY) {
7310
+ try {
7311
+ const r = await fetch(`https://www.googleapis.com/youtube/v3/videos?part=contentDetails,snippet&id=${encodeURIComponent(id)}&key=${encodeURIComponent(YOUTUBE_API_KEY)}`, { signal: AbortSignal.timeout(6000) });
7312
+ if (r.ok) {
7313
+ const d = await r.json();
7314
+ const item = d && d.items && d.items[0];
7315
+ if (item) {
7316
+ const durationSeconds = parseIsoDurationToSeconds(item.contentDetails && item.contentDetails.duration);
7317
+ return {
7318
+ durationSeconds,
7319
+ title: (item.snippet && item.snippet.title) || '',
7320
+ author: (item.snippet && item.snippet.channelTitle) || ''
7321
+ };
7322
+ }
7323
+ }
7324
+ } catch (e) {}
7325
+ }
7326
+ try {
7327
+ const res = await Promise.race([
7328
+ ytSearch({ videoId: id }),
7329
+ new Promise((_, rej) => setTimeout(() => rej(new Error('timeout')), 6000))
7330
+ ]);
7331
+ if (res) {
7332
+ const durationSeconds = Number(res.seconds || (res.duration && res.duration.seconds) || 0);
7333
+ return {
7334
+ durationSeconds,
7335
+ title: res.title || '',
7336
+ author: (res.author && res.author.name) || ''
7337
+ };
7338
+ }
7339
+ } catch (e) {}
7340
+ return { durationSeconds: 0, title: '', author: '' };
7341
+ }
7342
+
7343
  async function verifyYouTube(id) {
7344
+ let base = { valid: true, verified: false, id, title: '', author: '', thumb: `https://img.youtube.com/vi/${id}/hqdefault.jpg` };
7345
  try {
7346
  const r = await fetch(`https://www.youtube.com/oembed?url=${encodeURIComponent('https://www.youtube.com/watch?v=' + id)}&format=json`, { signal: AbortSignal.timeout(6000) });
7347
  if (r.status === 200) {
7348
  const d = await r.json();
7349
+ base = { valid: true, verified: true, id, title: d.title || '', author: d.author_name || '', thumb: `https://img.youtube.com/vi/${id}/hqdefault.jpg` };
7350
+ } else if (r.status === 404 || r.status === 400) {
7351
+ return { valid: false, error: "This video does not exist, is private or was removed." };
7352
  }
7353
+ } catch (e) {}
7354
+
7355
+ // Enforce 90-minute maximum duration limit before launching any downloader browser
7356
+ try {
7357
+ const meta = await getYouTubeDurationInfo(id);
7358
+ if (meta.title && !base.title) base.title = meta.title;
7359
+ if (meta.author && !base.author) base.author = meta.author;
7360
+ if (meta.durationSeconds > MAX_DL_DURATION_SEC) {
7361
+ const mins = Math.ceil(meta.durationSeconds / 60);
7362
+ return {
7363
+ valid: false,
7364
+ tooLong: true,
7365
+ durationSeconds: meta.durationSeconds,
7366
+ error: `This video/music is ${mins} minutes long. Videos longer than ${MAX_DL_DURATION_MIN} minutes cannot be downloaded.`
7367
+ };
7368
+ }
7369
+ if (meta.durationSeconds > 0) base.durationSeconds = meta.durationSeconds;
7370
+ } catch (e) {}
7371
+
7372
+ return base;
7373
  }
7374
 
7375
  async function handleVerifyYoutube(req, res) {