|
| 1 | +import sys |
| 2 | +import json |
| 3 | +import requests |
| 4 | +import stashapi.log as log |
| 5 | + |
| 6 | +def main(): |
| 7 | + try: |
| 8 | + # Standard way for Stash plugins to receive server info |
| 9 | + raw_input = sys.stdin.read() |
| 10 | + if not raw_input: |
| 11 | + log.error("No input received from Stash.") |
| 12 | + 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') |
| 22 | + except Exception as e: |
| 23 | + log.error(f"Initialization failed: {e}") |
| 24 | + return |
| 25 | + |
| 26 | + # GraphQL query to find all tags |
| 27 | + query = "{ findTags(filter: {per_page: -1}) { tags { id image_path } } }" |
| 28 | + 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", []) |
| 31 | + except Exception as e: |
| 32 | + log.error(f"Failed to fetch tags: {e}") |
| 33 | + return |
| 34 | + |
| 35 | + # Clear covers only for tags that have one |
| 36 | + tags_to_clear = [t for t in tags if t.get("image_path")] |
| 37 | + total = len(tags_to_clear) |
| 38 | + |
| 39 | + if total == 0: |
| 40 | + log.info("No tag covers found to clear.") |
| 41 | + return |
| 42 | + |
| 43 | + 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) |
| 46 | + log.progress(idx / total) |
| 47 | + |
| 48 | + log.info(f"Successfully cleared {total} tag cover images.") |
| 49 | + |
| 50 | +if __name__ == "__main__": |
| 51 | + main() |
0 commit comments