import fs from 'node:fs';
export class RssGenerator {
maxItem = 10;
items = [];
channel = {};
constructor(options) {
this.options = options;
}
// TODO: move to pass an option parameter and iterate over the key/values
setChannel (title, description, link, pubDate) {
this.channel.title = title;
this.channel.description = description;
this.channel.link = link;
this.channel.pubDate = pubDate;
return this;
}
addItem (item) {
if (!this.isItemValid(item)) {
console.error('[RssGenerator] addItem: item invalid.', item);
return this;
}
this.items.push(item);
if (this.items.lenght > this.maxItem) {
this.items.shift();
}
return this;
}
isItemValid(item) {
return true;
}
generateFile() {
let data = '';
data += '';
data += this.#generateChannelInfos();
data += this.#generateItems();
data += '';
this.#saveFile(data);
return this;
}
#generateChannelInfos() {
let infos = '';
infos += `
${this.channel.title}\n`;
infos += `${this.channel.description}\n`;
infos += `${this.channel.link}\n`;
infos += `${RssGenerator.formatDate(this.channel.pubDate)}\n`;
infos += `\n`;
return infos;
}
#generateItems() {
let data = '';
for (const item of this.items) {
data += '- \n';
data += `${item.title}\n`;
data += `${item.link}\n`;
data += `${item.link}\n`;
data += `${item.description}\n`;
data += '
\n';
}
return data;
}
static formatDate(date) {
return new Intl.DateTimeFormat("en-GB", {
hour12: false,
weekday: "short",
year: "numeric",
month: "short",
day: "2-digit",
hour: "2-digit",
minute: "2-digit",
second: "2-digit",
timeZone: "Europe/London",
}).formatToParts(date)
.filter(el => el.type !== "literal")
.reduce((acc, cur) => {
switch (cur.type) {
case "weekday":
acc += `${cur.value}, `;
break;
case "hour":
case "minute":
acc += `${cur.value}:`;
break;
case "timeZoneName":
acc += cur.value;
break;
default:
acc += `${cur.value} `;
break;
}
return acc;
}, '')
.concat(" GMT");
}
#saveFile(data) {
try {
fs.writeFileSync(this.options.filePath, data, { encoding: 'utf8' });
} catch (err) {
console.error('[RssGenerator] saveFile - ERROR');
console.error(err);
}
}
}