Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
What I used to manage my master tracks.
from os import makedirs, getcwd, walk, path
from zipfile import ZipFile, ZIP_DEFLATED
def create_archive(filename, root, files):
zipfile = path.join(getcwd(), 'Archives')
if not path.exists(zipfile):
makedirs(zipfile)
zipfile = path.join(zipfile, filename)
if not path.isfile(zipfile):
with ZipFile(zipfile, 'w', ZIP_DEFLATED, True, 4) as zf:
for file in files:
fdir = path.join(root, file)
if path.isfile(fdir):
f = path.split(fdir)
song = path.split(f[0])
artist = path.split(song[0])
zf.write(fdir, path.join(artist[1], song[1], f[1]))
if __name__ == '__main__':
audio = path.join(getcwd(), 'Audio')
if path.exists(audio):
for root, dirs, files in walk(audio):
song = path.split(root)
artist = path.split(song[0])
if artist[1] not in ['Audio', 'Python']:
filename = f'{artist[1]} - {song[1]}.zip'
create_archive(filename, root, files)
@echo off
pushd %~dp0
py %~dp0separator.py
py %~dp0archiver.py
popd
exit
from os import makedirs, getcwd, unlink, walk, path
from shutil import copy
INIFILE = 'song.ini'
BLACKLIST = ['notes.mid', 'label.png', 'ps.dat', 'crowd.ogg']
def safe_name(filename):
output = []
for char in filename:
if char not in ['\\', '/', ':', '*', '?', '"', '<', '>', '|', '.']:
output.append(char)
else:
output.append(' ')
return ''.join(output).strip()
def read_ini(inifile):
output = {
'artist': 'Unknown',
'songname': 'Unknown'
}
with open(inifile, 'r') as ini:
lines = ini.readlines()
for line in lines:
line = line.rstrip()
try:
if line.startswith('name'):
output['songname'] = safe_name(line.split(' = ')[1])
if line.startswith('artist'):
output['artist'] = safe_name(line.split(' = ')[1])
except:
''
return output
def fcopy(inidata, root, files):
artistpath = path.join(getcwd(), 'Audio', inidata['artist'])
if not path.exists(artistpath):
makedirs(artistpath)
songpath = path.join(artistpath, inidata['songname'])
if not path.exists(songpath):
makedirs(songpath)
for file in files:
fdir = path.join(root, file)
fout = path.join(songpath, file)
if not path.isfile(fout) and path.isfile(fdir):
copy(fdir, fout)
def main(basedir):
basedir = path.realpath(basedir)
if path.exists(basedir):
for root, dirs, files in walk(basedir, topdown=False):
for file in files:
fdir = path.join(root, file)
if file == INIFILE:
data = read_ini(fdir)
fcopy(data, root, files)
elif file in BLACKLIST:
unlink(fdir)
if __name__ == '__main__':
main('D:\\Games\\PhaseShift')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.