-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinkOperations.js
116 lines (109 loc) · 3.12 KB
/
linkOperations.js
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import needle from "needle";
const token = process.env.TOKEN;
console.log(token);
async function getTweets(link) {
const endpointUrl = "https://api.twitter.com/2/tweets/search/recent";
try {
const id = link.match("status/([0-9]+)*")[1];
const response = await getRequest(id, endpointUrl);
return response;
} catch (e) {
console.log(e);
}
}
async function getResponse(endpointUrl, params) {
const res = await needle("get", endpointUrl, params, {
headers: {
"User-Agent": "v2RecentSearchJS",
authorization: `Bearer ${token}`,
},
});
if (res) {
return res.body;
} else {
throw new Error("Unsuccessful request");
}
}
async function getUserFromTweetId(id) {
const params = {
ids: `${id}`,
"tweet.fields": "author_id,in_reply_to_user_id",
expansions: "author_id",
"user.fields": "username",
};
const res = await getResponse("https://api.twitter.com/2/tweets", params);
if (res.includes) {
return res.includes.users[0];
} else {
throw new Error("Bad link");
}
}
async function getPfp(username) {
const endPt = `https://api.twitter.com/2/users/by?usernames=${username}&user.fields=profile_image_url`;
const res = await needle("get", endPt, {
headers: {
"User-Agent": "v2RecentSearchJS",
authorization: `Bearer ${token}`,
},
});
return res.body.data[0].profile_image_url.slice(0, -10) + "bigger.jpg";
}
async function getImages(id) {
let images = [];
const params = {
ids: id,
expansions: "attachments.media_keys",
"media.fields": "url",
};
const endPoint = "https://api.twitter.com/2/tweets";
const resp = await getResponse(endPoint, params);
if (resp.includes) {
for (const mediaObj of resp.includes.media) {
if (mediaObj.url != undefined) images.push(mediaObj.url);
}
}
return images;
}
async function getRequest(id, endpointUrl) {
let images = [];
const user = await getUserFromTweetId(id);
const username = user.username;
const pfpUrl = await getPfp(username);
const userid = user.id;
const params = {
query: `from:${username} conversation_id:${id}`,
"tweet.fields": "author_id,conversation_id,in_reply_to_user_id,attachments",
"media.fields": "url,height,preview_image_url",
expansions: "attachments.media_keys",
max_results: 100,
};
const mParams = {
ids: `${id}`,
"tweet.fields": "author_id,conversation_id,in_reply_to_user_id,attachments",
"media.fields": "url,height,preview_image_url",
expansions: "attachments.media_keys",
};
let mainTweet = await getResponse(
"https://api.twitter.com/2/tweets",
mParams
);
mainTweet = mainTweet.data[0];
const resp = await getResponse(endpointUrl, params);
let filteredResp = resp.data.filter((response) => {
return response.in_reply_to_user_id === userid;
});
filteredResp = [...filteredResp, mainTweet];
filteredResp.reverse();
filteredResp = await Promise.all(
filteredResp.map(async (element) => {
element["images"] = await getImages(element.id);
return Promise.resolve(element);
})
);
return {
username,
pfpUrl,
filteredResp,
};
}
export default getTweets;