Update lib/twitter/twitter.js
Browse files- 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 |
-
|
|
|
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 |
-
|
26 |
-
|
27 |
-
|
28 |
-
if (!media || !media.variants) {
|
29 |
-
throw new Error("No video found in this tweet.");
|
30 |
}
|
31 |
|
32 |
-
const
|
33 |
-
|
34 |
-
|
|
|
|
|
|
|
35 |
|
36 |
-
return TwitterDlSchema.parse(
|
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 |
+
}
|