Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
glottisfaun0000 opened this issue Sep 3, 2023 · 8 comments

Comments

@glottisfaun0000
Copy link

glottisfaun0000 commented Sep 3, 2023

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
Copy link

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
Copy link
Contributor

Hrxn commented Sep 5, 2023

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
Copy link
Contributor

Hrxn commented Sep 5, 2023

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
Copy link
Owner

mikf commented Sep 5, 2023

{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
Copy link
Contributor

Hrxn commented Sep 5, 2023

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
Copy link
Owner

mikf commented Sep 5, 2023

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.

mikf added a commit that referenced this issue Sep 5, 2023
fixes {fieldname|''} evaluating to the value of 'keywords-default'
instead of an empty string
@Linda-chan
Copy link

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

@glottisfaun0000
Copy link
Author

glottisfaun0000 commented Sep 8, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants