1
0
mirror of https://github.com/obskyr/khinsider.git synced 2025-08-12 07:54:09 +02:00

Fixed Python 3 compatibility.

Bytestrings where needed.
This commit is contained in:
obskyr
2015-08-16 15:58:48 +02:00
parent 293d5feeae
commit 4825e0b83a

View File

@@ -67,10 +67,10 @@ def getSoup(url):
r = requests.get(url) r = requests.get(url)
# --- 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, '', r.content)) return BeautifulSoup(re.sub(removeRe, b'', r.content))
class NonexistentSoundtrackError(Exception): class NonexistentSoundtrackError(Exception):
def __init__(self, ostName=""): def __init__(self, ostName=""):
@@ -169,6 +169,8 @@ if __name__ == '__main__':
def doIt(): # Only in a function to be able to stop after errors, really. def doIt(): # Only in a function to be able to stop after errors, really.
try: try:
ostName = sys.argv[1].decode(sys.getfilesystemencoding()) ostName = sys.argv[1].decode(sys.getfilesystemencoding())
except AttributeError: # Python 3 doesn't have str.encode.
ostName = sys.argv[1]
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.")
@@ -187,11 +189,14 @@ if __name__ == '__main__':
try: try:
download(ostName, outPath, verbose=True) download(ostName, outPath, verbose=True)
except NonexistentSoundtrackError: except NonexistentSoundtrackError:
searchResults = search( try:
' '.join([a.decode(sys.getfilesystemencoding()) searchTerm = ' '.join([a.decode(sys.getfilesystemencoding())
for a in sys.argv[1:] for a in sys.argv[1:]
]).replace('-', ' ') ]).replace('-', ' ')
) except AttributeError: # Python 3, again
searchTerm = ' '.join(sys.argv[1:]).replace('-', ' ')
searchResults = search(searchTerm)
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'