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

Exit with cleaner message when no entries are expected in the ZIM #338

Merged
merged 2 commits into from
Jun 27, 2024
Merged
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Moved rules definition from JSON to YAML and documented update process (#216)

### Added

- Exit with cleaner message when no entries are expected in the ZIM (#336) and when main entry is not processable (#337)

## [2.0.2] - 2024-06-18

### Added
Expand Down
25 changes: 23 additions & 2 deletions src/warc2zim/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@
PY2JS_RULE_RX = re.compile(r"\\(\d)", re.ASCII)


class UnprocessableWarcError(Exception):
"""Exception raised when it is not possible to process WARC file(s) received"""

...


class Converter:
def __init__(self, args):
if args.verbose:
Expand Down Expand Up @@ -262,7 +268,12 @@ def run(self):
)
return 100

self.gather_information_from_warc()
try:
self.gather_information_from_warc()
except UnprocessableWarcError as exc:
logger.error(exc)
return 4

# Fallback language
if not self.language:
logger.warning("No valid ZIM language, fallbacking to `eng`.")
Expand Down Expand Up @@ -391,6 +402,10 @@ def gather_information_from_warc(self):

status_code = get_status_code(record)
if not can_process_status_code(status_code):
if record.rec_type == "response" and self.main_path == zim_path:
raise UnprocessableWarcError(
f"Main URL returned an unprocessable HTTP code: {status_code}"
)
continue

if status_code_is_processable_redirect(status_code):
Expand Down Expand Up @@ -480,10 +495,16 @@ def gather_information_from_warc(self):
logger.debug(f"Favicon: {self.favicon_url or self.favicon_path}")
main_page_found = True

if len(self.expected_zim_items) == 0:
raise UnprocessableWarcError(
"No entry found to push to the ZIM, WARC file(s) is unprocessable "
"and looks probably mostly empty"
)

logger.info(f"Expecting {len(self.expected_zim_items)} ZIM entries to files")

if not main_page_found:
raise KeyError(
raise UnprocessableWarcError(
f"Unable to find WARC record for main page: {self.main_path}, aborting"
)

Expand Down
Binary file added tests/data/main-entry-403.warc.gz
Binary file not shown.
26 changes: 25 additions & 1 deletion tests/test_warc_to_zim.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io
import json
import os
import pathlib
import re
import time
from urllib.parse import unquote
Expand Down Expand Up @@ -607,7 +608,7 @@ def test_fuzzy_urls(self, tmp_path, fuzzycheck):

def test_error_bad_main_page(self, tmp_path):
zim_output_not_created = "zim-out-not-created.zim"
with pytest.raises(KeyError, match="Unable to find WARC record for main page:"):
assert (
main(
[
"-v",
Expand All @@ -622,6 +623,29 @@ def test_error_bad_main_page(self, tmp_path):
zim_output_not_created,
]
)
== 4
)

def test_error_main_page_unprocessable(self, tmp_path):
zim_output_not_created = "zim-out-not-created.zim"
assert (
main(
[
"-v",
os.path.join(TEST_DATA_DIR, "main-entry-403.warc.gz"),
"-u",
"https://wikizilla.org/wiki/Doug",
"--output",
str(tmp_path),
"--name",
"bad",
"--zim-file",
zim_output_not_created,
]
)
== 4
)
assert not (pathlib.Path(tmp_path) / zim_output_not_created).exists()

def test_args_only(self):
# error, name required
Expand Down
Loading