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

fix issue-109 (use unique temp files for input/output of ExtractArticle.js) #110

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
16 changes: 10 additions & 6 deletions readabilipy/simple_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,17 @@ def simple_json_from_html_string(html, content_digests=False, node_indexes=False

if use_readability:
# Write input HTML to temporary file so it is available to the node.js script
with tempfile.NamedTemporaryFile(delete=False, mode="w+", encoding="utf-8") as f_html:
# It is important that this file be unique in case this function is called concurrently
with tempfile.NamedTemporaryFile(delete=False, mode="w+", encoding="utf-8", prefix="readabilipy") as f_html:
f_html.write(html)
f_html.close()
html_path = f_html.name

# Derive some output name
# (making the assumption this will be unique too)
json_path = html_path + ".json"

# Call Mozilla's Readability.js Readability.parse() function via node, writing output to a temporary file
article_json_path = f_html.name + ".json"
jsdir = os.path.join(os.path.dirname(__file__), 'javascript')
try:
result = subprocess.run(
Expand All @@ -64,12 +68,12 @@ def simple_json_from_html_string(html, content_digests=False, node_indexes=False
print(e.stderr)
raise

# Read output of call to Readability.parse() from JSON file and return as Python dictionary
with open(article_json_path, "r", encoding="utf-8") as json_file:
# Read output of call to Readability.parse() from JSON file as Python dictionary
with open(json_path, "r", encoding="utf-8") as json_file:
input_json = json.load(json_file)

# Deleting files after processing
os.unlink(article_json_path)
# Delete temporary input and output files after processing
os.unlink(json_path)
os.unlink(f_html.name)
else:
input_json = {
Expand Down
2 changes: 1 addition & 1 deletion tests/checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def check_extract_article(test_filename, expected_filename, content_digests=Fals
expected_article_json = json.loads(h.read())

# Test full JSON matches (checks for unexpected fields in either actual or expected JSON)
assert article_json == expected_article_json
assert article_json == expected_article_json, f"{article_json=} != {expected_article_json=}"


def check_extract_paragraphs_as_plain_text(test_filename, expected_filename):
Expand Down
Loading