22import json
33import random
44import time
5- import requests
65import stashapi .log as log
76from stashapi .stashapp import StashInterface
87
98def main ():
10- # 1. READ CONFIG FROM STASH
119 try :
12- # Read the JSON configuration passed by Stash
1310 input_data = json .loads (sys .stdin .read ())
14- server_info = input_data .get ("server_connection" , {})
15-
16- # Automatically get the base URL and API Key from Stash itself
17- base_url = f"{ server_info .get ('Scheme' , 'http' )} ://{ server_info .get ('Host' , 'localhost' )} :{ server_info .get ('Port' , 9999 )} "
18- api_key = server_info .get ('ApiKey' , '' )
19-
20- # Initialize the official Stash Interface
21- stash = StashInterface (server_info )
11+ stash = StashInterface (input_data ["server_connection" ])
2212 except Exception as e :
2313 log .error (f"Failed to initialize plugin connection: { e } " )
2414 return
2515
26- endpoint_url = f"{ base_url } /graphql"
27- headers = {'Content-Type' : 'application/json' }
28- if api_key :
29- headers ['ApiKey' ] = api_key
30-
31- # Helper function for GraphQL
32- def call_gql (query , variables = None ):
33- try :
34- response = requests .post (endpoint_url , json = {'query' : query , 'variables' : variables }, headers = headers , timeout = 10 )
35- return response .json ().get ("data" , {})
36- except Exception as e :
37- log .error (f"GQL Error: { e } " )
38- return {}
39-
40- # 2. START PROCESSING
4116 start_time = time .time ()
42-
17+
4318 tags_query = """
4419 query { findTags(tag_filter: {marker_count: {modifier: GREATER_THAN, value: 0}}, filter: {per_page: -1}) {
4520 tags { id name }
4621 }}
4722 """
48- tags_data = call_gql (tags_query )
49- tags = tags_data .get ("findTags" , {}).get ("tags" , [])
50-
23+ tags = stash .call_GQL (tags_query ).get ("findTags" , {}).get ("tags" , [])
24+
5125 if not tags :
5226 log .info ("No tags with markers found." )
5327 return
@@ -57,31 +31,25 @@ def call_gql(query, variables=None):
5731
5832 for idx , tag in enumerate (tags , 1 ):
5933 tag_id = tag ['id' ]
60-
61- # Find markers for this tag
34+
6235 marker_query = """
6336 query($id: ID!) { findSceneMarkers(scene_marker_filter: {tags: {value: [$id], modifier: INCLUDES}}, filter: {per_page: -1}) {
6437 scene_markers { id scene { id } }
6538 }}
6639 """
67- markers_data = call_gql (marker_query , {"id" : tag_id })
68- markers = markers_data .get ("findSceneMarkers" , {}).get ("scene_markers" , [])
69-
40+ markers = stash .call_GQL (marker_query , {"id" : tag_id }).get ("findSceneMarkers" , {}).get ("scene_markers" , [])
41+
7042 if markers :
7143 marker = random .choice (markers )
72- # Use the dynamic base_url
44+ server = input_data ["server_connection" ]
45+ base_url = f"{ server .get ('Scheme' , 'http' )} ://{ server .get ('Host' , 'localhost' )} :{ server .get ('Port' , 9999 )} "
7346 stream_url = f"{ base_url } /scene/{ marker ['scene' ]['id' ]} /scene_marker/{ marker ['id' ]} /stream"
7447
75- # RESET & UPDATE
76- clear_mut = "mutation($id: ID!) { tagUpdate(input: { id: $id, image: \" \" }) { id } }"
77- call_gql (clear_mut , {"id" : tag_id })
78-
79- update_mut = "mutation($id: ID!, $img: String!) { tagUpdate(input: { id: $id, image: $img }) { id } }"
80- call_gql (update_mut , {"id" : tag_id , "img" : stream_url })
48+ stash .call_GQL ("mutation($id: ID!) { tagUpdate(input: { id: $id, image: \" \" }) { id } }" , {"id" : tag_id })
49+ stash .call_GQL ("mutation($id: ID!, $img: String!) { tagUpdate(input: { id: $id, image: $img }) { id } }" , {"id" : tag_id , "img" : stream_url })
8150
82- # 3. UPDATE STASH UI PROGRESS BAR
8351 log .progress (idx / total )
84-
52+
8553 if idx % 10 == 0 :
8654 elapsed = time .time () - start_time
8755 eta = int ((elapsed / idx ) * (total - idx ))
0 commit comments