Skip to content

Instantly share code, notes, and snippets.

@greencoder
Created September 16, 2020 15:49
Show Gist options
  • Save greencoder/600f176cdcb879fb29b90b8585518f3c to your computer and use it in GitHub Desktop.
Save greencoder/600f176cdcb879fb29b90b8585518f3c to your computer and use it in GitHub Desktop.
Rename all the RSN files from snesmusic.org with their titles and create a zip file
import glob
import pathlib
import os
import shutil
import unrar.rarfile
# Convert the title to a string that is safe to use as a filename
def make_filename(string):
keepcharacters = (' ','.','_')
string = string[2:]
return ''.join(c for c in string if c.isalnum() or c in keepcharacters).rstrip()
# Create our output directory for extracted files
output_dir = pathlib.Path('SNES')
output_dir.mkdir(exist_ok=True)
# Extract every rsn file
for filename in glob.glob('spcsets/*.rsn'):
rar = unrar.rarfile.RarFile(filename)
contents = rar.read('info.txt')
title = str(contents).replace('\\r\\n', '\n').split('\n')[0]
slug = make_filename(title)
game_output_dir = output_dir / slug
game_output_dir.mkdir(exist_ok=True)
rar.extractall(path=str(game_output_dir))
# Create a new output dir for our zip files
zip_output_dir = pathlib.Path('./SNES Zips')
zip_output_dir.mkdir(exist_ok=True)
# Zip every directory
for dirname in glob.glob('SNES/*'):
src_dir = os.path.basename(dirname)
output_filepath = zip_output_dir / pathlib.Path(src_dir)
shutil.make_archive(output_filepath, 'zip', dirname)
print(output_filepath)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment