-
Notifications
You must be signed in to change notification settings - Fork 1
/
twitter.ts
156 lines (149 loc) · 6.07 KB
/
twitter.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
import { config, cosmicSync } from "@anandchowdhary/cosmic";
import dayjs from "dayjs";
import week from "dayjs/plugin/weekOfYear";
import { lstat, pathExists, readdir, readJson } from "fs-extra";
import { join } from "path";
import Twitter from "twitter-lite";
import { integrationConfig, write } from "../common";
import type { Integration } from "../integration";
dayjs.extend(week);
cosmicSync("stethoscope");
const client = new Twitter({
consumer_key: config("twitterApiKey") || "",
consumer_secret: config("twitterApiSecretKey") || "",
access_token_key: config("twitterAccessToken") || "",
access_token_secret: config("twitterAccessTokenSecret") || "",
});
interface Tweet {
created_at: string;
id_string: string;
text: string;
}
const getRecentTweets = async (count: number = 100) => {
if (integrationConfig("twitter", "tweets")) {
const response: Array<Tweet> = await client.get("statuses/user_timeline", {
screen_name: config("twitterScreenName") || "",
count,
});
const itemsByDate: { [index: string]: Tweet[] } = {};
for await (const item of response) {
const date = dayjs(item.created_at);
const year = date.format("YYYY");
const month = date.format("MM");
const day = date.format("DD");
itemsByDate[`${year}/${month}/${day}`] = itemsByDate[`${year}/${month}/${day}`] || [];
itemsByDate[`${year}/${month}/${day}`].push(item);
}
for await (const key of Object.keys(itemsByDate)) {
await write(
join(".", "data", "twitter-tweets", "daily", key, "tweets.json"),
JSON.stringify(itemsByDate[key], null, 2)
);
}
}
if (integrationConfig("twitter", "likes")) {
const response: Array<Tweet> = await client.get("favorites/list", {
screen_name: config("twitterScreenName") || "",
count,
});
const itemsByDate: { [index: string]: Tweet[] } = {};
for await (const item of response) {
const date = dayjs(item.created_at);
const year = date.format("YYYY");
const month = date.format("MM");
const day = date.format("DD");
itemsByDate[`${year}/${month}/${day}`] = itemsByDate[`${year}/${month}/${day}`] || [];
itemsByDate[`${year}/${month}/${day}`].push(item);
}
for await (const key of Object.keys(itemsByDate)) {
await write(
join(".", "data", "twitter-likes", "daily", key, "likes.json"),
JSON.stringify(itemsByDate[key], null, 2)
);
}
}
};
export default class TwitterI implements Integration {
name = "twitter";
cli = {};
async update() {
console.log("Twitter: Starting...");
await getRecentTweets();
console.log("Twitter: Added data");
console.log("Twitter: Added daily summaries");
}
async legacy() {}
async summary() {
for await (const typeName of ["twitter-tweets", "twitter-likes"]) {
if (
(await pathExists(join(".", "data", typeName, "daily"))) &&
(await lstat(join(".", "data", typeName, "daily"))).isDirectory()
) {
const years = (await readdir(join(".", "data", typeName, "daily"))).filter((i) => /^\d+$/.test(i));
const yearData: { [index: string]: number } = {};
const weeklyData: {
[index: string]: {
[index: string]: { [index: string]: number };
};
} = {};
for await (const year of years) {
let yearlySum = 0;
const monthlyData: { [index: string]: number } = {};
[...Array(13).keys()].slice(1).forEach((val) => (monthlyData[val.toString()] = 0));
const months = (await readdir(join(".", "data", typeName, "daily", year))).filter((i) => /^\d+$/.test(i));
for await (const month of months) {
let monthlySum = 0;
const dailyData: { [index: string]: number } = {};
[...Array(dayjs(`${year}-${month}-10`).daysInMonth()).keys()]
.slice(1)
.forEach((val) => (dailyData[val.toString()] = 0));
const days = (await readdir(join(".", "data", typeName, "daily", year, month))).filter((i) =>
/^\d+$/.test(i)
);
for await (const day of days) {
let json = await readJson(
join(".", "data", typeName, "daily", year, month, day, `${typeName.split("-").pop()}.json`)
);
let dailySum = json.length;
if (dailySum) dailyData[parseInt(day)] = dailySum;
monthlySum += dailySum;
yearlySum += dailySum;
Object.keys(dailyData).forEach((key) => {
const weekNumber = dayjs(`${year}-${month}-${key}`).week();
weeklyData[year] = weeklyData[year] || {};
weeklyData[year][weekNumber] = weeklyData[year][weekNumber] || {};
weeklyData[year][weekNumber][`${year}-${month}-${key}`] = dailyData[key];
});
}
if (Object.keys(dailyData).length)
await write(
join(".", "data", typeName, "summary", "days", year, `${month}.json`),
JSON.stringify(dailyData, null, 2)
);
if (monthlySum) monthlyData[parseInt(month)] = monthlySum;
}
if (Object.keys(monthlyData).length)
await write(
join(".", "data", typeName, "summary", "months", `${year}.json`),
JSON.stringify(monthlyData, null, 2)
);
if (yearlySum) yearData[parseInt(year)] = yearlySum;
}
if (Object.keys(yearData).length)
await write(join(".", "data", typeName, "summary", "years.json"), JSON.stringify(yearData, null, 2));
for await (const year of Object.keys(weeklyData)) {
for await (const week of Object.keys(weeklyData[year])) {
if (
Object.keys(weeklyData[year][week]).length &&
Object.values(weeklyData[year][week]).reduce((a, b) => a + b, 0)
)
await write(
join(".", "data", typeName, "summary", "weeks", year, `${week}.json`),
JSON.stringify(weeklyData[year][week], null, 2)
);
}
}
}
}
}
}