Skip to content

Commit 4a8df3e

Browse files
committed
Add maximum posts
1 parent 246ed2a commit 4a8df3e

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

config.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ stopPosts: false
2424
# (Note I cant control log messages sent by the bot library so those will still show. Just ones thrown by the bot wont)
2525
showLogs: false
2626

27+
# The maximum amount of posts it will do on every post check. Set to 0 for no limit. (Each post being posted to another instance is separate in here but itll finish up the same post before it stops)
28+
maxPosts: 5
29+
30+
# The time in milliseconds it will sleep before doing another post in the same post check
31+
postSleepDuration: 5000
32+
2733

2834
# ------------------------------------------------------------------------------
2935

main.js

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ let parser = new Parser({
1313
});
1414
console.log(`${chalk.magenta('STARTED:')} Started Bot`)
1515

16-
let { instances, feeds, markAsBot, postCheckInterval, dayCheckInterval, timezone, dayCutOff, stopPosts, showLogs } = load(readFileSync('config.yaml', 'utf8'));
16+
let { instances, feeds, markAsBot, postCheckInterval, dayCheckInterval, timezone, dayCutOff, stopPosts, showLogs, postSleepDuration, maxPosts } = load(readFileSync('config.yaml', 'utf8'));
1717

1818
markAsBot = markAsBot ?? true;
1919
postCheckInterval = postCheckInterval ?? 10;
@@ -22,6 +22,8 @@ timezone = timezone ?? 'America/Toronto';
2222
dayCutOff = dayCutOff ?? 7;
2323
stopPosts = stopPosts ?? false;
2424
showLogs = showLogs ?? false;
25+
postSleepDuration = postSleepDuration ?? 2000;
26+
maxPosts = maxPosts ?? 5;
2527

2628
log(`${chalk.grey('INSTANCES:')} ${Object.keys(instances).length} instances loaded.`)
2729
log(`${chalk.grey('FEEDS:')} ${Object.keys(feeds).length} feeds loaded.`)
@@ -83,9 +85,6 @@ const db = new sqlite3.Database('mega.sqlite3', (err) => {
8385
// -----------------------------------------------------------------------------
8486
// Main Bot Code
8587

86-
// Function for rate limits
87-
const sleepDuration = process.env.RATE_LIMIT_MS || 2000;
88-
8988
function sleep(ms) {
9089
return new Promise(resolve => setTimeout(resolve, ms));
9190
}
@@ -202,6 +201,8 @@ const bot = new LemmyBot.LemmyBot({
202201
}
203202
});
204203

204+
let donePosts = 0;
205+
205206
for (const item of commonItems) {
206207
let pin_days = 0;
207208
const itemDate = new Date((feed.datefield ? item[feed.datefield] : item.pubDate).trim());
@@ -232,6 +233,11 @@ const bot = new LemmyBot.LemmyBot({
232233

233234
for (const [instance, communities] of Object.entries(instances)) {
234235
for (const [community, value] of Object.entries(communities)) {
236+
if (maxPosts != 0 && donePosts >= maxPosts) {
237+
log(`${chalk.green('COMPLETE:')} Max posts reached.`);
238+
return;
239+
}
240+
235241
if (Object.values(value).includes(name)) {
236242
log(`${chalk.grey('CREATING:')} post for link ${item.link} in ${community }`);
237243
const communityId = await getCommunityId({ name: community, instance: instance });
@@ -243,6 +249,7 @@ const bot = new LemmyBot.LemmyBot({
243249
body = parseTags(body);
244250

245251
try {
252+
donePosts++;
246253
await createPost({
247254
name: title,
248255
body: body,
@@ -252,7 +259,7 @@ const bot = new LemmyBot.LemmyBot({
252259
} catch (e) {
253260
console.error(e);
254261
}
255-
await sleep(sleepDuration);
262+
await sleep(postSleepDuration);
256263
}
257264
}
258265
}
@@ -331,13 +338,27 @@ let tags = {
331338
'<br/>': '\n',
332339
'<br />': '\n',
333340
'&nbsp;': ' ',
341+
'<ol>': '',
342+
'</ol>': '',
343+
'<li>': '- ',
344+
'</li>': '',
345+
'<ul>': '',
346+
'</ul>': '',
347+
'&nbsp;': ' ',
334348
}
335349

336350
function parseTags(input) {
337351
let output = input;
338352
for (const [key, value] of Object.entries(tags)) {
339353
output = output.replace(key, value);
340354
}
355+
356+
// Fix links
357+
output = output.replace(/<a href="([^"]+)">([^<]+)<\/a>/g, '[$2]($1)');
358+
359+
// Fix font color
360+
output = output.replace(/<font color="([^"]+)">([^<]+)<\/font>/g, '$2');
361+
341362
return output;
342363
}
343364

0 commit comments

Comments
 (0)