Skip to content

Instantly share code, notes, and snippets.

@raleighlittles
Last active October 8, 2022 08:14
Show Gist options
  • Save raleighlittles/5ff37350c82ed60483bf0dc1a171f10c to your computer and use it in GitHub Desktop.
Save raleighlittles/5ff37350c82ed60483bf0dc1a171f10c to your computer and use it in GitHub Desktop.
A script for auto-renaming subfolders based on Steam Game ID

About

This script iterates over subdirectories and if the subdirectory's name is a Steam Game ID, it renames that directory to the year the game was released and its full name -- see example below.

output example screenshot

This makes use of a semi-undocumented portion of the Steam API: https://wiki.teamfortress.com/wiki/User:RJackson/StorefrontAPI

Usage/Code

Paste the following snippet into a python script, then run it, passing the full directory name as the input argument (e.g. python3 main.py /my/directory)

import os
import requests
import time
import sys
import pdb


if __name__ == "__main__":

    if (len(sys.argv) != 2):
        print("ERROR: Must provide directory as argument")
        sys.exit(2)

    current_directory = sys.argv[1]
    
    # os.path.isdir doesn't work for mounted directories: https://stackoverflow.com/questions/39492524/os-path-isfile-returns-false-for-file-on-network-drive
    subdirectories = os.listdir(current_directory)

    for directory in subdirectories:
        if all([char.isnumeric() for char in directory]):
            # Candidate for steam ID...
            steam_api_resp = requests.get("https://store.steampowered.com/api/appdetails?appids={}".format(directory))
            if (steam_api_resp.ok):
                print("Directory {} is a valid Steam game ID".format(directory))
                game_data_obj = steam_api_resp.json()[directory]['data']
                game_name, release_date_raw = game_data_obj['name'], game_data_obj['release_date']['date'] if (game_data_obj['release_date']['coming_soon'] == False) else ""

                if (release_date_raw == ""):
                    print("Game \" {} \" hasn't been released yet. Are you sure this is the right Steam ID?")
                    sys.exit(1)

                # Release date format is: "Aug 13, 2022"
                game_year_released = release_date_raw.split(" ")[-1]
                print("Game \"{}\" was released in year {}".format(game_name, game_year_released))
                os.rename(os.path.join(current_directory, directory), os.path.join(current_directory, "({}) {}".format(game_year_released, game_name)))
                
            else:
                print("Unable to find Steam game with ID ", directory)
                sys.exit(3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment