mirror of
https://github.com/obskyr/khinsider.git
synced 2025-09-02 01:22:32 +02:00
Handle song pages 404ing.
This happens with the soundtrack “shattered-horizon-2009-gamerip”. Fixes part of #59.
This commit is contained in:
26
khinsider.py
26
khinsider.py
@@ -196,6 +196,11 @@ def friendlyDownloadFile(file, path, index, total, verbose=False):
|
|||||||
str(index).zfill(len(str(total))),
|
str(index).zfill(len(str(total))),
|
||||||
str(total)
|
str(total)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if file is None and verbose:
|
||||||
|
print("Song {} is nonexistent (404: Not Found). Skipping over.".format(numberStr), file=sys.stderr)
|
||||||
|
return False
|
||||||
|
|
||||||
encoding = sys.getfilesystemencoding()
|
encoding = sys.getfilesystemencoding()
|
||||||
# Fun(?) fact: on Python 2, sys.getfilesystemencoding returns 'mbcs' even
|
# Fun(?) fact: on Python 2, sys.getfilesystemencoding returns 'mbcs' even
|
||||||
# on Windows NT (1993!) and later where filenames are natively Unicode.
|
# on Windows NT (1993!) and later where filenames are natively Unicode.
|
||||||
@@ -235,6 +240,9 @@ def friendlyDownloadFile(file, path, index, total, verbose=False):
|
|||||||
class KhinsiderError(Exception):
|
class KhinsiderError(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
class NonexistentSongError(KhinsiderError):
|
||||||
|
pass
|
||||||
|
|
||||||
class SoundtrackError(Exception):
|
class SoundtrackError(Exception):
|
||||||
def __init__(self, soundtrack):
|
def __init__(self, soundtrack):
|
||||||
self.soundtrack = soundtrack
|
self.soundtrack = soundtrack
|
||||||
@@ -262,7 +270,7 @@ class Soundtrack(object):
|
|||||||
Properties:
|
Properties:
|
||||||
* id: The soundtrack's unique ID, used at the end of its URL.
|
* id: The soundtrack's unique ID, used at the end of its URL.
|
||||||
* url: The full URL of the soundtrack.
|
* url: The full URL of the soundtrack.
|
||||||
* title: The textual title of the soundtrack.
|
* name: The textual title of the soundtrack.
|
||||||
* availableFormats: A list of the formats the soundtrack is available in.
|
* availableFormats: A list of the formats the soundtrack is available in.
|
||||||
* songs: A list of Song objects representing the songs in the soundtrack.
|
* songs: A list of Song objects representing the songs in the soundtrack.
|
||||||
* images: A list of File objects representing the images in the soundtrack.
|
* images: A list of File objects representing the images in the soundtrack.
|
||||||
@@ -289,7 +297,7 @@ class Soundtrack(object):
|
|||||||
return contentSoup
|
return contentSoup
|
||||||
|
|
||||||
@lazyProperty
|
@lazyProperty
|
||||||
def title(self):
|
def name(self):
|
||||||
return self._contentSoup.find('h2').get_text(strip=True)
|
return self._contentSoup.find('h2').get_text(strip=True)
|
||||||
|
|
||||||
@lazyProperty
|
@lazyProperty
|
||||||
@@ -341,7 +349,10 @@ class Soundtrack(object):
|
|||||||
print("Getting song list...")
|
print("Getting song list...")
|
||||||
files = []
|
files = []
|
||||||
for song in self.songs:
|
for song in self.songs:
|
||||||
files.append(getAppropriateFile(song, formatOrder))
|
try:
|
||||||
|
files.append(getAppropriateFile(song, formatOrder))
|
||||||
|
except NonexistentSongError:
|
||||||
|
files.append(None)
|
||||||
files.extend(self.images)
|
files.extend(self.images)
|
||||||
totalFiles = len(files)
|
totalFiles = len(files)
|
||||||
|
|
||||||
@@ -374,6 +385,9 @@ class Song(object):
|
|||||||
|
|
||||||
@lazyProperty
|
@lazyProperty
|
||||||
def _soup(self):
|
def _soup(self):
|
||||||
|
r = requests.get(self.url, timeout=10)
|
||||||
|
if r.url.rsplit('/', 1)[-1] == '404':
|
||||||
|
raise NonexistentSongError("Nonexistent song page (404).")
|
||||||
return getSoup(self.url)
|
return getSoup(self.url)
|
||||||
|
|
||||||
@lazyProperty
|
@lazyProperty
|
||||||
@@ -429,8 +443,8 @@ def download(soundtrackId, path='', makeDirs=True, formatOrder=None, verbose=Fal
|
|||||||
See Soundtrack.download for more information.
|
See Soundtrack.download for more information.
|
||||||
"""
|
"""
|
||||||
soundtrack = Soundtrack(soundtrackId)
|
soundtrack = Soundtrack(soundtrackId)
|
||||||
soundtrack.title # To conistently always load the content in advance.
|
soundtrack.name # To conistently always load the content in advance.
|
||||||
path = to_valid_filename(soundtrack.title) if path is None else path
|
path = to_valid_filename(soundtrack.name) if path is None else path
|
||||||
if verbose:
|
if verbose:
|
||||||
unicodePrint("Downloading to \"{}\".".format(path))
|
unicodePrint("Downloading to \"{}\".".format(path))
|
||||||
return soundtrack.download(path, makeDirs, formatOrder, verbose)
|
return soundtrack.download(path, makeDirs, formatOrder, verbose)
|
||||||
@@ -467,7 +481,7 @@ def printSearchResults(searchResults, file=sys.stdout):
|
|||||||
padLen = max(len(x.id) for x in searchResults)
|
padLen = max(len(x.id) for x in searchResults)
|
||||||
s = ""
|
s = ""
|
||||||
for soundtrack in searchResults:
|
for soundtrack in searchResults:
|
||||||
s += "{} {}. {}\n".format(soundtrack.id, '.' * (padLen - len(soundtrack.id)), soundtrack.title)
|
s += "{} {}. {}\n".format(soundtrack.id, '.' * (padLen - len(soundtrack.id)), soundtrack.name)
|
||||||
unicodePrint(s, end="", file=file)
|
unicodePrint(s, end="", file=file)
|
||||||
|
|
||||||
# --- And now for the execution. ---
|
# --- And now for the execution. ---
|
||||||
|
Reference in New Issue
Block a user