-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Notebook for merging StashDB tags to matched scenes
- Loading branch information
1 parent
c955a68
commit 8f87c31
Showing
1 changed file
with
133 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import pandas as pd\n", | ||
"import dotenv\n", | ||
"import os\n", | ||
"from libraries.client_stashapp import get_stashapp_client\n", | ||
"from libraries.StashDbClient import StashDbClient\n", | ||
"\n", | ||
"dotenv.load_dotenv()\n", | ||
"\n", | ||
"stash = get_stashapp_client()\n", | ||
"\n", | ||
"stashbox_client = StashDbClient(\n", | ||
" os.getenv(\"STASHDB_ENDPOINT\"),\n", | ||
" os.getenv(\"STASHDB_API_KEY\"),\n", | ||
")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"def get_scenes_with_stashdb_id():\n", | ||
" scenes = stash.find_scenes(\n", | ||
" {\n", | ||
" \"stash_id_endpoint\": {\n", | ||
" \"endpoint\": \"https://stashdb.org/graphql\",\n", | ||
" \"modifier\": \"NOT_NULL\",\n", | ||
" },\n", | ||
" \"tags\": {\n", | ||
" \"value\": [],\n", | ||
" \"modifier\": \"IS_NULL\"\n", | ||
" }\n", | ||
" },\n", | ||
" filter = {\n", | ||
" \"per_page\": 1000,\n", | ||
" \"page\": 1\n", | ||
" }\n", | ||
" )\n", | ||
" return scenes\n", | ||
"\n", | ||
"scenes_with_stashdb_id = get_scenes_with_stashdb_id()\n", | ||
"df_scenes = pd.DataFrame(scenes_with_stashdb_id)\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Step 2: Extract StashDB IDs and existing tags\n", | ||
"df_scenes['stashdb_id'] = df_scenes['stash_ids'].apply(\n", | ||
" lambda x: next((stash_id['stash_id'] for stash_id in x if stash_id['endpoint'] == 'https://stashdb.org/graphql'), None)\n", | ||
")\n", | ||
"df_scenes['existing_tags'] = df_scenes['tags'].apply(lambda x: sorted([tag['name'] for tag in x]))\n", | ||
"\n", | ||
"# Step 3: Fetch StashDB data for each scene\n", | ||
"def get_stashdb_data(stashdb_id):\n", | ||
" return stashbox_client.query_scenes(stashdb_id)\n", | ||
"\n", | ||
"df_scenes['stashdb_data'] = df_scenes['stashdb_id'].apply(get_stashdb_data)\n", | ||
"\n", | ||
"# Step 4: Extract tags from StashDB data\n", | ||
"def extract_tags(stashdb_data):\n", | ||
" if stashdb_data and 'data' in stashdb_data and 'findScene' in stashdb_data['data']:\n", | ||
" return sorted([tag['name'] for tag in stashdb_data['data']['findScene']['tags']])\n", | ||
" return []\n", | ||
"\n", | ||
"df_scenes['stashdb_tags'] = df_scenes['stashdb_data'].apply(extract_tags)\n", | ||
"\n", | ||
"# Step 5: Merge existing tags with StashDB tags\n", | ||
"df_scenes['merged_tags'] = df_scenes.apply(lambda row: sorted(list(set(row['existing_tags'] + row['stashdb_tags']))), axis=1)\n", | ||
"\n", | ||
"# Step 6: Compare existing tags with merged tags\n", | ||
"df_scenes['tags_identical'] = df_scenes.apply(lambda row: set(row['existing_tags']) == set(row['merged_tags']), axis=1)\n", | ||
"\n", | ||
"# Step 7: Find corresponding tags in local Stash for merged tags\n", | ||
"def find_stash_tags(tag_names):\n", | ||
" return sorted([stash.find_tag(tag_name) for tag_name in tag_names], key=lambda x: x['name'] if x else '')\n", | ||
"\n", | ||
"df_scenes['stash_tags'] = df_scenes['merged_tags'].apply(find_stash_tags)\n", | ||
"\n", | ||
"# Step 8: Select relevant columns\n", | ||
"df_selected = df_scenes[['id', 'title', 'details', 'date', 'existing_tags', 'stashdb_tags', 'merged_tags', 'tags_identical', 'stash_tags']]\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Update tags for scenes where tags are not identical\n", | ||
"for index, row in df_selected.iterrows():\n", | ||
" if not row['tags_identical'] and row['stash_tags']:\n", | ||
" stash.update_scene({\n", | ||
" \"id\": row['id'],\n", | ||
" \"tag_ids\": [tag['id'] for tag in row['stash_tags'] if tag]\n", | ||
" })" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "name_of_my_env", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.12.4" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |