Quick hack: EPUB renamer

13 Aug 2024 – Terrania, Italy

I have a quite large collection of ebooks, growing every month, that I’m buying on numerous independent stores. Shameless semi-plug: my favourite store for ebooks published in polish language is ArtRage, a store and publishing house that started as BookRage that I was a cofounder of. Of course for some of the english language books Amazon remains the only viable source, but whenever possible I buy from smaller stores or directly from the publisher. Those stores always provide both EPUB and MOBI files for readers, and I’ve been sending MOBI files to my Kindle ever since I got my first one back in 2011.

Since August 2022 Amazon has been announcing that Send-to-Kindle will no longer accept MOBI files and everybody should switch to EPUB. It is kind of funny since older Kindle devices (up to Basic Kindle from 2022) do not support EPUB natively – those EPUBs are being converted by Amazon to AZW3 format. Still, it’s a welcome change, as EPUB since version 3.0 is truly a superior format and MOBI can be safely retired, there’s no need for imposing that extra work on ebook publishers.

It is, however, a bumpy road so far for Kindle users, as Amazon’s support for EPUB leaves a lot to be desired. Covers now work for most of the EPUB files, but handling EPUB metadata by Amazon has been and still is kind of terrible. It has improved when using email Send-to-Kindle, but the Kindle Android app’s “add book to my library” still uses filename as title and my own name as author, instead of reading it from EPUB’s metadata. And this sucks, as the Android app is my preferred way of adding freshly bought books to my Kindle library.

This wouldn’t be much of a problem if stores named their files properly, preferably with full title and author in the filename, with proper capitalization, spaces_and_not_underscores, no diacritics etc. That’s not the case, though. Every store and publisher has their own idea how to name files and most of them are wrong. Some of those are actually artifacts of watermarking tools, so for example aforementioned ArtRage gives me files with my order ID and email in the title, with polish diacritics replaced with underscores.

So yesterday afternoon, while chilling in air-conditioned apartment to wait out the hottest part of the day here in Tirrenia, I’ve hacked a Ruby script to read EPUBs metadata and rename the file to follow “title - author.epub”. And if there’s a corresponding MOBI file - rename it as well. Diacritics like ą, ä, ç or ż will be replaced with their latin lookalikes. This will also help me navigate my collection, as I still use files and directories most of the time. Calibre is great but I don’t have it running on my phone – and I haven’t gotten yet to running the web version in my homelab.

Here’s the Github Gist. Just save those files in a directory, run bundle, give executable flag to the renamer.rb and they’re ready to use.

Running is simple, call the renamer.rb script with the path to EPUB file as argument:

./renamer.rb ~/ebooks/weirdly_named_file.epub

Here’s how I run it on all files in a given directory, using ZSH as my shell:

for file in ./20231006-order123/*.epub; ~/projects/epub-toolkit/renamer.rb $file

And here’s how I run it to process all files in all subdirectories, basically my entire ebook collection, thanks to ZSH’s globbing:

for file in **/*.epub; ~/projects/epub-toolkit/renamer.rb $file

It should work with any non-ancient Ruby version, the epub-parser gem requires Ruby 2.3 and that’s basically it.

Remember, if there’s a MOBI file named exactly like the EPUB in the same directory – it will be renamed as well.

As for future plans for this code, it probably will remain a gist, as it’s too little functionality to justify an entire gem and Github project. Contact me if that’s a wrong assumption!

Nostalgia trip: I never expected to pull out code I wrote back in 2009, that was mostly a copypaste of someone else’s code for “downscaling” diacritics, which was in turn based on first edition of Obie’s “The Rails Way”. If you remember those times, you probably had a great programming career, but now you should do regular checkups with your doctor!


Tomash

Written by

Updated