Skip to content

Commit 28c8ba4

Browse files
authored
Merge pull request #25 from Lurking987/Lurking987-improve-api-handling-FindMarker
Improve API handling
2 parents 1f2c9f9 + fbd6367 commit 28c8ba4

3 files changed

Lines changed: 15 additions & 47 deletions

File tree

plugins/Find Marker For Tag Images/Find Marker For Tag Images.py

Lines changed: 12 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,52 +2,26 @@
22
import json
33
import random
44
import time
5-
import requests
65
import stashapi.log as log
76
from stashapi.stashapp import StashInterface
87

98
def 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))

plugins/Find Marker For Tag Images/Find Marker For Tag Images.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: Find Marker For Tag Images
22
description: Associates random marker video endpoints as tag cover images.
3-
version: 1.0
3+
version: 1.1
44
url: https://github.com/Lurking987/stash-plugins/tree/main/plugins/Find%20Marker%20For%20Tag%20Images
55
exec:
66
- python

plugins/Find Marker For Tag Images/manifest

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ id: Find Marker For Tag Images
22
name: Assign Matching Random Marker Preview Video to Tag Images
33
metadata:
44
description: Script to find markers that match a tag and picks a random preview as the tag image
5-
version: 1.0
6-
date: "2026-02-25 20:20:00"
5+
version: 1.1
6+
date: "2026-04-12 09:42:00"
77
requires: []
88
source_repository:
99
files:

0 commit comments

Comments
 (0)