Skip to content

Instantly share code, notes, and snippets.

@Wamy-Dev
Last active January 23, 2023 10:12
Show Gist options
  • Save Wamy-Dev/67433289ee0080136a2e75fd3540e842 to your computer and use it in GitHub Desktop.
Save Wamy-Dev/67433289ee0080136a2e75fd3540e842 to your computer and use it in GitHub Desktop.
Autorenaming Switch Games
import os
import shutil
import subprocess
from subprocess import PIPE, run
import requests
import json
cwd = os.getcwd()
print(cwd)
def unrar():
for root, collectionfolders, files in os.walk(os.path.join(cwd, 'games')):
for gamefolder in collectionfolders:
for file in os.listdir(os.path.join(root, gamefolder)):
if file.endswith('.part1.rar') or file.endswith('.rar'):
cmd = ['unrar', 'e', os.path.join(root, gamefolder, file), os.path.join(cwd, 'games')]
print(cmd)
print(f"unraring file: {file}")
subprocess.call(cmd)
print(f"unrared file: {file}")
shutil.rmtree(os.path.join(root, gamefolder))
break
def getDatabase():
url = "https://raw.githubusercontent.com/blawar/titledb/master/US.en.json"
r = requests.get(url)
with open('usdb.json', 'wb') as f:
f.write(r.content)
return True
def rename():
for gamefile in os.listdir(os.path.join(cwd, 'games')):
if gamefile in open('renameddb.txt').read().splitlines():
print(f'{gamefile} already renamed')
continue
else:
if gamefile.endswith('.nsp'):
ending = ".nsp"
elif gamefile.endswith('.xci'):
ending = ".xci"
cmd = ['mono', 'nxgameinfo_cli.exe', '-d', './', '-k', './', os.path.join(cwd, 'games', gamefile)]
result = run(cmd, stdout=PIPE, stderr=PIPE, universal_newlines=True)
resultarray = result.stdout.splitlines()
for line in resultarray:
if line.startswith("├ Title ID:"):
titleid = line.split(":")[1].strip()
if len(titleid) == 0:
continue
if line.startswith("├ Title Name:"):
gamename = line.split(":")[1].strip()
if len(gamename) == 0:
with open('usdb.json') as f:
data = json.load(f)
for game in data.items():
if game[1]["id"] == titleid:
gamename = game[1]["name"]
if len(gamename) == 0:
continue
if line.startswith("├ Version:"):
version = line.split(":")[1].strip()
if line.startswith("├ Type:"):
type = line.split(":")[1].strip()
if type == "Update":
type = "UPD"
elif type == "Base":
type = "BASE"
newname = f"{gamename} [{type}][{titleid}][v{version}]{ending}"
os.rename(os.path.join(cwd, 'games', gamefile), os.path.join(cwd, 'games', newname))
f = open("renameddb.txt", "a")
f.write(f"{newname}: {gamefile}\n")
f.close()
print(f"---- RENAMED {gamefile} TO {newname} ----\n")
def main():
unrar()
getDatabase()
rename()
main()

This is a python script which autorenames switch games simply and quickly. It can get a lot of data from the .xci or .nsp file that you choose.

Right now it has not been adapted to run on its own, I might update that in the future, it is mainly for you to adapt the script for yourself based on the logic that you need.

Setup

  1. I use a linux machine daily so these instructions will be for linux, but for windows or mac it is nearly the same. The first step is to install python and its dependencies if you haven't already. This includes running pip install requests.
  2. Download and follow the instructions for installing https://github.com/garoxas/NX_Game_Info to your machine. You will want the CLI package from the releases which you can find here -> https://github.com/garoxas/NX_Game_Info/releases/tag/v0.7.1. The instructions for each os can be found here -> https://github.com/garoxas/NX_Game_Info#command-line-interface-windows-macos-linux
  3. Put the files from the release next to the script. You will also need to get a prod.keys file and put them next to the script as well. title.keys is optional. You can follow this guide to get them or find them yourself -> https://www.youtube.com/watch?v=E0E-YUceJRk
  4. Now your files should look like this:

image

  1. Now you will have to go into the code and change things in the way you want. I will outline the items you will likely have to change.
  • On line 11, you will have to change games to the directory that all of your gamefolders are in. This is optional if you dont need to unrar files.
  • On line 15 you will have to do the same for games. Again, not necessary if you don't need to unrar. If your files are simply just there, comment out line 71.
  • On line 29 you will have to change games to the directory where all your .nsp and .xci files are located.
  • On line 38 you will have to follow the install instructions for running NX_Game_Info cli on your machine. The command will change, but you can simply copy paste how you would run it in your terminal into the script.
  • On line 65 you will have to change the games folder to the foldername your files are in.
  1. MAKE SURE THE SCRIPT is next to your game folder where all of your games are, then simply run the script.

Again, this script is under development and isn't perfect but it works really well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment