|
1 | 1 | import sys |
2 | 2 | import json |
3 | | -import requests |
4 | 3 | import stashapi.log as log |
| 4 | +from stashapi.stashapp import StashInterface |
5 | 5 |
|
6 | 6 | def main(): |
7 | 7 | try: |
8 | | - # Standard way for Stash plugins to receive server info |
9 | 8 | raw_input = sys.stdin.read() |
10 | 9 | if not raw_input: |
11 | 10 | log.error("No input received from Stash.") |
12 | 11 | return |
13 | | - |
14 | | - input_data = json.loads(raw_input) |
15 | | - server = input_data.get("server_connection", {}) |
16 | | - |
17 | | - # Use dynamic base_url to handle local vs remote connections |
18 | | - base_url = f"{server.get('Scheme', 'http')}://{server.get('Host', 'localhost')}:{server.get('Port', 9999)}" |
19 | | - headers = {'Content-Type': 'application/json'} |
20 | | - if server.get('ApiKey'): |
21 | | - headers['ApiKey'] = server.get('ApiKey') |
| 12 | + |
| 13 | + json_input = json.loads(raw_input) |
| 14 | + stash = StashInterface(json_input["server_connection"]) |
| 15 | + |
22 | 16 | except Exception as e: |
23 | 17 | log.error(f"Initialization failed: {e}") |
24 | 18 | return |
25 | 19 |
|
26 | | - # GraphQL query to find all tags |
27 | | - query = "{ findTags(filter: {per_page: -1}) { tags { id image_path } } }" |
28 | 20 | try: |
29 | | - res = requests.post(f"{base_url}/graphql", json={'query': query}, headers=headers, timeout=10).json() |
30 | | - tags = res.get("data", {}).get("findTags", {}).get("tags", []) |
| 21 | + result = stash.call_GQL("{ findTags(filter: {per_page: -1}) { tags { id image_path } } }") |
| 22 | + tags = result.get("findTags", {}).get("tags", []) |
31 | 23 | except Exception as e: |
32 | 24 | log.error(f"Failed to fetch tags: {e}") |
33 | 25 | return |
34 | 26 |
|
35 | | - # Clear covers only for tags that have one |
36 | 27 | tags_to_clear = [t for t in tags if t.get("image_path")] |
37 | 28 | total = len(tags_to_clear) |
38 | | - |
| 29 | + |
39 | 30 | if total == 0: |
40 | 31 | log.info("No tag covers found to clear.") |
41 | 32 | return |
42 | 33 |
|
| 34 | + mutation = "mutation($id: ID!) { tagUpdate(input: { id: $id, image: \"\" }) { id } }" |
43 | 35 | for idx, tag in enumerate(tags_to_clear, 1): |
44 | | - mutation = "mutation($id: ID!) { tagUpdate(input: { id: $id, image: \"\" }) { id } }" |
45 | | - requests.post(f"{base_url}/graphql", json={'query': mutation, 'variables': {"id": tag['id']}}, headers=headers) |
| 36 | + try: |
| 37 | + stash.call_GQL(mutation, {"id": tag["id"]}) |
| 38 | + except Exception as e: |
| 39 | + log.error(f"Failed to clear tag {tag['id']}: {e}") |
46 | 40 | log.progress(idx / total) |
47 | 41 |
|
48 | 42 | log.info(f"Successfully cleared {total} tag cover images.") |
|
0 commit comments