-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
150 lines (123 loc) · 5.33 KB
/
main.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
const axios = require('axios');
const xml2js = require('xml2js');
const snoowrap = require('snoowrap');
const cron = require('node-cron');
const http = require('http');
console.log('Starting the RSS to Reddit poster app...');
// Reddit API credentials
const r = new snoowrap({
userAgent: 'MyRSSPoster/1.0',
clientId: '#',
clientSecret: '#',
username: '#',
password: '#'
});
console.log('Snoowrap instance created with Reddit credentials');
const subreddit = 'mysubreddit';
const rssUrl = 'https://example.com/rss/';
let lastCheckedDate = new Date(0);
console.log(`Subreddit set to: ${subreddit}`);
console.log(`RSS URL set to: ${rssUrl}`);
console.log(`Initial lastCheckedDate set to: ${lastCheckedDate}`);
async function isUrlAlreadyPosted(url) {
console.log(`Checking if URL is already posted: ${url}`);
try {
const recentPosts = await r.getSubreddit(subreddit).getNew({ limit: 100 });
for (const post of recentPosts) {
if (post.selftext.includes(url)) {
console.log(`URL found in post: ${post.title}`);
return true;
}
}
console.log(`URL not found in recent posts`);
return false;
} catch (error) {
console.error(`Error checking if URL is already posted: ${error.message}`);
return false;
}
}
async function checkAndPostNewArticles() {
console.log('Starting checkAndPostNewArticles function...');
try {
console.log(`Fetching RSS feed from ${rssUrl}...`);
const response = await axios.get(rssUrl);
console.log('RSS feed fetched successfully');
console.log('Parsing XML data...');
const result = await xml2js.parseStringPromise(response.data);
console.log('XML data parsed successfully');
const items = result.rss.channel[0].item;
console.log(`Found ${items.length} items in the RSS feed`);
let articlePosted = false;
for (const item of items) {
const pubDate = new Date(item.pubDate[0]);
console.log(`Checking item published on: ${pubDate}`);
if (pubDate > lastCheckedDate) {
const title = item.title[0];
const link = item.link[0];
let description = item.description[0].replace(/<!\[CDATA\[|\]\]>/g, '').trim();
description = description.replace(/\[.*$/, '').trim();
description = description.replace(/\.{3,}$|…$/, '').trim();
const lastPeriodIndex = description.lastIndexOf('.');
if (lastPeriodIndex !== -1) {
description = description.substring(0, lastPeriodIndex + 1).trim();
}
console.log(`Cleaned description: "${description}"`);
console.log(`Checking if article is already posted: ${link}`);
const alreadyPosted = await isUrlAlreadyPosted(link);
console.log(`Article already posted: ${alreadyPosted}`);
if (!alreadyPosted) {
console.log(`New article found: "${title}"`);
console.log(`Posting to Reddit...`);
try {
const submission = await r.getSubreddit(subreddit).submitSelfpost({
title: title,
text: `${description}\n\nSource: ${link}`
});
console.log(`Successfully posted: ${title}`);
try {
await submission.approve();
console.log('Post approved successfully');
} catch (approveError) {
console.error(`Error approving post: ${approveError.message}`);
}
lastCheckedDate = pubDate;
console.log(`Updated lastCheckedDate to: ${lastCheckedDate}`);
articlePosted = true;
break;
} catch (postError) {
console.error(`Error posting to Reddit: ${postError.message}`);
}
} else {
console.log(`Article "${title}" has already been posted. Checking next article.`);
}
} else {
console.log(`No new articles found since last check`);
break;
}
}
if (!articlePosted) {
console.log('No new articles found to post in this check');
}
} catch (error) {
console.error('Error occurred:', error.message);
console.error('Stack trace:', error.stack);
}
console.log('Finished checkAndPostNewArticles function');
}
console.log('Setting up cron job to run every hour...');
// Run the check every hour
cron.schedule('0 * * * *', () => {
console.log('Cron job triggered, running checkAndPostNewArticles...');
checkAndPostNewArticles();
});
const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('RSS to Reddit poster is running');
});
const port = process.env.PORT || 8080;
server.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
console.log('RSS to Reddit poster is now running. Press Ctrl+C to stop.');
console.log('Performing initial check...');
checkAndPostNewArticles();