Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 41 additions & 2 deletions plugins/stashdb-tag-sync/src/stashdbTagSync.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
logging.getLogger('stash_graphql_client').setLevel(logging.CRITICAL)

try:
from stashapi.stashapp import StashInterface
from graphql_client import StashDBClient
from stash_client import StashClient
from core.tag_transfer import transfer_tags_graphql
Expand All @@ -22,6 +23,41 @@
sys.exit(1)


async def fetch_api_key(server_conn: Dict[str, Any]) -> str:
"""Fetch Stash API key from configuration

Connects to Stash using the configuration fragment received during plugin
initialization, and queries Stash's general configuration to get the server
API key (if any).

This is required when Stash generates a session cookie to use for
authentication instead of an API key, but the API key is needed for
the GraphQL client as it does not support authentication via cookies.

Args:
server_conn: The `server_connection` fragment from plugin init

Returns:
A string with the API key (if found). Otherwise empty string.
"""
stash_client = StashInterface(server_conn)

config = stash_client.get_configuration()
if not config:
log.error("No configuration was retrieved")
return ""

if 'general' in config and 'apiKey' in config.get('general', {}):
api_key = config['general']['apiKey'] or ""
logging.debug(f'Fetched API key from configuration: {api_key[0:16]}...')
if not api_key:
log.warning("API key configuration found but empty!")
return api_key

log.error("No API key configuration found in Stash")
return ""


async def fetch_stashdb_config(stash_client: StashClient) -> tuple[str, str]:
"""Fetch StashDB configuration from Stash's configuration.

Expand Down Expand Up @@ -83,12 +119,15 @@ async def plugin_main(input_data: Dict[str, Any]) -> None:
# Extract server connection information
server_conn = input_data.get("server_connection", {})

# Fetch API key from server config if not given in connection fragment
api_key = server_conn.get("ApiKey", await fetch_api_key(server_conn))

# Create Stash connection to query configuration
stash_conn = StashConnection(
scheme=server_conn.get("Scheme", "http"),
host=server_conn.get("Host", "localhost"),
port=server_conn.get("Port", 9999),
api_key=server_conn.get("ApiKey")
api_key=api_key
)

# Use defaults for other settings
Expand All @@ -105,7 +144,7 @@ async def plugin_main(input_data: Dict[str, Any]) -> None:
scheme=stash_conn.scheme,
host=stash_conn.host,
port=stash_conn.port,
api_key=""
api_key=api_key
)

# Temporarily suppress stash_graphql_client's noisy warnings during config fetch
Expand Down