From b611d34d19db480af72f57ef55eacd0a32c8d1e8 Mon Sep 17 00:00:00 2001 From: Rhet Turnbull Date: Sat, 29 Aug 2020 21:53:57 -0700 Subject: [PATCH] Added force_download.py to examples --- examples/force_download.py | 42 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 examples/force_download.py diff --git a/examples/force_download.py b/examples/force_download.py new file mode 100644 index 00000000..73883d43 --- /dev/null +++ b/examples/force_download.py @@ -0,0 +1,42 @@ +""" use osxphotos to force the download of photos from iCloud + downloads images to a temporary directory then deletes them + resulting in the photo being downloaded to Photos library +""" + +import os +import sys +import tempfile + +import osxphotos + + +def main(): + photosdb = osxphotos.PhotosDB() + tempdir = tempfile.TemporaryDirectory() + photos = photosdb.photos() + downloaded = 0 + missing = [photo for photo in photos if photo.ismissing and not photo.shared] + + if not missing: + print(f"Did not find any missing photos to download") + sys.exit(0) + + print(f"Downloading {len(missing)} photos") + for photo in missing: + if photo.ismissing: + print(f"Downloading photo {photo.original_filename}") + downloaded += 1 + exported = photo.export(tempdir.name, use_photos_export=True, timeout=300) + if photo.hasadjustments: + exported.extend( + photo.export(tempdir.name, use_photos_export=True, edited=True, timeout=300) + ) + for filename in exported: + print(f"Removing temporary file {filename}") + os.unlink(filename) + print(f"Downloaded {downloaded} photos") + tempdir.cleanup() + + +if __name__ == "__main__": + main()