coyotte508 HF Staff commited on
Commit
9920173
·
1 Parent(s): 987f2db

✨ Extract image from tweet

Browse files
Files changed (1) hide show
  1. server.ts +17 -7
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 firstTweetId = data.data[0].id;
 
 
 
 
 
 
76
 
77
- const conversation: TweetLookups = await ff(`tweets?ids=${firstTweetId}&tweet.fields=conversation_id`);
 
 
 
78
 
79
- const conversation_id = conversation.data[0].conversation_id;
80
 
81
- const tweets = await ff(
82
- `tweets/search/recent?query=conversation_id:${conversation_id}&tweet.fields=in_reply_to_user_id,author_id,created_at,conversation_id&expansions=in_reply_to_user_id`
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() {