1818import hashlib
1919import requests
2020from typing import Optional
21+
22+ from stashapi .stashapp import StashInterface
2123try :
2224 from bs4 import BeautifulSoup
2325except ImportError :
@@ -84,8 +86,6 @@ def progress(pct: float):
8486 except Exception as _e :
8587 log .warning (f"Could not read config.json: { _e } " )
8688
87- STASH_URL = os .getenv ("STASH_URL" ) or _config .get ("stash_url" , "http://localhost:9999" )
88- STASH_API_KEY = os .getenv ("STASH_API_KEY" ) or _config .get ("stash_api_key" , "" )
8989ADVE_SESSION_COOKIE = os .getenv ("ADVE_SESSION_COOKIE" ) or _config .get ("adve_session_cookie" , "" )
9090
9191HEADERS = {
@@ -104,21 +104,7 @@ def progress(pct: float):
104104# STASH GRAPHQL
105105# ─────────────────────────────────────────────
106106
107- def stash_gql (query : str , variables : dict = None ) -> dict :
108- headers = {"Content-Type" : "application/json" }
109- if STASH_API_KEY :
110- headers ["ApiKey" ] = STASH_API_KEY
111- resp = requests .post (
112- f"{ STASH_URL } /graphql" ,
113- json = {"query" : query , "variables" : variables or {}},
114- headers = headers ,
115- timeout = 15 ,
116- )
117- resp .raise_for_status ()
118- return resp .json ().get ("data" , {})
119-
120-
121- def get_group (group_id : str ) -> dict :
107+ def get_group (stash : StashInterface , group_id : str ) -> dict :
122108 query = """
123109 query FindGroup($id: ID!) {
124110 findGroup(id: $id) {
@@ -134,10 +120,11 @@ def get_group(group_id: str) -> dict:
134120 }
135121 }
136122 """
137- return stash_gql (query , {"id" : group_id }).get ("findGroup" )
123+ result = stash .call_GQL (query , {"id" : group_id })
124+ return result .get ("findGroup" )
138125
139126
140- def get_all_groups () -> list :
127+ def get_all_groups (stash : StashInterface ) -> list :
141128 query = """
142129 query AllGroups($filter: FindFilterType, $group_filter: GroupFilterType) {
143130 findGroups(filter: $filter, group_filter: $group_filter) {
@@ -147,15 +134,16 @@ def get_all_groups() -> list:
147134 """
148135 # per_page: -1 returns all records; group_filter restricts to groups
149136 # whose URL list contains an adultdvdempire.com URL.
150- return stash_gql (query , {
137+ result = stash . call_GQL (query , {
151138 "filter" : {"per_page" : - 1 },
152139 "group_filter" : {
153140 "url" : {
154141 "value" : "adultdvdempire.com" ,
155142 "modifier" : "INCLUDES" ,
156143 }
157144 },
158- }).get ("findGroups" , {}).get ("groups" , [])
145+ })
146+ return result .get ("findGroups" , {}).get ("groups" , [])
159147
160148
161149# ─────────────────────────────────────────────
@@ -418,8 +406,8 @@ def match_scene(adve_scene: dict, stash_scenes: list) -> Optional[dict]:
418406 return None
419407
420408
421- def build_comparison (group_id : str ) -> dict :
422- group = get_group (group_id )
409+ def build_comparison (stash : StashInterface , group_id : str ) -> dict :
410+ group = get_group (stash , group_id )
423411 if not group :
424412 return {"error" : f"Group { group_id } not found in Stash." }
425413
@@ -473,32 +461,31 @@ def main():
473461 sys .exit (1 )
474462
475463def _run ():
476- raw = sys .stdin .read ().strip ()
477- try :
478- plugin_input = json .loads (raw ) if raw else {}
479- except json .JSONDecodeError :
480- plugin_input = {}
464+ json_input = json .loads (sys .stdin .read ())
481465
482466 if ADVE_SESSION_COOKIE :
483467 log .info ("Session cookie loaded from config.json" )
484468 else :
485469 log .warning ("No session cookie found in config.json or environment" )
486470
487- args = plugin_input .get ("args" , {})
471+ server_connection = json_input ["server_connection" ]
472+ stash = StashInterface (server_connection )
473+
474+ args = json_input .get ("args" , {})
488475 mode = args .get ("mode" , "check_group" )
489476
490477 if mode == "check_group" :
491478 group_id = args .get ("group_id" )
492479 if not group_id :
493480 result = {"error" : "group_id is required" }
494481 else :
495- result = build_comparison (group_id )
482+ result = build_comparison (stash , group_id )
496483 # Write to result file so JS can poll for it
497484 write_result (group_id , result )
498485 print (json .dumps (result ))
499486
500487 elif mode == "scrape_all" :
501- groups = get_all_groups ()
488+ groups = get_all_groups (stash )
502489 total = len (groups )
503490 summary = []
504491 log .info (f"Scraping { total } groups with ADVE URLs..." )
@@ -508,7 +495,7 @@ def _run():
508495 for idx , g in enumerate (groups , start = 1 ):
509496 if find_adve_url (g .get ("urls" , [])):
510497 log .info (f"[{ idx } /{ total } ] { g ['name' ]} " )
511- result = build_comparison (g ["id" ])
498+ result = build_comparison (stash , g ["id" ])
512499 write_result (g ["id" ], result )
513500 summary .append ({
514501 "group_id" : g ["id" ],
0 commit comments