Skip to content

Commit e17145d

Browse files
Implemented new unfollowing logic
1 parent 1f1668f commit e17145d

File tree

4 files changed

+98
-3
lines changed

4 files changed

+98
-3
lines changed

config.example.json

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"initialStartup": {
3+
"completed": false
4+
},
5+
"configValues": {
6+
"maxTotalFollowing": 1000,
7+
"minWaitTimeSeconds": 500,
8+
"maxWaitTimeSeconds": 900,
9+
"queueListMaxUsers": 1000,
10+
"pendingFollowBackWaitTimeDays": 7
11+
}
12+
}

package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
"name": "github-auto-followers-bot",
33
"version": "1.0.0",
44
"description": "https://github.com/KevinTrinh1227/Github-Auto-Followers-Bot",
5-
"main": "src/main.js",
5+
"main": "src/index.js",
66
"src": {
7-
"github-auto-followers-bot": "src/main.js"
7+
"github-auto-followers-bot": "src/index.js"
88
},
99
"scripts": {
10-
"start": "node src/main.js",
10+
"start": "node src/index.js",
1111
"test": "echo \"Error: no test specified\" && exit 1"
1212
},
1313
"repository": {

src/main.js src/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const {
1212
nextInUnfollowQueue,
1313
addUserToUnfollowQueue,
1414
removeUserFromFollowQueue,
15+
moveExpiredUsersToUnfollowQueue,
1516
} = require("./queues");
1617
const { getTotalFollowing } = require("./followingList");
1718
const {
@@ -87,6 +88,7 @@ async function processQueue() {
8788
await unfollowUser(nextUnfollower);
8889
} else {
8990
console.log("No users to unfollow.");
91+
await moveExpiredUsersToUnfollowQueue();
9092
}
9193

9294
console.log("Waiting before the next cycle...\n\n\n");

src/queues.js

+81
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const pendingFollowBackPath = path.join(
88
"../data/pending_follow_back.json"
99
);
1010
const unfollowQueuePath = path.join(__dirname, "../data/unfollow_queue.json");
11+
const configPath = path.join(__dirname, "../config.json");
1112

1213
// Function to remove users from follow_queue.json if they are present in the followedUsersSet
1314
async function cleanFollowQueue(followedUsersSet) {
@@ -62,6 +63,85 @@ async function nextInFollowQueue() {
6263
}
6364
}
6465

66+
// Function to check if the user's follow back period has expired and move them to unfollow queue
67+
async function moveExpiredUsersToUnfollowQueue() {
68+
try {
69+
// Read and parse pending_follow_back.json
70+
const pendingFollowBackData = await fs.readFile(
71+
pendingFollowBackPath,
72+
"utf8"
73+
);
74+
const pendingFollowBack = JSON.parse(pendingFollowBackData);
75+
76+
// Read and parse config.json
77+
const configData = await fs.readFile(configPath, "utf8");
78+
const config = JSON.parse(configData);
79+
const pendingFollowBackWaitTimeDays =
80+
config.configValues.pendingFollowBackWaitTimeDays;
81+
82+
// Read or initialize unfollow_queue.json
83+
let unfollowQueue = {};
84+
try {
85+
const unfollowQueueData = await fs.readFile(unfollowQueuePath, "utf8");
86+
unfollowQueue = JSON.parse(unfollowQueueData);
87+
} catch (error) {
88+
console.log("Unfollow queue file not found, starting a new one.");
89+
}
90+
91+
// Get the current date
92+
const currentDate = new Date();
93+
94+
// Loop through pending_follow_back.json and check expiration
95+
for (const userId in pendingFollowBack) {
96+
if (pendingFollowBack.hasOwnProperty(userId)) {
97+
const user = pendingFollowBack[userId];
98+
99+
// Calculate the expiration date based on the followed_on date
100+
const followedOnDate = new Date(user.followed_on);
101+
const expirationDate = new Date(followedOnDate);
102+
expirationDate.setDate(
103+
followedOnDate.getDate() + pendingFollowBackWaitTimeDays
104+
);
105+
106+
// Check if the user has exceeded the expiration date
107+
if (currentDate > expirationDate) {
108+
console.log(
109+
`User ${user.login} exceeded the follow back period, moving to unfollow queue.`
110+
);
111+
112+
// Add the user to the unfollow queue with a timestamp
113+
unfollowQueue[user.id] = {
114+
...user,
115+
added_to_queue_on: new Date().toISOString(), // Add a timestamp for when the user was added
116+
};
117+
118+
// Remove the user from pending_follow_back.json
119+
delete pendingFollowBack[userId];
120+
}
121+
}
122+
}
123+
124+
// Write the updated unfollow_queue.json
125+
await fs.writeFile(
126+
unfollowQueuePath,
127+
JSON.stringify(unfollowQueue, null, 4),
128+
"utf8"
129+
);
130+
// Write the updated pending_follow_back.json
131+
await fs.writeFile(
132+
pendingFollowBackPath,
133+
JSON.stringify(pendingFollowBack, null, 4),
134+
"utf8"
135+
);
136+
137+
console.log(
138+
"Users successfully moved to the unfollow queue and pending follow back list updated."
139+
);
140+
} catch (error) {
141+
console.error("Error moving expired users to unfollow queue:", error);
142+
}
143+
}
144+
65145
// Function to return the next user object from the unfollow queue
66146
async function nextInUnfollowQueue() {
67147
try {
@@ -196,6 +276,7 @@ module.exports = {
196276
removeUserFromFollowQueue,
197277
addUserToPendingFollowBack,
198278
cleanFollowQueue,
279+
moveExpiredUsersToUnfollowQueue,
199280
};
200281
// Example usage
201282
// nextInQueue().then(follower => console.log("Next follower to process:", follower));

0 commit comments

Comments
 (0)