-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·210 lines (182 loc) · 5.33 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
#!/usr/bin/env node
const RSSParser = require("rss-parser");
const inquirer = require("inquirer").default;
const fs = require("fs");
const path = require("path");
const { exec } = require("child_process");
const { JSDOM } = require("jsdom");
const p = require("picocolors");
const parser = new RSSParser();
const homeDir = require("os").homedir();
const configFilePath = path.join(homeDir, ".feedbombrc");
process.stdout.write("\x1Bc");
let feeds = [];
const loadFeeds = () => {
if (fs.existsSync(configFilePath)) {
const data = fs.readFileSync(configFilePath);
feeds = JSON.parse(data);
}
};
const saveFeeds = () => {
fs.writeFileSync(configFilePath, JSON.stringify(feeds, null, 2));
};
const addFeed = async () => {
const { feedName, feedUrl } = await inquirer.prompt([
{
type: "input",
name: "feedName",
message: "Enter a name for the feed:",
},
{
type: "input",
name: "feedUrl",
message: "Enter the feed URL:",
},
]);
feeds.push({ name: feedName, url: feedUrl });
saveFeeds();
};
const manageFeeds = async () => {
if (feeds.length === 0) {
console.log("No feeds available to manage. Please add a feed.");
return;
}
const choices = feeds.map((feed) => ({
name: feed.name,
value: feed,
checked: true,
}));
const { selectedFeeds } = await inquirer.prompt([
{
type: "checkbox",
name: "selectedFeeds",
message: "Select feeds to keep:",
choices: choices,
},
]);
feeds = selectedFeeds;
saveFeeds();
process.stdout.write("\x1Bc");
};
async function fetchFeed(url) {
try {
const feed = await parser.parseURL(url);
return feed.items.map((item) => {
const html = `${item.contentSnippet || item.summary || item.content}`
.replaceAll("<br>", "\n")
.replaceAll("<br />", "\n")
.replaceAll("<br/>", "\n");
const dom = new JSDOM(html);
const cleanedHTML = dom.window.document.body.textContent;
return {
title: item.title,
link: item.link,
pubDate: item.pubDate,
contentSnippet: cleanedHTML,
author: item.author,
};
});
} catch (error) {
return [];
}
}
async function runRSSReader() {
try {
if (feeds.length === 0) {
console.log("We couldn't find any feeds. Please add a feed.");
await addFeed();
}
while (true) {
const feedChoices = feeds
.map((feed) => feed.name)
.concat(["Add a new feed", "Manage feeds"]);
const { selectedFeed } = await inquirer.prompt([
{
type: "list",
name: "selectedFeed",
message: "Your feeds",
choices: feedChoices,
},
]);
if (selectedFeed === "Add a new feed") {
await addFeed();
continue;
} else if (selectedFeed === "Manage feeds") {
await manageFeeds();
continue;
}
const feedUrl = feeds.find((feed) => feed.name === selectedFeed).url;
const articles = await fetchFeed(feedUrl);
if (articles.length === 0) {
console.log(
p.red(
"This feed doesn't appear to have any articles. Check that the URL exists."
)
);
process.exit(1);
}
let currentIndex = 0;
while (true) {
const article = articles[currentIndex];
console.clear();
console.log(`\n${p.bold(selectedFeed)}\n`);
console.log(
`${p.green(`(${currentIndex + 1}/${articles.length})`)} ${
article.title
}`
);
console.log(`${p.italic(new Date(article.pubDate).toLocaleString())}`);
console.log("\n" + article.contentSnippet);
console.log(`\n${p.blue(`Link`)} ${article.link}`);
console.log(`\n${p.magenta("Author")} ${article.author}`);
console.log("\n---\n");
const { action } = await inquirer.prompt([
{
type: "list",
name: "action",
message: "Choose an action:",
choices: [
"Next article",
"Previous article",
"Choose another feed",
"Open link in browser",
"Quit application",
],
},
]);
if (action === "Next article") {
process.stdout.write("\x1Bc");
currentIndex = (currentIndex + 1) % articles.length;
} else if (action === "Previous article") {
process.stdout.write("\x1Bc");
currentIndex = (currentIndex - 1 + articles.length) % articles.length;
} else if (action === "Choose another feed") {
process.stdout.write("\x1Bc");
break;
} else if (action === "Open link in browser") {
const command =
process.platform === "win32"
? `start ${article.link}`
: process.platform === "darwin"
? `open ${article.link}`
: `xdg-open ${article.link}`;
exec(command, (err) => {
if (err) {
console.error("Failed to open link:", err);
}
});
} else if (action === "Quit application") {
return;
}
}
}
} catch (error) {
if (error.message === "User force closed the prompt with 0 null") {
return;
} else {
console.log("An error occurred:", error.message);
}
}
}
loadFeeds();
runRSSReader();