Skip to content

add option for skipping download of images with broken url #13

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

Merged
merged 1 commit into from
Jul 29, 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
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ usage: exporter.py [-h] [-p PATH] [-t TOKEN_FILE] [-H HOST]
[--additional-headers ADDITIONAL_HEADERS [ADDITIONAL_HEADERS ...]]
[-l {pages,chapters,books} [{pages,chapters,books} ...]]
[--force-update-files] [--images] [--markdown-images]
[--images-dir IMAGES_DIR] [--dont-export-attachments]
[--dont-export-external-attachments] [-V {debug,info,warning,error}]
[--images-dir IMAGES_DIR] [--skip-broken-image-links]
[--dont-export-attachments] [--dont-export-external-attachments]
[-V {debug,info,warning,error}]

BookStack exporter

Expand Down Expand Up @@ -93,6 +94,10 @@ options:
When exporting images, they will be organized in directory located at
the same path as exported document. This parameter defines name of
this directory.
--skip-broken-image-links
Don't fail and skip downloading images if their url obtained from
images gallery API seem broken (image cannot be downloaded OR fails to
download).
--dont-export-attachments
Set this to prevent exporting any attachments.
--dont-export-external-attachments
Expand Down
17 changes: 16 additions & 1 deletion exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import sys
from typing import Dict, List, Union
from urllib.request import urlopen, Request
from urllib.error import URLError, HTTPError
import urllib.parse
import base64
from time import time
Expand Down Expand Up @@ -129,6 +130,12 @@
help='When exporting images, they will be organized in'
' directory located at the same path as exported document.'
' This parameter defines name of this directory.')
parser.add_argument('--skip-broken-image-links',
default=False,
action='store_true',
help="Don't fail and skip downloading images if their "
"url obtained from images gallery API seem broken "
"(image cannot be downloaded OR fails to download).")
parser.add_argument('--dont-export-attachments',
default=False,
action='store_true',
Expand Down Expand Up @@ -196,6 +203,7 @@ def removesuffix(text, suffix):
HEADERS_NO_TOKEN[values[0]] = values[1]

SKIP_TIMESTAMPS: bool = args.force_update_files
SKIP_BROKEN_IMAGE_LINKS: bool = args.skip_broken_image_links


class ApiRateLimiter:
Expand Down Expand Up @@ -533,7 +541,14 @@ def export_images():
if not check_if_update_needed(path, img):
continue

data: bytes = api_get_bytes(img.get_url(), raw_url=True)
try:
data: bytes = api_get_bytes(img.get_url(), raw_url=True)
except (URLError, HTTPError) as exc:
error(f"Failed downloading image '{img.get_url()}': {exc}")
if not SKIP_BROKEN_IMAGE_LINKS:
sys.exit(1)
else:
continue
with open(path, 'wb') as file:
info(f"Saving {path}")
file.write(data)
Expand Down