-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathutils.ts
222 lines (199 loc) Β· 6.57 KB
/
utils.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
import { Article, DevArticle, SOURCE } from "./types";
import crypto from "crypto";
import oauth1a from "oauth-1.0a";
const CONSUMER_KEY = process.env.CONSUMER_KEY as string;
const CONSUMER_KEY_SECRET = process.env.CONSUMER_KEY_SECRET as string;
const ACCESS_TOKEN = process.env.TWITTER_ACCESS_TOKEN as string;
const ACCESS_TOKEN_SECRET = process.env.TWITTER_ACCESS_TOKEN_SECRET as string;
const SHORTNER_API_KEY = process.env.SHORTNER_API_KEY;
const SHORTNER_DOMAIN = process.env.SHORTNER_DOMAIN;
const MEDIUM_USERNAME = process.env.MEDIUM_USERNAME;
const MEDIUM_USER_ID = process.env.MEDIUM_USER_ID as string;
const YT_CHANNEL_ID = process.env.YT_CHANNEL_ID as string;
const YT_API_KEY = process.env.YT_API_KEY as string;
import { TwitterClient } from "twitter-api-client";
export const twitterClient = new TwitterClient({
apiKey: CONSUMER_KEY,
apiSecret: CONSUMER_KEY_SECRET,
accessToken: ACCESS_TOKEN,
accessTokenSecret: ACCESS_TOKEN_SECRET,
});
export async function getPublishedArticlesFromDEV() {
const res = await fetch(`${process.env.DEV_API_URL}/articles/me/published`, {
headers: {
"api-key": process.env.DEV_API_KEY as string,
},
});
return res.json();
}
export async function getMediumFollowers() {
const res = await fetch("https://medium.com/@anshuman-bhardwaj?format=json", {
headers: {
"user-agent": "insomnia/2021.7.2", // didn't work without this for me
},
});
// Medium adds this to the JSON text
const hijackString = "])}while(1);</x>";
const jsonText = await res.text();
// remove the hijackString from JSON before parsing
const data = JSON.parse(jsonText.replace(hijackString, ""));
return (
data?.payload?.references?.SocialStats?.[MEDIUM_USER_ID]
?.usersFollowedByCount || 20
);
}
export async function getYoutubeSubscribers() {
const res = await fetch(
`https://youtube.googleapis.com/youtube/v3/channels?part=statistics&id=${YT_CHANNEL_ID}&key=${YT_API_KEY}`
);
const data = await res.json();
return data?.items[0]?.statistics?.subscriberCount || 330;
}
// fetch all followers
export async function getFollowersFromDev(): Promise<string[]> {
// start with page 1
let page = 1,
limit = 1000;
const followers = [];
// repeat until page number exists
while (page) {
const res = await fetch(
`${process.env.DEV_API_URL}/followers/users?per_page=${limit}&page=${page}`,
{
headers: {
"api-key": process.env.DEV_API_KEY as string,
},
}
);
const answer = await res.json();
if (answer && Array.isArray(answer) && answer.length) {
followers.push(...answer);
// increment page number if this page is full, otherwise set to 0
page = answer.length === limit ? page + 1 : 0;
} else {
// no more followers, so set page to 0
page = 0;
}
}
return followers;
}
export async function createDevArticle(article: DevArticle): Promise<Article> {
const shortUrl = await getShortUrl(article);
return {
id: article.id,
source: SOURCE.dev,
shortUrl,
title: article.title,
published_at: new Date(article.published_timestamp).getTime(),
lastViewsMilestone: 0,
lastReactionsMilestone: 0,
lastTweetedAt: Date.now(),
};
}
export function formatLog(val: string) {
const separator = "=".repeat(20);
const padding = " ".repeat(10);
return separator + padding + val + padding + separator;
}
function getAuthHeaderForRequest(request: any) {
const oauth = new oauth1a({
consumer: { key: CONSUMER_KEY, secret: CONSUMER_KEY_SECRET },
signature_method: "HMAC-SHA1",
hash_function(base_string, key) {
return crypto
.createHmac("sha1", key)
.update(base_string)
.digest("base64");
},
});
const authorization = oauth.authorize(request, {
key: ACCESS_TOKEN,
secret: ACCESS_TOKEN_SECRET,
});
return oauth.toHeader(authorization);
}
export async function sendTweet(text: string) {
const request = {
url: "https://api.twitter.com/2/tweets",
method: "POST",
body: {
text,
},
};
const authHeader = getAuthHeaderForRequest(request);
try {
const data = await fetch(request.url, {
body: JSON.stringify(request.body),
method: request.method,
// @ts-ignore
headers: { "content-type": "application/json", ...authHeader },
});
console.info(formatLog("Tweet status: " + data.status));
return data.status === 201;
} catch (err) {
console.error(err);
return false;
}
}
const MessageStart = `π Yayy! π\nMy article on DEV has been`;
const hashTags = "#javascript #DEVCommunity";
const footerText = "\n Thanks, please follow me for more such posts!";
function TagLine(url: string) {
return `In case you missed it, please check it out now! ${url}`;
}
function createMessage(value: string) {
return `${MessageStart} ${value} \n ${footerText} ${hashTags}`;
}
export function getViewsTweetBody(article: Article & DevArticle): string {
return createMessage(
`viewed more than ${article.page_views_count} times. ${TagLine(
article.shortUrl
)}`
);
}
export function getFollowersTweetBody(value: number): string {
return `π Yayy!! π I've reached ${value} followers on DEV. Thanks for all the love and support. Please check out my DEV profile https://dev.to/anshuman_bhardwaj \n ${footerText} ${hashTags}`;
}
export function getReactionsTweetBody(article: Article & DevArticle): string {
return createMessage(
`liked more than ${article.public_reactions_count} times. ${TagLine(
article.shortUrl
)}`
);
}
export async function getShortUrl(article: DevArticle) {
const options = {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
apikey: SHORTNER_API_KEY,
},
body: JSON.stringify({
domain: { fullName: SHORTNER_DOMAIN },
destination: article.url,
title: article.title,
}),
};
// @ts-ignore
const res = await fetch("https://api.rebrandly.com/v1/links", options);
const data = await res.json();
return data.shortUrl.startsWith("http")
? data.shortUrl
: `https://${data.shortUrl}`;
}
export async function deleteLinks() {
const allLinksRes = await fetch("https://api.rebrandly.com/v1/links", {
// @ts-ignore
headers: { apiKey: SHORTNER_API_KEY },
});
const data = await allLinksRes.json();
console.log("π ~ file: utils.ts ~ line 121 ~ deleteLinks ~ data", data);
for (let i = 0; i < data.length; i++) {
await fetch("https://api.rebrandly.com/v1/links/" + data[i].id, {
method: "DELETE",
// @ts-ignore
headers: { apiKey: SHORTNER_API_KEY },
});
}
}