97 lines
3.1 KiB
JavaScript
97 lines
3.1 KiB
JavaScript
import { parseXml } from '@rgrove/parse-xml';
|
|
|
|
class Updater {
|
|
archiveURL;
|
|
feedType;
|
|
constructor(archiveURL = "/", feedType = "atom") {
|
|
this.archiveURL = archiveURL;
|
|
this.feedType = feedType;
|
|
}
|
|
async getRawArchive() {
|
|
const res = await fetch(`http://cors.emaker.limited/?url=${this.archiveURL}.${this.feedType}`, {
|
|
// "mode": "cors"
|
|
});
|
|
const text = await res.text();
|
|
return text;
|
|
}
|
|
// atom feeds
|
|
atomGetVersionDetails(entry) {
|
|
let outEntry = { title: "", date: new Date, link: "", html: "" };
|
|
entry.children.forEach((elm) => {
|
|
let element = elm;
|
|
if (element.name == "title") {
|
|
outEntry.title = element.children[0].text;
|
|
}
|
|
else if (element.name == "updated") {
|
|
outEntry.date = new Date(element.children[0].text);
|
|
}
|
|
else if (element.name == "link") {
|
|
outEntry.link = element.attributes["href"];
|
|
}
|
|
else if (element.name == "content") {
|
|
outEntry.html = element.children[0].text;
|
|
}
|
|
});
|
|
return outEntry;
|
|
}
|
|
async atomGetArchive() {
|
|
const rawArchive = await this.getRawArchive();
|
|
const releaseNotes = parseXml(rawArchive);
|
|
const output = [];
|
|
// console.dir(releaseNotes)
|
|
releaseNotes.children[0].children.forEach((elm) => {
|
|
if (elm.type == "element") {
|
|
const element = elm;
|
|
if (element.name == "entry") {
|
|
output.push(this.atomGetVersionDetails(element));
|
|
}
|
|
}
|
|
});
|
|
return output;
|
|
}
|
|
// rss feeds
|
|
rssGetVersionDetails(entry) {
|
|
let outEntry = { title: "", date: new Date, link: "", html: "" };
|
|
entry.children.forEach((elm) => {
|
|
let element = elm;
|
|
if (element.name == "title") {
|
|
outEntry.title = element.children[0].text;
|
|
}
|
|
else if (element.name == "pubDate") {
|
|
outEntry.date = element.children[0].text;
|
|
}
|
|
else if (element.name == "link") {
|
|
outEntry.link = element.children[0].text;
|
|
}
|
|
else if (element.name == "content") {
|
|
outEntry.html = element.children[0].text;
|
|
}
|
|
});
|
|
return outEntry;
|
|
}
|
|
async rssGetArchive() {
|
|
const rawArchive = await this.getRawArchive();
|
|
const releaseNotes = parseXml(rawArchive);
|
|
const output = [];
|
|
releaseNotes.children[0].children[1].children.forEach((elm) => {
|
|
if (elm.type == "element") {
|
|
const element = elm;
|
|
if (element.name == "item") {
|
|
output.push(this.atomGetVersionDetails(element));
|
|
}
|
|
}
|
|
});
|
|
return output;
|
|
}
|
|
async getArchive() {
|
|
if (this.feedType == "atom") {
|
|
return this.atomGetArchive();
|
|
}
|
|
else if (this.feedType == "rss") {
|
|
return this.rssGetArchive();
|
|
}
|
|
}
|
|
}
|
|
|
|
export { Updater as default };
|