-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
207 lines (177 loc) · 4.64 KB
/
index.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
const EXPANSIONS = [
"author_id",
"in_reply_to_user_id",
"referenced_tweets.id",
"referenced_tweets.id.author_id",
"entities.mentions.username",
"attachments.poll_ids",
"attachments.media_keys",
"geo.place_id",
]
const USER_FIELDS = [
"created_at",
"description",
"entities",
"id",
"location",
"name",
"pinned_tweet_id",
"profile_image_url",
"protected",
"public_metrics",
"url",
"username",
"verified",
"withheld",
]
const TWEET_FIELDS = [
"attachments",
"author_id",
"context_annotations",
"conversation_id",
"created_at",
"entities",
"geo",
"id",
"in_reply_to_user_id",
"lang",
"public_metrics",
// "non_public_metrics", private
// "organic_metrics", private
// "promoted_metrics", private
"text",
"possibly_sensitive",
"referenced_tweets",
"reply_settings",
"source",
"withheld",
]
const MEDIA_FIELDS = [
"alt_text",
"duration_ms",
"height",
"media_key",
"preview_image_url",
"type",
"url",
"width",
// "non_public_metrics", private
// "organic_metrics", private
// "promoted_metrics", private
"public_metrics",
]
const POLL_FIELDS = ["duration_minutes", "end_datetime", "id", "options", "voting_status"]
const PLACE_FIELDS = [
"contained_within",
"country",
"country_code",
"full_name",
"geo",
"id",
"name",
"place_type",
]
EVERYTHING = {
"expansions": EXPANSIONS.join(","),
"user.fields": USER_FIELDS.join(","),
"tweet.fields": TWEET_FIELDS.join(","),
"media.fields": MEDIA_FIELDS.join(","),
"poll.fields": POLL_FIELDS.join(","),
"place.fields": PLACE_FIELDS.join(","),
}
function extractIncludes(response, expansion, _id="id") {
const includes = {}
if (response.includes && response.includes[expansion]) {
for (include of response.includes[expansion]) {
includes[include[_id]] = include
}
}
return includes
}
function flatten(response) {
// users need to be looked up by user and id
const includes_users = {
...extractIncludes(response, "users", "id"),
...extractIncludes(response, "users", "username"),
}
// Media is by media_key, not id
const includesMedia = extractIncludes(response, "media", "media_key")
const includesPolls = extractIncludes(response, "polls")
const includesPlaces = extractIncludes(response, "places")
// Tweets in includes will themselves be expanded
const includesTweets = extractIncludes(response, "tweets")
function expandPayload(payload) {
// Don't try to expand on primitive values, return strings as is:
if (["string", "boolean", "number"].indexOf(typeof(payload)) != -1) {
return payload
}
// expand list items individually:
else if (Array.isArray(payload)) {
return payload.map(item => expandPayload(item))
}
// Try to expand on dicts within dicts:
else if (typeof(payload) == "object") {
for (const key in payload) {
payload[key] = expandPayload(payload[key])
}
}
if (payload.author_id) {
payload.author = includes_users[payload.author_id]
}
if (payload.in_reply_to_user_id) {
payload.in_reply_to_user = includes_users[payload.in_reply_to_user_id]
}
if (payload.media_keys) {
payload.media = payload.media_keys.map(k => includesMedia[k])
}
if (payload.poll_ids && payload.poll_ids.length > 0) {
const poll_id = payload["poll_ids"][0]
payload.poll = includesPolls[poll_id]
}
if (payload.geo && payload.geo.place_id) {
const place_id = payload.geo.place_id
payload.geo = {
...payload.geo,
...includesPlaces[place_id]
}
}
if (payload.mentions) {
payload.mentions = payload.mentions.map(user => ({
...user,
...includes_users[user.username]
}))
}
if (payload.referenced_tweets) {
payload["referenced_tweets"] = payload.referenced_tweets.map(tweet => ({
...tweet,
...includesTweets[tweet.id]
}))
}
if (payload.pinned_tweet_id) {
payload.pinned_tweet = includesTweets[payload.pinned_tweet_id]
}
return payload
}
// expand the tweets in "includes", before processing actual result tweets
for (const [id, tweet] of Object.entries(extractIncludes(response, "tweets"))) {
includesTweets[id] = expandPayload(tweet)
}
// expand the list of tweets or an individual tweet in "data"
let tweets = []
if (response.data) {
const data = response["data"]
if (Array.isArray(data)) {
tweets = expandPayload(response["data"])
} else if (typeof(data) == "object") {
tweets = [expandPayload(response["data"])]
}
}
return {
...response,
data: tweets
}
}
module.exports = {
flatten,
EVERYTHING
}