mirror of
https://github.com/obskyr/khinsider.git
synced 2025-08-31 08:45:27 +02:00
Only create directory for soundtrack if soundtrack exists.
This commit is contained in:
23
khinsider.py
23
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.")
|
||||
|
||||
|
Reference in New Issue
Block a user