Forum teuk.org

Bugfix & Refactoring Pass: Safer APIs, Cleaner UrlTitle, and Stronger Tests

in Mediabot · started by TeuK · 1w ago

TeuK · 1w ago

Bugfix & Refactoring Pass: Safer APIs, Cleaner UrlTitle, and Stronger Tests

Article

Over the last development pass, Mediabot v3 received a solid cleanup focused on reliability, safer external API handling, and better regression coverage.

This was not a flashy feature sprint. It was the kind of maintenance work that keeps a bot stable when real users, real IRC clients, and real network failures start throwing cursed objects around the castle.

What changed

UrlTitle rendering fixes

Several UrlTitle handlers were cleaned up so that their visual badges keep their intended style, while the displayed title text is reset immediately afterward.

This prevents IRC background color leakage on clients/themes where a badge background could accidentally continue across the title text.

Affected handlers include:

  • YouTube
  • Spotify
  • Instagram
  • Apple Music
  • Facebook
  • X/Twitter
  • Generic URL titles

The intended rule is now clear:

[Badge]  -> may keep its styled background
Title    -> must render with transparent/default background

Spotify title extraction improved

Spotify handling was improved so Mediabot no longer settles for useless shell titles like:

Spotify – Web Player

The handler now tries richer metadata sources, including Spotify oEmbed, embed pages, standard page metadata, and Chromium-rendered DOM fallback when needed.

When available, Spotify output can include:

  • track/title
  • artist
  • album
  • release year
  • duration

YouTube cleanup and fixes

The YouTube rendering code was cleaned up and made more consistent.

A duplicate helper/import conflict around _yt_format_duration() was fixed. This removed the Perl warning at startup:

Subroutine _yt_format_duration redefined

YouTube helper ownership is now clearer: YouTube-specific formatting helpers live in Mediabot::External, not in Mediabot::Helpers.

The YouTube JSON dump was also moved from DEBUG4 to DEBUG5, because full API responses are too verbose for normal debugging.

Safer HTTP/API handling

Several network calls were hardened with local eval guards and clean fallback response hashes. This prevents DNS, SSL, proxy, or API exceptions from breaking command execution.

Hardened areas include:

  • YouTube search API calls
  • OpenAI model listing
  • OpenAI test command
  • ChatGPT API calls
  • helper HTTP calls such as version checks and country lookup
  • TMDB lookup logging/error handling

Instead of letting raw network exceptions escape, Mediabot now logs or reports failures more cleanly.

ChatGPT logging cleanup

Verbose ChatGPT logs were moved to DEBUG5:

  • full prompt
  • raw API response
  • raw answer

That keeps DEBUG4 usable for flow debugging without flooding logs with large payloads.

TMDB improvements

TMDB output truncation was made safer by building the final IRC reply first and then truncating the complete message.

This avoids odd behavior when prefixes are long or MAIN_PROG_MAXLEN is configured too low.

TMDB errors also now go through the Mediabot logger instead of using direct warn output to STDERR.

Stale code/comment cleanup

A misleading copied comment before getFortniteId() was removed and replaced with a correct Fortnite-specific comment.

This kind of cleanup matters during refactoring: misleading comments are small traps that eventually waste time or cause wrong assumptions.

Regression tests added or updated

Several regression tests were added or adjusted to lock in the new behavior:

  • UrlTitle badge reset/stringification
  • Spotify rich metadata handling
  • generic UrlTitle cleanup
  • YouTube helper ownership
  • YouTube transparent-background rendering
  • YouTube HTTP guards
  • OpenAI HTTP guards
  • helper HTTP guards
  • ChatGPT HTTP guard and DEBUG5 logging
  • TMDB final message truncation
  • TMDB logger usage
  • Fortnite comment cleanup

The goal is simple: when this code is touched again later, tests should catch the same class of mistakes before they reach the running bot.

Why this matters

Most of these changes are defensive, but they make a real difference in production-like IRC usage.

Mediabot talks to many external services: YouTube, Spotify, TMDB, OpenAI, country lookup APIs, and generic web pages. Any of those can fail, return junk, change their markup, or behave differently depending on the client/environment.

This pass makes Mediabot more resilient against those situations.

Summary

This update is mainly about reliability and polish:

  • fewer startup warnings
  • safer network calls
  • cleaner logs
  • better UrlTitle rendering
  • richer Spotify output
  • stronger regression tests
  • less stale code

A proper little defensive magic pass.

Mischief managed.

You must be logged in to reply.