Skip to content

Fix filename extension detection for parenthesized format strings - #31

Open
croakingtoad wants to merge 1 commit into
zelestcarlyone:masterfrom
croakingtoad:fix/filename-extension-detection
Open

Fix filename extension detection for parenthesized format strings#31
croakingtoad wants to merge 1 commit into
zelestcarlyone:masterfrom
croakingtoad:fix/filename-extension-detection

Conversation

@croakingtoad

Copy link
Copy Markdown

Summary

  • Fixes a bug where files downloaded from Anna's Archive are saved without a proper extension when the metadata includes the format in parentheses (e.g. (mobi), (epub), (pdf))
  • Python's Path.suffix misparses filenames like Book (v5.0) (mobi) as having extension .0) (mobi) instead of .mobi, causing downstream tools to not recognize the file type

Problem

When the "Filepath" metadata from Anna's Archive contains the format in parentheses rather than as a dotted extension, the existing extension validation in direct.py fails:

  1. Path("Book (v5.0) (mobi)").suffix returns .0) (mobi)
  2. This is not in LEGAL_FILES, so it logs a warning but doesn't fix the filename
  3. The file is saved as Book (v5.0) (mobi) with no usable extension

Fix

When Path.suffix returns an extension not in LEGAL_FILES, the code now scans the filename for known format strings in parentheses (e.g. (mobi), (epub), (pdf)). If found, it strips the parenthesized format from the end and appends the correct dotted extension.

Examples:

Input Output
Homer - The Iliad (v5.0) (mobi) Homer - The Iliad (v5.0).mobi
Some Book Title (epub) Some Book Title.epub
Book With (v2.1) (pdf) Book With (v2.1).pdf
Normal Book.epub Normal Book.epub (unchanged)
No Extension At All No Extension At All.epub (existing fallback)

Test plan

  • Tested with real-world filename from Anna's Archive that triggered the bug
  • Verified normal filenames with proper extensions are not modified
  • Verified the existing no-extension fallback (adds .epub) still works
  • Verified filenames with version numbers in parens like (v5.0) are preserved

🤖 Generated with Claude Code

When Anna's Archive metadata includes the file format in parentheses
(e.g. "Book Title (v5.0) (mobi)"), Python's Path.suffix incorrectly
parses the extension as ".0) (mobi)" instead of ".mobi". This causes
files to be saved without a proper extension.

This fix scans the filename for known format strings in parentheses
when the detected extension is not in LEGAL_FILES, strips the
parenthesized format, and appends the correct extension.

Examples:
  "Book (v5.0) (mobi)" -> "Book (v5.0).mobi"
  "Book (epub)"         -> "Book.epub"
  "Book (pdf)"          -> "Book.pdf"

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@croakingtoad

Copy link
Copy Markdown
Author

Hope you find this helpful. Was running into this problem when using stacks to download to booklore's bookdrop folder.

@wozniakpawel

Copy link
Copy Markdown

Hey @zelestcarlyone any plans on merging this in?

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