From 479ef67fb903e1b5e6950362a0518af8a77abb0c Mon Sep 17 00:00:00 2001 From: obskyr Date: Fri, 7 Jul 2017 18:55:39 +0200 Subject: [PATCH] Only create directory for soundtrack if soundtrack exists. --- khinsider.py | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/khinsider.py b/khinsider.py index bbb3ce0..e1f3434 100644 --- a/khinsider.py +++ b/khinsider.py @@ -65,7 +65,7 @@ import os import re # For the syntax error in the HTML. # Different printin' for different Pythons. -normal_print = print +normalPrint = print def print(*args, **kwargs): encoding = sys.stdout.encoding or 'utf-8' if sys.version_info[0] > 2: # Python 3 can't print bytes properly (!?) @@ -82,7 +82,7 @@ def print(*args, **kwargs): if isinstance(arg, unicodeType) else arg for arg in args ] - normal_print(*args, **kwargs) + normalPrint(*args, **kwargs) def getSoup(*args, **kwargs): r = requests.get(*args, **kwargs) @@ -157,12 +157,16 @@ def getSongUrl(songPage): url = songPage('p')[3].find('a')['href'] # Download link. return url -def download(ostName, path="", verbose=False): +def download(ostName, path=".", makeDirs=True, verbose=False): """Download an OST with the ID `ostName` to `path`.""" if verbose: print("Getting song list...") songInfos = getFileList(ostName) totalSongs = len(songInfos) + + if makeDirs and not os.path.isdir(path): + os.makedirs(os.path.abspath(os.path.realpath(path))) + for songNumber, (name, url) in enumerate(songInfos): if not os.path.isfile(os.path.join(path, name)): downloadSong(url, path, name, verbose=verbose, @@ -234,11 +238,6 @@ if __name__ == '__main__': except IndexError: outPath = ostName - madeDir = False - if not os.path.isdir(outPath): - os.mkdir(outPath) - madeDir = True - try: download(ostName, outPath, verbose=True) except NonexistentSoundtrackError: @@ -258,17 +257,9 @@ if __name__ == '__main__': print("These exist, though:") for name in searchResults: print(name) - - if madeDir: - os.rmdir(outPath) - return except requests.ConnectionError: print("Could not connect to KHInsider.") print("Make sure you have a working internet connection.") - - if madeDir: - os.rmdir(outPath) - return except KeyboardInterrupt: print("Stopped download.")