Skip to content

Commit

Permalink
Merge pull request #13 from Szwendacz99/skip-broken-images
Browse files Browse the repository at this point in the history
add option for skipping download of images with broken url
  • Loading branch information
Szwendacz99 authored Jul 29, 2024
2 parents 30b6ffc + e10e698 commit 6684bc3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
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

0 comments on commit 6684bc3

Please sign in to comment.