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

Refactor scraper to exit properly when exceptions are raised #288

Merged
merged 3 commits into from
Aug 9, 2024
Merged
Changes from 1 commit
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
Next Next commit
Refactor scraper.py to exit properly when exceptions are raised
dan-niles committed Aug 7, 2024
commit ebe3fb9abcb7bfbbd3155443aaeab36e1bfa265b
12 changes: 7 additions & 5 deletions scraper/src/youtube2zim/scraper.py
Original file line number Diff line number Diff line change
@@ -24,7 +24,6 @@
from libzim.writer import IndexData # type: ignore
from pif import get_public_ip
from zimscraperlib.download import stream_file
from zimscraperlib.filesystem import delete_callback
from zimscraperlib.i18n import NotFound, get_language_details
from zimscraperlib.image.convertion import convert_image
from zimscraperlib.image.presets import WebpHigh
@@ -66,6 +65,7 @@
)
from youtube2zim.utils import (
clean_text,
delete_callback,
get_slug,
load_json,
load_mandatory_json,
@@ -423,17 +423,19 @@
except KeyboardInterrupt:
self.zim_file.can_finish = False
logger.error("KeyboardInterrupt, exiting.")
return 1

Check warning on line 426 in scraper/src/youtube2zim/scraper.py

Codecov / codecov/patch

scraper/src/youtube2zim/scraper.py#L426

Added line #L426 was not covered by tests
except Exception as exc:
# request Creator not to create a ZIM file on finish
self.zim_file.can_finish = False
logger.error(f"Interrupting process due to error: {exc}")
logger.exception(exc)
finally:
return 1

Check warning on line 432 in scraper/src/youtube2zim/scraper.py

Codecov / codecov/patch

scraper/src/youtube2zim/scraper.py#L432

Added line #L432 was not covered by tests
else:
logger.info("Finishing ZIM file…")
self.zim_file.finish()

logger.info("removing temp folder")
shutil.rmtree(self.build_dir, ignore_errors=True)
finally:
logger.info("removing temp folder")

Check warning on line 437 in scraper/src/youtube2zim/scraper.py

Codecov / codecov/patch

scraper/src/youtube2zim/scraper.py#L437

Added line #L437 was not covered by tests
shutil.rmtree(self.build_dir, ignore_errors=True)

logger.info("all done!")

7 changes: 7 additions & 0 deletions scraper/src/youtube2zim/utils.py
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@
# vim: ai ts=4 sts=4 et sw=4 nu

import json
import os

Check warning on line 5 in scraper/src/youtube2zim/utils.py

Codecov / codecov/patch

scraper/src/youtube2zim/utils.py#L5

Added line #L5 was not covered by tests
from pathlib import Path

from slugify import slugify
@@ -44,3 +45,9 @@
def has_argument(arg_name, all_args):
"""whether --arg_name is specified in all_args"""
return list(filter(lambda x: x.startswith(f"--{arg_name}"), all_args))


def delete_callback(fpath: str | Path):

Check warning on line 50 in scraper/src/youtube2zim/utils.py

Codecov / codecov/patch

scraper/src/youtube2zim/utils.py#L50

Added line #L50 was not covered by tests
"""callback to delete file"""
if Path(fpath).exists():
os.unlink(fpath)

Check warning on line 53 in scraper/src/youtube2zim/utils.py

Codecov / codecov/patch

scraper/src/youtube2zim/utils.py#L53

Added line #L53 was not covered by tests