Fixes and improvements.
This commit is contained in:
+74
-58
@@ -1,6 +1,6 @@
|
||||
import logging
|
||||
from functools import cache
|
||||
from typing import Any, Dict, List, Optional, Union
|
||||
from typing import Any, Dict, List, Optional
|
||||
|
||||
import requests
|
||||
|
||||
@@ -99,33 +99,43 @@ class IGDBQuery(object):
|
||||
"""
|
||||
if not query or not query.strip():
|
||||
return None
|
||||
|
||||
|
||||
try:
|
||||
# Search for games with fuzzy matching
|
||||
games = self.generalized_igdb_query(
|
||||
{"name": query.strip()},
|
||||
"games",
|
||||
[
|
||||
"id", "name", "summary", "storyline", "rating", "aggregated_rating",
|
||||
"first_release_date", "genres.name", "platforms.name", "developers.name",
|
||||
"publishers.name", "game_modes.name", "themes.name", "cover.url"
|
||||
"id",
|
||||
"name",
|
||||
"summary",
|
||||
"storyline",
|
||||
"rating",
|
||||
"aggregated_rating",
|
||||
"first_release_date",
|
||||
"genres.name",
|
||||
"platforms.name",
|
||||
"involved_companies.company.name",
|
||||
"game_modes.name",
|
||||
"themes.name",
|
||||
"cover.url",
|
||||
],
|
||||
additional_filters={"category": "= 0"}, # Main games only
|
||||
limit=limit
|
||||
limit=limit,
|
||||
)
|
||||
|
||||
|
||||
if not games:
|
||||
return None
|
||||
|
||||
|
||||
# Format games for AI consumption
|
||||
formatted_games = []
|
||||
for game in games:
|
||||
formatted_game = self._format_game_for_ai(game)
|
||||
if formatted_game:
|
||||
formatted_games.append(formatted_game)
|
||||
|
||||
|
||||
return formatted_games if formatted_games else None
|
||||
|
||||
|
||||
except Exception as e:
|
||||
logging.error(f"Error searching games for query '{query}': {e}")
|
||||
return None
|
||||
@@ -139,22 +149,37 @@ class IGDBQuery(object):
|
||||
{},
|
||||
"games",
|
||||
[
|
||||
"id", "name", "summary", "storyline", "rating", "aggregated_rating",
|
||||
"first_release_date", "genres.name", "platforms.name", "developers.name",
|
||||
"publishers.name", "game_modes.name", "themes.name", "keywords.name",
|
||||
"similar_games.name", "cover.url", "screenshots.url", "videos.video_id",
|
||||
"release_dates.date", "release_dates.platform.name", "age_ratings.rating"
|
||||
"id",
|
||||
"name",
|
||||
"summary",
|
||||
"storyline",
|
||||
"rating",
|
||||
"aggregated_rating",
|
||||
"first_release_date",
|
||||
"genres.name",
|
||||
"platforms.name",
|
||||
"involved_companies.company.name",
|
||||
"game_modes.name",
|
||||
"themes.name",
|
||||
"keywords.name",
|
||||
"similar_games.name",
|
||||
"cover.url",
|
||||
"screenshots.url",
|
||||
"videos.video_id",
|
||||
"release_dates.date",
|
||||
"release_dates.platform.name",
|
||||
"age_ratings.rating",
|
||||
],
|
||||
additional_filters={"id": f"= {game_id}"},
|
||||
limit=1
|
||||
limit=1,
|
||||
)
|
||||
|
||||
|
||||
if games and len(games) > 0:
|
||||
return self._format_game_for_ai(games[0], detailed=True)
|
||||
|
||||
|
||||
except Exception as e:
|
||||
logging.error(f"Error getting game details for ID {game_id}: {e}")
|
||||
|
||||
|
||||
return None
|
||||
|
||||
def _format_game_for_ai(self, game_data: Dict[str, Any], detailed: bool = False) -> Dict[str, Any]:
|
||||
@@ -162,60 +187,56 @@ class IGDBQuery(object):
|
||||
Format game data in a way that's easy for AI to understand and present to users.
|
||||
"""
|
||||
try:
|
||||
formatted = {
|
||||
"name": game_data.get("name", "Unknown"),
|
||||
"summary": game_data.get("summary", "No summary available")
|
||||
}
|
||||
|
||||
formatted = {"name": game_data.get("name", "Unknown"), "summary": game_data.get("summary", "No summary available")}
|
||||
|
||||
# Add basic info
|
||||
if "rating" in game_data:
|
||||
formatted["rating"] = f"{game_data['rating']:.1f}/100"
|
||||
if "aggregated_rating" in game_data:
|
||||
formatted["user_rating"] = f"{game_data['aggregated_rating']:.1f}/100"
|
||||
|
||||
|
||||
# Release information
|
||||
if "first_release_date" in game_data:
|
||||
import datetime
|
||||
|
||||
release_date = datetime.datetime.fromtimestamp(game_data["first_release_date"])
|
||||
formatted["release_year"] = release_date.year
|
||||
if detailed:
|
||||
formatted["release_date"] = release_date.strftime("%Y-%m-%d")
|
||||
|
||||
|
||||
# Platforms
|
||||
if "platforms" in game_data and game_data["platforms"]:
|
||||
platforms = [p.get("name", "") for p in game_data["platforms"] if p.get("name")]
|
||||
formatted["platforms"] = platforms[:5] # Limit to prevent overflow
|
||||
|
||||
# Genres
|
||||
|
||||
# Genres
|
||||
if "genres" in game_data and game_data["genres"]:
|
||||
genres = [g.get("name", "") for g in game_data["genres"] if g.get("name")]
|
||||
formatted["genres"] = genres
|
||||
|
||||
# Developers
|
||||
if "developers" in game_data and game_data["developers"]:
|
||||
developers = [d.get("name", "") for d in game_data["developers"] if d.get("name")]
|
||||
formatted["developers"] = developers[:3] # Limit for readability
|
||||
|
||||
# Publishers
|
||||
if "publishers" in game_data and game_data["publishers"]:
|
||||
publishers = [p.get("name", "") for p in game_data["publishers"] if p.get("name")]
|
||||
formatted["publishers"] = publishers[:2]
|
||||
|
||||
|
||||
# Companies (developers/publishers)
|
||||
if "involved_companies" in game_data and game_data["involved_companies"]:
|
||||
companies = []
|
||||
for company_data in game_data["involved_companies"]:
|
||||
if "company" in company_data and "name" in company_data["company"]:
|
||||
companies.append(company_data["company"]["name"])
|
||||
formatted["companies"] = companies[:5] # Limit for readability
|
||||
|
||||
if detailed:
|
||||
# Add more detailed info for specific requests
|
||||
if "storyline" in game_data and game_data["storyline"]:
|
||||
formatted["storyline"] = game_data["storyline"]
|
||||
|
||||
|
||||
if "game_modes" in game_data and game_data["game_modes"]:
|
||||
modes = [m.get("name", "") for m in game_data["game_modes"] if m.get("name")]
|
||||
formatted["game_modes"] = modes
|
||||
|
||||
|
||||
if "themes" in game_data and game_data["themes"]:
|
||||
themes = [t.get("name", "") for t in game_data["themes"] if t.get("name")]
|
||||
formatted["themes"] = themes
|
||||
|
||||
|
||||
return formatted
|
||||
|
||||
|
||||
except Exception as e:
|
||||
logging.error(f"Error formatting game data: {e}")
|
||||
return {"name": game_data.get("name", "Unknown"), "summary": "Error retrieving game information"}
|
||||
@@ -234,30 +255,25 @@ class IGDBQuery(object):
|
||||
"properties": {
|
||||
"query": {
|
||||
"type": "string",
|
||||
"description": "The game name or search query (e.g., 'Elden Ring', 'Mario', 'Zelda Breath of the Wild')"
|
||||
"description": "The game name or search query (e.g., 'Elden Ring', 'Mario', 'Zelda Breath of the Wild')",
|
||||
},
|
||||
"limit": {
|
||||
"type": "integer",
|
||||
"type": "integer",
|
||||
"description": "Maximum number of games to return (default: 5, max: 10)",
|
||||
"minimum": 1,
|
||||
"maximum": 10
|
||||
}
|
||||
"maximum": 10,
|
||||
},
|
||||
},
|
||||
"required": ["query"]
|
||||
}
|
||||
"required": ["query"],
|
||||
},
|
||||
},
|
||||
{
|
||||
"name": "get_game_details",
|
||||
"description": "Get detailed information about a specific game when you have its ID from a previous search.",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"game_id": {
|
||||
"type": "integer",
|
||||
"description": "The IGDB game ID from a previous search result"
|
||||
}
|
||||
},
|
||||
"required": ["game_id"]
|
||||
}
|
||||
}
|
||||
"properties": {"game_id": {"type": "integer", "description": "The IGDB game ID from a previous search result"}},
|
||||
"required": ["game_id"],
|
||||
},
|
||||
},
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user