coyotte508 HF Staff commited on
Commit
2b4727d
·
1 Parent(s): 85078f5

✨ Fetch only tweets we need

Browse files
Files changed (1) hide show
  1. server.ts +7 -9
server.ts CHANGED
@@ -34,6 +34,7 @@ interface TweetLookups {
34
  id: string;
35
  conversation_id: "string";
36
  text: string;
 
37
  attachments?: { media_keys: string[] };
38
  }>;
39
  includes: { media: Array<{ media_key: string; url: string }> };
@@ -57,20 +58,17 @@ async function lookupTweets() {
57
  const data: TweetMentions = await ff(`users/${BOT_ID}/mentions`);
58
 
59
  let lookups: TweetLookups = await ff(
60
- `tweets?ids=${data.data
61
- .map((t) => t.id)
62
- .join(
63
- ","
64
- )}&expansions=attachments.media_keys&media.fields=duration_ms,height,media_key,preview_image_url,public_metrics,type,url,width,alt_text`
65
  );
66
 
67
  if (!lastMention) {
68
- console.log("added mention", lookups.data[0].id);
69
- lastMention = lookups.data[0].id;
70
  return;
71
  }
72
- const tweets = lookups.data.filter((tweet) => tweet.attachments?.media_keys.length === 1 && tweet.id > lastMention);
73
- lastMention = lookups.data[0].id;
74
  console.log(lastMention);
75
 
76
  for (const tweet of tweets) {
 
34
  id: string;
35
  conversation_id: "string";
36
  text: string;
37
+ created_at: string;
38
  attachments?: { media_keys: string[] };
39
  }>;
40
  includes: { media: Array<{ media_key: string; url: string }> };
 
58
  const data: TweetMentions = await ff(`users/${BOT_ID}/mentions`);
59
 
60
  let lookups: TweetLookups = await ff(
61
+ `tweets?ids=${data.data.map((t) => t.id).join(",")}${
62
+ lastMention && `start_time=${new Date(new Date(lastMention).getTime() + 1).toJSON()}`
63
+ }&tweet.fields=created_at&expansions=attachments.media_keys&media.fields=duration_ms,height,media_key,preview_image_url,public_metrics,type,url,width,alt_text`
 
 
64
  );
65
 
66
  if (!lastMention) {
67
+ console.log("added mention", lookups.data[0].created_at);
68
+ lastMention = lookups.data[0].created_at;
69
  return;
70
  }
71
+ const tweets = lookups.data.filter((tweet) => tweet.attachments?.media_keys.length === 1);
 
72
  console.log(lastMention);
73
 
74
  for (const tweet of tweets) {