diff --git a/rss.js b/rss.js
new file mode 100644
index 0000000..d4723b2
--- /dev/null
+++ b/rss.js
@@ -0,0 +1,101 @@
+const fs = require('fs')
+const { uuidv4 } = require('./utils')
+
+class RSS {
+
+ constructor(name) {
+ this.name = name;
+ this.filename = 'rss.xml';
+ this.feed = null;
+
+ this.read()
+ }
+
+ // reads RSS file
+ read() {
+ fs.readFile(this.filename, 'utf8', (err, content) => {
+ if (err) {
+ console.error("Unable to read file:", this.filename)
+ console.error(err);
+ return;
+ }
+
+ this.feed = content;
+ });
+
+ }
+
+ // writes RSS file
+ write(content=this.feed) {
+ fs.writeFile(this.filename, content, (err) => {
+ if (err) {
+ console.error(`Error writing to ${this.filename}:`, err);
+ } else {
+ console.log(`Successfully wrote to ${this.filename}`);
+ }
+ });
+ }
+
+ itemTemplate(times, date, url, n) {
+ const relativeDate = new Date().getTime() - (n * 100000000)
+ const time = this.formatDate(new Date(relativeDate))
+ // const currentRSSDate = this.formatDate(new Date());
+
+ return `
+ -
+
+ Vi minner om miljøbilen fra FolloRen besøker oss på ${this.name} kl ${times.from}-${times.to} den ${date}.
+ ${url}
+ ${uuidv4()}
+ ${time}
+
+
+ `;
+ }
+
+ getRSSItems() {
+
+ }
+
+ generate(times, dates, url) {
+ const description = "Viser hentetider for miljøbilen fra folloren.no";
+ const currentRSSDate = this.formatDate(new Date())
+
+ const blocks = dates.reverse().map((date, n) => this.itemTemplate(times, date, url, n))
+ this.feed = `
+
+
+
+ Miljøbilen hentetider ${this.name}
+ ${description}
+ https://github.com/kevinmidboe/miljobilen-rss
+ 2020 Example.com All rights reserved
+ ${currentRSSDate}
+ ${currentRSSDate}
+ 1800
+
+ ${blocks.join('')}
+
+
+
+ `;
+ }
+
+ formatDate(date) {
+ const days = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
+ const months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
+
+ const day = days[date.getUTCDay()];
+ const dayOfMonth = date.getUTCDate();
+ const month = months[date.getUTCMonth()];
+ const year = date.getUTCFullYear();
+ const hours = String(date.getUTCHours()).padStart(2, '0');
+ const minutes = String(date.getUTCMinutes()).padStart(2, '0');
+ const seconds = String(date.getUTCSeconds()).padStart(2, '0');
+
+ return `${day}, ${dayOfMonth} ${month} ${year} ${hours}:${minutes}:${seconds} +0000`;
+ }
+}
+
+
+module.exports = RSS
diff --git a/run.js b/run.js
index 8350d3e..3667a45 100644
--- a/run.js
+++ b/run.js
@@ -1,4 +1,4 @@
-const fs = require('fs')
+const RSS = require('./rss.js');
const CURRENT_DATE = new Date()
@@ -20,22 +20,6 @@ const CURRENT_DATE = new Date()
// real date objects. It should include logic for look-ahead
// to adress rollover.
-function writeStringToFile(filePath, content) {
- fs.writeFile(filePath, content, (err) => {
- if (err) {
- console.error(`Error writing to ${filePath}:`, err);
- } else {
- console.log(`Successfully wrote to ${filePath}`);
- }
- });
-}
-
-function uuidv4() {
- return "10000000-1000-4000-8000-100000000000".replace(/[018]/g, c =>
- (+c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> +c / 4).toString(16)
- );
-}
-
async function getSite(url) {
return fetch(url).then(async (resp) => {
if (!resp.ok) {
@@ -74,16 +58,13 @@ function getTimeForLocation(text, location) {
function getDatesForLocation(text, location) {
regexpDatesString = new RegExp(`${location}.*
((\\d+.\\d+).*)
`, "i");
datesString = text.match(regexpDatesString)[0];
- console.log(text.match(regexpDatesString))
// only care about first paragraph
- console.log("datestring", datesString)
+ // TODO make regex stop at first capture
datesString = datesString.split('')[0]
// TODO null check
- console.log("datestring", datesString)
-
regexpDates = /(\d+\.\d+)+/g
dates = datesString.match(regexpDates)
@@ -100,59 +81,7 @@ function getSolberg(site, TITLE) {
return { name, times: { from, to }, dates };
}
-function formatRSSDate(date) {
- const days = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
- const months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
-
- const day = days[date.getUTCDay()];
- const dayOfMonth = date.getUTCDate();
- const month = months[date.getUTCMonth()];
- const year = date.getUTCFullYear();
- const hours = String(date.getUTCHours()).padStart(2, '0');
- const minutes = String(date.getUTCMinutes()).padStart(2, '0');
- const seconds = String(date.getUTCSeconds()).padStart(2, '0');
-
- return `${day}, ${dayOfMonth} ${month} ${year} ${hours}:${minutes}:${seconds} +0000`;
-}
-
-function generateRSS(name, times, dates, URL) {
- const description = "Viser hentetider for miljøbilen fra folloren.no"
- const updatedDate = formatRSSDate(new Date())
-
- const blocks = dates.reverse().map((date, n) => {
- const relativeDate = new Date().getTime() - (n * 100000000)
- const time = formatRSSDate(new Date(relativeDate))
-
- return `
- -
-
- Vi minner om miljøbilen fra FolloRen besøker oss på ${name} kl ${times.from}-${times.to} den ${date}.
- ${URL}
- ${uuidv4()}
- ${time}
-
- `
- })
-
- return `
-
-
-
- Miljøbilen hentetider ${name}
- ${description}
- https://github.com/kevinmidboe/miljobilen-rss
- 2020 Example.com All rights reserved
- ${updatedDate}
- ${updatedDate}
- 1800
-
- ${blocks.join('')}
-
-
-
- `
-}
-
+// convert string DD.MM to JS date object
function websiteDateToTime(dateString) {
const date = new Date()
let [_, day, month] = dateString.match(/(\d+).(\d+)/)
@@ -168,6 +97,7 @@ function websiteDateToTime(dateString) {
return date
}
+// convert JS date object to DD.MM string
function timeToWebsiteDate(date) {
const day = date.getDate()
const month = date.getMonth() + 1
@@ -182,6 +112,8 @@ function relevantDates(allDates) {
let index = 0;
let date = 0
+ // this selects all dates before current date AND the
+ // next one since index incrementation is after push
while (date <= CURRENT_DATE) {
date = websiteDateToTime(allDates[index])
@@ -192,6 +124,14 @@ function relevantDates(allDates) {
return relevantDates
}
+// fetch websites
+// parse for name, time and dates
+// convert to JS dates
+// parse RSS feed
+// convert to JS dates
+// add dates not in feed
+// write feed
+
async function main() {
const URL = "https://folloren.no/levering-av-avfall/miljobilen"
site = await getSite(URL);
@@ -212,8 +152,9 @@ async function main() {
// todo relevant dates elsewhere
dates = relevantDates(dates)
console.log("rel dates:", dates)
- rss = generateRSS(name, times, dates, URL)
- writeStringToFile('rss.xml', rss)
+ const rss = new RSS(name);
+ rss.generate(times, dates, URL)
+ rss.write()
}
try {
diff --git a/utils.js b/utils.js
new file mode 100644
index 0000000..d370d0e
--- /dev/null
+++ b/utils.js
@@ -0,0 +1,9 @@
+function uuidv4() {
+ return "10000000-1000-4000-8000-100000000000".replace(/[018]/g, c =>
+ (+c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> +c / 4).toString(16)
+ );
+}
+
+module.exports = {
+ uuidv4
+}