Skip to content

fix: resolve episode audio from the podcast feed - #8

Open
ofa1 wants to merge 2 commits into
mainfrom
fix/feed-based-audio-lookup
Open

ofa1 wants to merge 2 commits into
mainfrom
fix/feed-based-audio-lookup

Conversation

@ofa1

@ofa1 ofa1 commented Sep 6, 2026

Copy link
Copy Markdown
Member

The cron has thrown on every run for ~24h (114 consecutive exceptions,
none before that). The latest episode was published as a .wav, so
PowerPress rendered a link-only player instead of an HTML5 one, and the
audio source selector in getAudioUrl matched nothing.

Reading the media URL out of theme output meant any rendering change
could break the pipeline. The podcast feed carries the same URL in its
tag, which is a stable contract, so getEnclosure replaces
getHTML/getAudioUrl and src/audio.ts goes away along with the cheerio
dependency.

The feed also declares the file size, which fixes a second latent bug:
sendTelegramAudio buffered the whole file via arrayBuffer(), so the
current 104 MB episode would exhaust the 128 MB isolate long before the
50 MB guard fired. Files at or under 20 MB are now handed to Telegram as
a URL and never pass through the worker; larger ones still buffer. The
Blob content type is derived from the file extension rather than being
hardcoded to audio/mpeg.

Co-Authored-By: Claude Opus 5 noreply@anthropic.com
Claude-Session: https://claude.ai/code/session_014pdQQY5i3n8hCfmmRV93Gy

The cron has thrown on every run for ~24h (114 consecutive exceptions,
none before that). The latest episode was published as a .wav, so
PowerPress rendered a link-only player instead of an HTML5 one, and the
`audio source` selector in getAudioUrl matched nothing.

Reading the media URL out of theme output meant any rendering change
could break the pipeline. The podcast feed carries the same URL in its
<enclosure> tag, which is a stable contract, so getEnclosure replaces
getHTML/getAudioUrl and src/audio.ts goes away along with the cheerio
dependency.

The feed also declares the file size, which fixes a second latent bug:
sendTelegramAudio buffered the whole file via arrayBuffer(), so the
current 104 MB episode would exhaust the 128 MB isolate long before the
50 MB guard fired. Files at or under 20 MB are now handed to Telegram as
a URL and never pass through the worker; larger ones still buffer. The
Blob content type is derived from the file extension rather than being
hardcoded to audio/mpeg.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014pdQQY5i3n8hCfmmRV93Gy
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Sep 6, 2026 •

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Updated (UTC)
✅ Deployment successful!
View logs
mhmic-telegram-cron d809de4 Sep 07 2026, 04:02 PM

@omarmosid omarmosid left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary

Solid fix: replaces the brittle �udio source HTML scrape with a stable feed lookup, and routes audio ≤20 MB to Telegram by URL so the worker never buffers the whole file (the 104 MB WAV was exhausting the 128 MB isolate). Good root-cause, good tests, clean cheerio removal. No blockers.

WARN (src/feed.ts): the fallback matched ?? items[0] can silently send the previous episode when the feed hasn't caught up to the newest post (REST API is ahead). Consider throwing instead of sending the newest item, or flagging it as an opt-in safety valve.

NIT (src/telegram.ts): the URL-vs-buffer size branching has no unit test — worth adding one.

NIT (optional): �ttr() only matches double-quoted attributes.

Comment thread src/feed.ts Outdated
console.warn(`No feed item matched slug "${slug}"; falling back to the newest item`);
}

const enclosure = toEnclosure(matched ?? items[0]);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WARN — fallback risk: when the feed hasn't caught up to the newest post (the REST API is ahead), this silently sends the previous episode's audio. Consider throwing when no item matches the slug instead, or make the fallback opt-in.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in d809de4 — the fallback is gone, it now throws when no item matches the slug.

Digging into it, the risk was worse than sending the previous episode: handlers.ts records lastSentAt only after a successful send, so the fallback would have delivered the wrong audio and marked the new post as delivered — the real audio would never go out. Throwing leaves the position unchanged, so the next 10-minute tick retries once the feed catches up.

The error message is deliberately distinct from the no-enclosure one, since the two need different responses (wait vs. go attach media in WordPress). Doc comment and the dead console.warn updated, and the existing fallback test was inverted rather than deleted.

Comment thread src/telegram.ts
// Preferred path: hand Telegram the URL. Downloading into the worker risks
// exhausting the 128 MB isolate long before the 50 MB guard below fires — a
// 104 MB WAV episode is what surfaced this.
if (length > 0 && length <= MAX_URL_SEND_BYTES) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT — the URL-vs-buffer size branching (20 MB URL path, 50 MB guard, missing-length case) is the trickiest logic in this PR but has no unit coverage. feed.test.ts is thorough; worth adding a test for sendTelegramAudio here.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added tests/unit/telegram.test.ts in d809de4 — 8 tests covering the branching:

  • ≤20 MB → sends by URL, asserting exactly one fetch (i.e. nothing was downloaded)
  • 50 MB → throws with fetch never called (the 104 MB WAV case)

  • length: 0 and the 20–50 MB range → buffer/upload path, multipart body
  • plus the content-length guard, a failed download, the Telegram error description, and the non-http URL rejection

One gap worth naming: this does not cover the metadata enrichment. parseBuffer throws on the small test fixtures and is caught, so duration/performer are never appended in any test.

Comment thread src/feed.ts

const ITEM_RE = /<item\b[^>]*>([\s\S]*?)<\/item>/gi;

const attr = (tag: string, name: string): string | null => {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT (optional) — attr() only matches double-quoted attributes; a single-quoted or unquoted url value would be ignored and the item treated as having no enclosure. Fine for most feed generators today.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in d809de4 — attr() now accepts double-quoted, single-quoted, and unquoted values via an alternation, taking the first defined group (?? rather than ||, so an empty url="" stays "" instead of falling through).

Also added a \b before the name while in there: attr(tag, 'type') would previously have matched the tail of an unrelated attribute like xtype="...". Covered by a test with single-quoted and unquoted attributes.

Falling back to the newest feed item did more than send the wrong audio:
the caller records lastSentAt only after a successful send, so the new
post was marked delivered and its real audio never went out. Throwing
leaves the position unchanged and the next cron tick retries once the
feed catches up.

Also accept single-quoted and unquoted enclosure attributes, and cover
the sendTelegramAudio size branching (URL path, 50 MB guard, missing
length) with unit tests.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants