new artwork

This commit is contained in:
Roy 2024-08-07 21:29:31 -07:00 committed by GitHub
parent c0afa31385
commit fe73b88efd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -176,8 +176,7 @@ def get_unsigned_shortcut_id(signed_shortcut_id):
api_cache = {} api_cache = {}
#API KEYS FOR NONSTEAMLAUNCHER USE ONLY #API KEYS FOR NONSTEAMLAUNCHER USE ONLY
sgdb = SteamGridDB('e3bf9f166d7a80ae260387f90e36d10e') BASE_URL = 'https://myproxycache-203e9b0bbe5b.herokuapp.com/api'
api_key = 'e3bf9f166d7a80ae260387f90e36d10e'
#GLOBAL VARS #GLOBAL VARS
created_shortcuts = [] created_shortcuts = []
@ -236,23 +235,22 @@ def get_sgdb_art(game_id, app_id):
global logo64 global logo64
global hero64 global hero64
print(f"Downloading icons artwork...") print(f"Downloading icons artwork...")
download_artwork(game_id, api_key, "icons", app_id) download_artwork(game_id, "icons", app_id)
print(f"Downloading logos artwork...") print(f"Downloading logos artwork...")
logo64 = download_artwork(game_id, api_key, "logos", app_id) logo64 = download_artwork(game_id, "logos", app_id)
print(f"Downloading heroes artwork...") print(f"Downloading heroes artwork...")
hero64 = download_artwork(game_id, api_key, "heroes", app_id) hero64 = download_artwork(game_id, "heroes", app_id)
print("Downloading grids artwork of size 600x900...") print("Downloading grids artwork of size 600x900...")
gridp64 = download_artwork(game_id, api_key, "grids", app_id, "600x900") gridp64 = download_artwork(game_id, "grids", app_id, "600x900")
print("Downloading grids artwork of size 920x430...") print("Downloading grids artwork of size 920x430...")
grid64 =download_artwork(game_id, api_key, "grids", app_id, "920x430") grid64 = download_artwork(game_id, "grids", app_id, "920x430")
def download_artwork(game_id, art_type, shortcut_id, dimensions=None):
if game_id is None:
print("Invalid game ID. Skipping download.")
return
def download_artwork(game_id, api_key, art_type, shortcut_id, dimensions=None):
# Create a cache key based on the function's arguments
cache_key = (game_id, art_type, dimensions) cache_key = (game_id, art_type, dimensions)
# Check if the artwork already exists
if dimensions is not None: if dimensions is not None:
filename = get_file_name(art_type, shortcut_id, dimensions) filename = get_file_name(art_type, shortcut_id, dimensions)
else: else:
@ -268,37 +266,32 @@ def download_artwork(game_id, api_key, art_type, shortcut_id, dimensions=None):
with open(file_path, 'rb') as image_file: with open(file_path, 'rb') as image_file:
return b64encode(image_file.read()).decode('utf-8') return b64encode(image_file.read()).decode('utf-8')
# If the result is in the cache, use it
if cache_key in api_cache: if cache_key in api_cache:
data = api_cache[cache_key] data = api_cache[cache_key]
else: else:
try: try:
# If the result is not in the cache, make the API call print(f"Game ID: {game_id}")
print(f"Game ID: {game_id}, API Key: {api_key}") url = f"{BASE_URL}/{art_type}/game/{game_id}"
url = f"https://www.steamgriddb.com/api/v2/{art_type}/game/{game_id}"
if dimensions: if dimensions:
url += f"?dimensions={dimensions}" url += f"?dimensions={dimensions}"
headers = {'Authorization': f'Bearer {api_key}'} print(f"Request URL: {url}")
print(f"Sending request to: {url}") # Added print statement response = requests.get(url)
response = requests.get(url, headers=headers)
response.raise_for_status() response.raise_for_status()
data = response.json() data = response.json()
# Store the result in the cache print(f"Response data: {data}")
api_cache[cache_key] = data api_cache[cache_key] = data
except Exception as e: # Catching a general exception except Exception as e:
print(f"Error making API call: {e}") print(f"Error making API call: {e}")
api_cache[cache_key] = None api_cache[cache_key] = None
return return
# Ensure data is not None before proceeding
if data is None: if data is None:
print(f"No data available for {game_id}. Skipping download.") print(f"No data available for {game_id}. Skipping download.")
return return
# Continue with the rest of your function using `data`
for artwork in data['data']: for artwork in data['data']:
image_url = artwork['thumb'] image_url = artwork['thumb']
print(f"Downloading image from: {image_url}") # Added print statement print(f"Downloading image from: {image_url}")
try: try:
response = requests.get(image_url, stream=True) response = requests.get(image_url, stream=True)
response.raise_for_status() response.raise_for_status()
@ -309,29 +302,49 @@ def download_artwork(game_id, api_key, art_type, shortcut_id, dimensions=None):
except requests.exceptions.RequestException as e: except requests.exceptions.RequestException as e:
print(f"Error downloading image: {e}") print(f"Error downloading image: {e}")
if art_type == 'icons': if art_type == 'icons':
download_artwork(game_id, api_key, 'icons_ico', shortcut_id) download_artwork(game_id, 'icons_ico', shortcut_id)
if data is None:
print(f"No data available for {game_id}. Skipping download.")
return
for artwork in data['data']:
image_url = artwork['thumb']
print(f"Downloading image from: {image_url}")
try:
response = requests.get(image_url, stream=True)
response.raise_for_status()
if response.status_code == 200:
with open(file_path, 'wb') as file:
file.write(response.content)
return b64encode(response.content).decode('utf-8')
except requests.exceptions.RequestException as e:
print(f"Error downloading image: {e}")
if art_type == 'icons':
download_artwork(game_id, 'icons_ico', shortcut_id)
def get_game_id(game_name): def get_game_id(game_name):
print(f"Searching for game ID for: {game_name}") print(f"Searching for game ID for: {game_name}")
try: try:
games = sgdb.search_game(game_name) encoded_game_name = quote(game_name)
for game in games: url = f"{BASE_URL}/search/{encoded_game_name}"
if game.name == game_name: # Case-sensitive comparison print(f"Encoded game name: {encoded_game_name}")
print(f"Found game ID: {game.id}") print(f"Request URL: {url}")
return game.id response = requests.get(url)
# Fallback: return the ID of the first game in the search results response.raise_for_status()
if games: data = response.json()
print(f"No exact match found. Using game ID of the first result: {games[0].name}: {games[0].id}") print(f"Response data: {data}")
return games[0].id if data['data']:
print("No game ID found") game_id = data['data'][0]['id']
return "default_game_id" # Return a default value when no games are found print(f"Found game ID: {game_id}")
except Exception as e: # Catching a general exception return game_id
print(f"No game ID found for game name: {game_name}")
return None
except Exception as e:
print(f"Error searching for game ID: {e}") print(f"Error searching for game ID: {e}")
return "default_game_id" # Return a default value in case of an error return None
def get_file_name(art_type, shortcut_id, dimensions=None): def get_file_name(art_type, shortcut_id, dimensions=None):