Enhance CivitaiClient to return HTTP status code with model metadata; update LoraScanner to handle deleted models

This commit is contained in:
Will Miao
2025-03-12 11:18:19 +08:00
parent 8e5e16ce68
commit 9142cc4cde
2 changed files with 34 additions and 11 deletions

View File

@@ -163,14 +163,16 @@ class CivitaiClient:
logger.error(f"Error fetching model version info: {e}") logger.error(f"Error fetching model version info: {e}")
return None return None
async def get_model_metadata(self, model_id: str) -> Optional[Dict]: async def get_model_metadata(self, model_id: str) -> Tuple[Optional[Dict], int]:
"""Fetch model metadata (description and tags) from Civitai API """Fetch model metadata (description and tags) from Civitai API
Args: Args:
model_id: The Civitai model ID model_id: The Civitai model ID
Returns: Returns:
Optional[Dict]: A dictionary containing model metadata or None if not found Tuple[Optional[Dict], int]: A tuple containing:
- A dictionary with model metadata or None if not found
- The HTTP status code from the request
""" """
try: try:
session = await self.session session = await self.session
@@ -178,9 +180,11 @@ class CivitaiClient:
url = f"{self.base_url}/models/{model_id}" url = f"{self.base_url}/models/{model_id}"
async with session.get(url, headers=headers) as response: async with session.get(url, headers=headers) as response:
if response.status != 200: status_code = response.status
logger.warning(f"Failed to fetch model metadata: Status {response.status}")
return None if status_code != 200:
logger.warning(f"Failed to fetch model metadata: Status {status_code}")
return None, status_code
data = await response.json() data = await response.json()
@@ -191,19 +195,19 @@ class CivitaiClient:
} }
if metadata["description"] or metadata["tags"]: if metadata["description"] or metadata["tags"]:
return metadata return metadata, status_code
else: else:
logger.warning(f"No metadata found for model {model_id}") logger.warning(f"No metadata found for model {model_id}")
return None return None, status_code
except Exception as e: except Exception as e:
logger.error(f"Error fetching model metadata: {e}", exc_info=True) logger.error(f"Error fetching model metadata: {e}", exc_info=True)
return None return None, 0
# Keep old method for backward compatibility, delegating to the new one # Keep old method for backward compatibility, delegating to the new one
async def get_model_description(self, model_id: str) -> Optional[str]: async def get_model_description(self, model_id: str) -> Optional[str]:
"""Fetch the model description from Civitai API (Legacy method)""" """Fetch the model description from Civitai API (Legacy method)"""
metadata = await self.get_model_metadata(model_id) metadata, _ = await self.get_model_metadata(model_id)
return metadata.get("description") if metadata else None return metadata.get("description") if metadata else None
async def close(self): async def close(self):

View File

@@ -384,6 +384,11 @@ class LoraScanner:
lora_data: Lora metadata dictionary to update lora_data: Lora metadata dictionary to update
""" """
try: try:
# Skip if already marked as deleted on Civitai
if lora_data.get('civitai_deleted', False):
logger.debug(f"Skipping metadata fetch for {file_path}: marked as deleted on Civitai")
return
# Check if we need to fetch additional metadata from Civitai # Check if we need to fetch additional metadata from Civitai
needs_metadata_update = False needs_metadata_update = False
model_id = None model_id = None
@@ -408,10 +413,24 @@ class LoraScanner:
logger.info(f"Fetching missing metadata for {file_path} with model ID {model_id}") logger.info(f"Fetching missing metadata for {file_path} with model ID {model_id}")
from ..services.civitai_client import CivitaiClient from ..services.civitai_client import CivitaiClient
client = CivitaiClient() client = CivitaiClient()
model_metadata = await client.get_model_metadata(model_id)
# Get metadata and status code
model_metadata, status_code = await client.get_model_metadata(model_id)
await client.close() await client.close()
if (model_metadata): # Handle 404 status (model deleted from Civitai)
if status_code == 404:
logger.warning(f"Model {model_id} appears to be deleted from Civitai (404 response)")
# Mark as deleted to avoid future API calls
lora_data['civitai_deleted'] = True
# Save the updated metadata back to file
metadata_path = os.path.splitext(file_path)[0] + '.metadata.json'
with open(metadata_path, 'w', encoding='utf-8') as f:
json.dump(lora_data, f, indent=2, ensure_ascii=False)
# Process valid metadata if available
elif model_metadata:
logger.info(f"Updating metadata for {file_path} with model ID {model_id}") logger.info(f"Updating metadata for {file_path} with model ID {model_id}")
# Update tags if they were missing # Update tags if they were missing