Skip to content

Commit

Permalink
Handle login errors + add login delay (#6)
Browse files Browse the repository at this point in the history
* Add some login delay

* Check for login errors

* Add back login confirmation message
  • Loading branch information
Jeto143 authored Apr 26, 2024
1 parent 7cd4ef6 commit 89cc194
Showing 1 changed file with 24 additions and 9 deletions.
33 changes: 24 additions & 9 deletions 5m5v-bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,40 @@ if (!config.users.every(user => ['language', 'email', 'username', 'password'].ev

const languages = config.users.map(user => user.language);
const pollingIntervalMs = 40 * 1000;
const loginDelayMs = 5 * 60 * 1000;
const retweetDelayMs = config.delaytime || 2 * 60 * 1000;
const isDryRun = process.argv[2] === '--dry-run';
const tweetFilter = new TweetFilter(config.exclude, languages);

async function getApiKey(user) {
if (!user.apiKey) {
console.log(`Logging in as ${user.username}...`);
async function getApiKey(user, { retry = true } = {}) {
if (user.apiKey) {
return user.apiKey;
}

user.apiKey = await new Rettiwt().auth.login(user.email, user.username, user.password);
while (true) {
try {
console.log(`Logging in as ${user.username}...`);

console.log('Logged in!');
}
await new Promise(resolve => setTimeout(resolve, loginDelayMs));

user.apiKey = await new Rettiwt().auth.login(user.email, user.username, user.password);

return user.apiKey;
console.log('Logged in!');

return user.apiKey;
} catch (e) {
console.error(`Unable to log in: ${e.message}`);

if (!retry) {
throw e;
}
}
}
}

(async () => {
while (true) {
const rettiwt = new Rettiwt({ apiKey: await getApiKey(config.users[0]) });
const rettiwt = new Rettiwt({ apiKey: await getApiKey(config.users[0], { retry: true }) });

console.log(isDryRun ? 'Looking for new tweets (dry run)...' : 'Looking for new tweets...');

Expand All @@ -56,7 +71,7 @@ async function getApiKey(user) {

try {
if (!isDryRun) {
const rettiwt = new Rettiwt({ apiKey: await getApiKey(user) });
const rettiwt = new Rettiwt({ apiKey: await getApiKey(user, { retry: false }) });

await rettiwt.tweet.retweet(tweet.id);
}
Expand Down

0 comments on commit 89cc194

Please sign in to comment.