Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use stdin as input and stdout as output files #113

Merged
merged 4 commits into from
Dec 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 29 additions & 9 deletions readabilipy/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,28 @@

import argparse
import json
import sys

from .__version__ import __version__
from .simple_json import simple_json_from_html_string, have_node


def main():
parser = argparse.ArgumentParser(
description="Extract article data from a HTML file using either Mozilla's Readability.js package or a simplified python-only alternative."
description="Extract article data from a HTML file using either Mozilla's Readability.js package or a simplified python-only alternative.",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument(
"-i",
"--input-file",
required=True,
help="Path to input file containing HTML.",
default="-",
help="Path to input file containing HTML, use '-' for stdin.",
)
parser.add_argument(
"-o",
"--output-file",
required=True,
help="Path to file to output the article data to as JSON.",
default="-",
help="Path to file to output the article data to as JSON, use '-' for stdout.",
)
parser.add_argument(
"-c",
Expand Down Expand Up @@ -55,8 +57,18 @@ def main():

args = parser.parse_args()

with open(args.input_file, encoding="utf-8", errors="replace") as h:
html = h.read()
# Open input file or stream
if args.input_file == "-":
if hasattr(sys.stdin, "reconfigure"):
sys.stdin.reconfigure(encoding="utf-8", errors="replace")
input_file = sys.stdin
else:
input_file = open(args.input_file, encoding="utf-8", errors="replace") # pylint: disable=consider-using-with

# Read from input then close if appropriate
html = input_file.read()
haron marked this conversation as resolved.
Show resolved Hide resolved
if not input_file.isatty():
input_file.close()

article = simple_json_from_html_string(
html,
Expand All @@ -65,8 +77,16 @@ def main():
use_readability=(not args.use_python_parser),
)

with open(args.output_file, "w", encoding="utf-8") as j:
json.dump(article, j, ensure_ascii=False)
# Open output file or stream
if args.output_file == "-":
output_file = sys.stdout
else:
output_file = open(args.output_file, "w", encoding="utf-8") # pylint: disable=consider-using-with

# Write to output then close if appropriate
json.dump(article, output_file, ensure_ascii=False)
if not output_file.isatty():
output_file.close()


if __name__ == "__main__":
Expand Down