From e6b7142829d196ba02f117f6d6e469b6d229ff57 Mon Sep 17 00:00:00 2001 From: Kuba Orlik Date: Mon, 18 Jul 2022 11:05:12 +0200 Subject: [PATCH] Attempt at fixing the slice --- index.ts | 120 +++++++++++++++++++++++++------------------------------ 1 file changed, 55 insertions(+), 65 deletions(-) diff --git a/index.ts b/index.ts index b161ab4..303f3c0 100644 --- a/index.ts +++ b/index.ts @@ -5,74 +5,64 @@ 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, - "" - ); + 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]; + 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].length < 255 - ? item["itunes:subtitle"][0] - : item["itunes:subtitle"][0].slice(0, 255) + "…"; - 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; + 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; + const subtitle = item["itunes:subtitle"][0]; + subtitle.length < 254 ? subtitle : subtitle.slice(0, 254) + "…"; + 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);