diff --git a/plugins/markerTagToScene/markerTagToScene.js b/plugins/markerTagToScene/markerTagToScene.js deleted file mode 100644 index 77ceecca..00000000 --- a/plugins/markerTagToScene/markerTagToScene.js +++ /dev/null @@ -1,81 +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 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(); - } - - // 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); - } - - // set the tag on the scene if not present - tagIDs.push(primaryTagID); - - setSceneTags(sceneID, tagIDs); - log.Info("added primary tag " + primaryTagID + " 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(); \ No newline at end of file diff --git a/plugins/markerTagToScene/markerTagToScene.py b/plugins/markerTagToScene/markerTagToScene.py new file mode 100644 index 00000000..bc8b3164 --- /dev/null +++ b/plugins/markerTagToScene/markerTagToScene.py @@ -0,0 +1,83 @@ +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"]) + + 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"]) + + 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 exist on scene") + + log.exit() + +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"]] + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/plugins/markerTagToScene/markerTagToScene.yml b/plugins/markerTagToScene/markerTagToScene.yml index 2dcb10e7..788718b0 100644 --- a/plugins/markerTagToScene/markerTagToScene.yml +++ b/plugins/markerTagToScene/markerTagToScene.yml @@ -1,14 +1,23 @@ -# 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 -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. 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