import requests import csv from datetime import datetime import os from dotenv import load_dotenv # Load environment variables from .env file load_dotenv() def get_steam_id_from_vanity_url(api_key, vanity_url): """Convert a Steam vanity URL username to a Steam ID""" url = f"https://api.steampowered.com/ISteamUser/ResolveVanityURL/v0001/?key={api_key}&vanityurl={vanity_url}" response = requests.get(url) data = response.json() if data['response']['success'] == 1: return data['response']['steamid'] return None def get_owned_games(api_key, steam_id): """Get all owned games with playtime""" url = f"https://api.steampowered.com/IPlayerService/GetOwnedGames/v0001/?key={api_key}&steamid={steam_id}&format=json&include_appinfo=1&include_played_free_games=1" response = requests.get(url) return response.json() def get_player_summaries(api_key, steam_id): """Get player information including profile creation date""" url = f"https://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key={api_key}&steamids={steam_id}" response = requests.get(url) return response.json() def filter_games_played_in_2024(games_data): """Filter games played in 2024 based on playtime""" current_year = datetime.now().year # We can only get games that have been played at all # There's no direct way to know exactly when the playtime occurred beyond the last 2 weeks games_played = [] if 'response' in games_data and 'games' in games_data['response']: for game in games_data['response']['games']: last_played = game.get("rtime_last_played", 0) if last_played == 0: # Skip games that have never been played continue parsed_time = datetime.fromtimestamp(last_played) # Games played in the last 2 weeks (definitely played in 2024 if current year is 2024) if parsed_time.year == 2024: games_played.append({ 'appid': game['appid'], 'name': game['name'], 'total_playtime_minutes': game['playtime_forever'], 'img_url': f"https://cdn.akamai.steamstatic.com/steam/apps/{game['appid']}/header.jpg" }) # Sort by playtime (descending) games_played.sort(key=lambda x: x['total_playtime_minutes'], reverse=True) return games_played def export_to_csv(games, filename='steam_games_2024.csv'): """Export games data to CSV""" with open(filename, 'w', newline='', encoding='utf-8') as csvfile: fieldnames = ['Game Name','Image URL', 'Total Playtime (Hours)'] writer = csv.DictWriter(csvfile, fieldnames=fieldnames) writer.writeheader() for game in games: writer.writerow({ 'Game Name': game['name'], 'Image URL': game['img_url'], 'Total Playtime (Hours)': round(game['total_playtime_minutes'] / 60, 2), }) print(f"CSV file created: {filename}") def main(): api_key = os.getenv("STEAM_API_KEY") if not api_key: print("Please set your Steam API key in the .env file") return steam_identifier = os.getenv("STEAM_ID") if not steam_identifier: print("Please set your Steam ID in the .env file") return # Determine if the input is a Steam ID or vanity URL if steam_identifier.isdigit(): steam_id = steam_identifier else: steam_id = get_steam_id_from_vanity_url(api_key, steam_identifier) if not steam_id: print("Could not resolve vanity URL. Please use your numeric Steam ID.") return print("Fetching game data from Steam...") owned_games = get_owned_games(api_key, steam_id) print("Filtering games played in 2024...") games_2024 = filter_games_played_in_2024(owned_games) if games_2024: export_to_csv(games_2024) print(f"Found {len(games_2024)} games played in 2024") else: print("No games found for 2024") if __name__ == "__main__": main()