use stderr and hint in case of parser returning no urls instead of bare exception

This commit is contained in:
Nick Sweeting 2021-03-31 01:39:01 -04:00
parent 05da16db99
commit 5d3a03b299

View file

@ -20,6 +20,8 @@ from ..config import (
OUTPUT_DIR, OUTPUT_DIR,
SOURCES_DIR_NAME, SOURCES_DIR_NAME,
TIMEOUT, TIMEOUT,
stderr,
hint,
) )
from ..util import ( from ..util import (
basename, basename,
@ -112,7 +114,10 @@ def run_parser_functions(to_parse: IO[str], timer, root_url: Optional[str]=None,
parser_name, parser_func = PARSERS[parser] parser_name, parser_func = PARSERS[parser]
parsed_links = list(parser_func(to_parse, root_url=root_url)) parsed_links = list(parser_func(to_parse, root_url=root_url))
if not parsed_links: if not parsed_links:
raise Exception('no links found') stderr()
stderr(f'[X] No links found using {parser_name} parser', color='red')
hint('Try a different parser or double check the input?')
stderr()
timer.end() timer.end()
return parsed_links, parser_name return parsed_links, parser_name
@ -121,7 +126,7 @@ def run_parser_functions(to_parse: IO[str], timer, root_url: Optional[str]=None,
try: try:
parsed_links = list(parser_func(to_parse, root_url=root_url)) parsed_links = list(parser_func(to_parse, root_url=root_url))
if not parsed_links: if not parsed_links:
raise Exception('no links found') raise Exception(f'No links found using {parser_name} parser')
# print(f'[√] Parser {parser_name} succeeded: {len(parsed_links)} links parsed') # print(f'[√] Parser {parser_name} succeeded: {len(parsed_links)} links parsed')
if len(parsed_links) > len(most_links): if len(parsed_links) > len(most_links):
@ -130,8 +135,8 @@ def run_parser_functions(to_parse: IO[str], timer, root_url: Optional[str]=None,
except Exception as err: # noqa except Exception as err: # noqa
# Parsers are tried one by one down the list, and the first one # Parsers are tried one by one down the list, and the first one
# that succeeds is used. To see why a certain parser was not used # that succeeds is used. To debug why a certain parser was not used
# due to error or format incompatibility, uncomment this line: # due to python error or format incompatibility, uncomment this line:
# print('[!] Parser {} failed: {} {}'.format(parser_name, err.__class__.__name__, err)) # print('[!] Parser {} failed: {} {}'.format(parser_name, err.__class__.__name__, err))
# raise # raise