diff options
Diffstat (limited to 'src/lib/szurubooru.py')
| -rw-r--r-- | src/lib/szurubooru.py | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/src/lib/szurubooru.py b/src/lib/szurubooru.py index 7438f76..03261dd 100644 --- a/src/lib/szurubooru.py +++ b/src/lib/szurubooru.py @@ -683,3 +683,67 @@ class Szurubooru: }, ) return self.__map_tag_resource_response(res, name=tag.name) + + def tag_delete(self, tag: Tag) -> None: + """Deletes the given tag on the Szurubooru instance. + + Args: + tag (Tag): Tag dataclass of the tag that should be reledte. Needs to contain + the current version number. + """ + self.__delete( + path=f"/api/tag/{quote(tag.name)}", params={"version": tag.version} + ) + return + + def tag_merge(self, tag_to_merge: Tag, tag_to_merge_into: Tag) -> Tag: + """Removes tag_to_merge and merges all uses into tag_to_merge_into. + + Removes source tag and merges all of its usages, suggestions and implications to + the target tag. Other tag properties such as category and aliases do not get + transferred and are discarded. + + Args: + tag_to_merge (Tag): Tag dataclass representing the Tag that will be removed + and merged into the second Tag. The version parameter is + required. + tag_to_merge_into (Tag): Tag dataclass representing the Tag that the first + tag will be merged into. The version parameter is + required. + + Returns: + Tag: Resulting Tag dataclass that was returned by the Szurubooru REST API + representing the new (merged) tag on the Szurubooru instance. + """ + + res = self.__post( + path="/api/tag-merge", + params={ + "removeVersion": tag_to_merge.version, + "remove": tag_to_merge.name, + "mergeToVersion": tag_to_merge_into.version, + "mergeTo": tag_to_merge_into.name, + }, + ) + return self.__map_tag_resource_response(res, name=tag_to_merge_into.name) + + def tag_list_siblings(self, tag: Tag) -> List[dict[str, int]]: + """Lists siblings of the given Tag + + Lists siblings of given tag, e.g. tags that were used in the same posts as the + given tag. `occurrences` field signifies how many times a given sibling appears + with given tag. Results are sorted by occurrences count and the list is + truncated to the first 50 elements. Doesn't use paging. + + Args: + tag (Tag): Tag that will queried. Only uses the tag.name parameter. + + Returns: + List[dict[str, int]]: Returns a List of dictionaries with the primary tag + name as the key and the number of occurrences as the + value. + """ + + res = self.__get(path=f"/api/tag-siblings/{tag.name}") + results = json.loads(res.content)["results"] + return list(map(lambda a: {a["tag"]["names"][0]: a["occurrences"]}, results)) |