TPDB marker notebook

This commit is contained in:
Minasuki Hikimuna
2024-09-10 09:02:44 +03:00
parent 14bb324da6
commit dc6e49028a
+226
View File
@@ -0,0 +1,226 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Create Stash app client\n",
"\n",
"import pandas as pd\n",
"import dotenv\n",
"\n",
"from libraries.client_stashapp import get_stashapp_client\n",
"\n",
"dotenv.load_dotenv()\n",
"\n",
"stash = get_stashapp_client()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import requests\n",
"\n",
"tpdb_headers = {\n",
" \"Content-Type\": \"application/json\",\n",
" \"Authorization\": f\"Bearer {os.environ['TPDB_API_KEY']}\",\n",
"}\n",
"\n",
"def get_scene_tpdb_id(stash_scene):\n",
" # Extract TPDB ID from stash_ids\n",
" tpdb_id = None\n",
" for stash_id in stash_scene.get('stash_ids', []):\n",
" if stash_id['endpoint'] == 'https://theporndb.net/graphql':\n",
" tpdb_id = stash_id['stash_id']\n",
" break\n",
" return tpdb_id\n",
"\n",
"def get_tpdb_scene_data(tpdb_id):\n",
" url = f\"https://api.theporndb.net/scenes/{tpdb_id}\"\n",
" response = requests.get(url, headers=tpdb_headers)\n",
" return response.json()\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"marker_source_tpdb = \"Marker Source: TPDB\"\n",
"marker_source_skier = \"Marker Source: Skier AI\"\n",
"marker_source_manual = \"Marker Source: Manual\"\n",
"\n",
"marker_source_tpdb_tag = stash.find_tag(marker_source_tpdb)\n",
"marker_source_skier_tag = stash.find_tag(marker_source_skier)\n",
"marker_source_manual_tag = stash.find_tag(marker_source_manual)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"scene_id = 12661\n",
"\n",
"stash_scene = stash.find_scene(scene_id)\n",
"print(stash_scene['title'])\n",
"\n",
"tpdb_id = get_scene_tpdb_id(stash_scene)\n",
"print(tpdb_id)\n",
"\n",
"tpdb_scene_data = get_tpdb_scene_data(tpdb_id)\n",
"print(tpdb_scene_data['data']['title'])\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"tpdb_markers = tpdb_scene_data['data']['markers']\n",
"tpdb_markers\n",
"\n",
"for marker in tpdb_markers:\n",
" print(marker)\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"scene_markers = stash_scene['scene_markers']\n",
"\n",
"for marker in scene_markers:\n",
" print({\n",
" 'title': marker['title'],\n",
" 'seconds': marker['seconds'],\n",
" 'primary_tag': marker['primary_tag']['name'],\n",
" 'tags': [tag['name'] for tag in marker['tags']]\n",
" })"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Convert Stash markers to a set of (title, seconds) tuples for easy comparison\n",
"stash_markers_set = set((marker['title'], marker['seconds']) for marker in scene_markers)\n",
"\n",
"# Function to convert seconds to MM:SS or HH:MM:SS format\n",
"def format_time(seconds):\n",
" minutes, secs = divmod(seconds, 60)\n",
" hours, minutes = divmod(minutes, 60)\n",
" if hours > 0:\n",
" return f\"{hours:02d}:{minutes:02d}:{secs:02d}\"\n",
" else:\n",
" return f\"{minutes:02d}:{secs:02d}\"\n",
"\n",
"# Find markers in TPDB that are not in Stash\n",
"new_markers = []\n",
"print(\"Markers in TPDB but not in Stash:\")\n",
"for tpdb_marker in tpdb_markers:\n",
" title = tpdb_marker['title']\n",
" seconds = tpdb_marker['start_time']\n",
" formatted_time = format_time(seconds)\n",
" if (title, seconds) not in stash_markers_set:\n",
" print(f\"- {title} at {formatted_time}\")\n",
" # Get the tag ID from Stash\n",
" tag = stash.find_tag(title)\n",
" if tag:\n",
" tpdb_marker['tag_id'] = tag['id']\n",
" else:\n",
" print(f\"Warning: Tag '{title}' not found in Stash\")\n",
" new_markers.append(tpdb_marker)\n",
"\n",
"print(f\"\\nTotal new markers to add: {len(new_markers)}\")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for new_marker in new_markers:\n",
" stash.create_scene_marker({\n",
" \"scene_id\": scene_id,\n",
" \"title\": new_marker['title'],\n",
" \"primary_tag_id\": new_marker['tag_id'],\n",
" \"seconds\": new_marker['start_time'],\n",
" \"tag_ids\": [marker_source_tpdb_tag['id']],\n",
" })\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Batch operation"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"scenes_with_tpdb_id = stash.find_scenes({\n",
" \"stash_id_endpoint\": {\"endpoint\": \"https://theporndb.net/graphql\", \"modifier\": \"NOT_NULL\" },\n",
" \"title\": { \"modifier\": \"EQUALS\", \"value\": \"Insatiable Redhead Jia Loves BBC And Anal\" }\n",
"})\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for scene in scenes_with_tpdb_id:\n",
" stash_scene = stash.find_scene(scene_id)\n",
" print(stash_scene['title'])\n",
"\n",
" tpdb_id = get_scene_tpdb_id(stash_scene)\n",
" print(tpdb_id)\n",
"\n",
" tpdb_scene_data = get_tpdb_scene_data(tpdb_id)\n",
" print(tpdb_scene_data['data']['title'])\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
}