1313import time
1414from pathlib import Path
1515from types import FrameType
16- from typing import Any , Dict , Optional , Tuple
16+ from typing import Any , Dict , Optional
1717
1818import flickr_api as Flickr
1919import yaml
@@ -105,35 +105,6 @@ def _get_metadata_db(dirname: str) -> sqlite3.Connection:
105105 return conn
106106
107107
108- def _get_size_and_suffix (photo : Photo , size_label : Optional [str ]) -> Tuple [Optional [str ], str ]:
109- photo_size_label : Optional [str ] = size_label
110- if photo .get ("video" ):
111- photo_sizes = _get_photo_sizes (photo )
112- if photo_sizes and "HD MP4" in photo_sizes :
113- photo_size_label = "HD MP4"
114- else :
115- # Fall back for old 'short videos'. This might not exist, but
116- # Photo.save() will croak on auto-detecting these old videos, so
117- # better not download than throwing an exception...
118- photo_size_label = "Site MP4"
119- suffix = ".mp4"
120- return (photo_size_label , ".mp4" )
121-
122- suffix = ".jpg"
123- # Flickr returns JPEG, except for when downloading originals. The only way
124- # to find the original type it seems is through the source filename. This
125- # is not pretty...
126- if photo_size_label == "Original" or not photo_size_label :
127- photo_sizes = _get_photo_sizes (photo )
128- meta = photo_sizes and photo_sizes .get ("Original" )
129- if meta and meta ["source" ]:
130- ext = os .path .splitext (meta ["source" ])[1 ]
131- if ext :
132- suffix = ext
133-
134- return (photo_size_label , suffix )
135-
136-
137108def download_set (
138109 set_id : str ,
139110 get_filename : FilenameHandler ,
@@ -239,16 +210,16 @@ def do_download_photo(
239210 @param save_json: save photo info as .json file
240211 @param metadata_db: optional metadata database to record downloads in
241212 """
242- orig_suffix = suffix or ""
243213 if metadata_db :
244214 if metadata_db .execute (
245215 "SELECT * FROM downloads WHERE photo_id = ? AND size_label = ? AND suffix = ?" ,
246- (photo .id , size_label or "" , orig_suffix ),
216+ (photo .id , size_label or "" , suffix ),
247217 ).fetchone ():
248218 logging .info ("Skipping download of already downloaded photo with ID: %s" , photo .id )
249219 return
250220
251221 fname = get_full_path (dirname , get_filename (pset , photo , suffix ))
222+ fname = photo ._getOutputFilename (fname , size_label )
252223 json_fname = fname + ".json"
253224
254225 if not photo ["loaded" ]:
@@ -274,10 +245,6 @@ def do_download_photo(
274245 except Exception :
275246 logging .warning ("Trouble saving photo info: %s" , sys .exc_info ()[0 ])
276247
277- (photo_size_label , suffix ) = _get_size_and_suffix (photo , size_label )
278- if suffix :
279- fname += suffix
280-
281248 if os .path .exists (fname ):
282249 # TODO: Ideally we should check for file size / md5 here
283250 # to handle failed downloads.
@@ -289,7 +256,7 @@ def do_download_photo(
289256 return
290257
291258 try :
292- photo .save (fname , photo_size_label )
259+ photo .save (fname , size_label )
293260 except IOError as ex :
294261 logging .error ("IO error saving photo: %s" , ex )
295262 return
@@ -302,7 +269,7 @@ def do_download_photo(
302269
303270 if metadata_db :
304271 metadata_db .execute (
305- "INSERT INTO downloads VALUES (?, ?, ?)" , (photo .id , size_label or "" , orig_suffix )
272+ "INSERT INTO downloads VALUES (?, ?, ?)" , (photo .id , size_label or "" , suffix )
306273 )
307274 metadata_db .commit ()
308275
0 commit comments