-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathunfollow.js
More file actions
64 lines (52 loc) · 2.1 KB
/
unfollow.js
File metadata and controls
64 lines (52 loc) · 2.1 KB
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
require('dotenv').config();
const axios = require('axios');
const GITHUB_USERNAME = process.env.GITHUB_USERNAME;
const GITHUB_TOKEN = process.env.GITHUB_TOKEN;
const EXCLUDED_USERS = ['prabhatojha', 'KumarGourav07', 'github', 'torvalds']; // Add usernames you don't want to unfollow here
const api = axios.create({
baseURL: 'https://api.github.com',
headers: {
Authorization: `Bearer ${GITHUB_TOKEN}`,
'User-Agent': GITHUB_USERNAME,
},
});
// Function to fetch all paginated data
const fetchPaginatedData = async (url) => {
let results = [];
let page = 1;
let response;
try {
do {
response = await api.get(`${url}?per_page=100&page=${page}`);
results = results.concat(response.data);
page++;
} while (response.data.length > 0);
} catch (error) {
console.error('Error fetching paginated data:', error.response ? error.response.data : error.message);
}
return results;
};
(async () => {
try {
// Fetch all followers (handles pagination)
const followers = await fetchPaginatedData(`/users/${GITHUB_USERNAME}/followers`);
const followerLogins = followers.map((user) => user.login);
// Fetch all accounts you follow (handles pagination)
const following = await fetchPaginatedData(`/users/${GITHUB_USERNAME}/following`);
const followingLogins = following.map((user) => user.login);
// Identify non-reciprocal accounts
const nonFollowers = followingLogins.filter((user) => !followerLogins.includes(user));
// Exclude specific usernames
const filteredNonFollowers = nonFollowers.filter((user) => !EXCLUDED_USERS.includes(user));
console.log(`Accounts not following you back: ${filteredNonFollowers.join(', ')}`);
// Unfollow non-reciprocal accounts
for (const user of filteredNonFollowers) {
console.log(`Unfollowing ${user}...`);
await api.delete(`/user/following/${user}`);
console.log(`Unfollowed ${user}`);
}
console.log('Unfollowed all non-reciprocal accounts, excluding specified users!');
} catch (error) {
console.error('Error:', error.response ? error.response.data : error.message);
}
})();