mirror of
https://github.com/obskyr/khinsider.git
synced 2025-09-02 09:32:34 +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.
|
import re # For the syntax error in the HTML.
|
||||||
|
|
||||||
# Different printin' for different Pythons.
|
# Different printin' for different Pythons.
|
||||||
normal_print = print
|
normalPrint = print
|
||||||
def print(*args, **kwargs):
|
def print(*args, **kwargs):
|
||||||
encoding = sys.stdout.encoding or 'utf-8'
|
encoding = sys.stdout.encoding or 'utf-8'
|
||||||
if sys.version_info[0] > 2: # Python 3 can't print bytes properly (!?)
|
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
|
if isinstance(arg, unicodeType) else arg
|
||||||
for arg in args
|
for arg in args
|
||||||
]
|
]
|
||||||
normal_print(*args, **kwargs)
|
normalPrint(*args, **kwargs)
|
||||||
|
|
||||||
def getSoup(*args, **kwargs):
|
def getSoup(*args, **kwargs):
|
||||||
r = requests.get(*args, **kwargs)
|
r = requests.get(*args, **kwargs)
|
||||||
@@ -157,12 +157,16 @@ def getSongUrl(songPage):
|
|||||||
url = songPage('p')[3].find('a')['href'] # Download link.
|
url = songPage('p')[3].find('a')['href'] # Download link.
|
||||||
return url
|
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`."""
|
"""Download an OST with the ID `ostName` to `path`."""
|
||||||
if verbose:
|
if verbose:
|
||||||
print("Getting song list...")
|
print("Getting song list...")
|
||||||
songInfos = getFileList(ostName)
|
songInfos = getFileList(ostName)
|
||||||
totalSongs = len(songInfos)
|
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):
|
for songNumber, (name, url) in enumerate(songInfos):
|
||||||
if not os.path.isfile(os.path.join(path, name)):
|
if not os.path.isfile(os.path.join(path, name)):
|
||||||
downloadSong(url, path, name, verbose=verbose,
|
downloadSong(url, path, name, verbose=verbose,
|
||||||
@@ -234,11 +238,6 @@ if __name__ == '__main__':
|
|||||||
except IndexError:
|
except IndexError:
|
||||||
outPath = ostName
|
outPath = ostName
|
||||||
|
|
||||||
madeDir = False
|
|
||||||
if not os.path.isdir(outPath):
|
|
||||||
os.mkdir(outPath)
|
|
||||||
madeDir = True
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
download(ostName, outPath, verbose=True)
|
download(ostName, outPath, verbose=True)
|
||||||
except NonexistentSoundtrackError:
|
except NonexistentSoundtrackError:
|
||||||
@@ -258,17 +257,9 @@ if __name__ == '__main__':
|
|||||||
print("These exist, though:")
|
print("These exist, though:")
|
||||||
for name in searchResults:
|
for name in searchResults:
|
||||||
print(name)
|
print(name)
|
||||||
|
|
||||||
if madeDir:
|
|
||||||
os.rmdir(outPath)
|
|
||||||
return
|
|
||||||
except requests.ConnectionError:
|
except requests.ConnectionError:
|
||||||
print("Could not connect to KHInsider.")
|
print("Could not connect to KHInsider.")
|
||||||
print("Make sure you have a working internet connection.")
|
print("Make sure you have a working internet connection.")
|
||||||
|
|
||||||
if madeDir:
|
|
||||||
os.rmdir(outPath)
|
|
||||||
return
|
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
print("Stopped download.")
|
print("Stopped download.")
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user