rss parser
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "updaterweblibrary",
|
"name": "updaterweblibrary",
|
||||||
"version": "1.0.3",
|
"version": "1.0.4",
|
||||||
"description": "OTA Updater App frontend library",
|
"description": "OTA Updater App frontend library",
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
|
|||||||
54
src/index.ts
54
src/index.ts
@@ -4,7 +4,7 @@ import { parseXml, XmlDocument, XmlElement, XmlText } from "@rgrove/parse-xml";
|
|||||||
export default class Updater {
|
export default class Updater {
|
||||||
public archiveURL: string;
|
public archiveURL: string;
|
||||||
public feedType: string;
|
public feedType: string;
|
||||||
constructor(archiveURL: string = "/", feedType: string = "rss") {
|
constructor(archiveURL: string = "/", feedType: string = "atom") {
|
||||||
this.archiveURL = archiveURL;
|
this.archiveURL = archiveURL;
|
||||||
this.feedType = feedType;
|
this.feedType = feedType;
|
||||||
}
|
}
|
||||||
@@ -17,7 +17,8 @@ export default class Updater {
|
|||||||
return text;
|
return text;
|
||||||
}
|
}
|
||||||
|
|
||||||
private getVersionDetails(entry: XmlElement): types.versionNotes {
|
// atom feeds
|
||||||
|
private atomGetVersionDetails(entry: XmlElement): types.versionNotes {
|
||||||
let outEntry: types.versionNotes = {title: "", date: new Date, link: "", html: ""};
|
let outEntry: types.versionNotes = {title: "", date: new Date, link: "", html: ""};
|
||||||
entry.children.forEach((elm) => {
|
entry.children.forEach((elm) => {
|
||||||
let element = elm as XmlElement;
|
let element = elm as XmlElement;
|
||||||
@@ -37,7 +38,7 @@ export default class Updater {
|
|||||||
return outEntry;
|
return outEntry;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async getArchive(): Promise<types.versionNotes[]> {
|
private async atomGetArchive(): Promise<types.versionNotes[]> {
|
||||||
const rawArchive: string = await this.getRawArchive();
|
const rawArchive: string = await this.getRawArchive();
|
||||||
const releaseNotes: XmlDocument = parseXml(rawArchive);
|
const releaseNotes: XmlDocument = parseXml(rawArchive);
|
||||||
|
|
||||||
@@ -47,11 +48,56 @@ export default class Updater {
|
|||||||
if (elm.type == "element") {
|
if (elm.type == "element") {
|
||||||
const element = elm as XmlElement;
|
const element = elm as XmlElement;
|
||||||
if (element.name == "entry") {
|
if (element.name == "entry") {
|
||||||
output.push(this.getVersionDetails(element))
|
output.push(this.atomGetVersionDetails(element))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// rss feeds
|
||||||
|
private rssGetVersionDetails(entry: XmlElement): types.versionNotes {
|
||||||
|
let outEntry: types.versionNotes = {title: "", date: new Date, link: "", html: ""};
|
||||||
|
entry.children.forEach((elm) => {
|
||||||
|
let element = elm as XmlElement;
|
||||||
|
if (element.name == "title") {
|
||||||
|
outEntry.title = (element.children[0] as XmlText).text;
|
||||||
|
}
|
||||||
|
else if (element.name == "pubDate") {
|
||||||
|
outEntry.date = (element.children[0] as XmlText).text;
|
||||||
|
}
|
||||||
|
else if (element.name == "link") {
|
||||||
|
outEntry.link = (element.children[0] as XmlText).text;
|
||||||
|
} else if (element.name == "content") {
|
||||||
|
outEntry.html = (element.children[0] as XmlText).text;
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
return outEntry;
|
||||||
|
}
|
||||||
|
|
||||||
|
private async rssGetArchive(): Promise<types.versionNotes[]> {
|
||||||
|
const rawArchive: string = await this.getRawArchive();
|
||||||
|
const releaseNotes: XmlDocument = parseXml(rawArchive);
|
||||||
|
|
||||||
|
const output: types.versionNotes[] = [];
|
||||||
|
((releaseNotes.children[0] as XmlElement).children[1] as XmlElement).children.forEach((elm) => {
|
||||||
|
if (elm.type == "element") {
|
||||||
|
const element = elm as XmlElement;
|
||||||
|
if (element.name == "item") {
|
||||||
|
output.push(this.atomGetVersionDetails(element))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async getArchive(): Promise<types.versionNotes[]> {
|
||||||
|
if (this.feedType == "atom") {
|
||||||
|
return this.atomGetArchive()
|
||||||
|
} else if (this.feedType == "rss"){
|
||||||
|
return this.rssGetArchive()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
export type versionNotes = {
|
export type versionNotes = {
|
||||||
title: string;
|
title: string;
|
||||||
date: Date;
|
date: Date | string;
|
||||||
link: string;
|
link: string;
|
||||||
html: string;
|
html: string;
|
||||||
}
|
}
|
||||||
@@ -2,10 +2,23 @@ import { expect, expectTypeOf, test } from "vitest";
|
|||||||
import Updater from "../src/index.ts";
|
import Updater from "../src/index.ts";
|
||||||
import * as types from "../src/types.ts";
|
import * as types from "../src/types.ts";
|
||||||
|
|
||||||
test("get archive from gitea", async (): Promise<void> => {
|
test("get an atom feed from gitea", async (): Promise<void> => {
|
||||||
const updater = new Updater("https://git.emaker.limited/MicrocontrollerCD/SoftwareRelease/releases", "atom");
|
const updater = new Updater("https://git.emaker.limited/MicrocontrollerCD/SoftwareRelease/releases", "atom");
|
||||||
const res = await updater.getArchive();
|
const res = await updater.getArchive();
|
||||||
expectTypeOf(res).toBeArray();
|
|
||||||
expectTypeOf(res[0]).toMatchObjectType<types.versionNotes>();
|
|
||||||
expect(res).not.toBe(null);
|
expect(res).not.toBe(null);
|
||||||
|
expect(res.length).toBeGreaterThan(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("get an rss feed from gitea", async(): Promise<void> => {
|
||||||
|
const updater = new Updater("https://git.emaker.limited/MicrocontrollerCD/SoftwareRelease/releases", "rss");
|
||||||
|
const res = await updater.getArchive();
|
||||||
|
expect(res).not.toBe(null);
|
||||||
|
expect(res.length).toBeGreaterThan(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("get an atom feed from github", async (): Promise<void> => {
|
||||||
|
const updater = new Updater("https://github.com/chopster44/Phaser_3_pong/releases", "atom");
|
||||||
|
const res = await updater.getArchive();
|
||||||
|
expect(res).not.toBe(null);
|
||||||
|
expect(res.length).toBeGreaterThan(0);
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user