-
Notifications
You must be signed in to change notification settings - Fork 0
/
autodelete.py
57 lines (48 loc) · 2.08 KB
/
autodelete.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
"""
Delete any files found in "Recently Deleted"
"""
import os
from tzlocal import get_localzone
from icloudpd.paths import local_download_path, live_photo_download_path
def delete_file(logger, path) -> bool:
""" Actual deletion of files """
os.remove(path)
logger.info("Deleted %s", path)
return True
def delete_file_dry_run(logger, path) -> bool:
""" Dry run deletion of files """
logger.info("[DRY RUN] Would delete %s", path)
return True
def autodelete_photos(logger, dry_run, icloud, folder_structure, directory):
"""
Scans the "Recently Deleted" folder and deletes any matching files
from the download directory.
(I.e. If you delete a photo on your phone, it's also deleted on your computer.)
"""
logger.info("Deleting any files found in 'Recently Deleted'...")
recently_deleted = icloud.photos.albums["Recently Deleted"]
for media in recently_deleted:
try:
created_date = media.created.astimezone(get_localzone())
except (ValueError, OSError):
logger.error(
"Could not convert media created date to local timezone %s",
media.created)
created_date = media.created
date_path = folder_structure.format(created_date)
download_dir = os.path.join(directory, date_path)
for size in [None, "original", "medium", "thumb"]:
path = os.path.normpath(
local_download_path(
media, size, download_dir))
if os.path.exists(path):
logger.debug("Deleting %s...", path)
delete_local = delete_file_dry_run if dry_run else delete_file
delete_local(logger, path)
live_photo_path = os.path.normpath(
live_photo_download_path(
media, size, download_dir))
if os.path.exists(live_photo_path):
logger.debug("Deleting live photo %s...", live_photo_path)
delete_local = delete_file_dry_run if dry_run else delete_file
delete_local(logger, live_photo_path)