1
0
mirror of https://github.com/obskyr/khinsider.git synced 2025-08-30 08:20:24 +02:00

Unicode printing in both Python 2 and 3.

This commit is contained in:
obskyr
2015-10-18 21:41:07 +02:00
parent af88ec102e
commit 5dc3ed7d4e

View File

@@ -64,11 +64,31 @@ 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.
normal_print = print
def print(*args, **kwargs):
if sys.version_info[0] > 2: # Python 3 can't print bytes properly (!?)
# This lambda is ACTUALLY a "reasonable"
# way to print Unicode in Python 3. What.
printEncode = lambda s: s.encode(sys.stdout.encoding,
'replace').decode(sys.stdout.encoding)
unicodeType = str
else:
printEncode = lambda s: s.encode(sys.stdout.encoding, 'replace')
unicodeType = unicode
args = [
printEncode(arg)
if isinstance(arg, unicodeType) else arg
for arg in args
]
normal_print(*args, **kwargs)
def getSoup(*args, **kwargs): def getSoup(*args, **kwargs):
r = requests.get(*args, **kwargs) r = requests.get(*args, **kwargs)
# --- Fix errors in khinsider's HTML # --- Fix errors in khinsider's HTML
removeRe = re.compile(r"^</td>\s*$", re.MULTILINE) removeRe = re.compile(br"^</td>\s*$", re.MULTILINE)
# --- # ---
return BeautifulSoup(re.sub(removeRe, b'', r.content), 'html.parser') return BeautifulSoup(re.sub(removeRe, b'', r.content), 'html.parser')
@@ -132,23 +152,20 @@ def download(ostName, path="", verbose=False):
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("Downloading {}...".format(name).encode( print("Downloading {}...".format(name))
sys.stdout.encoding, 'replace'))
tries = 0 tries = 0
while tries < numTries: while tries < numTries:
try: try:
if tries and verbose: if tries and verbose:
print("Couldn't download {}. Trying again...".format( print("Couldn't download {}. Trying again...".format(name))
name).encode(sys.stdout.encoding, 'replace'))
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("Couldn't download {}. Skipping over.".format( print("Couldn't download {}. Skipping over.".format(name))
name).encode(sys.stdout.encoding, 'replace'))
return return
try: try:
@@ -156,8 +173,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("Couldn't save {}. Check your permissions.".format( print("Couldn't save {}. Check your permissions.".format(name))
name).encode(sys.stdout.encoding, 'replace'))
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`."""
@@ -204,7 +220,7 @@ if __name__ == '__main__':
# I don't know, maybe in some crazy circumstance the encoding for # I don't know, maybe in some crazy circumstance the encoding for
# arguments doesn't match stdout's encoding. # arguments doesn't match stdout's encoding.
print("\nThe soundtrack \"{}\" does not seem to exist.".format( print("\nThe soundtrack \"{}\" does not seem to exist.".format(
ostName).encode(sys.stdout.encoding, 'replace')) ostName))
if searchResults: # aww yeah we gon' do some searchin' if searchResults: # aww yeah we gon' do some searchin'
print() print()