Aivean / royalroad-downloader

https://royalroad.com book downloader
MIT License
57 stars 4 forks source link

Add option to include download date in filename (off by default) #34

Closed trilader closed 7 months ago

trilader commented 8 months ago

Introduce a new option to include the download date in ISO format (YYYY-MM-DD, e.g. 2024-01-18 for the 18th of January 2024) at the end of the filename.

I usually keep multiple versions of stories around to keep track of (stealth) edits of earlier chapters and also to prevent stubbing of works removing anything from my archive.

This commit adds the --add-iso-date/-a option which, as the name implies adds the current date to the filename as the last part before the extension.

Aivean commented 8 months ago

I wonder whether it makes more sense to introduce the option to specify output file name (or even path) which supports templates.

E.g.

royalroad -f 140 --output "{title}_{chapters}_{date}_my_custom_suffix.html" https://...

will generate: AwesomeFiction_chapters_140-205_2024-01-18_my_custom_suffix.html

The implementation would be also straightforward, just take the output arg (default is "{title}_{chapters}.html") and string-replace every supported template key (e.g. "{title}", "{chapters}", etc. with it's rendered value).

I think this approach is more generic, as it allows to add more template keys in the future without polluting options space, as well as allows users to specify their own templates, e.g. date could be supplied externally from bash, like this:

royalroad -f 140 --output "$(date +%Y-%m-%d)_{title}_{chapters}_my_custom_suffix.html" https://...

trilader commented 8 months ago

I implemented something like this locally. The part I got stuck on and which lead me to do it like this (and #33) at first was that my knowledge of Scala is severely limited (this is my first time really using it outside of a single lecture unit at university some years ago).

My implementation was a naive string replacement method, which is an easy but somewhat inflexible way to solve this. The main issue I encountered was specifying {title}_{chapters}.html (so the default) but not specifying any -f option resulting in AwesomeFiction_Royal_Road_.html as {chapters} is empty in that case and has no way of knowing about/removing the leading _.

However that might be normal behavior for this kind of parameter in which case it is okay to produce that kind of output. If you like I can re-do my prototype and submit that instead and rename the PR.

Aivean commented 7 months ago

My implementation was a naive string replacement method, which is an easy but somewhat inflexible way to solve this. The main issue I encountered was specifying {title}_{chapters}.html (so the default) but not specifying any -f option resulting in AwesomeFiction_Royal_Road_.html as {chapters} is empty in that case and has no way of knowing about/removing the leading _.

Yeah, that is annoying. One way to solve this is following: introduce a "smart separator" template key, like {sep}. Which gets collapsed into a single _. Implementation wise, you just do a regex replacement _*(\{sep\}_*)+ -> _ as the last step (maybe also trim {sep} from the beginning and the end beforehand). This means that any template key, like {chapters} could be surrounded by {sep}, and, if it's rendered into an empty string, the separators would be auto-collapsed, leading to the pretty result.

However I realise that this is becoming a bit more involved implementation wise. I have it visualized in my head, and I think I can implement it myself quickly, but I don't want to step over your toes, so please let me know if you want to take a stab at it first (I can also help along the way or finish the implementation if you're ok with that).

trilader commented 7 months ago

I'm okay with you just implementing such a thing. I'm just here for the option to have todays' ISO-date in the filename :-)

Aivean commented 7 months ago

Ok, implemented in https://github.com/Aivean/royalroad-downloader/releases/tag/v2.8.0