|
1 | 1 | import os |
| 2 | +import io |
2 | 3 | import random |
3 | 4 | import platform |
4 | 5 | import webbrowser |
|
12 | 13 | import concurrent.futures |
13 | 14 | import threading |
14 | 15 | import time |
| 16 | +import json |
15 | 17 |
|
16 | 18 | # Constants |
17 | 19 | STEAM_PATH = os.path.expanduser(r"C:\Program Files (x86)\Steam") |
@@ -169,6 +171,21 @@ def __init__(self, root, installed_games, drives): |
169 | 171 | except Exception as e: |
170 | 172 | print(f"Error setting window icon: {e}") |
171 | 173 |
|
| 174 | + # Path to the folder containing the header images |
| 175 | + if getattr(sys, 'frozen', False): # If running as an executable |
| 176 | + base_path = sys._MEIPASS |
| 177 | + else: # If running as a script |
| 178 | + base_path = os.path.dirname(os.path.abspath(__file__)) |
| 179 | + |
| 180 | + # Define the relative path to the 'header_images' folder |
| 181 | + self.header_images_folder = os.path.join(base_path, "image_cache") |
| 182 | + |
| 183 | + # Try to load images locally, if they don't exist, fallback to Steam API |
| 184 | + if os.path.exists(self.header_images_folder) and os.listdir(self.header_images_folder): |
| 185 | + self.header_images = self.load_header_images(self.header_images_folder) |
| 186 | + else: |
| 187 | + self.header_images = self.fetch_random_game_header_from_steam() |
| 188 | + |
172 | 189 | # Label displaying "Games Found on Drives" (bottom right corner) |
173 | 190 | self.label_game_count = tk.Label(root, text=self.generate_games_found_text(), font=("Arial", 10)) |
174 | 191 | self.label_game_count.place(relx=1.0, rely=1.0, anchor='se') |
@@ -233,6 +250,8 @@ def __init__(self, root, installed_games, drives): |
233 | 250 | # Preload the images |
234 | 251 | self.preloaded_images = {} |
235 | 252 | self.preload_images() |
| 253 | + # Display a random header image on startup |
| 254 | + self.display_random_header_image() |
236 | 255 |
|
237 | 256 | # Use resource_path to get the correct logo path |
238 | 257 | logo_path = resource_path("SteamRouletteLogo.png") |
@@ -260,6 +279,8 @@ def __init__(self, root, installed_games, drives): |
260 | 279 |
|
261 | 280 | # Set initial theme (light mode) |
262 | 281 | self.set_light_mode() |
| 282 | + # Display a random header image on startup |
| 283 | + self.display_random_header_image() |
263 | 284 |
|
264 | 285 | def preload_images(self): |
265 | 286 | """Preload images into memory for smoother animation with parallel processing.""" |
@@ -300,6 +321,81 @@ def set_api_key(self): |
300 | 321 | self.api_key = api_key |
301 | 322 | messagebox.showinfo("API Key", "API Key saved successfully.") |
302 | 323 |
|
| 324 | + def load_header_images(self, folder_path): |
| 325 | + """Load all image files from the specified folder.""" |
| 326 | + image_files = [] |
| 327 | + for filename in os.listdir(folder_path): |
| 328 | + # Check if the file is a valid image (add more file extensions if needed) |
| 329 | + if filename.endswith(('.png', '.jpg', '.jpeg', '.gif')): |
| 330 | + image_files.append(os.path.join(folder_path, filename)) |
| 331 | + return image_files |
| 332 | + |
| 333 | + def fetch_random_game_header_from_steam(self): |
| 334 | + """Fetch a random game's header image from the Steam API.""" |
| 335 | + random_game_id = random.randint(1, 100000) # Example: random game ID between 1 and 100000 |
| 336 | + url = f"https://store.steampowered.com/api/appdetails?appids={random_game_id}" |
| 337 | + try: |
| 338 | + response = requests.get(url) |
| 339 | + data = response.json() |
| 340 | + |
| 341 | + if data.get(str(random_game_id)) and data[str(random_game_id)].get("data"): |
| 342 | + game_data = data[str(random_game_id)]["data"] |
| 343 | + if game_data.get("header_image"): |
| 344 | + header_image_url = game_data["header_image"] |
| 345 | + return [header_image_url] # Return the header image URL as a list for consistency |
| 346 | + else: |
| 347 | + print("No header image found for the game.") |
| 348 | + else: |
| 349 | + print("Failed to retrieve data for the game.") |
| 350 | + except Exception as e: |
| 351 | + print(f"Error fetching header from Steam API: {e}") |
| 352 | + return [] |
| 353 | + |
| 354 | + def display_random_header_image(self): |
| 355 | + """Display a random header image on the canvas.""" |
| 356 | + if self.header_images: |
| 357 | + # Select a random image from the list of header images |
| 358 | + random_image_path = random.choice(self.header_images) |
| 359 | + |
| 360 | + # If the image is a URL (Steam API), download and display it |
| 361 | + if random_image_path.startswith("http"): # Check if the path is a URL |
| 362 | + self.display_image_from_url(random_image_path) |
| 363 | + else: |
| 364 | + self.display_image_from_file(random_image_path) |
| 365 | + |
| 366 | + def display_image_from_file(self, image_path): |
| 367 | + """Display the image from a local file on the canvas.""" |
| 368 | + try: |
| 369 | + img = Image.open(image_path) |
| 370 | + img_resized = img.resize((600, 300), Image.Resampling.LANCZOS) # Resize image to fit the canvas |
| 371 | + img_tk = ImageTk.PhotoImage(img_resized) |
| 372 | + |
| 373 | + # Display the image on the canvas |
| 374 | + self.canvas.create_image(0, 0, anchor='nw', image=img_tk) |
| 375 | + |
| 376 | + # Keep a reference to the image to avoid garbage collection |
| 377 | + self.canvas.image = img_tk # Keep a reference so the image stays in memory |
| 378 | + |
| 379 | + except Exception as e: |
| 380 | + print(f"Error loading image from file {image_path}: {e}") |
| 381 | + |
| 382 | + def display_image_from_url(self, image_url): |
| 383 | + """Download and display the image from a URL on the canvas.""" |
| 384 | + try: |
| 385 | + img_data = requests.get(image_url).content |
| 386 | + img = Image.open(io.BytesIO(img_data)) |
| 387 | + img_resized = img.resize((600, 300), Image.Resampling.LANCZOS) # Resize image to fit the canvas |
| 388 | + img_tk = ImageTk.PhotoImage(img_resized) |
| 389 | + |
| 390 | + # Display the image on the canvas |
| 391 | + self.canvas.create_image(0, 0, anchor='nw', image=img_tk) |
| 392 | + |
| 393 | + # Keep a reference to the image to avoid garbage collection |
| 394 | + self.canvas.image = img_tk # Keep a reference so the image stays in memory |
| 395 | + |
| 396 | + except Exception as e: |
| 397 | + print(f"Error loading image from URL {image_url}: {e}") |
| 398 | + |
303 | 399 | def set_light_mode(self): |
304 | 400 | """Set the window to light mode.""" |
305 | 401 | self.update_theme(self.root, self.light_mode_bg, self.light_mode_fg) |
|
0 commit comments