Skip to content

[markerTagToScene] add all marker tags to scene not just primary #155

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 0 additions & 81 deletions plugins/markerTagToScene/markerTagToScene.js

This file was deleted.

83 changes: 83 additions & 0 deletions plugins/markerTagToScene/markerTagToScene.py
Original file line number Diff line number Diff line change
@@ -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()
17 changes: 13 additions & 4 deletions plugins/markerTagToScene/markerTagToScene.yml
Original file line number Diff line number Diff line change
@@ -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