-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
91 lines (74 loc) · 1.72 KB
/
main.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
import { config } from "https://deno.land/x/dotenv/mod.ts";
const { GITHUB_API_TOKEN } = config();
const GITHUB_BASE_URL = "https://api.github.com";
const MAX_USERS = 100;
/*
* Parse following usernames
*/
function parseFollowingNames(users = []) {
return users.map((user) => user.login);
}
/*
* Fetch api
*/
async function api(url, method = "GET") {
try {
const response = await fetch(`${GITHUB_BASE_URL}/${url}`, {
method,
headers: {
accept: "application/vnd.github.v3+json",
Authorization: `token ${GITHUB_API_TOKEN}`,
},
});
const status = response.status;
const data = status !== 204 ? await response.json() : {};
return { data, status, error: null };
} catch (error) {
console.log("Error", error);
return {
data: null,
status: null,
error: error,
};
}
}
/*
* Fetch followed users count
*/
async function fetchFollowingCount() {
const { data, error } = await api("user");
if (!error) {
return data.following;
}
return 0;
}
/*
* Fetch followed users
*/
async function fetchFollowedUsers() {
const { data, error } = await api("user/following?per_page=100");
if (!error) {
return parseFollowingNames(data);
}
return [];
}
/*
* Batch unfollow users
*/
async function batchUnfollowUsers(users = []) {
for (let user of users) {
console.log(`Unfollowing ${`/user/following/${user}`}`);
await api(`user/following/${user}`, "DELETE");
}
}
/*
* Main
*/
async function main() {
const followingCount = await fetchFollowingCount();
for (let count = 0; count < Math.ceil(followingCount / MAX_USERS); count++) {
const users = await fetchFollowedUsers();
await batchUnfollowUsers(users);
}
}
main();