-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccounts.js
100 lines (89 loc) · 2.96 KB
/
accounts.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
export async function getAccount(handle) {
handle = canonicalHandle(handle);
const {instance} = splitHandle(handle);
let accountArray = []
try {
const ret = await getPublicJson(instance, "/api/v2/search", {q:handle, type:"accounts", limit:1});
accountArray = ret.accounts || [];
} catch (e) {
console.error(e);
throw new Error(`Error attempting to get account information for ${handle}`);
}
if (accountArray.length >= 1) {
return accountArray[0];
} else {
throw new Error(`No account found for ${handle}`)
}
}
export function splitHandle(handle) {
const a = handle.split("@");
return {userName:a[a.length - 2], instance:a[a.length - 1]}
}
export function canonicalHandle(handle, userInstance = null) {
handle = handle.trim();
if(!handle.startsWith("@")) {
handle = "@" + handle;
}
if (handle.lastIndexOf("@") === 0 && userInstance) {
handle = `${handle}@${userInstance}`
}
return handle;
}
export async function getFollowers(handle) {
//No non-public information is recorded
//const headers = { "Content-Type": "application/json", authorization:`Bearer ${loginInfo.accessToken}`};
const headers = { "Content-Type": "application/json"};
const account = await getAccount(handle);
const accountId = account.id;
const MAX_ACCOUNTS = 4000; //50 requests
async function getBatch(url) {
console.log(url);
const res = await fetch(url, {headers:headers});
if (!res.ok) {
//TODO: supply message not just code
throw new Error(`${res.code}: Error fetching ${url}`);
}
const link = res.headers.get("Link");
let nextUrl = null;
if (link) {
const matches = link.matchAll(/<([^>]+)>; *rel=["']([^'"]+)['"]/g);
for (let match of matches) {
if(match[2] === "next") {
nextUrl = match[1]
}
}
}
const accounts = await res.json();
return {accounts, nextUrl}
}
const followers = [];
const {instance} = splitHandle(handle);
let url = `https://${instance}/api/v1/accounts/${accountId}/followers?limit=80`
while (url && followers.length < MAX_ACCOUNTS) {
let batch = await getBatch(url);
batch.accounts.forEach(account=>{
followers.push(account)
});
url = batch.nextUrl;
}
if (followers.length >= MAX_ACCOUNTS) {
console.log(`Account list may be incomplete, no more than ${MAX_ACCOUNTS} supported.`)
}
return followers;
}
async function getPublicJson(instance, path, params) {
const queryString = params ? "?" + new URLSearchParams(params).toString() : "";
const headers = { "Content-Type": "application/json" };
if (path.startsWith("/")) {
path = path.substring(1);
}
const url = `https://${instance}/${path}${queryString}`
const res = await fetch(url, {
headers:headers
});
if (!res.ok) {
throw new Error(`${res.code}: Error fetching ${path}`)
}
const data = await res.json();
return data;
}