|
9 | 9 | import sys |
10 | 10 | from typing import Dict, List, Union |
11 | 11 | from urllib.request import urlopen, Request |
| 12 | +from urllib.error import URLError, HTTPError |
12 | 13 | import urllib.parse |
13 | 14 | import base64 |
14 | 15 | from time import time |
|
129 | 130 | help='When exporting images, they will be organized in' |
130 | 131 | ' directory located at the same path as exported document.' |
131 | 132 | ' 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).") |
132 | 139 | parser.add_argument('--dont-export-attachments', |
133 | 140 | default=False, |
134 | 141 | action='store_true', |
@@ -196,6 +203,7 @@ def removesuffix(text, suffix): |
196 | 203 | HEADERS_NO_TOKEN[values[0]] = values[1] |
197 | 204 |
|
198 | 205 | SKIP_TIMESTAMPS: bool = args.force_update_files |
| 206 | +SKIP_BROKEN_IMAGE_LINKS: bool = args.skip_broken_image_links |
199 | 207 |
|
200 | 208 |
|
201 | 209 | class ApiRateLimiter: |
@@ -533,7 +541,14 @@ def export_images(): |
533 | 541 | if not check_if_update_needed(path, img): |
534 | 542 | continue |
535 | 543 |
|
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 |
537 | 552 | with open(path, 'wb') as file: |
538 | 553 | info(f"Saving {path}") |
539 | 554 | file.write(data) |
|
0 commit comments