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

[pixiv] No random tags sorting #3266

Closed
AlttiRi opened this issue Nov 20, 2022 · 6 comments
Closed

[pixiv] No random tags sorting #3266

AlttiRi opened this issue Nov 20, 2022 · 6 comments

Comments

@AlttiRi
Copy link

AlttiRi commented Nov 20, 2022

I noted that my config: #3265 (comment)

(that uses <div class="tags">["{tags:J", "}"]</div> in the metadata postprocessor)

produces always the different metadata files since the tags are randomly sorted.

For example, https://www.pixiv.net/ajax/illust/63625443 API returns tags with the same order for each requests, so it's seems like a bug.

@mikf
Copy link
Owner

mikf commented Nov 20, 2022

tags are randomly sorted

Tags get returned in the same order as provided by Pixiv.
(Unless you are on Python 3.4 or 3.5, then they are truly random)

work["tags"] = [tag["name"] for tag in work["tags"]]

@AlttiRi
Copy link
Author

AlttiRi commented Nov 20, 2022

I use Python 3.9.13.

@AlttiRi
Copy link
Author

AlttiRi commented Nov 22, 2022

Here is the problem code

work["tags"] = list(set(tag["translated_name"] or tag["name"] for tag in work["tags"]))

work["tags"] = list(set(
tag["translated_name"] or tag["name"]
for tag in work["tags"]))

Because of set() does not keep the order. Absolutely.

@AlttiRi
Copy link
Author

AlttiRi commented Nov 22, 2022

https://stackoverflow.com/questions/7961363/removing-duplicates-in-lists

It suggests to use OrderedDict.fromkeys.

@AlttiRi
Copy link
Author

AlttiRi commented Nov 22, 2022

Yes, it works fine. Checked.

...

  import hashlib
+ from collections import OrderedDict

...

            def transform_tags(work):
-                work["tags"] = list(set(
+                work["tags"] = list(OrderedDict.fromkeys(
                    tag["translated_name"] or tag["name"]
                    for tag in work["tags"]))
...

mikf added a commit that referenced this issue Nov 24, 2022
mikf added a commit that referenced this issue Nov 24, 2022
for '"tags": "translated"'

As it turns out, set() does *not* preserve insertion order.
@mikf
Copy link
Owner

mikf commented Nov 26, 2022

Because of set() does not keep the order. Absolutely.

I actually was not aware of that and thought sets behaved like dicts in this regard.

Anyway, tag order is now preserved (5a17e15) and I also added an S format specifier to sort lists (42481ae).

@mikf mikf closed this as completed Nov 26, 2022
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

2 participants