Fix handling video info error

This commit is contained in:
taizan-hokuto
2020-09-11 00:18:09 +09:00
parent d3f1643a40
commit 8fee67c2d4
2 changed files with 24 additions and 13 deletions

View File

@@ -2,6 +2,7 @@ import argparse
import os import os
import signal import signal
import time
from json.decoder import JSONDecodeError from json.decoder import JSONDecodeError
from pathlib import Path from pathlib import Path
from .arguments import Arguments from .arguments import Arguments
@@ -55,7 +56,21 @@ def main():
path = Path(Arguments().output + video_id + '.html') path = Path(Arguments().output + video_id + '.html')
else: else:
raise FileNotFoundError raise FileNotFoundError
info = VideoInfo(video_id) err = None
for _ in range(3): # retry 3 times
try:
info = VideoInfo(video_id)
break
except (PatternUnmatchError, JSONDecodeError, InvalidVideoIdException) as e:
err = e
time.sleep(2)
continue
else:
print("Cannot parse video information.:{}".format(video_id))
if Arguments().save_error_data:
util.save(err.doc, "ERR", ".dat")
continue
if len(Arguments().video_ids) > 1: if len(Arguments().video_ids) > 1:
print(f"\n{'-' * 10} video:{counter + 1} of {len(Arguments().video_ids)} {'-' * 10}") print(f"\n{'-' * 10} video:{counter + 1} of {len(Arguments().video_ids)} {'-' * 10}")
print(f"\n" print(f"\n"
@@ -86,8 +101,6 @@ def main():
print() print()
if pbar.is_cancelled(): if pbar.is_cancelled():
print("\nThe extraction process has been discontinued.\n") print("\nThe extraction process has been discontinued.\n")
except InvalidVideoIdException: except InvalidVideoIdException:
print("Invalid Video ID or URL:", video_id) print("Invalid Video ID or URL:", video_id)
except NoContents as e: except NoContents as e:
@@ -96,14 +109,9 @@ def main():
print("The specified directory does not exist.:{}".format(Arguments().output)) print("The specified directory does not exist.:{}".format(Arguments().output))
except JSONDecodeError as e: except JSONDecodeError as e:
print(e.msg) print(e.msg)
print("Cannot parse video information.:{}".format(video_id)) print("JSONDecodeError.:{}".format(video_id))
if Arguments().save_error_data: if Arguments().save_error_data:
util.save(e.doc, "ERR_JSON_DECODE", ".dat") util.save(e.doc, "ERR_JSON_DECODE", ".dat")
except PatternUnmatchError as e:
print(e.msg)
print("Cannot parse video information.:{}".format(video_id))
if Arguments().save_error_data:
util.save(e.doc, "ERR_PATTERN_UNMATCH", ".dat")
return return

View File

@@ -38,7 +38,10 @@ class InvalidVideoIdException(Exception):
''' '''
Thrown when the video_id is not exist (VideoInfo). Thrown when the video_id is not exist (VideoInfo).
''' '''
pass def __init__(self, doc):
self.msg = "InvalidVideoIdException"
self.doc = doc
class UnknownConnectionError(Exception): class UnknownConnectionError(Exception):
@@ -47,7 +50,7 @@ class UnknownConnectionError(Exception):
class RetryExceedMaxCount(Exception): class RetryExceedMaxCount(Exception):
''' '''
thrown when the number of retries exceeds the maximum value. Thrown when the number of retries exceeds the maximum value.
''' '''
pass pass
@@ -66,13 +69,13 @@ class FailedExtractContinuation(ChatDataFinished):
class VideoInfoParseError(Exception): class VideoInfoParseError(Exception):
''' '''
thrown when failed to parse video info Base exception when parsing video info.
''' '''
class PatternUnmatchError(VideoInfoParseError): class PatternUnmatchError(VideoInfoParseError):
''' '''
thrown when failed to parse video info with unmatched pattern Thrown when failed to parse video info with unmatched pattern.
''' '''
def __init__(self, doc): def __init__(self, doc):
self.msg = "PatternUnmatchError" self.msg = "PatternUnmatchError"