From 9f78efb7b4a6c5c2926ca4a60acc71aa19bfba5d Mon Sep 17 00:00:00 2001 From: Arkadiusz Wieczorek Date: Tue, 26 Oct 2021 14:26:41 +0200 Subject: [PATCH] Moved repository to common gitea repository, update link to our website --- README.md | 1 + index.ts | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 22 +++++++++++++++ tsconfig.json | 14 +++++++++ 4 files changed, 115 insertions(+) create mode 100644 index.ts create mode 100644 package.json create mode 100644 tsconfig.json diff --git a/README.md b/README.md index af2c1c4..f0f6ceb 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,3 @@ # xml-expander +This code is daemonized using systemd - look for /lib/systemd/system/xml-expander.service \ No newline at end of file diff --git a/index.ts b/index.ts new file mode 100644 index 0000000..7769f45 --- /dev/null +++ b/index.ts @@ -0,0 +1,78 @@ +import Koa from "koa"; +import Axios from "axios"; +import * as xml2js from "xml2js"; + +const app = new Koa(); + +app.use(async (ctx) => { + try { + const { data } = await Axios.get( + "https://podcast.midline.pl/api/v1/channels/Midline/rss" + ); + const builder = new xml2js.Builder(); + const parsed_data = await xml2js.parseStringPromise(data); + parsed_data.rss.channel[0]["itunes:author"] = + "Arkadiusz Wieczorek, Kuba Orlik"; + parsed_data.rss.channel[0]["itunes:owner"][0]["itunes:email"] = + "kontakt@midline.pl"; + parsed_data.rss.channel[0]["itunes:owner"][0]["itunes:name"] = + "Arkadiusz Wieczorek, Kuba Orlik"; + parsed_data.rss.channel[0]["link"] = "https://www.internet-czas-dzialac.pl"; + parsed_data.rss.channel[0][ + "itunes:keywords" + ][0] = parsed_data.rss.channel[0]["itunes:keywords"][0] + .split(" ") + .toString(); + parsed_data.rss.channel[0][ + "itunes:summary" + ][0] = parsed_data.rss.channel[0]["itunes:summary"][0].replace( + /(<([^>]+)>)/gi, + "" + ); + + for ( + let i = 0; + i < parsed_data.rss.channel[0]["atom:link"].length; + i++ + ) { + const atomLink = parsed_data.rss.channel[0]["atom:link"][i]; + + if (atomLink.$.rel === "self") { + atomLink.$.href = "https://podcast.midline.pl/feed.xml"; + } + } + + let last_index = 1; + for (let i = 0; i < parsed_data.rss.channel[0]["item"].length; i++) { + const item = parsed_data.rss.channel[0]["item"][i]; + + item["guid"][0].$.isPermaLink = item["guid"][0].$.isPermalink; + delete item["guid"][0].$.isPermalink; + item["itunes:subtitle"][0] = + item["itunes:subtitle"][0].slice(0, -2) + "…"; + item["itunes:summary"][0] = item["itunes:summary"][0].replace( + /(<([^>]+)>)/gi, + "" + ); + if (item["title"][0].match(/#[0-9]*/)[0].substring(1) !== "0") { + try { + last_index = item["title"][0] + .match(/#[1-9][0-9]*/)[0] + .substring(1); + } catch (e) { + last_index = last_index + 1; + } + item["itunes:episode"] = [last_index]; + } + } + + ctx.body = builder.buildObject(parsed_data); + ctx.type = "application/rss+xml"; + } catch (e) { + console.error(e); + ctx.body = "error"; + ctx.status = 500; + } +}); + +app.listen(3000); diff --git a/package.json b/package.json new file mode 100644 index 0000000..6f50ed0 --- /dev/null +++ b/package.json @@ -0,0 +1,22 @@ +{ + "name": "xml-expander", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "build": "tsc" + }, + "author": "", + "license": "ISC", + "dependencies": { + "axios": "^0.19.2", + "koa": "^2.12.0", + "xml2js": "^0.4.23" + }, + "devDependencies": { + "@types/axios": "^0.14.0", + "@types/koa": "^2.11.3", + "@types/xml2js": "^0.4.5", + "typescript": "^3.9.5" + } +} \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..8ddb31c --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,14 @@ +{ + "compilerOptions": { + "moduleResolution": "node", + "noImplicitAny": true, + "noImplicitThis": true, + "sourceMap": true, + "module": "commonjs", + "target": "es6", + "allowJs": false, + "strictNullChecks": true, + "esModuleInterop": true, + "outDir": "build" + } +}