Commit
·
9920173
1
Parent(s):
987f2db
✨ Extract image from tweet
Browse files
server.ts
CHANGED
|
@@ -54,7 +54,9 @@ interface TweetLookups {
|
|
| 54 |
id: string;
|
| 55 |
conversation_id: "string";
|
| 56 |
text: string;
|
|
|
|
| 57 |
}>;
|
|
|
|
| 58 |
}
|
| 59 |
|
| 60 |
async function ff(url: string) {
|
|
@@ -72,16 +74,24 @@ async function ff(url: string) {
|
|
| 72 |
async function lookupTweets() {
|
| 73 |
const data: TweetMentions = await ff(`users/${BOT_ID}/mentions`);
|
| 74 |
|
| 75 |
-
const
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
-
const
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
-
|
| 80 |
|
| 81 |
-
const
|
| 82 |
-
|
| 83 |
-
);
|
| 84 |
-
debug(tweets);
|
| 85 |
}
|
| 86 |
|
| 87 |
async function listen() {
|
|
|
|
| 54 |
id: string;
|
| 55 |
conversation_id: "string";
|
| 56 |
text: string;
|
| 57 |
+
attachments?: { media_keys: string[] };
|
| 58 |
}>;
|
| 59 |
+
includes: { media: Array<{ media_key: string; url: string }> };
|
| 60 |
}
|
| 61 |
|
| 62 |
async function ff(url: string) {
|
|
|
|
| 74 |
async function lookupTweets() {
|
| 75 |
const data: TweetMentions = await ff(`users/${BOT_ID}/mentions`);
|
| 76 |
|
| 77 |
+
const lookups: TweetLookups = await ff(
|
| 78 |
+
`tweets?ids=${data.data
|
| 79 |
+
.map((t) => t.id)
|
| 80 |
+
.join(
|
| 81 |
+
","
|
| 82 |
+
)}&expansions=attachments.media_keys&media.fields=duration_ms,height,media_key,preview_image_url,public_metrics,type,url,width,alt_text`
|
| 83 |
+
);
|
| 84 |
|
| 85 |
+
const tweetWithImage = lookups.data.find((tweet) => tweet.attachments?.media_keys);
|
| 86 |
+
const imageUrl = lookups.includes.media.find(
|
| 87 |
+
(media) => media.media_key === tweetWithImage?.attachments!.media_keys[0]
|
| 88 |
+
)?.url!;
|
| 89 |
|
| 90 |
+
console.log("imageUrl", imageUrl);
|
| 91 |
|
| 92 |
+
const image = await (await fetch(imageUrl)).arrayBuffer();
|
| 93 |
+
|
| 94 |
+
console.log(image);
|
|
|
|
| 95 |
}
|
| 96 |
|
| 97 |
async function listen() {
|