Skip to content

Commit

Permalink
basics are working
Browse files Browse the repository at this point in the history
  • Loading branch information
neferin12 committed Apr 21, 2021
1 parent fe60c06 commit b4d3dca
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 13 deletions.
9 changes: 5 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
import dotenv from "dotenv";

dotenv.config();

import Client from "./matrix/Client";
import Feed from "./rss/Feed";

import {Database, JsonDB} from "./db";

const db: Database = new JsonDB();

const feeds: Array<Feed> = [];
const client = new Client(db);
const client = new Client(db, ((roomID, url) => feeds.push(new Feed(url, roomID))));

(async () => {
await client.start();
console.log("Client started");
const entries = db.getAllEntries();
for (const roomID of Object.keys(entries)) {
console.log(roomID);
feeds.push(new Feed(entries[roomID].url, roomID, entries[roomID].date));
feeds.push(new Feed(entries[roomID].url, roomID, new Date(entries[roomID].date)));
}

for (const feed of feeds) {
feed.getFeed(db);
feed.startSync(db, client);
}
})();
10 changes: 8 additions & 2 deletions src/matrix/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Client {
private readonly client: MatrixClient;
private readonly db: Database;

constructor(db: Database) {
constructor(db: Database, newFeed:(roomID:string, url:string) => void) {
this.db = db;

const storage = new SimpleFsStorageProvider("bot.json");
Expand All @@ -26,7 +26,9 @@ class Client {
if (body.startsWith("!status")) {
this.client.sendNotice(roomId, "Studon URL is " + this.db.getEntry(roomId)?.url);
} else if (body.startsWith("!set")) {
this.db.setEntry(roomId, {url: body.split(" ")[1]});
const url = body.split(" ")[1];
this.db.setEntry(roomId, {url});
newFeed(roomId, url);
this.client.sendNotice(roomId, "Studon URL was set");
} else if (body.startsWith("!reset")) {
this.db.deleteEntry(roomId);
Expand All @@ -40,6 +42,10 @@ class Client {
return this.client.start();
}

sendHTMLMessage(message: string, roomID: string): Promise<string> {
return this.client.sendHtmlNotice(roomID, message);
}

}

export default Client;
30 changes: 23 additions & 7 deletions src/rss/Feed.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import RSSItem from "./RSSItem";
import Parser from "rss-parser"
import {Database} from "../db";
import Client from "../matrix/Client";

export default class Feed {
private feed: Array<RSSItem> = [];
private url: string;
private parser: Parser;
private lastSync: Date;
private readonly roomID: string;
readonly roomID: string;

constructor(url: string, roomID: string, lastSync: Date = new Date(0)) {
this.lastSync = lastSync;
Expand All @@ -16,24 +17,39 @@ export default class Feed {
}

setUrl(url: string): void {
if(!url) return;
if (!url) return;
this.url = url;
this.parser = new Parser();
}

getFeed(db: Database): Promise<void> {
startSync(db: Database, client: Client): void {
console.log("sync")
this.getFeed(db).then(res => {
for (const item of res) {
client.sendHTMLMessage(item.buildHTMLMessage(), this.roomID);
}
}).catch(console.error).finally(() => setTimeout(() => this.startSync.call(this, db, client), 1000*60));
}

private getFeed(db: Database): Promise<Array<RSSItem>> {
// eslint-disable-next-line no-async-promise-executor
return new Promise(async (resolve, reject) => {
try{
try {
const data: Array<Parser.Item> = (await this.parser.parseURL(this.url)).items;
for (const datum of data) {
this.feed.push(new RSSItem(datum));
}
this.feed = this.feed.filter(item => item.date > this.lastSync);
console.log(this.feed.length);
this.feed = this.feed.filter(item => {
if (!(item.date instanceof Date && this.lastSync instanceof Date)) {
reject("Not Dates");
}
return item.date > this.lastSync
});

this.lastSync = new Date();
db.setEntry(this.roomID, {date: this.lastSync, url: this.url})
}catch (e) {
resolve(this.feed);
} catch (e) {
reject(e);
}
});
Expand Down
3 changes: 3 additions & 0 deletions src/rss/RSSItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,7 @@ export default class RSSItem {
this.link = item.guid;
}

buildHTMLMessage(): string {
return `<h3 style="margin-bottom: 0">${this.title}</h3><p style="margin-top: 2px; color: lightgray"><b>${this.subTitle || ""}</b></p><p>${this.info}</p><hr><p>${this.content}</p><br><a href="${this.link}">Mehr</a>`
}
}

0 comments on commit b4d3dca

Please sign in to comment.