Skip to content

Commit 3706603

Browse files
committed
Add get country counts
1 parent dbb9d9b commit 3706603

1 file changed

Lines changed: 50 additions & 0 deletions

File tree

get-country-counts.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import Cryptr from 'cryptr';
2+
3+
const cryptr = new Cryptr('insert secret key here');
4+
5+
export function encrypt(plaintext: string) {
6+
return cryptr.encrypt(plaintext);
7+
}
8+
9+
export function decrypt(ciphertext: string) {
10+
return cryptr.decrypt(ciphertext);
11+
}
12+
13+
const tokens = [''];
14+
15+
export async function getUserData(token: string) {
16+
const userDataURL = new URL(`https://auth.hackclub.com/api/v1/me`);
17+
const userDataRes = await fetch(userDataURL, {
18+
method: 'GET',
19+
headers: {
20+
Authorization: `Bearer ${token}`
21+
}
22+
});
23+
24+
if (!userDataRes.ok) {
25+
throw new Error('Failed to fetch user data');
26+
}
27+
28+
const userDataJSON = await userDataRes.json();
29+
30+
return userDataJSON.identity!;
31+
}
32+
33+
const countries: Record<string, number> = {};
34+
35+
for (let i = 0; i < tokens.length; i++) {
36+
try {
37+
const token = decrypt(tokens[i]);
38+
const userData = await getUserData(token);
39+
const { addresses } = userData;
40+
const address = addresses?.find((address: { primary: boolean }) => address.primary);
41+
console.log('Address', i + '/' + tokens.length + ':', address.country);
42+
43+
countries[address.country as string] = (countries[address.country as string] ?? 0) + 1;
44+
} catch {
45+
console.warn('Failed: user ' + i);
46+
}
47+
}
48+
49+
console.log('\n');
50+
console.log(countries);

0 commit comments

Comments
 (0)