From 8687d9138833d363aab2c9bbb0a7579f7cb80739 Mon Sep 17 00:00:00 2001 From: obskyr Date: Mon, 12 Oct 2015 16:03:46 +0200 Subject: [PATCH] Unicode literals + readme typo fix. --- khinsider.py | 19 ++++++++++--------- readme.md | 2 +- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/khinsider.py b/khinsider.py index b63baff..3b9e048 100644 --- a/khinsider.py +++ b/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"^\s*$", re.MULTILINE) + removeRe = re.compile(r"^\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() diff --git a/readme.md b/readme.md index 825f9bd..000f36f 100644 --- a/readme.md +++ b/readme.md @@ -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.