From 0aa2875f54a020cd78e3eccc9573680773e7aee6 Mon Sep 17 00:00:00 2001 From: stg-annon <14135675+stg-annon@users.noreply.github.com> Date: Tue, 28 Mar 2023 11:03:11 -0400 Subject: [PATCH 1/3] add all marker tags to scene not just primary looks at all marker tags from `SceneMarkerUpdateInput` and compares them to existing scene `tag_ids` adds any tags that are missing from scene --- plugins/markerTagToScene/markerTagToScene.js | 25 +++++++++----------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/plugins/markerTagToScene/markerTagToScene.js b/plugins/markerTagToScene/markerTagToScene.js index 77ceecca..09a4d5b2 100644 --- a/plugins/markerTagToScene/markerTagToScene.js +++ b/plugins/markerTagToScene/markerTagToScene.js @@ -8,6 +8,7 @@ function main() { var hookContext = input.Args.hookContext; var opInput = hookContext.input; var primaryTagID = opInput.primary_tag_id; + var markerTagIDs = opInput.tag_ids ? opInput.tag_ids : []; var sceneID = opInput.scene_id; // we can't currently find scene markers. If it's not in the input @@ -16,25 +17,21 @@ function main() { // just return return ok(); } + markerTagIDs.push(primaryTagID) // get the existing scene tags var sceneTags = getSceneTags(sceneID); - var tagIDs = []; - for (var i = 0; i < sceneTags.length; ++i) { - var tagID = sceneTags[i].id; - if (tagID == primaryTagID) { - log.Debug("primary tag already exists on scene"); - return; - } - - tagIDs.push(tagID); + var missingTagIDs = markerTagIDs.filter(markerTagID => !sceneTags.includes(markerTagID)) + + if (missingTagIDs.length == 0){ + log.Debug("all marker tags already exists on scene"); + return; } - // set the tag on the scene if not present - tagIDs.push(primaryTagID); - + // add missing tags to existing scene tags + var tagIDs = sceneTags.concat(missingTagIDs) setSceneTags(sceneID, tagIDs); - log.Info("added primary tag " + primaryTagID + " to scene " + sceneID); + log.Info(`added missing tag(s) ${missingTagIDs} to scene ${sceneID}`); } function getSceneTags(sceneID) { @@ -78,4 +75,4 @@ mutation sceneUpdate($input: SceneUpdateInput!) {\ gql.Do(mutation, variables); } -main(); \ No newline at end of file +main(); From bcf30ec62cf48fff0aefd0e403e93a3bdbe11663 Mon Sep 17 00:00:00 2001 From: stg-annon <14135675+stg-annon@users.noreply.github.com> Date: Thu, 6 Apr 2023 20:33:39 -0400 Subject: [PATCH 2/3] port plugin to python bumps version to 2.0 as language change here is somewhat significant --- plugins/markerTagToScene/markerTagToScene.js | 78 ------------------- plugins/markerTagToScene/markerTagToScene.py | 40 ++++++++++ plugins/markerTagToScene/markerTagToScene.yml | 7 +- 3 files changed, 44 insertions(+), 81 deletions(-) delete mode 100644 plugins/markerTagToScene/markerTagToScene.js create mode 100644 plugins/markerTagToScene/markerTagToScene.py diff --git a/plugins/markerTagToScene/markerTagToScene.js b/plugins/markerTagToScene/markerTagToScene.js deleted file mode 100644 index 09a4d5b2..00000000 --- a/plugins/markerTagToScene/markerTagToScene.js +++ /dev/null @@ -1,78 +0,0 @@ -function ok() { - return { - output: "ok" - }; -} - -function main() { - var hookContext = input.Args.hookContext; - var opInput = hookContext.input; - var primaryTagID = opInput.primary_tag_id; - var markerTagIDs = opInput.tag_ids ? opInput.tag_ids : []; - var sceneID = opInput.scene_id; - - // we can't currently find scene markers. If it's not in the input - // then just return - if (!primaryTagID || !sceneID) { - // just return - return ok(); - } - markerTagIDs.push(primaryTagID) - - // get the existing scene tags - var sceneTags = getSceneTags(sceneID); - var missingTagIDs = markerTagIDs.filter(markerTagID => !sceneTags.includes(markerTagID)) - - if (missingTagIDs.length == 0){ - log.Debug("all marker tags already exists on scene"); - return; - } - - // add missing tags to existing scene tags - var tagIDs = sceneTags.concat(missingTagIDs) - setSceneTags(sceneID, tagIDs); - log.Info(`added missing tag(s) ${missingTagIDs} to scene ${sceneID}`); -} - -function getSceneTags(sceneID) { - var query = "\ -query findScene($id: ID) {\ - findScene(id: $id) {\ - tags {\ - id\ - }\ - }\ -}"; - - var variables = { - id: sceneID - }; - - var result = gql.Do(query, variables); - var findScene = result.findScene; - if (findScene) { - return findScene.tags; - } - - return []; -} - -function setSceneTags(sceneID, tagIDs) { - var mutation = "\ -mutation sceneUpdate($input: SceneUpdateInput!) {\ - sceneUpdate(input: $input) {\ - id\ - }\ -}"; - - var variables = { - input: { - id: sceneID, - tag_ids: tagIDs - } - }; - - gql.Do(mutation, variables); -} - -main(); diff --git a/plugins/markerTagToScene/markerTagToScene.py b/plugins/markerTagToScene/markerTagToScene.py new file mode 100644 index 00000000..06e6f429 --- /dev/null +++ b/plugins/markerTagToScene/markerTagToScene.py @@ -0,0 +1,40 @@ +import sys, json + +try: + import stashapi.log as log + from stashapi.stashapp import StashInterface +except ModuleNotFoundError: + print("You need to install the stashapi module. (pip install stashapp-tools)", + file=sys.stderr) + +def main(): + global stash + + fragment = json.loads(sys.stdin.read()) + + stash = StashInterface(fragment["server_connection"]) + context = fragment["args"]["hookContext"] + + marker_tag_ids = context["input"].get("tag_ids", []) + marker_tag_ids.append(context["input"]["primary_tag_id"]) + scene_id = context["input"]["scene_id"] + + scene_tag_ids = get_scene_tags(scene_id) + log.info(f"{marker_tag_ids=} {scene_id=} {scene_tag_ids=}") + + missing_tag_ids = [tid for tid in marker_tag_ids if tid not in scene_tag_ids] + if missing_tag_ids: + scene_tag_ids.extend(missing_tag_ids) + stash.update_scene({"id":scene_id, "tag_ids": scene_tag_ids}) + log.info(f'added missing tag(s) {missing_tag_ids} to scene {scene_id}') + else: + log.debug("all marker tags already exists on scene") + + log.exit() + +def get_scene_tags(scene_id): + scene = stash.find_scene(scene_id, fragment="tags { id }") + return [t["id"] for t in scene["tags"]] + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/plugins/markerTagToScene/markerTagToScene.yml b/plugins/markerTagToScene/markerTagToScene.yml index 2dcb10e7..0a5e5a0c 100644 --- a/plugins/markerTagToScene/markerTagToScene.yml +++ b/plugins/markerTagToScene/markerTagToScene.yml @@ -2,10 +2,11 @@ name: Scene Marker Tags to Scene description: Adds primary tag of Scene Marker to the Scene on marker create/update. url: https://github.com/stashapp/CommunityScripts -version: 1.0 +version: 2.0 exec: - - markerTagToScene.js -interface: js + - python + - "{pluginDir}/markerTagToScene.py" +interface: raw hooks: - name: Update scene with scene marker tag description: Adds primary tag of Scene Marker to the Scene on marker create/update. From d290f2e28e525e38bf602186ee9771cc53fbd8c0 Mon Sep 17 00:00:00 2001 From: stg-annon <14135675+stg-annon@users.noreply.github.com> Date: Mon, 24 Apr 2023 20:59:07 -0400 Subject: [PATCH 3/3] add tasks --- plugins/markerTagToScene/markerTagToScene.py | 61 ++++++++++++++++--- plugins/markerTagToScene/markerTagToScene.yml | 10 ++- 2 files changed, 61 insertions(+), 10 deletions(-) diff --git a/plugins/markerTagToScene/markerTagToScene.py b/plugins/markerTagToScene/markerTagToScene.py index 06e6f429..bc8b3164 100644 --- a/plugins/markerTagToScene/markerTagToScene.py +++ b/plugins/markerTagToScene/markerTagToScene.py @@ -11,16 +11,23 @@ def main(): global stash fragment = json.loads(sys.stdin.read()) - stash = StashInterface(fragment["server_connection"]) - context = fragment["args"]["hookContext"] - + + mode = fragment['args'].get("mode") + context = fragment["args"].get("hookContext") + + if context: + run_hook(context) + if mode == "updateAllScenes": + update_all_scenes() + if mode == "dryRunUpdateAllScenes": + update_all_scenes(True) + +def run_hook(context): + scene_id = context["input"]["scene_id"] + scene_tag_ids = get_scene_tag_ids(scene_id) marker_tag_ids = context["input"].get("tag_ids", []) marker_tag_ids.append(context["input"]["primary_tag_id"]) - scene_id = context["input"]["scene_id"] - - scene_tag_ids = get_scene_tags(scene_id) - log.info(f"{marker_tag_ids=} {scene_id=} {scene_tag_ids=}") missing_tag_ids = [tid for tid in marker_tag_ids if tid not in scene_tag_ids] if missing_tag_ids: @@ -28,11 +35,47 @@ def main(): stash.update_scene({"id":scene_id, "tag_ids": scene_tag_ids}) log.info(f'added missing tag(s) {missing_tag_ids} to scene {scene_id}') else: - log.debug("all marker tags already exists on scene") + log.debug("all marker tags already exist on scene") log.exit() -def get_scene_tags(scene_id): +def update_all_scenes(dry_run=False): + log.info("Querying Scenes with markers from Stash...") + + count, scenes = stash.find_scenes({"has_markers": "true"}, fragment="id tags { id } scene_markers { primary_tag { id } tags { id } }", get_count=True) + + log.info(f"processing {count} scenes with markers") + for i, s in enumerate(scenes): + log.progress(i/count) + scene_id = s["id"] + + scene_tag_ids = [t["id"] for t in s["tags"]] + marker_tag_ids = get_scene_marker_tag_ids(scene=s) + + missing_tag_ids = [tid for tid in marker_tag_ids if tid not in scene_tag_ids] + if missing_tag_ids == []: + log.debug(f"all marker tags already exist on scene {scene_id}") + continue + if dry_run: + log.info(f'missing tag(s) {missing_tag_ids} from scene {scene_id}') + continue + scene_tag_ids.extend(missing_tag_ids) + stash.update_scene({"id":scene_id, "tag_ids": scene_tag_ids}) + log.info(f'added missing tag(s) {missing_tag_ids} to scene {scene_id}') + + +def get_marker_tag_ids(marker): + tag_ids = [t["id"] for t in marker["tags"]] + tag_ids.append(marker["primary_tag"]["id"]) + return tag_ids +def get_scene_marker_tag_ids(scene=None, scene_id=None): + if scene: + all_markers_ids = [get_marker_tag_ids(m) for m in scene["scene_markers"]] + return sum(all_markers_ids, []) + if scene_id: + scene = stash.find_scene(fragment="id scene_markers { primary_tag { id } tags { id } }") + return get_scene_marker_tag_ids( scene=scene ) +def get_scene_tag_ids(scene_id): scene = stash.find_scene(scene_id, fragment="tags { id }") return [t["id"] for t in scene["tags"]] diff --git a/plugins/markerTagToScene/markerTagToScene.yml b/plugins/markerTagToScene/markerTagToScene.yml index 0a5e5a0c..788718b0 100644 --- a/plugins/markerTagToScene/markerTagToScene.yml +++ b/plugins/markerTagToScene/markerTagToScene.yml @@ -1,4 +1,3 @@ -# example plugin config name: Scene Marker Tags to Scene description: Adds primary tag of Scene Marker to the Scene on marker create/update. url: https://github.com/stashapp/CommunityScripts @@ -13,3 +12,12 @@ hooks: triggeredBy: - SceneMarker.Create.Post - SceneMarker.Update.Post +tasks: + - name: Update Scenes Tags + description: Adds primary tag or all tags of Scene Markers to their Scenes + defaultArgs: + mode: updateAllScenes + - name: Dry Run + description: Dry run of adding tags of Scene Markers to their Scenes + defaultArgs: + mode: dryRunUpdateAllScenes \ No newline at end of file