-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.11ty.js
400 lines (353 loc) · 13.9 KB
/
index.11ty.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
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
const swearjar = require("swearjar");
const metadata = require("./_data/metadata.js");
const Twitter = require("./src/twitter");
const EmojiAggregator = require( "./src/EmojiAggregator" );
const dataSource = require("./src/DataSource");
class Index extends Twitter {
data() {
return {
layout: "layout.11ty.js"
};
}
getTopUsersToRetweets(tweets) {
let users = {};
for(let tweet of tweets) {
if(!this.isRetweet(tweet)) {
continue;
}
if(tweet.entities && tweet.entities.user_mentions && tweet.entities.user_mentions[0]) {
let username = tweet.entities.user_mentions[0].screen_name;
if(!users[username]) {
users[username] = {
count: 0,
username: username
};
}
users[username].count++;
}
}
return Object.values(users).sort((a, b) => b.count - a.count);
}
getTopReplies(tweets) {
let counts = {};
for( let tweet of tweets ) {
let username = tweet.in_reply_to_screen_name;
if(username && username !== metadata.username) {
if(!counts[username]) {
counts[username] = {
count: 0,
username: username
};
}
counts[username].count++;
}
}
return Object.values(counts).sort((a, b) => b.count - a.count);
}
getTopMentions(tweets) {
let counts = {};
for( let tweet of tweets ) {
if(this.isMention(tweet)) {
let username = tweet.full_text.trim().split(" ").shift();
username = username.substr(1);
if(username && username !== metadata.username) {
if(!counts[username]) {
counts[username] = {
count: 0,
username: username
};
}
counts[username].count++;
}
}
}
return Object.values(counts).sort((a, b) => b.count - a.count);
}
renderSwearWord(word) {
return word.split("").map((letter, index) => index === 1 ? "_" : letter).join("");
}
getSwearWordsFromText(text) {
let splits = swearjar.censor(text).split(/([\*]+)/g);
let swears = {};
let index = 0;
for(let split of splits) {
if( split.length > 1 && split.match(/[\*]+/) ) {
let word = text.substr(index, split.length).toLowerCase();
if(!swears[word]) {
swears[word] = 0;
}
swears[word]++;
}
index += split.length;
}
return swears;
}
//, includeReplies = true
getTopSwearWords(tweets = []) {
let words = {};
tweets.filter(tweet => {
return !this.isRetweet(tweet) && swearjar.profane(tweet.full_text);
}).forEach(tweet => {
let swears = this.getSwearWordsFromText(tweet.full_text);
for(let swear in swears) {
if(!words[swear]) {
words[swear] = {
count: 0,
word: swear,
tweets: []
};
}
words[swear].count += swears[swear];
words[swear].tweets.push(tweet);
}
});
return Object.values(words).sort((a, b) => {
return b.count - a.count;
});
}
getHashTagsFromText(text = "") {
let words = {};
let splits = text.split(/(\#[A-Za-z][^\s\.\'\"\!\,\?\;\}\{]*)/g);
for(let split of splits) {
if(split.startsWith("#")) {
let tag = split.substr(1).toLowerCase();
if(!words[tag]) {
words[tag] = 0;
}
words[tag]++;
}
}
return words;
}
getTopHashTags(tweets = []) {
let words = {};
tweets.filter(tweet => {
return !this.isRetweet(tweet) && tweet.full_text.indexOf("#");
}).forEach(tweet => {
let tags = this.getHashTagsFromText(tweet.full_text);
for(let tag in tags) {
if(!words[tag]) {
words[tag] = {
count: 0,
tag: tag,
tweets: []
};
}
words[tag].count += tags[tag];
words[tag].tweets.push(tweet);
}
});
return Object.values(words).sort((a, b) => {
return b.count - a.count;
});
}
getAllLinks(tweets = []) {
let links = [];
for(let tweet of tweets) {
let tweetLinks = this.getLinkUrls(tweet);
for(let link of tweetLinks) {
links.push(link);
}
}
return links;
}
getTopHosts(tweets = []) {
let topHosts = {};
for(let tweet of tweets) {
let links = this.getLinkUrls(tweet);
for(let entry of links) {
if(!topHosts[entry.host]) {
topHosts[entry.host] = Object.assign({
count: 0
}, entry);
}
topHosts[entry.host].count++;
}
}
let arr = [];
for(let entry in topHosts) {
arr.push(topHosts[entry]);
}
return arr.sort((a, b) => b.count - a.count);
}
getTopDomains(tweets = []) {
let topDomains = {};
for(let tweet of tweets) {
let links = this.getLinkUrls(tweet);
for(let entry of links) {
if(!topDomains[entry.domain]) {
topDomains[entry.domain] = Object.assign({
count: 0
}, entry);
}
topDomains[entry.domain].count++;
}
}
let arr = [];
for(let entry in topDomains) {
arr.push(topDomains[entry]);
}
return arr.sort((a, b) => b.count - a.count);
}
async render(data) {
let {transform: twitterLink} = await import("@tweetback/canonical");
let tweets = await dataSource.getAllTweets();
let last12MonthsTweets = tweets.filter(tweet => tweet.date - new Date(Date.now() - 1000*60*60*24*365) > 0);
let tweetCount = tweets.length;
let retweetCount = tweets.filter(tweet => this.isRetweet(tweet)).length;
let noRetweetsTweetCount = tweets.length - retweetCount;
let replyCount = tweets.filter(tweet => this.isReply(tweet)).length;
let mentionNotReplyCount = tweets.filter(tweet => this.isMention(tweet)).length;
// let ambiguousReplyMentionCount = tweets.filter(tweet => this.isAmbiguousReplyMention(tweet)).length;
let retweetsEarnedCount = tweets.filter(tweet => !this.isRetweet(tweet)).reduce((accumulator, tweet) => accumulator + parseInt(tweet.retweet_count, 10), 0);
let likesEarnedCount = tweets.filter(tweet => !this.isRetweet(tweet)).reduce((accumulator, tweet) => accumulator + parseInt(tweet.favorite_count, 10), 0);
let topSwears = this.getTopSwearWords(tweets);
let swearCount = topSwears.reduce((accumulator, obj) => accumulator + obj.count, 0);
let tweetSwearCount = topSwears.reduce((accumulator, obj) => accumulator + obj.tweets.length, 0);
let topHashes = this.getTopHashTags(tweets);
let hashCount = topHashes.reduce((accumulator, obj) => accumulator + obj.count, 0);
let tweetHashCount = topHashes.reduce((accumulator, obj) => accumulator + obj.tweets.length, 0);
const emoji = new EmojiAggregator();
for(let tweet of tweets) {
if( !this.isRetweet(tweet) ) {
emoji.add(tweet);
}
}
let emojis = emoji.getSorted();
let mostRecentTweets = tweets.filter(tweet => this.isOriginalPost(tweet)).sort(function(a,b) {
return b.date - a.date;
}).slice(0, 15);
let recentTweetsHtml = await Promise.all(mostRecentTweets.map(tweet => this.renderTweet(tweet)));
let mostPopularTweetsHtml = await Promise.all(this.getMostPopularTweets(tweets).slice(0, 6).map(tweet => this.renderTweet(tweet, { showPopularity: true })));
let links = this.getAllLinks(tweets);
let linksCount = links.length;
let httpsLinksCount = links.filter(entry => entry.origin.startsWith("https:")).length;
let links12Months = this.getAllLinks(last12MonthsTweets);
let linksCount12Months = links12Months.length;
let httpsLinksCount12Months = links12Months.filter(entry => entry.origin.startsWith("https:")).length;
return `
<h2 class="tweets-primary-count">
<span class="tweets-primary-count-num">${this.renderNumber(tweetCount)}</span> tweet${tweetCount !== 1 ? "s" : ""}
</h2>
<form class="js" method="get" id="search-url">
<h2>Search for <label for="tweet-url">Tweet URL</label>:</h2>
<div class="tweets-search">
<div class="lo" style="--lo-margin-h: 1em; align-items: center;">
<div class="lo-c" style="flex-grow: 100">
<input type="url" id="tweet-url" required placeholder="Tweet URL" style="width: 100%">
</div>
<div class="lo-c" style="flex-grow: .001;">
<button type="submit">Search</button>
</div>
</div>
</div>
</form>
<div>
<h2><a href="/recent/">Recent:</a></h2>
<div class="twtr-sentiment twtr-sentiment-max js">
<div class="twtr-sentiment-chart ct-chart"></div>
<div class="twtr-sentiment-label">
⬅️ New
<span>⬆️ 🙂<br>⬇️ 🙁</span>
</div>
</div>
<ol class="tweets tweets-linear-list" id="tweets-recent-home">
${recentTweetsHtml.join("")}
</ol>
</div>
<div>
<h2><a href="/popular/">Popular:</a></h2>
<ol class="tweets tweets-linear-list">
${mostPopularTweetsHtml.join("")}
</ol>
</div>
<h2 id="retweets">I’ve retweeted other tweets ${this.renderNumber(retweetCount)} times (${this.renderPercentage(retweetCount, tweetCount)})</h2>
<div class="lo" style="--lo-stackpoint: 20em">
<div class="lo-c">
<h3>Most Retweeted</h3>
<ol>
${this.getTopUsersToRetweets(tweets).slice(0, 10).map(user => `<li><a href="${twitterLink(`https://twitter.com/${user.username}`)}">${user.username}</a> ${user.count} retweet${user.count != 1 ? "s" : ""}</li>`).join("")}
</ol>
</div>
<div class="lo-c">
<h3>Most Retweeted (Last 12 months)</h3>
<ol>
${this.getTopUsersToRetweets(last12MonthsTweets).slice(0, 10).map(user => `<li><a href="${twitterLink(`https://twitter.com/${user.username}`)}">${user.username}</a> ${user.count} retweet${user.count != 1 ? "s" : ""}</li>`).join("")}
</ol>
</div>
</div>
<h2 id="replies">Replies and Mentions</h2>
<h3>${this.renderPercentage(replyCount, tweetCount)} of my tweets are replies (×${this.renderNumber(replyCount)})</h3>
<div class="lo" style="--lo-stackpoint: 20em">
<div class="lo-c">
<h4>Most Replies To</h4>
<ol>
${this.getTopReplies(tweets).slice(0, 5).map(user => `<li><a href="${twitterLink(`https://twitter.com/${user.username}`)}">${user.username}</a> ${user.count} repl${user.count != 1 ? "ies" : "y"}</li>`).join("")}
</ol>
</div>
<div class="lo-c">
<h4>Most Replies To (Last 12 months)</h4>
<ol>
${this.getTopReplies(last12MonthsTweets).slice(0, 5).map(user => `<li><a href="${twitterLink(`https://twitter.com/${user.username}`)}">${user.username}</a> ${user.count} repl${user.count != 1 ? "ies" : "y"}</li>`).join("")}
</ol>
</div>
</div>
<h3>I’ve sent someone a mention ${this.renderNumber(mentionNotReplyCount)} times (${this.renderPercentage(mentionNotReplyCount, tweetCount)})</h3>
<h2 id="links">Most Frequent Sites I’ve Linked To</h2>
<h3>${this.renderPercentage(httpsLinksCount, linksCount)} of the links I’ve posted are using the <code>https:</code> protocol (${this.renderNumber(httpsLinksCount)} of ${this.renderNumber(linksCount)})</h3>
<h3>${this.renderPercentage(httpsLinksCount12Months, linksCount12Months)} of the links I’ve posted in the last 12 months are using the <code>https:</code> protocol (${this.renderNumber(httpsLinksCount12Months)} of ${this.renderNumber(linksCount12Months)})</h3>
<div class="lo" style="--lo-stackpoint: 20em">
<div class="lo-c">
<h4>Top Domains</h4>
<ol>
${this.getTopDomains(tweets).slice(0, 10).map(entry => `<li><a href="https://${entry.domain}">${entry.domain}</a> ${entry.count} tweets</li>`).join("")}
</ol>
</div>
<div class="lo-c">
<h4>Top Hosts</h4>
<ol>
${this.getTopHosts(tweets).slice(0, 10).map(entry => `<li><a href="https://${entry.host}">${entry.host}</a> ${entry.count} tweets</li>`).join("")}
</ol>
</div>
</div>
<h2 id="shared">My tweets have been given about <span class="tag tag-lite tag-retweet">♻️ ${this.renderNumber(retweetsEarnedCount)}</span> retweets and <span class="tag tag-lite tag-favorite">❤️ ${this.renderNumber(likesEarnedCount)}</span> likes</h2>
<h2 id="emoji">Top 5 Emoji Used in Tweets</h2>
<ol>
${emojis.slice(0, 5).map(obj => `<li>${obj.glyph} used ${obj.count} times on ${obj.tweetcount} tweets</li>`).join("")}
</ol>
<p><em>${this.renderNumber(emojis.length)} unique emoji on ${this.renderNumber(emoji.getTweetCount())} tweets (${this.renderPercentage(emoji.getTweetCount(), noRetweetsTweetCount)} of all tweets***)</em></p>
<h2 id="hashtags">Top 5 Hashtags</h2>
<ol>
${topHashes.slice(0, 5).map(hash => `<li><code>${hash.tag}</code> used ${hash.count} times ${hash.count > 1 && hash.count > hash.tweets.length ? `on ${hash.tweets.length} tweet${hash.tweets.length !== 1 ? "s" : ""}` : ""}</li>`).join("")}
</ol>
<p><em>${this.renderNumber(hashCount)} hashtags on ${this.renderNumber(tweetHashCount)} tweets (${this.renderPercentage(tweetHashCount, noRetweetsTweetCount)} of all tweets***)</em></p>
<h2 id="swears">Top 5 Swear Words</h2>
<ol>
${topSwears.slice(0, 5).map(swear => `<li><code>${this.renderSwearWord(swear.word)}</code> used ${swear.count} times ${swear.count > 1 && swear.count > swear.tweets.length ? `on ${swear.tweets.length} tweet${swear.tweets.length !== 1 ? "s" : ""}` : ""}</li>`).join("")}
</ol>
<p><em>${this.renderNumber(swearCount)} swear words on ${this.renderNumber(tweetSwearCount)} tweets (${this.renderPercentage(tweetSwearCount, noRetweetsTweetCount)} of all tweets***)</em></p>
<p>***: does not include retweets</p>
<script>
var searchForm = document.getElementById("search-url");
if(searchForm) {
searchForm.addEventListener("submit", function(e) {
e.preventDefault();
var urlInput = searchForm.querySelector('input[type="url"]');
if(urlInput && urlInput.value) {
var tweetIdMatch = urlInput.value.match(/\\/(\\d+)/);
if(tweetIdMatch && tweetIdMatch.length) {
document.location.href = "/" + tweetIdMatch[1] + "/";
}
}
}, false);
}
var series = getSentimentsFromList( '#tweets-recent-home' );
makeSentimentChart( '.twtr-sentiment-chart', series );
</script>
`;
// <h3>Before 2012, it was not possible to tell the difference between a mention and reply. This happened ${this.renderNumber(ambiguousReplyMentionCount)} times (${this.renderPercentage(ambiguousReplyMentionCount, tweetCount)})</h3>
// <h3>I’ve sent someone a mention ${this.renderNumber(mentionNotReplyCount)} times (${this.renderPercentage(mentionNotReplyCount, tweetCount)})</h3>
// <p>Mentions are tweets sent to a single person but not as a reply to an existing tweet. Note that this number is overinflated for old data—Twitter didn’t support official replies before July 2012.</p>
}
}
module.exports = Index;