mikf / gallery-dl

Command-line program to download image galleries and collections from several image hosting sites
GNU General Public License v2.0
11.76k stars 963 forks source link

String Formatting: Leave empty/skip if keyword isn't present rather than "None" #4492

Closed glottisfaun0000 closed 1 year ago

glottisfaun0000 commented 1 year ago

If I have a directory /booru where search urls create a subdirectory named by {search_tags}, how can I skip that part of the path and place the image directly in /booru when {search_tags} isn't present?

For extractor.danbooru, "directory":["booru","{empty|search_tags}"], gives me /booru/None/*.jpg, for invidual posts which don't return a search_tags with gallery-dl -K.

Originally I thought it would be a matter of filtering None with a Format Specifier, but search_tags isn't returned at all with -K, so I guess it inserts the None as a config fallback.

invididual post example url: https://danbooru.donmai.us/posts/6640084?q=link search example url (-K returns search_tags): https://danbooru.donmai.us/posts?tags=konnyakutarooou&z=1

Linda-chan commented 1 year ago

Similar issue with MangaDex folder and file names formatting. Default format string somehow add «chapter_minor» if it present like this: v01 c001.5, v01 c002. But when I use format string like v{volume:>02} c{chapter:>03}{empty|chapter_minor} I get v01 c003None and v01 c004.5.

Documentation doesn't cover this. What should I do?

Hrxn commented 1 year ago

What is {empty|chapter_minor} supposed to even mean?

If you want a value or nothing if not present, use the empty string:

{fieldname|''}

Or, maybe also useful in general, use the "keywords-default" option and set it to an empty string:

"keywords-default": ""

Can also be done on CLI: gallery-dl -o keywords-default=""

Hrxn commented 1 year ago

Ah, does this stem from the combination example in formatting.md?

All of these methods can be combined as needed. For example {title[24]|empty|extractor.url[15:-1]} would result in .org

Here's how I understand it, please correct me if wrong:

The example field {title} has the value Hello World, it gets accessed with element index 24, which is more than this string's length so it's out of range, so the first alternation operator triggers, which uses the field {empty} which does not exist in this example, so the next alternation triggers, choosing {extractor.url} with the value https://example.org/, and this gets sliced from 15:-1, which is .org

mikf commented 1 year ago
{fieldname|''}

This is supposed to work, but it actually doesn't ...

As an alternative, one can use

{fieldname:?//}

Here's how I understand it, please correct me if wrong:

That's how it is supposed to be understood, yes.

MangaDex

Use -E to get the default format string:

$ gallery-dl -E https://mangadex.org/chapter/f946ac53-0b71-4b5d-aeb2-7931b13c4aaa
...
Filename format (default):
  "{manga}_c{chapter:>03}{chapter_minor}_{page:>03}.{extension}"

({chapter_minor} is an empty string if it doesn't have a value)

Hrxn commented 1 year ago

It doesn't work? 🤔

That's strange, because I have been using this format for custom path prefixes in my "directory" settings for quite a long time now:

[..] "{bkey|'Unsorted'}", "{ckey|''}", "{tkey|''}", [..]

Or does it work because I also have set "keywords-default": ""?

Or does it work because these keywords are initially set like this "keywords": {"bkey": "", "ckey": "", [..]?

mikf commented 1 year ago

Or does it work because I also have set "keywords-default": ""?

This. When the last field in a list of alternatives is considered false, like an empty string, you get the keywords-default value, which is None by default.

Linda-chan commented 1 year ago

Looks like I overlooked something. {chapter_minor} without alternatives works fine and resolves to empty string without changing keywords-default. Sorry.

glottisfaun0000 commented 1 year ago

Awesome, so {fieldname|''} along with "keywords-default": "" is working great for me. Thanks everyone

FWIW I think where I and maybe @Linda-chan got the presumption of {fieldname|empty} from is the use of empty at https://github.com/mikf/gallery-dl/blob/master/docs/formatting.md#field-names like Alternatives | {empty|title} | Hello World