Skip to content

Commit

Permalink
allow non-mpeg audio (#91)
Browse files Browse the repository at this point in the history
fixes #90
  • Loading branch information
wydengyre authored Feb 24, 2024
1 parent fe4c09f commit 322625a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
6 changes: 4 additions & 2 deletions packages/rai/feed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class RssConvertor {
const podcast: Podcast = {
title: feed.title,
description: feed.podcast_info.description,
// TODO: maybe this podcasts have this info?
// TODO: maybe the podcasts have this info?
language: "it",
image,
items: results,
Expand All @@ -111,6 +111,7 @@ class RssConvertor {
url: mediaInfo.url,
length: mediaInfo.size,
image,
contentType: mediaInfo.type,
};
}
}
Expand All @@ -132,6 +133,7 @@ type PodcastItem = {
url: URL;
length: number;
image: URL;
contentType: string;
};

function podcastRss(p: Podcast): string {
Expand All @@ -158,7 +160,7 @@ function itemRss(pi: PodcastItem): string {
<description>${cdata(pi.description)}</description>
<guid isPermaLink="false">${pi.guid}</guid>
<pubDate>${pi.pubDate.toUTCString()}</pubDate>
<enclosure url="${pi.url}" length="${pi.length}" type="audio/mpeg"/>
<enclosure url="${pi.url}" length="${pi.length}" type="${pi.contentType}"/>
<itunes:summary>${cdata(pi.description)}</itunes:summary>
<itunes:explicit>false</itunes:explicit>
<itunes:image href="${pi.image}"/>
Expand Down
1 change: 1 addition & 0 deletions packages/rai/media.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ async function fetchInfoSuccess() {
assert.deepStrictEqual(info, {
url: mediaUrl,
size: 123456789,
type: "audio/mpeg",
});
}
16 changes: 7 additions & 9 deletions packages/rai/media.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type MediaUrl = URL;
type MediaInfo = {
url: URL;
size: number;
type: string;
};

const relinkerRe = /^\?cont=[a-zA-Z0-9]+$/;
Expand All @@ -32,21 +33,18 @@ const mkFetchInfo =
};
const resp = await fetchWithErr(url, chromeHeadInit);

const expectedContentType = "audio/mpeg";
const contentType = resp.headers.get("content-type");
if (contentType !== expectedContentType) {
throw new Error(
`Invalid content type: ${contentType}, wanted ${expectedContentType}`,
);
}

const contentLength = resp.headers.get("content-length");
const length = Number(contentLength);
if (Number.isNaN(length)) {
throw new Error(`Invalid content length: ${contentLength}`);
}

return { url: new URL(resp.url), size: length };
const type = resp.headers.get("content-type");
if (type === null) {
throw new Error("Missing content type");
}

return { url: new URL(resp.url), size: length, type };
};

function mkMediaUrl(urlStr: string): MediaUrl | string {
Expand Down

0 comments on commit 322625a

Please sign in to comment.