Repository navigation
Expand file tree
/
Copy pathtweets.ts
More file actions
66 lines (57 loc) · 2.05 KB
/
Copy pathtweets.ts
File metadata and controls
66 lines (57 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import qs from 'qs';
import { RawTweetType, TransformedTweet, TweetData } from '../types/tweet';
export const getTweets = async (ids: string[]) => {
const queryParams = qs.stringify({
ids: ids.join(','),
expansions:
'author_id,attachments.media_keys,referenced_tweets.id,referenced_tweets.id.author_id',
'tweet.fields':
'attachments,author_id,public_metrics,created_at,id,in_reply_to_user_id,referenced_tweets,text',
'user.fields': 'id,name,profile_image_url,protected,url,username,verified',
'media.fields':
'duration_ms,height,media_key,preview_image_url,type,url,width,public_metrics',
});
const response = await fetch(
`https://api.twitter.com/2/tweets?${queryParams}`,
{
headers: {
Authorization: `Bearer ${process.env.TWITTER_API_KEY}`,
},
}
);
const tweets = (await response.json()) as RawTweetType;
const getAuthorInfo = (author_id: string) => {
return tweets.includes.users.find((user) => user.id === author_id)!;
};
const getReferencedTweets = (mainTweet: TweetData) => {
return (
mainTweet?.referenced_tweets?.map((referencedTweet) => {
const fullReferencedTweet = tweets.includes.tweets.find(
(tweet) => tweet.id === referencedTweet.id
)!;
return {
...fullReferencedTweet,
type: referencedTweet.type,
author: getAuthorInfo(fullReferencedTweet.author_id),
};
}) || []
);
};
return tweets.data.reduce(
(allTweets: Record<string, TransformedTweet>, tweet: TweetData) => {
const tweetWithAuthor = {
...tweet,
media:
tweet?.attachments?.media_keys.map((key) =>
tweets.includes.media.find((media) => media.media_key === key)
) || [],
referenced_tweets: getReferencedTweets(tweet),
author: getAuthorInfo(tweet.author_id),
};
// @ts-ignore @MaximeHeckel: somehow media types are conflicting
allTweets[tweet.id] = tweetWithAuthor;
return allTweets;
},
{} as Record<string, TransformedTweet>
);
};