Skip to content

Commit 6684bc3

Browse files
authored
Merge pull request #13 from Szwendacz99/skip-broken-images
add option for skipping download of images with broken url
2 parents 30b6ffc + e10e698 commit 6684bc3

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,9 @@ usage: exporter.py [-h] [-p PATH] [-t TOKEN_FILE] [-H HOST]
4646
[--additional-headers ADDITIONAL_HEADERS [ADDITIONAL_HEADERS ...]]
4747
[-l {pages,chapters,books} [{pages,chapters,books} ...]]
4848
[--force-update-files] [--images] [--markdown-images]
49-
[--images-dir IMAGES_DIR] [--dont-export-attachments]
50-
[--dont-export-external-attachments] [-V {debug,info,warning,error}]
49+
[--images-dir IMAGES_DIR] [--skip-broken-image-links]
50+
[--dont-export-attachments] [--dont-export-external-attachments]
51+
[-V {debug,info,warning,error}]
5152
5253
BookStack exporter
5354
@@ -93,6 +94,10 @@ options:
9394
When exporting images, they will be organized in directory located at
9495
the same path as exported document. This parameter defines name of
9596
this directory.
97+
--skip-broken-image-links
98+
Don't fail and skip downloading images if their url obtained from
99+
images gallery API seem broken (image cannot be downloaded OR fails to
100+
download).
96101
--dont-export-attachments
97102
Set this to prevent exporting any attachments.
98103
--dont-export-external-attachments

exporter.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import sys
1010
from typing import Dict, List, Union
1111
from urllib.request import urlopen, Request
12+
from urllib.error import URLError, HTTPError
1213
import urllib.parse
1314
import base64
1415
from time import time
@@ -129,6 +130,12 @@
129130
help='When exporting images, they will be organized in'
130131
' directory located at the same path as exported document.'
131132
' This parameter defines name of this directory.')
133+
parser.add_argument('--skip-broken-image-links',
134+
default=False,
135+
action='store_true',
136+
help="Don't fail and skip downloading images if their "
137+
"url obtained from images gallery API seem broken "
138+
"(image cannot be downloaded OR fails to download).")
132139
parser.add_argument('--dont-export-attachments',
133140
default=False,
134141
action='store_true',
@@ -196,6 +203,7 @@ def removesuffix(text, suffix):
196203
HEADERS_NO_TOKEN[values[0]] = values[1]
197204

198205
SKIP_TIMESTAMPS: bool = args.force_update_files
206+
SKIP_BROKEN_IMAGE_LINKS: bool = args.skip_broken_image_links
199207

200208

201209
class ApiRateLimiter:
@@ -533,7 +541,14 @@ def export_images():
533541
if not check_if_update_needed(path, img):
534542
continue
535543

536-
data: bytes = api_get_bytes(img.get_url(), raw_url=True)
544+
try:
545+
data: bytes = api_get_bytes(img.get_url(), raw_url=True)
546+
except (URLError, HTTPError) as exc:
547+
error(f"Failed downloading image '{img.get_url()}': {exc}")
548+
if not SKIP_BROKEN_IMAGE_LINKS:
549+
sys.exit(1)
550+
else:
551+
continue
537552
with open(path, 'wb') as file:
538553
info(f"Saving {path}")
539554
file.write(data)

0 commit comments

Comments
 (0)