randydev commited on
Commit
523b92a
·
verified ·
1 Parent(s): c8a1d56

Update lib/twitter/twitter.js

Browse files
Files changed (1) hide show
  1. lib/twitter/twitter.js +12 -11
lib/twitter/twitter.js CHANGED
@@ -4,7 +4,8 @@ import { TwitterDlArgsSchema, TwitterDLResponseSchema, TwitterDlSchema } from '.
4
  import { DEFAULT_HEADERS } from '../constant.js';
5
  import { stringifyCookies, generateTokenId } from './util.js';
6
 
7
- export async function twitterdl(url) {
 
8
  TwitterDlArgsSchema.parse([url]);
9
 
10
  const idMatch = url.match(/status\/(\d+)/) || url.match(/(\d+)/);
@@ -22,20 +23,20 @@ export async function twitterdl(url) {
22
  }
23
  }).json();
24
 
25
- const json = TwitterDLResponseSchema.parse(data);
26
- const media = json.includes?.media?.find((m) => m.type === 'video');
27
-
28
- if (!media || !media.variants) {
29
- throw new Error("No video found in this tweet.");
30
  }
31
 
32
- const result = media.variants
33
- .filter((variant) => variant.content_type !== 'application/x-mpegURL')
34
- .filter(Boolean);
 
 
 
35
 
36
- return TwitterDlSchema.parse(result);
37
  } catch (error) {
38
  console.error("Error fetching Twitter video:", error.message);
39
  throw new Error("Failed to fetch Twitter video. Please try again later.");
40
  }
41
- }
 
4
  import { DEFAULT_HEADERS } from '../constant.js';
5
  import { stringifyCookies, generateTokenId } from './util.js';
6
 
7
+
8
+ export default async function twitterdl(url) {
9
  TwitterDlArgsSchema.parse([url]);
10
 
11
  const idMatch = url.match(/status\/(\d+)/) || url.match(/(\d+)/);
 
23
  }
24
  }).json();
25
 
26
+ if (!Array.isArray(data)) {
27
+ throw new Error("Unexpected response format: Expected an array.");
 
 
 
28
  }
29
 
30
+ const json = TwitterDLResponseSchema.parse({ includes: { media: data } });
31
+
32
+ const videos = json.includes.media
33
+ .filter((m) => m.type === 'video')
34
+ .flatMap((m) => m.variants)
35
+ .filter((v) => v.content_type !== 'application/x-mpegURL');
36
 
37
+ return TwitterDlSchema.parse(videos);
38
  } catch (error) {
39
  console.error("Error fetching Twitter video:", error.message);
40
  throw new Error("Failed to fetch Twitter video. Please try again later.");
41
  }
42
+ }