mirror of
https://github.com/obskyr/khinsider.git
synced 2025-09-01 01:01:45 +02:00
43
khinsider.py
43
khinsider.py
@@ -3,6 +3,9 @@
|
|||||||
|
|
||||||
# A script to download full soundtracks from khinsider.
|
# A script to download full soundtracks from khinsider.
|
||||||
|
|
||||||
|
# __future__ import for forwards compatibility with Python 3
|
||||||
|
from __future__ import print_function
|
||||||
|
|
||||||
# --- Install prerequisites---
|
# --- Install prerequisites---
|
||||||
|
|
||||||
# (This section in `if __name__ == '__main__':` is entirely unrelated to the
|
# (This section in `if __name__ == '__main__':` is entirely unrelated to the
|
||||||
@@ -34,7 +37,7 @@ if __name__ == '__main__':
|
|||||||
def installModules(modules, verbose=True):
|
def installModules(modules, verbose=True):
|
||||||
for module in modules:
|
for module in modules:
|
||||||
if verbose:
|
if verbose:
|
||||||
print "Installing {}...".format(module[1])
|
print("Installing {}...".format(module[1]))
|
||||||
install(module[1])
|
install(module[1])
|
||||||
def installRequiredModules(needed=None, verbose=True):
|
def installRequiredModules(needed=None, verbose=True):
|
||||||
needed = neededInstalls() if needed is None else needed
|
needed = neededInstalls() if needed is None else needed
|
||||||
@@ -45,8 +48,8 @@ if __name__ == '__main__':
|
|||||||
try:
|
try:
|
||||||
import pip # To install modules if they're not there.
|
import pip # To install modules if they're not there.
|
||||||
except ImportError:
|
except ImportError:
|
||||||
print "You don't seem to have pip installed!"
|
print("You don't seem to have pip installed!")
|
||||||
print "Get it from https://pip.readthedocs.org/en/latest/installing.html"
|
print("Get it from https://pip.readthedocs.org/en/latest/installing.html")
|
||||||
|
|
||||||
installRequiredModules(needed)
|
installRequiredModules(needed)
|
||||||
|
|
||||||
@@ -75,7 +78,7 @@ class NonexistentSoundtrackError(Exception):
|
|||||||
self.ostName = ostName
|
self.ostName = ostName
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
if not self.ostName or len(self.ostName) > 80:
|
if not self.ostName or len(self.ostName) > 80:
|
||||||
s = u"The soundtrack does not exist."
|
s = "The soundtrack does not exist."
|
||||||
else:
|
else:
|
||||||
s = u"The soundtrack \"{ost}\" does not exist.".format(ost=self.ostName)
|
s = u"The soundtrack \"{ost}\" does not exist.".format(ost=self.ostName)
|
||||||
return s
|
return s
|
||||||
@@ -121,27 +124,27 @@ def getSongUrl(songPage):
|
|||||||
def download(ostName, path="", verbose=False):
|
def download(ostName, path="", 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 = getSongList(ostName)
|
songInfos = getSongList(ostName)
|
||||||
for name, url in songInfos:
|
for name, url in songInfos:
|
||||||
downloadSong(url, path, name, verbose=verbose)
|
downloadSong(url, path, name, verbose=verbose)
|
||||||
def downloadSong(songUrl, path, name="song", numTries=3, verbose=False):
|
def downloadSong(songUrl, path, name="song", numTries=3, verbose=False):
|
||||||
"""Download a single song at `songUrl` to `path`."""
|
"""Download a single song at `songUrl` to `path`."""
|
||||||
if verbose:
|
if verbose:
|
||||||
print u"Downloading {}...".format(name)
|
print(u"Downloading {}...".format(name))
|
||||||
|
|
||||||
tries = 0
|
tries = 0
|
||||||
while tries < numTries:
|
while tries < numTries:
|
||||||
try:
|
try:
|
||||||
if tries and verbose:
|
if tries and verbose:
|
||||||
print u"Couldn't download {}. Trying again...".format(name)
|
print(u"Couldn't download {}. Trying again...".format(name))
|
||||||
song = requests.get(songUrl)
|
song = requests.get(songUrl)
|
||||||
break
|
break
|
||||||
except requests.ConnectionError:
|
except requests.ConnectionError:
|
||||||
tries += 1
|
tries += 1
|
||||||
else:
|
else:
|
||||||
if verbose:
|
if verbose:
|
||||||
print u"Couldn't download {}. Skipping over.".format(name)
|
print(u"Couldn't download {}. Skipping over.".format(name))
|
||||||
return
|
return
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@@ -149,7 +152,7 @@ def downloadSong(songUrl, path, name="song", numTries=3, verbose=False):
|
|||||||
outfile.write(song.content)
|
outfile.write(song.content)
|
||||||
except IOError:
|
except IOError:
|
||||||
if verbose:
|
if verbose:
|
||||||
print u"Couldn't save {}. Check your permissions.".format(name)
|
print(u"Couldn't save {}. Check your permissions.".format(name))
|
||||||
|
|
||||||
def search(term):
|
def search(term):
|
||||||
"""Return a list of OST IDs for the search term `term`."""
|
"""Return a list of OST IDs for the search term `term`."""
|
||||||
@@ -157,7 +160,7 @@ def search(term):
|
|||||||
soup = BeautifulSoup(r.text)
|
soup = BeautifulSoup(r.text)
|
||||||
anchors = soup('p')[1]('a')
|
anchors = soup('p')[1]('a')
|
||||||
ostNames = [a['href'].split('/')[-1] for a in anchors]
|
ostNames = [a['href'].split('/')[-1] for a in anchors]
|
||||||
|
|
||||||
return ostNames
|
return ostNames
|
||||||
|
|
||||||
# --- And now for the execution. ---
|
# --- And now for the execution. ---
|
||||||
@@ -167,9 +170,9 @@ if __name__ == '__main__':
|
|||||||
try:
|
try:
|
||||||
ostName = sys.argv[1].decode(sys.getfilesystemencoding())
|
ostName = sys.argv[1].decode(sys.getfilesystemencoding())
|
||||||
except IndexError:
|
except IndexError:
|
||||||
print "No soundtrack specified! As the first parameter, use the name the soundtrack uses in its URL."
|
print("No soundtrack specified! As the first parameter, use the name the soundtrack uses in its URL.")
|
||||||
print "If you want to, you can also specify an output directory as the second parameter."
|
print("If you want to, you can also specify an output directory as the second parameter.")
|
||||||
print "You can also search for soundtracks by using your search term as parameter - as long as it's not an existing soundtrack."
|
print("You can also search for soundtracks by using your search term as parameter - as long as it's not an existing soundtrack.")
|
||||||
return
|
return
|
||||||
try:
|
try:
|
||||||
outPath = sys.argv[2]
|
outPath = sys.argv[2]
|
||||||
@@ -189,20 +192,20 @@ if __name__ == '__main__':
|
|||||||
for a in sys.argv[1:]
|
for a in sys.argv[1:]
|
||||||
]).replace('-', ' ')
|
]).replace('-', ' ')
|
||||||
)
|
)
|
||||||
print u"\nThe soundtrack \"{}\" does not seem to exist.".format(ostName)
|
print(u"\nThe soundtrack \"{}\" does not seem to exist.".format(ostName))
|
||||||
|
|
||||||
if searchResults: # aww yeah we gon' do some searchin'
|
if searchResults: # aww yeah we gon' do some searchin'
|
||||||
print
|
print()
|
||||||
print "These exist, though:"
|
print("These exist, though:")
|
||||||
for name in searchResults:
|
for name in searchResults:
|
||||||
print name
|
print(name)
|
||||||
|
|
||||||
if madeDir:
|
if madeDir:
|
||||||
os.rmdir(outPath)
|
os.rmdir(outPath)
|
||||||
return
|
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:
|
if madeDir:
|
||||||
os.rmdir(outPath)
|
os.rmdir(outPath)
|
||||||
|
Reference in New Issue
Block a user