From d0c9bec7f42c7a0a9a601ae598632b25f11d3af8 Mon Sep 17 00:00:00 2001 From: Roland Date: Tue, 7 May 2024 09:41:48 -0600 Subject: [PATCH] Modifying singlefile_to_archive to accept file list in stdin. --- scripts/singlefile_to_archive.py | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/scripts/singlefile_to_archive.py b/scripts/singlefile_to_archive.py index 894c03f2..b3bd5bd1 100755 --- a/scripts/singlefile_to_archive.py +++ b/scripts/singlefile_to_archive.py @@ -47,6 +47,7 @@ def singlefile_to_archive_files(singlefile: pathlib.Path): # The real Readability the user wants. readability_html = readability.pop('content', None) if readability and not readability_html_path.is_file() and readability_html: + logger.info(f'Writing readability html: {readability_html_path}') try: readability_html = format_html_string(readability_html) readability_html_path.write_text(readability_html) @@ -56,19 +57,23 @@ def singlefile_to_archive_files(singlefile: pathlib.Path): # The text from within the Readability, used for searching. readability_text = readability.pop('textContent', None) if readability and not readability_text_path.is_file() and readability_text: + logger.info(f'Writing readability txt: {readability_text_path}') readability_text_path.write_text(readability_text) # Write the JSON last now that content/textContent have been removed. - if readability and not readability_json_path.is_file(): + keys = set(json.loads(readability_json_path.read_text()).keys()) if readability_json_path.is_file() else None + if readability and (not readability_json_path.is_file() or keys == {'url'}): + logger.info(f'Writing readability json: {readability_json_path}') try: with readability_json_path.open('wt') as fh: fh.write(json.dumps(readability, indent=2)) except Exception as e: - logger.error(f'Failed to extract Readability text: {singlefile}', exc_info=e) + logger.error(f'Failed to write Readability json: {singlefile}', exc_info=e) # Only generate a screenshot if the file is missing. screenshot_path = singlefile.with_suffix('.png') if not screenshot_path.is_file(): + logger.info(f'Writing screenshot: {screenshot_path}') try: screenshot = html_screenshot(contents) screenshot_path.write_bytes(screenshot) @@ -78,8 +83,8 @@ def singlefile_to_archive_files(singlefile: pathlib.Path): if __name__ == '__main__': parser = argparse.ArgumentParser() - parser.add_argument('files', nargs='+') - parser.add_argument('-v', action='count') + parser.add_argument('files', nargs='*', default=['-']) + parser.add_argument('-v', action='count', default=0) args = parser.parse_args() if args.v == 0: @@ -94,7 +99,19 @@ if __name__ == '__main__': print(f'Cannot find "readability-extractor". Install it.') sys.exit(1) - files = [pathlib.Path(i) for i in args.files] + if args.files == ['-', ]: + # Read list of files from stdin, separated by newlines. + print('Reading list of files from stdin', file=sys.stderr) + files = [pathlib.Path(i.strip()) for i in sys.stdin.readlines()] + else: + files = [pathlib.Path(i) for i in args.files] + + non_existent_files = [i for i in files if not i.is_file()] + if non_existent_files: + for i in non_existent_files: + print(f'File does not exist: {i}') + sys.exit(1) + invalid_files = [i for i in files if not archive_lib.is_singlefile_file(i)] if invalid_files: for i in invalid_files: