-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
394 lines (317 loc) · 12.8 KB
/
index.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
require('dotenv').config({
path: '../twitter-clients/unfollower-client/.env',
// debug: true
});
const { execSync } = require('child_process');
const { Scraper } = require('agent-twitter-client');
const fs = require('fs');
const axios = require('axios');
// const cron = require('node-cron');
const readline = require('readline');
const path = require('path');
// Ensure base directory exists
const BASE_PATH = process.env.BASE_PATH || './';
if (!fs.existsSync(BASE_PATH)) {
console.log(`BASE_PATH ${BASE_PATH} does not exist.`);
}
function getCookies(scraper) {
scraper.getCookies().then((cookies) => {
const cookiesArray = cookies.map(cookie => ({
name: cookie.name,
value: cookie.value,
domain: cookie.domain,
path: cookie.path,
expires: cookie.expires,
httpOnly: cookie.httpOnly,
secure: cookie.secure
}));
const folderPath = path.join(BASE_PATH, 'cookies');
if (!fs.existsSync(folderPath)) {
fs.mkdirSync(folderPath, { recursive: true });
}
fs.writeFileSync(path.join(folderPath, `${process.env.TWITTER_USERNAME}.json`), JSON.stringify(cookiesArray, null, 2));
});
}
async function getProfile(scraper, username) {
return await scraper.getProfile(username)
}
async function getFollowers(scraper, username, userId) {
const profile = await scraper.getProfile(username);
const followerCount = profile.followersCount;
const followerResults = await scraper.getFollowers(userId, 1500);
const followers = [];
let recordedFollowerCount = 0;
for await (const follower of followerResults) {
recordedFollowerCount++;
console.log(`Recording ${recordedFollowerCount} follower... ${follower.username}`);
const folderPath = path.join(BASE_PATH, 'followers');
if (!fs.existsSync(folderPath)) {
fs.mkdirSync(folderPath, { recursive: true });
}
const newUsernames = [follower?.username]
const filePath = path.join(folderPath, `${username}.json`);
let existingData = { followers: [], count: 0 };
if (fs.existsSync(filePath)) {
const fileContent = fs.readFileSync(filePath, 'utf8');
existingData = JSON.parse(fileContent);
}
// Append to existing data
existingData.followers = [...new Set([...existingData.followers, ...newUsernames])];
existingData.count = existingData.followers.length;
fs.writeFileSync(filePath, JSON.stringify(existingData, null, 2));
}
}
async function getFollowersPaged(scraper, username, userId, resumeFrom = 0, cursor) {
const profile = await scraper.getProfile(username);
const followerCount = profile.followersCount;
const followersPerPage = 50; // Assuming 100 followers per page
const pagesToFetch = Math.ceil(followerCount / followersPerPage);
console.log({ followerCount, followersPerPage, pagesToFetch });
const folderPath = path.join(BASE_PATH, 'followers');
if (!fs.existsSync(folderPath)) {
fs.mkdirSync(folderPath, { recursive: true });
}
const sourceFilePath = path.join(folderPath, `${username}.json`);
const destinationFilePath = path.join(folderPath, `${username}-copy.json`);
if (fs.existsSync(sourceFilePath)) {
fs.copyFileSync(sourceFilePath, destinationFilePath);
console.log(`File ${username}.json has been duplicated to ${username}-copy.json`);
fs.writeFileSync(sourceFilePath, JSON.stringify({ followers: [], count: 0, username }, null, 2));
console.log(`File ${username}.json has been reset`);
} else {
console.log(`File ${username}.json does not exist.`);
}
let currentFollowersFetched = 0
let existingData = { followers: [], count: 0, username };
for (let page = resumeFrom; page < pagesToFetch; page++) {
const followerResults = await scraper.fetchProfileFollowers(userId, followersPerPage, cursor);
console.log(`Fetched page ${page} out of ${pagesToFetch}. next: ${followerResults?.next}, previous: ${followerResults?.previous}`);
const newUsernames = followerResults?.profiles.map(profile => profile.username).filter(Boolean);
console.log({ newUsernames });
if (fs.existsSync(sourceFilePath)) {
const fileContent = fs.readFileSync(sourceFilePath, 'utf8');
existingData = JSON.parse(fileContent);
}
existingData.followers = [...new Set([...existingData.followers, ...newUsernames])];
existingData.count = existingData.followers.length;
currentFollowersFetched += newUsernames.length;
fs.writeFileSync(sourceFilePath, JSON.stringify(existingData, null, 2));
cursor = followerResults.next;
console.log(`existingData.count: ${currentFollowersFetched} followers fetched out of ${followerCount}`)
// NOTE:
// we don't need this because the scraper automatically waits.
// so cool.
//
// if (existingData.count >= 2400 && existingData.count <= 2500) {
// console.log("Pausing for 15 minutes before resuming loop...");
// await new Promise(resolve => setTimeout(resolve, 15 * 60 * 1000));
// console.log("Resuming after 15 minutes pause");
// }
}
}
function compareFollowers(scraper, username) {
const oldFilePath = path.join(BASE_PATH, 'followers', `${username}-copy.json`);
const newFilePath = path.join(BASE_PATH, 'followers', `${username}.json`);
if (!fs.existsSync(oldFilePath) || !fs.existsSync(newFilePath)) {
console.log("One or both of the required files do not exist.");
return;
}
const oldFileContent = fs.readFileSync(oldFilePath, 'utf8');
const newFileContent = fs.readFileSync(newFilePath, 'utf8');
const oldData = JSON.parse(oldFileContent);
const newData = JSON.parse(newFileContent);
const oldFollowers = new Set(oldData.followers);
const newFollowers = new Set(newData.followers);
const unfollowers = [...oldFollowers].filter(follower => !newFollowers.has(follower));
if (unfollowers.length > 0) {
console.log(`Unfollowers as of: ${new Date().toLocaleString()}`);
unfollowers.forEach(async unfollower => {
console.log(`https://x.com/${unfollower}`);
const doubleCheck = await purgedOrNotOrRestricted(scraper, unfollower)
await sendToTelegramChannel(`
New unfollower / changed username / bot purged: @${unfollower}
@${unfollower} is ${doubleCheck}
Profile Link: https://x.com/${unfollower}
(${new Date().toLocaleString('en-US', { timeZone: 'Asia/Singapore' })})
`);
});
} else {
console.log("No unfollowers found.");
sendToTelegramChannel(`No unfollowers today. (${new Date().toLocaleString('en-US', { timeZone: 'Asia/Singapore' })})`)
}
}
async function gitPull() {
// Git operations before updating follower jsons
try {
execSync('git pull origin');
console.log('"git pull origin" completed successfully');
} catch (error) {
console.error('Error during "git pull origin":', error.message);
}
}
async function gitCommitAndPush() {
// Git operations after compareFollowers
const now = new Date();
const timezoneOffset = -now.getTimezoneOffset() / 60;
const timezone = `GMT${timezoneOffset >= 0 ? '+' : ''}${timezoneOffset}`;
const timestamp = `${now.toISOString().split('T')[0]} ${now.toTimeString().split(' ')[0]} ${timezone}`;
try {
execSync(`git commit -am "unfollowers update ${timestamp}"`);
execSync('git push');
console.log('Git operations completed successfully');
} catch (error) {
console.error('Error during git operations:', error.message);
}
}
async function sendToTelegramChannel(message) {
const botToken = process.env.TELEGRAM_BOT_NEWTWITTERFOLLOWERSBOT_TOKEN;
const chatId = process.env.TELEGRAM_BOT_NEWTWITTERFOLLOWERSBOT_CHATID;
if (!botToken || !chatId) {
console.error('Telegram bot token or chat ID is not set in environment variables.');
return;
}
const url = `https://api.telegram.org/bot${botToken}/sendMessage`;
try {
const response = await axios.post(url, {
chat_id: chatId,
text: message,
parse_mode: 'HTML'
});
if (response.data.ok) {
console.log('Message sent successfully to Telegram channel');
} else {
console.error('Failed to send message to Telegram channel:', response.data.description);
}
} catch (error) {
console.error('Error sending message to Telegram channel:', error.message);
}
}
async function runTwitterAgent(username = 'pseudokid') {
const scraper = new Scraper();
console.log(`logging in as ${process.env.TWITTER_USERNAME}`);
await scraper.login(process.env.TWITTER_USERNAME, process.env.TWITTER_PASSWORD);
// await getCookies(scraper, process.env.TWITTER_USERNAME)
const profile = await getProfile(scraper, username)
// await gitPull()
await getFollowersPaged(scraper, username, profile?.userId, 0, null)
// await getFollowers(scraper, username, profile?.userId)
compareFollowers(scraper, username)
// await gitCommitAndPush()
}
// script entry point - start
async function runStandalone() {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
// Prompt user to run immediately with 10-second timer
const timerDuration = 10000; // 10 seconds
let timer;
const promptMessage = `Do you want to run the agent now? (Y/N) [Auto N in ${timerDuration / 1000} seconds]: `;
// Start the countdown
let remainingTime = timerDuration / 1000;
const countdown = setInterval(() => {
process.stdout.clearLine();
process.stdout.cursorTo(0);
process.stdout.write(promptMessage.replace(/\d+/, remainingTime));
remainingTime--;
}, 1000);
// Set timeout for automatic 'N' response
timer = setTimeout(() => {
clearInterval(countdown);
console.log('\nNo response received, defaulting to N');
rl.close();
console.log('Skipping immediate run. Agent will run on schedule.');
displayLiveTimerUntilNextCronRun();
}, timerDuration);
// Ask the question
rl.question(promptMessage, (answer) => {
clearTimeout(timer);
clearInterval(countdown);
if (answer.toLowerCase() === 'y') {
console.log('\nRunning agent immediately...');
runTwitterAgent();
} else {
console.log('\nSkipping immediate run. Agent will run on schedule.');
}
rl.close();
});
// Schedule runTwitterAgent() to run daily at 12:00 AM
cron.schedule('0 1 * * *', () => {
console.log('Running scheduled task...');
runTwitterAgent();
});
}
// script entry point - end
// Calculate time remaining until next scheduled run
function getTimeUntilNextCronRun() {
const now = new Date();
const nextRun = new Date();
nextRun.setHours(1, 0, 0, 0); // Next run at 12:00 AM
if (now.getHours() === 0 && now.getMinutes() === 0) {
nextRun.setDate(nextRun.getDate() + 1);
}
return nextRun - now;
}
// Display live timer for next scheduled run
function displayLiveTimerUntilNextCronRun() {
let timeRemaining = getTimeUntilNextCronRun();
const liveTimer = setInterval(() => {
process.stdout.clearLine();
process.stdout.cursorTo(0);
const hours = Math.floor(timeRemaining / (1000 * 60 * 60));
const minutes = Math.floor((timeRemaining % (1000 * 60 * 60)) / (1000 * 60));
const seconds = Math.floor((timeRemaining % (1000 * 60)) / 1000);
process.stdout.write(`Time until next scheduled run: ${hours}h ${minutes}m ${seconds}s`);
timeRemaining -= 1000;
if (timeRemaining < 0) {
clearInterval(liveTimer);
process.stdout.clearLine();
process.stdout.cursorTo(0);
process.stdout.write('\n\nScheduled task is running now...');
}
}, 1000);
}
async function purgedOrNotOrRestricted(scraper, username) {
try {
const profile = await scraper.getProfile(username);
// console.log({ profile })
if (profile.followingCount === 0 || profile.friendsCount === 0) {
console.log(`${username} RESTRICTED - could be a voluntary/involuntary unfollower`)
return 'RESTRICTED - could be a voluntary/involuntary unfollower'
} else {
console.log(`${username} NOT_PURGED - is a voluntary unfollower`)
return 'NOT_PURGED - is a voluntary unfollower'
}
} catch (e) {
console.log(`${username} PURGED - could be a voluntary/involuntary unfollower`)
return 'PURGED - could be a voluntary/involuntary unfollower'
}
}
// async function playground() {
// const scraper = new Scraper();
// await scraper.login(process.env.TWITTER_USERNAME, process.env.TWITTER_PASSWORD);
// const username = 'pseudokid'
// await getCookies(scraper, process.env.TWITTER_USERNAME)
// await purgedOrNotOrRestricted(scraper, 'elonmusk')
// await purgedOrNotOrRestricted(scraper, 'elonmusk')
// await purgedOrNotOrRestricted(scraper, 'elonmusk')
// await purgedOrNotOrRestricted(scraper, 'elonmusk')
// await purgedOrNotOrRestricted(scraper, 'elonmusk')
// }
// playground()
module.exports = {
getCookies,
getProfile,
getFollowers,
getFollowersPaged,
compareFollowers,
gitPull,
gitCommitAndPush,
sendToTelegramChannel,
runTwitterAgent,
getTimeUntilNextCronRun,
displayLiveTimerUntilNextCronRun,
purgedOrNotOrRestricted,
runStandalone
};