|
| 1 | +from stashapi.stashapp import StashInterface |
| 2 | +import sys |
| 3 | +import json |
| 4 | + |
| 5 | +def processAll(): |
| 6 | + query = { |
| 7 | + "tags": { |
| 8 | + "modifier": "NOT_NULL", |
| 9 | + }, |
| 10 | + "image_count": { |
| 11 | + "modifier": "NOT_EQUALS", |
| 12 | + "value": 0, |
| 13 | + }, |
| 14 | + } |
| 15 | + performersTotal = stash.find_performers(f=query, get_count=True)[0] |
| 16 | + i = 0 |
| 17 | + tags = [] |
| 18 | + images = [] |
| 19 | + while i < performersTotal: |
| 20 | + perf = stash.find_performers(f=query, filter={"page": i, "per_page": 1}) |
| 21 | + for tag in perf[0]["tags"]: |
| 22 | + tags.append(tag["id"]) |
| 23 | + for image in perf[0]["images"]: |
| 24 | + images.append(image["id"]) |
| 25 | + stash.update_images( |
| 26 | + { |
| 27 | + "ids": images, |
| 28 | + "tag_ids": {"mode": "ADD", "ids": tags}, |
| 29 | + } |
| 30 | + ) |
| 31 | + i = i + 1 |
| 32 | + tags = [] |
| 33 | + images = [] |
| 34 | + |
| 35 | + |
| 36 | +def processImage(image): |
| 37 | + tags = [] |
| 38 | + performersIds = [] |
| 39 | + should_tag = True |
| 40 | + if settings["excludeImageWithTag"] != "": |
| 41 | + for tag in image["tags"]: |
| 42 | + if tag["name"] == settings["excludeImageWithTag"]: |
| 43 | + should_tag = False |
| 44 | + break |
| 45 | + |
| 46 | + if settings['excludeImageOrganized']: |
| 47 | + if image['organized']: |
| 48 | + should_tag = False |
| 49 | + |
| 50 | + if should_tag: |
| 51 | + for perf in image["performers"]: |
| 52 | + performersIds.append(perf["id"]) |
| 53 | + performers = [] |
| 54 | + for perfId in performersIds: |
| 55 | + performers.append(stash.find_performer(perfId)) |
| 56 | + for perf in performers: |
| 57 | + for tag in perf["tags"]: |
| 58 | + tags.append(tag["id"]) |
| 59 | + stash.update_images({"ids": image["id"], "tag_ids": {"mode": "ADD", "ids": tags}}) |
| 60 | + tags = [] |
| 61 | + performersIds = [] |
| 62 | + performers = [] |
| 63 | + |
| 64 | + |
| 65 | +json_input = json.loads(sys.stdin.read()) |
| 66 | +FRAGMENT_SERVER = json_input["server_connection"] |
| 67 | +stash = StashInterface(FRAGMENT_SERVER) |
| 68 | +config = stash.get_configuration() |
| 69 | +settings = { |
| 70 | + "excludeImageWithTag": "", |
| 71 | + "excludeImageOrganized": False |
| 72 | +} |
| 73 | +if "tagImagesWithPerfTags" in config["plugins"]: |
| 74 | + settings.update(config["plugins"]["tagImagesWithPerfTags"]) |
| 75 | + |
| 76 | +if "mode" in json_input["args"]: |
| 77 | + PLUGIN_ARGS = json_input["args"]["mode"] |
| 78 | + if "processAll" in PLUGIN_ARGS: |
| 79 | + processAll() |
| 80 | +elif "hookContext" in json_input["args"]: |
| 81 | + id = json_input["args"]["hookContext"]["id"] |
| 82 | + if ( |
| 83 | + ( |
| 84 | + json_input["args"]["hookContext"]["type"] == "Image.Update.Post" |
| 85 | + or "Image.Create.Post" |
| 86 | + ) and "inputFields" in json_input["args"]["hookContext"] |
| 87 | + and len(json_input["args"]["hookContext"]["inputFields"]) > 2 |
| 88 | + ): |
| 89 | + image = stash.find_image(id) |
| 90 | + processImage(image) |
0 commit comments