Skip to content

Commit

Permalink
Updated feed parsing to handle multiple link tags.
Browse files Browse the repository at this point in the history
  • Loading branch information
aegrumet committed Mar 8, 2024
1 parent 8699424 commit 1069ca5
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 12 deletions.
26 changes: 21 additions & 5 deletions src/interfaces/rss.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,33 @@ export const RssWithPodrollSchema = z.object({
}),
});

export const RssLinkSchema = z.object({
_text: z.string(),
});

export type RssLink = z.infer<typeof RssLinkSchema>;

export const RssLinkWithRelSchema = z.object({
_attributes: z.object({
rel: z.string(),
type: z.optional(z.string()),
href: z.optional(z.string()),
xmlns: z.optional(z.string()),
}),
});

export type RssLinkWithRel = z.infer<typeof RssLinkWithRelSchema>;

export const RssFeedInfoSchema = z.object({
rss: z.object({
channel: z.object({
title: z.object({
_text: z.string(),
}),
link: z.optional(
z.object({
_text: z.string(),
})
),
link: z.union([
RssLinkSchema,
z.array(z.union([RssLinkSchema, RssLinkWithRelSchema])),
]),
description: z.optional(
z.object({
_text: z.string(),
Expand Down
48 changes: 41 additions & 7 deletions src/lib/podroll.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { xml2js, Context } from "../../deps.ts";
import umbilicalUserAgent from "../config.ts";
import { podcastByGuid } from "./piapi.ts";
import { RssFeedInfo, RssFeedInfoSchema } from "../interfaces/rss.ts";
import {
RssFeedInfo,
RssFeedInfoSchema,
RssLink,
RssLinkWithRel,
} from "../interfaces/rss.ts";
import { FeedForOpml, FeedForOpmlSchema } from "../interfaces/opml.ts";
import { z } from "../../deps.ts";

export async function feedInfoFromFeedGuid(
feedGuid: string,
Expand Down Expand Up @@ -57,19 +63,47 @@ export async function feedInfoFromFeedUrl(
return null;
}

const parseResult = RssFeedInfoSchema.safeParse(json as RssFeedInfo);
// deno-lint-ignore no-explicit-any
let parseResult: any;
let success = false;

if (!parseResult.success) {
try {
parseResult = RssFeedInfoSchema.parse(json as RssFeedInfo);
success = true;
} catch (err) {
if (err instanceof z.ZodError) {
console.log(err.issues);
}
}

if (!success) {
return null;
}

let link = "";
if (parseResult.rss.channel.link !== undefined) {
if (Array.isArray(parseResult.rss.channel.link)) {
const arr = parseResult.rss.channel.link as unknown as Array<
RssLinkWithRel | RssLink
>;
for (let i = 0; i < arr.length; i++) {
if ("_text" in arr[i]) {
link = (arr[i] as RssLink)._text;
break;
}
}
} else {
link = parseResult.rss.channel.link._text;
}
}

return {
feed: {
url: feedUrl,
title: parseResult.data.rss.channel.title._text,
link: parseResult.data.rss.channel.link!._text ?? "",
description: parseResult.data.rss.channel.description!._text ?? "",
image: parseResult.data.rss.channel.image!.url._text ?? "",
title: parseResult.rss.channel.title._text,
link,
description: parseResult.rss.channel.description!._text ?? "",
image: parseResult.rss.channel.image!.url._text ?? "",
},
};
}

0 comments on commit 1069ca5

Please sign in to comment.