|
15 | 15 | import time |
16 | 16 | from pathlib import Path |
17 | 17 | from types import FrameType |
18 | | -from typing import Any, Dict, Optional |
| 18 | +from typing import Any, Dict, Optional, Tuple |
19 | 19 |
|
20 | 20 | import flickr_api as Flickr |
21 | 21 | import yaml |
@@ -99,6 +99,35 @@ def _get_metadata_db(dirname: str) -> sqlite3.Connection: |
99 | 99 | return conn |
100 | 100 |
|
101 | 101 |
|
| 102 | +def _get_size_and_suffix(photo: Photo, size_label: Optional[str]) -> Tuple[Optional[str], str]: |
| 103 | + photo_size_label: Optional[str] = size_label |
| 104 | + if photo.get("video"): |
| 105 | + pSizes = _get_photo_sizes(photo) |
| 106 | + if pSizes and "HD MP4" in pSizes: |
| 107 | + photo_size_label = "HD MP4" |
| 108 | + else: |
| 109 | + # Fall back for old 'short videos'. This might not exist, but |
| 110 | + # Photo.save() will croak on auto-detecting these old videos, so |
| 111 | + # better not download than throwing an exception... |
| 112 | + photo_size_label = "Site MP4" |
| 113 | + suffix = ".mp4" |
| 114 | + return (photo_size_label, ".mp4") |
| 115 | + |
| 116 | + suffix = ".jpg" |
| 117 | + # Flickr returns JPEG, except for when downloading originals. The only way |
| 118 | + # to find the original type it seems is through the source filename. This |
| 119 | + # is not pretty... |
| 120 | + if photo_size_label == "Original" or not photo_size_label: |
| 121 | + pSizes = _get_photo_sizes(photo) |
| 122 | + meta = pSizes and pSizes.get("Original") |
| 123 | + if meta and meta["source"]: |
| 124 | + ext = os.path.splitext(meta["source"])[1] |
| 125 | + if ext: |
| 126 | + suffix = ext |
| 127 | + |
| 128 | + return (photo_size_label, suffix) |
| 129 | + |
| 130 | + |
102 | 131 | def download_set( |
103 | 132 | set_id: str, |
104 | 133 | get_filename: FilenameHandler, |
@@ -233,30 +262,9 @@ def do_download_photo( |
233 | 262 | except Exception: |
234 | 263 | logging.warning("Trouble saving photo info:", sys.exc_info()[0]) |
235 | 264 |
|
236 | | - photo_size_label: Optional[str] |
237 | | - if photo.get("video"): |
238 | | - pSizes = get_photo_sizes(photo) |
239 | | - if pSizes and "HD MP4" in pSizes: |
240 | | - photo_size_label = "HD MP4" |
241 | | - else: |
242 | | - # Fall back for old 'short videos' |
243 | | - photo_size_label = "Site MP4" |
244 | | - fname = fname + ".mp4" |
245 | | - else: |
246 | | - photo_size_label = size_label |
247 | | - suffix = ".jpg" |
248 | | - # Flickr returns JPEG, except for when downloading originals. The only way to find the |
249 | | - # original type it seems is through the source filename. This is not pretty... |
250 | | - if photo_size_label == "Original" or not photo_size_label: |
251 | | - pSizes = get_photo_sizes(photo) |
252 | | - meta = pSizes and pSizes.get("Original") |
253 | | - if meta and meta["source"]: |
254 | | - ext = os.path.splitext(meta["source"])[1] |
255 | | - if ext: |
256 | | - suffix = ext |
257 | | - |
258 | | - if suffix: |
259 | | - fname = fname + suffix |
| 265 | + (photo_size_label, suffix) = _get_size_and_suffix(photo, size_label) |
| 266 | + if suffix: |
| 267 | + fname += suffix |
260 | 268 |
|
261 | 269 | if os.path.exists(fname): |
262 | 270 | # TODO: Ideally we should check for file size / md5 here |
@@ -416,7 +424,7 @@ def serialize_json(obj: Any) -> Any: |
416 | 424 | return ret |
417 | 425 |
|
418 | 426 |
|
419 | | -def get_photo_sizes(photo: Photo) -> Dict[str, Any]: |
| 427 | +def _get_photo_sizes(photo: Photo) -> Dict[str, Any]: |
420 | 428 | for attempt in range(1, (API_RETRIES + 1)): |
421 | 429 | try: |
422 | 430 | return photo.getSizes() |
|
0 commit comments