This repository was archived by the owner on Feb 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtweet.ts
426 lines (353 loc) · 10.6 KB
/
tweet.ts
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
import Item from './item';
import TweetTextParser, {TweetTextToken} from '../tweet_parser';
import {Twitter} from 'twit';
const shell = global.require('electron').shell;
const re_normal_size = /normal(?=\.\w+$)/i;
const re_size_part = /_normal(?=\.\w+$)/i;
function truncateCount(count: number) {
if (count >= 1000000) {
return (Math.floor(count / 100000) / 10) + 'M';
} else if (count >= 1000) {
return (Math.floor(count / 100) / 10) + 'K';
} else if (count === 0) {
return '';
} else {
return count.toString();
}
}
export class TwitterUser {
constructor(public json: Twitter.User) {}
get original_icon_url() {
return this.json.profile_image_url.replace(re_size_part, '');
}
get icon_url() {
const url = this.json.profile_image_url;
if (window.devicePixelRatio < 1.5) {
return url;
} else {
return url.replace(re_normal_size, 'bigger');
}
}
get mini_icon_url() {
if (window.devicePixelRatio < 1.5) {
return this.icon_url_24x24;
} else {
return this.icon_url_48x48;
}
}
get icon_url_73x73() {
return this.json.profile_image_url.replace(re_normal_size, 'bigger');
}
get icon_url_48x48() {
return this.json.profile_image_url;
}
get icon_url_24x24() {
return this.json.profile_image_url.replace(re_normal_size, 'mini');
}
get screen_name() {
return this.json.screen_name;
}
get protected() {
return this.json.protected;
}
get name() {
return this.json.name;
}
get id() {
return this.json.id;
}
get max_size_banner_url() {
if (!this.json.profile_banner_url) {
return null;
}
// Note: Currently 1500x500 is biggest banner image.
return this.json.profile_banner_url + '/1500x500';
}
get big_banner_url() {
return this.getBannerUrl('web');
}
get mini_banner_url() {
return this.getBannerUrl('mobile');
}
get bg_color() {
return this.json.profile_background_color;
}
get description() {
return this.json.description;
}
get statuses_count() {
return truncateCount(this.json.statuses_count);
}
get likes_count() {
return truncateCount(this.json.favourites_count);
}
get followings_count() {
return this.json.friends_count;
}
get followers_count() {
return this.json.followers_count;
}
get link_color() {
return this.json.profile_link_color;
}
get user_site_url(): Twitter.UrlEntity | null {
if (this.json.entities &&
this.json.entities.url &&
this.json.entities.url.urls!.length > 0) {
return this.json.entities.url.urls![0];
} else if (this.json.url) {
// Note: fallback
return {
display_url: this.json.url,
expanded_url: this.json.url,
indices: null,
url: this.json.url,
};
} else {
return null;
}
}
get location() {
return this.json.location;
}
followingPageUrl() {
return `https://twitter.com/${this.json.screen_name}/following`;
}
followerPageUrl() {
return `https://twitter.com/${this.json.screen_name}/followers`;
}
likePageUrl() {
return `https://twitter.com/${this.json.screen_name}/likes`;
}
userPageUrl() {
return `https://twitter.com/${this.json.screen_name}`;
}
openUserPageInBrowser() {
shell.openExternal(this.userPageUrl());
}
openWebsiteInBrowser() {
const url = this.user_site_url;
if (url === null) {
return;
}
shell.openExternal(url.expanded_url);
}
private getBannerUrl(label: string) {
if (!this.json.profile_banner_url) {
return null;
}
const size = window.devicePixelRatio < 1.5 ? `/${label}` : `/${label}_retina`;
return this.json.profile_banner_url + size;
}
}
export default class Tweet implements Item {
public user: TwitterUser;
public related_statuses: Tweet[];
public in_reply_to_status: Tweet | null;
private retweeted_status_memo: Tweet | null;
private quoted_status_memo: Tweet | null;
private parsed_tokens_memo: TweetTextToken[] | null;
private created_at_memo: Date | null;
constructor(public json: Twitter.Status) {
this.user = new TwitterUser(json.user);
this.retweeted_status_memo = null;
this.quoted_status_memo = null;
this.parsed_tokens_memo = null;
this.created_at_memo = null;
this.related_statuses = [];
this.in_reply_to_status = null;
}
get id() {
return this.json.id_str;
}
get created_at() {
if (this.created_at_memo === null) {
const created_at = this.json.created_at;
if (created_at !== undefined) {
this.created_at_memo = new Date(created_at);
}
}
return this.created_at_memo!;
}
get retweeted() {
return this.json.retweeted;
}
get favorited() {
return this.json.favorited;
}
get retweeted_status() {
if (!this.json.retweeted_status) {
return null;
}
if (this.retweeted_status_memo === null) {
this.retweeted_status_memo = new Tweet(this.json.retweeted_status);
}
return this.retweeted_status_memo;
}
get retweet_count() {
if (this.json.retweeted_status) {
return this.json.retweeted_status.retweet_count;
} else {
return this.json.retweet_count;
}
}
get favorite_count() {
if (this.json.retweeted_status) {
return this.json.retweeted_status.favorite_count;
} else {
return this.json.favorite_count;
}
}
get quoted_status() {
if (!this.json.quoted_status) {
return null;
}
if (this.quoted_status_memo === null) {
this.quoted_status_memo = new Tweet(this.json.quoted_status);
}
return this.quoted_status_memo;
}
get parsed_tokens() {
if (this.parsed_tokens_memo === null) {
const parser = new TweetTextParser(this.json);
this.parsed_tokens_memo = parser.parse();
this.json.text = parser.text;
}
return this.parsed_tokens_memo;
}
get in_reply_to_status_id() {
return this.json.in_reply_to_status_id_str;
}
get in_reply_to_user_id() {
return this.json.in_reply_to_user_id;
}
get text() {
return this.json.text;
}
get urls() {
const json = this.getMainJson();
if (!json.entities || !json.entities.urls) {
return [];
}
return json.entities.urls;
}
get media() {
if (!this.json.extended_entities) {
return [];
}
if (!this.json.extended_entities.media) {
return [];
}
return this.json.extended_entities.media;
}
get mentions() {
if (!this.json.entities || !this.json.entities.user_mentions) {
return [];
}
return this.json.entities.user_mentions;
}
get hashtags() {
if (!this.json.entities || !this.json.entities.hashtags) {
return [];
}
return this.json.entities.hashtags;
}
getMainStatus() {
if (this.json.retweeted_status) {
return this.retweeted_status!;
} else {
return this;
}
}
getMainJson(): Twitter.Status {
return this.json.retweeted_status || this.json;
}
isRetweet() {
return !!this.json.retweeted_status;
}
isQuotedTweet() {
return !!this.json.quoted_status;
}
getRetweetedUser() {
if (!this.json.retweeted_status) {
return null;
}
return this.user;
}
hasQuote() {
return !!this.json.quoted_status;
}
hasInReplyTo() {
return !!this.json.in_reply_to_status_id_str;
}
mentionsTo(user: TwitterUser) {
const my_id = user.id;
if (this.json.in_reply_to_user_id === my_id) {
return true;
}
if (this.json.retweeted_status && this.json.retweeted_status.user.id === my_id) {
return true;
}
if (this.json.quoted_status && this.json.quoted_status.user.id === my_id) {
return true;
}
if (this.json.entities && this.json.entities.user_mentions) {
for (const m of this.json.entities.user_mentions) {
if (m.id === my_id) {
return true;
}
}
}
return false;
}
getCreatedAtString() {
const date = this.created_at;
const hh = date.getHours();
const mm = date.getMinutes();
const m = date.getMonth();
const d = date.getDate();
const yyyy = date.getFullYear();
return `${('0' + hh).slice(-2)}:${('0' + mm).slice(-2)} ${m + 1}/${d} ${yyyy}`;
}
statusPageUrl() {
return `https://twitter.com/${this.json.user.screen_name}/status/${this.getMainStatus().json.id_str}`;
}
openStatusPageInBrowser() {
shell.openExternal(this.statusPageUrl());
}
openAllLinksInBrowser() {
for (const u of this.urls.map(u => u.expanded_url)) {
shell.openExternal(u);
}
}
getChainedRelatedStatuses() {
const push = Array.prototype.push;
let ret = [] as Tweet[];
for (const s of this.related_statuses) {
push.apply(ret, s.getChainedRelatedStatuses());
}
push.apply(ret, this.related_statuses);
return ret.sort((l, r) => -l.compareId(r));
}
// -1: lhs.id < rhs.id
// 0: lhs.id == rhs.id
// 1: lhs.id > rhs.id
compareId(rhs: Tweet) {
const s = Math.floor(this.json.id_str.length / 2);
const lh = parseInt(this.json.id_str.slice(0, s), 10);
const rh = parseInt(rhs.json.id_str.slice(0, s), 10);
let v = lh - rh;
if (v === 0) {
const ll = parseInt(this.json.id_str.slice(s), 10);
const rl = parseInt(rhs.json.id_str.slice(s), 10);
v = ll - rl;
}
return v < 0 ? -1 : v > 0 ? 1 : 0;
}
clone() {
const cloned = new Tweet(this.json);
cloned.related_statuses = this.related_statuses;
cloned.in_reply_to_status = this.in_reply_to_status;
return cloned;
}
}