|
| 1 | +import stashapi.log as log |
| 2 | +from stashapi.stashapp import StashInterface |
| 3 | +import stashapi.marker_parse as mp |
| 4 | +import sys |
| 5 | +from pathlib import Path |
| 6 | +import json |
| 7 | +import re |
| 8 | +import time |
| 9 | + |
| 10 | + |
| 11 | +def processScene(scene): |
| 12 | + log.debug(scene["scene_markers"]) |
| 13 | + if len(scene["scene_markers"]) == 0: |
| 14 | + for f in scene["files"]: |
| 15 | + scriptfile = Path(f["path"]).parent / (Path(f["path"]).stem + ".funscript") |
| 16 | + log.debug(scriptfile) |
| 17 | + if scriptfile.exists(): |
| 18 | + with open(scriptfile) as f2: |
| 19 | + script_json = json.load(f2) |
| 20 | + if "metadata" in script_json: |
| 21 | + markers = [] |
| 22 | + if "chapters" in script_json["metadata"]: |
| 23 | + for c in script_json["metadata"]["chapters"]: |
| 24 | + name = c["name"] |
| 25 | + if len(name) == 0: |
| 26 | + name = "#" |
| 27 | + marker = { |
| 28 | + "seconds": sum( |
| 29 | + x * float(t) |
| 30 | + for x, t in zip( |
| 31 | + [3600, 60, 1], |
| 32 | + re.split(r"[:]", c["startTime"]), |
| 33 | + ) |
| 34 | + ), |
| 35 | + "primary_tag": name, |
| 36 | + "tags": [], |
| 37 | + "title": name, |
| 38 | + } |
| 39 | + log.debug(marker) |
| 40 | + markers.append(marker) |
| 41 | + if len(markers) > 0: |
| 42 | + mp.import_scene_markers(stash, markers, scene["id"], 15) |
| 43 | + |
| 44 | + |
| 45 | +def processAll(): |
| 46 | + query = { |
| 47 | + "has_markers": "false", |
| 48 | + "interactive": True, |
| 49 | + } |
| 50 | + per_page = 100 |
| 51 | + log.info("Getting scene count") |
| 52 | + count = stash.find_scenes( |
| 53 | + f=query, |
| 54 | + filter={"per_page": 1}, |
| 55 | + get_count=True, |
| 56 | + )[0] |
| 57 | + log.info(str(count) + " scenes to process.") |
| 58 | + # i = 0 |
| 59 | + # 98 |
| 60 | + for r in range(1, int(count / per_page) + 2): |
| 61 | + i = (r - 1) * per_page |
| 62 | + log.info( |
| 63 | + "fetching data: %s - %s %0.1f%%" |
| 64 | + % ( |
| 65 | + (r - 1) * per_page, |
| 66 | + r * per_page, |
| 67 | + (i / count) * 100, |
| 68 | + ) |
| 69 | + ) |
| 70 | + scenes = stash.find_scenes( |
| 71 | + f=query, |
| 72 | + filter={"page": r, "per_page": per_page}, |
| 73 | + ) |
| 74 | + for s in scenes: |
| 75 | + processScene(s) |
| 76 | + i = i + 1 |
| 77 | + log.progress((i / count)) |
| 78 | + time.sleep(2) |
| 79 | + |
| 80 | + |
| 81 | +json_input = json.loads(sys.stdin.read()) |
| 82 | + |
| 83 | +FRAGMENT_SERVER = json_input["server_connection"] |
| 84 | +stash = StashInterface(FRAGMENT_SERVER) |
| 85 | + |
| 86 | +if "mode" in json_input["args"]: |
| 87 | + PLUGIN_ARGS = json_input["args"]["mode"] |
| 88 | + if "processAll" == PLUGIN_ARGS: |
| 89 | + processAll() |
| 90 | +elif "hookContext" in json_input["args"]: |
| 91 | + _id = json_input["args"]["hookContext"]["id"] |
| 92 | + _type = json_input["args"]["hookContext"]["type"] |
| 93 | + if _type == "Scene.Update.Post": |
| 94 | + scene = stash.find_scene(_id) |
| 95 | + processScene(scene) |
0 commit comments