-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
38 lines (34 loc) · 1.29 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
require("dotenv").config();
const { fetchRSSFeed } = require("./utils/fetchRSS");
const { publishToMedium } = require("./utils/publishToMedium");
const { scheduleJob } = require("./utils/scheduler");
const { readPublishedPosts, storePublishedPost } = require("./utils/storage");
// Fetch and publish one RSS feed item
const syncRSSFeedToMedium = async () => {
const posts = await fetchRSSFeed();
if (posts && posts.length > 0) {
const publishedPosts = readPublishedPosts();
const unpublishedPosts = posts.filter(
(post) => !publishedPosts.includes(post.link)
);
if (unpublishedPosts.length > 0) {
const postToPublish = unpublishedPosts[0];
try {
await publishToMedium(postToPublish);
storePublishedPost(postToPublish.link);
console.log(`Post published successfully: ${postToPublish.title}`);
} catch (error) {
console.error(
`Error publishing post: ${postToPublish.title} - ${error.message}`
);
}
} else {
console.log("No new unpublished posts found.");
}
} else {
console.log("No new posts found.");
}
};
// Schedule the job to run periodically
scheduleJob("0 * * * *", syncRSSFeedToMedium); // Runs every day at 11:00 AM and 6:00 PM
console.log("RSS to Medium autopublisher is running...");