mirror of
https://github.com/obskyr/khinsider.git
synced 2025-08-31 00:39:51 +02:00
Unicode literals + readme typo fix.
This commit is contained in:
19
khinsider.py
19
khinsider.py
@@ -5,6 +5,7 @@
|
||||
|
||||
# __future__ import for forwards compatibility with Python 3
|
||||
from __future__ import print_function
|
||||
from __future__ import unicode_literals
|
||||
|
||||
# --- Install prerequisites---
|
||||
|
||||
@@ -67,7 +68,7 @@ def getSoup(*args, **kwargs):
|
||||
r = requests.get(*args, **kwargs)
|
||||
|
||||
# --- Fix errors in khinsider's HTML
|
||||
removeRe = re.compile(br"^</td>\s*$", re.MULTILINE)
|
||||
removeRe = re.compile(r"^</td>\s*$", re.MULTILINE)
|
||||
# ---
|
||||
|
||||
return BeautifulSoup(re.sub(removeRe, b'', r.content), 'html.parser')
|
||||
@@ -80,11 +81,11 @@ class NonexistentSoundtrackError(Exception):
|
||||
if not self.ostName or len(self.ostName) > 80:
|
||||
s = "The soundtrack does not exist."
|
||||
else:
|
||||
s = u"The soundtrack \"{ost}\" does not exist.".format(ost=self.ostName)
|
||||
s = "The soundtrack \"{ost}\" does not exist.".format(ost=self.ostName)
|
||||
return s
|
||||
|
||||
def getOstSoup(ostName):
|
||||
url = u"http://downloads.khinsider.com/game-soundtracks/album/" + ostName
|
||||
url = "http://downloads.khinsider.com/game-soundtracks/album/" + ostName
|
||||
soup = getSoup(url)
|
||||
if soup.find(id='EchoTopic').find('p').string == "No such album":
|
||||
# The EchoTopic and p exist even if the soundtrack doesn't, so no
|
||||
@@ -131,20 +132,20 @@ def download(ostName, path="", verbose=False):
|
||||
def downloadSong(songUrl, path, name="song", numTries=3, verbose=False):
|
||||
"""Download a single song at `songUrl` to `path`."""
|
||||
if verbose:
|
||||
print(u"Downloading {}...".format(name))
|
||||
print("Downloading {}...".format(name))
|
||||
|
||||
tries = 0
|
||||
while tries < numTries:
|
||||
try:
|
||||
if tries and verbose:
|
||||
print(u"Couldn't download {}. Trying again...".format(name))
|
||||
print("Couldn't download {}. Trying again...".format(name))
|
||||
song = requests.get(songUrl)
|
||||
break
|
||||
except requests.ConnectionError:
|
||||
tries += 1
|
||||
else:
|
||||
if verbose:
|
||||
print(u"Couldn't download {}. Skipping over.".format(name))
|
||||
print("Couldn't download {}. Skipping over.".format(name))
|
||||
return
|
||||
|
||||
try:
|
||||
@@ -152,7 +153,7 @@ def downloadSong(songUrl, path, name="song", numTries=3, verbose=False):
|
||||
outfile.write(song.content)
|
||||
except IOError:
|
||||
if verbose:
|
||||
print(u"Couldn't save {}. Check your permissions.".format(name))
|
||||
print("Couldn't save {}. Check your permissions.".format(name))
|
||||
|
||||
def search(term):
|
||||
"""Return a list of OST IDs for the search term `term`."""
|
||||
@@ -168,7 +169,7 @@ if __name__ == '__main__':
|
||||
def doIt(): # Only in a function to be able to stop after errors, really.
|
||||
try:
|
||||
ostName = sys.argv[1].decode(sys.getfilesystemencoding())
|
||||
except AttributeError: # Python 3 doesn't have str.encode.
|
||||
except AttributeError: # Python 3's argv is in Unicode
|
||||
ostName = sys.argv[1]
|
||||
except IndexError:
|
||||
print("No soundtrack specified! As the first parameter, use the name the soundtrack uses in its URL.")
|
||||
@@ -196,7 +197,7 @@ if __name__ == '__main__':
|
||||
searchTerm = ' '.join(sys.argv[1:]).replace('-', ' ')
|
||||
|
||||
searchResults = search(searchTerm)
|
||||
print(u"\nThe soundtrack \"{}\" does not seem to exist.".format(ostName))
|
||||
print("\nThe soundtrack \"{}\" does not seem to exist.".format(ostName))
|
||||
|
||||
if searchResults: # aww yeah we gon' do some searchin'
|
||||
print()
|
||||
|
@@ -20,7 +20,7 @@ Just run `khinsider.py` from the command line with the sole parameter being the
|
||||
|
||||
If you want, you can also add another parameter as the output folder, but that's optional.
|
||||
|
||||
If you don't want to go to the actual site to look for soundtracks, you can also just type a search term as the first parameter(s), and provided it's not a valid soundtrack, `khinsider.py` ill give you a list of soundtracks matching that term.
|
||||
If you don't want to go to the actual site to look for soundtracks, you can also just type a search term as the first parameter(s), and provided it's not a valid soundtrack, `khinsider.py` will give you a list of soundtracks matching that term.
|
||||
|
||||
You're going to need [Python](https://www.python.org/downloads/) (2 or 3 - `khinsider.py` works with both), so install that (and [add it to your path](http://superuser.com/a/143121)) if you haven't already.
|
||||
|
||||
|
Reference in New Issue
Block a user