Skip to content

Commit 2437b20

Browse files
committed
adds some tests for the size and suffix logic
1 parent 20773de commit 2437b20

File tree

2 files changed

+89
-26
lines changed

2 files changed

+89
-26
lines changed

flickr_download/flick_download.py

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import time
1616
from pathlib import Path
1717
from types import FrameType
18-
from typing import Any, Dict, Optional
18+
from typing import Any, Dict, Optional, Tuple
1919

2020
import flickr_api as Flickr
2121
import yaml
@@ -99,6 +99,35 @@ def _get_metadata_db(dirname: str) -> sqlite3.Connection:
9999
return conn
100100

101101

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+
102131
def download_set(
103132
set_id: str,
104133
get_filename: FilenameHandler,
@@ -233,30 +262,9 @@ def do_download_photo(
233262
except Exception:
234263
logging.warning("Trouble saving photo info:", sys.exc_info()[0])
235264

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
260268

261269
if os.path.exists(fname):
262270
# TODO: Ideally we should check for file size / md5 here
@@ -416,7 +424,7 @@ def serialize_json(obj: Any) -> Any:
416424
return ret
417425

418426

419-
def get_photo_sizes(photo: Photo) -> Dict[str, Any]:
427+
def _get_photo_sizes(photo: Photo) -> Dict[str, Any]:
420428
for attempt in range(1, (API_RETRIES + 1)):
421429
try:
422430
return photo.getSizes()

tests/test_core.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
from unittest.mock import Mock
2+
3+
from flickr_api.objects import Photo
4+
from flickr_download.flick_download import _get_size_and_suffix
5+
6+
7+
def tests_get_size_and_suffix_video() -> None:
8+
# Fallback when no video formats
9+
photo = Mock(Photo)
10+
photo.getSizes.return_value = None
11+
# The code does a .get("video")... slightly ugly mocking.
12+
photo.get.return_value = True
13+
(size, suffix) = _get_size_and_suffix(photo, None)
14+
assert size == "Site MP4"
15+
assert suffix == ".mp4"
16+
17+
# The standard video format
18+
photo.getSizes.return_value = {"HD MP4": {}}
19+
(size, suffix) = _get_size_and_suffix(photo, None)
20+
assert size == "HD MP4"
21+
assert suffix == ".mp4"
22+
23+
24+
def tests_get_size_and_suffix_extension() -> None:
25+
# Source is there
26+
photo = Mock(Photo)
27+
photo.getSizes.return_value = {"Original": {"source": "some_file.png"}}
28+
photo.get.return_value = False
29+
(size, suffix) = _get_size_and_suffix(photo, None)
30+
assert size is None
31+
assert suffix == ".png"
32+
33+
# Source is not there
34+
photo = Mock(Photo)
35+
photo.getSizes.return_value = {}
36+
photo.get.return_value = False
37+
(size, suffix) = _get_size_and_suffix(photo, None)
38+
assert size is None
39+
assert suffix == ".jpg"
40+
41+
# Different source is there
42+
photo = Mock(Photo)
43+
photo.getSizes.return_value = {"Large": {"source": "some_file.png"}}
44+
photo.get.return_value = False
45+
(size, suffix) = _get_size_and_suffix(photo, None)
46+
assert size is None
47+
assert suffix == ".jpg"
48+
49+
# Source is there, size is passed in
50+
photo = Mock(Photo)
51+
photo.getSizes.return_value = {"Original": {"source": "some_file.jpeg"}}
52+
photo.get.return_value = False
53+
(size, suffix) = _get_size_and_suffix(photo, "Original")
54+
assert size == "Original"
55+
assert suffix == ".jpeg"

0 commit comments

Comments
 (0)