forked from spencerwooo/substats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
300 lines (277 loc) ยท 11.5 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
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
import qs from 'qs'
// import all handlers from different platforms into a global object
import { handlerImporter } from './utils/handlerImporter'
//! register sources that return non-numbers here, if a source doesn't return a number, we will handle it separately
const singleOnlySources = ['afdianIncome']
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
/**
* Parse request parameters
*
* @param {Request} req
*/
function parseRequest(req) {
try {
const params = {
// check if query is valid
valid: true,
// if query is empty, send default greetings message
greet: false,
// some sources don't return a number, for those we accept only single requests
singleOnly: false,
singleOnlySrc: '',
// service providers
source: [],
// query keys (feed link for RSS; username or slug for others)
queryKey: [],
}
const url = new URL(req)
const query = qs.parse(url.search, { ignoreQueryPrefix: true })
// empty query, send greetings
if (Object.keys(query).length === 0) {
params.greet = true
return params
}
// if source is null or query key is null, send invalid request
if (
query.source === '' ||
typeof query.source === 'undefined' ||
query.queryKey === '' ||
typeof query.queryKey === 'undefined'
) {
params.valid = false
return params
}
// if returned value is not a number, only single requests are supported
if (typeof query.source !== 'string' && query.source.length > 1) {
query.source.forEach(src => {
if (singleOnlySources.includes(src)) {
params.valid = false
params.singleOnly = true
params.singleOnlySrc = src
return params
}
})
}
// parse source and queryString in query string
if (typeof query.source === 'string' && typeof query.queryKey === 'string') {
// single query key, maybe multiple sources
params.source = query.source.split('|')
if (params.source.length === 1) {
// single source
params.singleOnly = true
// populate query key list (we takes a list as input, even when there's only one query)
params.queryKey.push(query.queryKey)
} else {
// multiple sources
params.source.forEach(src => {
// detect if a singleOnly source is requested inside multiple requests
if (singleOnlySources.includes(src)) {
params.valid = false
params.singleOnly = true
params.singleOnlySrc = src
}
// populate query key list
params.queryKey.push(query.queryKey)
})
}
} else if (query.source.length > 1 || query.queryKey.length > 1) {
// multiple query key, multiple sources
if (query.source.length === query.queryKey.length) {
params.source = query.source
params.queryKey = query.queryKey
} else {
// source and queryKey index doesn't match
params.valid = false
}
} else {
params.valid = false
}
console.log(params)
return params
} catch (error) {
console.error(error)
return null
}
}
/**
* Fetch subscriber stats from list of service providers
*
* @param {bool} singleOnly Whether or not we add up all subs for each source
* @param {list} sources List of service providers to query
* @param {list} queryKey Target query key list
*/
async function fetchStats(singleOnly, sources, queryKey) {
// function's returning value
const fetchStatsRes = {
totalSubs: 0,
subsInEachSource: {},
failedSources: {},
}
// reference handlers
const handlers = handlerImporter()
// construct big concurrent promise array
const resPromise = []
sources.forEach((source, i) => {
// init source specific array with 0 subs each
fetchStatsRes.subsInEachSource[source] = 0
// create promise array
if (source in handlers) {
resPromise.push(handlers[source](queryKey[i]))
} else {
// not implemented
fetchStatsRes.failedSources[source] = 'Not implemented'
}
})
// parallel requests take off!
await Promise.allSettled(resPromise).then(sourceReturns => {
sourceReturns.forEach(res => {
if (res.status === 'fulfilled') {
// promise fulfilled (fetch succeeded)
if (res.value.failed) {
// error occured on fetch end
fetchStatsRes.failedSources[res.value.source] = res.value.failedMsg
} else {
// successfully fetched subs
if (singleOnly) {
// if requested source is singleOnly source, we won't add up all sources
fetchStatsRes.totalSubs = res.value.subs
} else {
fetchStatsRes.totalSubs += res.value.subs
}
fetchStatsRes.subsInEachSource[res.value.source] = res.value.subs
}
}
// promise rejected (fetch failed)
if (res.status === 'rejected') {
fetchStatsRes.failedSources['substats-error'] = res.reason
}
})
})
return fetchStatsRes
}
/**
* Respond to query
*
* @param {Request} request
*/
async function handleRequest(request) {
// enable CORS
const headersJson = {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'Origin, X-Requested-With, Content-Type, Accept',
}
// default page returns html
const headersHtml = {
'Content-Type': 'text/html',
}
const respInit = {
ok: { status: 200, headers: headersJson },
greet: { status: 200, headers: headersHtml },
ban: { status: 403, headers: headersJson },
invalid: { status: 400, headers: headersJson },
}
let response = null
// Ban all requests other than GET
if (request.method !== 'GET') {
const errorResp = {
status: respInit.ban.status,
data: {
errorMsg: 'Substats: Sorry, only GET requests are accepted.',
requestMethod: request.method,
},
}
return new Response(JSON.stringify(errorResp), respInit.ban)
}
const resp = parseRequest(request.url)
// Empty request, default to landing page
if (resp.greet) {
// latest commit hash for jsDelivr to use when loading external JS and CSS
const COMMIT_HASH = 'a5dbafea730dc41afd7c9cf57c0943152171ffd4'
// Cloudflare doesn't allow static site hosting for free plans,
// so I have to settle for inline HTML and styles. Sad!
const landing = `
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Home | Substats API</title>
<link href="https://fonts.googleapis.com/css2?family=Fira+Code:wght@400;600&display=swap" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/gh/spencerwooo/Substats@${COMMIT_HASH}/styles/default.min.css" rel="stylesheet">
<link rel="shortcut icon" type="image/png" sizes="16x16" href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAACCUlEQVQ4jY2Rz2sTURDHJ0hBPYj4A2yyzcwW7U1UFIrU7Ir/hIfeevAf8KQ28ObZ9qh4KRU8WA2CIVs8elZRhB5EKPWiFTwoWmmSnZeku9HnIbvJNlbsg3d5M3xmvp8HwvjLMG4Ku6uG8YEwTocLJ47DXo8w2V1uQxQ+lrJ75v8ARRvC9F00dncBtUXTvS1Fh/8JsBZyVuUPtudcDJmuhoqeGk2yE4RrUnZO7zlWfRbdkGlRNHUykM22Hiv91dzSxamOGp/YULR/uBYq1wsZP2YgP0TR2Z0OGH+bXv5vomm5qYtTFiCX1tsqXxSm95lIHxrKOZIBDInT2BXGR02VP9bfUhUcYfqU9oRMS5AOEcZKyPhaGM0QbL0x65xKIU3tloT7TrbrauzC4CcAcq05p2AUzhumLGjNLhSO9p0wLWZqTyATFQaT8KIwfR1EomVre41bt1wMGUNhsoZRzE0cBVuZPBQH/o0oKC3Gz/wSAEBTFS8ZTa3EfNwo02Q6wDBVU7hROANxcLkWBb5Nbiuqeed6X0h3h6Slzqb7AKaHENX8KAOwceBdBwDoKOekMEVJ82d7//xIb7vxCWGMk/d3EAX+ywygG694VwAAbBX2CdN6at3M4ygAwJc7zgGTeBCmn9Cueu524FWiFf9FVPNmUmFJ3iXD9NYwvanfRhcAYPUajAjT8947vvoDw+eYQXI6eiYAAAAASUVORK5CYII=" />
</head>
<body>
<img id="banner" src="https://substats.spencerwoo.com/img/substats.svg" alt="Substats" width="360px" height="auto" />
<h6>๐๐ Shhhh...we're counting your subscribers!</h6>
<div>
<a href="https://api.spencerwoo.com/substats/"><img
src="https://img.shields.io/badge/Now%20on-Cloudflare%20Workers-f38020?logo=cloudflare&logoColor=f38020"
alt="Now on Cloudflare Workers"></a>
<a href="https://stats.uptimerobot.com/92yjVTmk63/784533782"><img
src="https://img.shields.io/uptimerobot/status/m784533782-966fa87a7f1afd93c9cc4e51?label=Status&color=00B0D8&logo=probot&logoColor=white"
alt="Uptime Robot status"></a>
<a href="https://github.com/spencerwooo/Substats/actions?query=workflow%3ADeploy"><img
src="https://github.com/spencerwooo/Substats/workflows/Deploy/badge.svg" alt="Deploy"></a>
<a href="https://substats.spencerwoo.com/"><img
src="https://badgen.net/https/now.swoo.workers.dev/dpl_FiMBkC6zs1o9eb36Hzd1fgnstM1D?labelColor=black&label=Vercel&icon=zeit"
alt="Vercel"></a>
</div>
<div id="code-banner-container">
<div id="code-banner">
<code>
<em>Query Format</em><br>
<span class="token accent">GET</span> <span class="token source-highlight">/?source=</span>{YOUR_SERVICE_PROVIDER}<span class="token query-highlight">&queryKey=</span><span>{YOUR_QUERY}</span><br><br>
<em>For instance</em><br>
<a href="/substats/?source=sspai&queryKey=spencerwoo"><span class="token accent">GET</span> <span class="token source-highlight">/?source=</span><span>sspai</span><span class="token query-highlight">&queryKey=</span><span>spencerwoo</span></a><br>
<a href="/substats/?source=github&queryKey=spencerwooo"><span class="token accent">GET</span> <span class="token source-highlight">/?source=</span><span>github</span><span class="token query-highlight">&queryKey=</span><span>spencerwooo</span></a><br>
<em>...</em>
<span id="code-class">HTTP<span>
</code>
</div>
</div>
<div class="first-line">๐ IF YOU SEE THIS PAGE, THEN <strong>SUBSTATS</strong> IS UP AND RUNNING.</div>
<div class="second-line">๐ฎ FOR ONLINE DOCUMENTATION, PLEASE REFER TO <a
href="https://substats.spencerwoo.com/">SUBSTATS.SPENCERWOO.COM</a>.</div>
<footer>
<div><a href="https://github.com/spencerwooo/Substats">GITHUB</a> | <a
href="https://substats.spencerwoo.com/">DOCUMENTATION</a> | <a
href="https://blog.spencerwoo.com/2020/03/substats/">BLOG POST</a></div>
<div class="second-line">MADE WITH ๐ BY <a href="https://spencerwoo.com">SPENCER WOO</a> ยฉ2020</div>
</footer>
</body>
<script src="https://cdn.jsdelivr.net/gh/spencerwooo/Substats@${COMMIT_HASH}/styles/default.min.js"></script>
</html>
`
return new Response(landing, respInit.greet)
}
// Invalid request, send 400 bad request
const invalidErr = `Substats: Invalid request. You should structure your query like so: /?source={YOUR_SERVICE_PROVIDER}&queryKey={YOUR_QUERY}`
const singleOnlyErr = `Substats: Invalid request. For source: '${resp.singleOnlySrc}' we only accept single requests.`
if (resp.valid === false) {
let err = ''
if (resp.singleOnly) {
err = singleOnlyErr
} else {
err = invalidErr
}
const invalidResp = {
status: respInit.invalid.status,
data: {
err: err,
request: request.url,
},
}
return new Response(JSON.stringify(invalidResp), respInit.invalid)
}
// Fetch statistics
const result = await fetchStats(resp.singleOnly, resp.source, resp.queryKey)
const finalResp = {
status: respInit.ok.status,
data: result,
}
response = new Response(JSON.stringify(finalResp), respInit.ok)
return response
}