Skip to content

Commit

Permalink
support --dry-run parameter (#663)
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreyNikiforov authored Jul 16, 2023
1 parent 6012337 commit e6482c6
Show file tree
Hide file tree
Showing 10 changed files with 554 additions and 167 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased

- feature: `--dry-run` parameter to run icloudpd without changes to local files and iCloud
- fix: pypi.org license and description

## 1.14.5 (2023-07-06)
Expand Down
7 changes: 3 additions & 4 deletions src/icloudpd/authentication.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
"""Handles username/password authentication and two-step authentication"""

import logging
import sys
import click
import pyicloud_ipd
from icloudpd.logger import setup_logger


class TwoStepAuthRequiredError(Exception):
Expand All @@ -13,7 +13,7 @@ class TwoStepAuthRequiredError(Exception):
"""


def authenticator(domain):
def authenticator(logger, domain):
"""Wraping authentication with domain context"""
def authenticate_(
username,
Expand All @@ -23,8 +23,7 @@ def authenticate_(
client_id=None,
):
"""Authenticate with iCloud username and password"""
logger = setup_logger()
logger.debug("Authenticating...")
logger.tqdm_write("Authenticating...", logging.DEBUG)
while True:
try:
# If password not provided on command line variable will be set to None
Expand Down
24 changes: 17 additions & 7 deletions src/icloudpd/autodelete.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,35 @@
import os
import logging
from tzlocal import get_localzone
from icloudpd.logger import setup_logger
from icloudpd.paths import local_download_path


def autodelete_photos(icloud, folder_structure, directory):
def delete_file(logger, path) -> bool:
""" Actual deletion of files """
os.remove(path)
logger.tqdm_write(f"Deleted {path}", logging.INFO)
return True

def delete_file_dry_run(logger, path) -> bool:
""" Dry run deletion of files """
logger.tqdm_write(f"[DRY RUN] Would delete {path}", logging.INFO)
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 = setup_logger()
logger.info("Deleting any files found in 'Recently Deleted'...")
logger.tqdm_write("Deleting any files found in 'Recently Deleted'...", logging.INFO)

recently_deleted = icloud.photos.albums["Recently Deleted"]

for media in recently_deleted:
try:
created_date = media.created.astimezone(get_localzone())
except (ValueError, OSError):
logger.set_tqdm_description(
logger.tqdm_write(
f"Could not convert media created date to local timezone {media.created}",
logging.ERROR)
created_date = media.created
Expand All @@ -36,5 +45,6 @@ def autodelete_photos(icloud, folder_structure, directory):
local_download_path(
media, size, download_dir))
if os.path.exists(path):
logger.info("Deleting %s!", path)
os.remove(path)
logger.tqdm_write(f"Deleting {path}...", logging.DEBUG)
delete_local = delete_file_dry_run if dry_run else delete_file
delete_local(logger, path)
Loading

0 comments on commit e6482c6

Please sign in to comment.