Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto delete date fix testing #645

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion icloudpd/autodelete.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
Delete any files found in "Recently Deleted"
"""
import os
import logging
from tzlocal import get_localzone
from icloudpd.logger import setup_logger
from icloudpd.paths import local_download_path

Expand All @@ -18,7 +20,14 @@ def autodelete_photos(icloud, folder_structure, directory):
recently_deleted = icloud.photos.albums["Recently Deleted"]

for media in recently_deleted:
created_date = media.created
try:
created_date = media.created.astimezone(get_localzone())
except (ValueError, OSError):
logger.set_tqdm_description(
f"Could not convert media created date to local timezone {media.created}",
logging.ERROR)
created_date = media.created

date_path = folder_structure.format(created_date)
download_dir = os.path.join(directory, date_path)

Expand Down
217 changes: 206 additions & 11 deletions tests/test_autodelete_photos.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@
import os
import shutil
import pytest
import mock
import datetime
import pytz
from tzlocal import get_localzone
from click.testing import CliRunner
from pyicloud_ipd.services.photos import PhotoAsset
from icloudpd.base import main
from tests.helpers.print_result_exception import print_result_exception
import inspect
import glob

Expand All @@ -16,6 +22,194 @@ class AutodeletePhotosTestCase(TestCase):
def inject_fixtures(self, caplog):
self._caplog = caplog

def test_autodelete_invalid_creation_date(self):
base_dir = os.path.normpath(f"tests/fixtures/Photos/{inspect.stack()[0][3]}")
if os.path.exists(base_dir):
shutil.rmtree(base_dir)
os.makedirs(base_dir)

files = [
"2018/01/01/IMG_3589.JPG"
]

with mock.patch.object(PhotoAsset, "created", new_callable=mock.PropertyMock) as dt_mock:
# Can't mock `astimezone` because it's a readonly property, so have to
# create a new class that inherits from datetime.datetime
class NewDateTime(datetime.datetime):
def astimezone(self, tz=None):
raise ValueError('Invalid date')
dt_mock.return_value = NewDateTime(2018,1,1,0,0,0)

with vcr.use_cassette("tests/vcr_cassettes/download_autodelete_photos.yml"):
# Pass fixed client ID via environment variable
runner = CliRunner(env={
"CLIENT_ID": "DE309E26-942E-11E8-92F5-14109FE0B321"
})
result = runner.invoke(
main,
[
"--username",
"[email protected]",
"--password",
"password1",
"--recent",
"1",
"--delete-after-download",
"-d",
base_dir,
],
)

self.assertIn("DEBUG Looking up all photos and videos from album All Photos...", self._caplog.text)
self.assertIn(
f"INFO Downloading the first original photo or video to {base_dir} ...",
self._caplog.text,
)
self.assertIn(
f"ERROR Could not convert photo created date to local timezone (2018-01-01 00:00:00)",
self._caplog.text,
)
self.assertIn(
f"INFO Downloading {os.path.join(base_dir, os.path.normpath('2018/01/01/IMG_3589.JPG'))}",
self._caplog.text,
)
self.assertIn(
f"INFO Deleting IMG_3589.JPG",
self._caplog.text,
)
self.assertIn(
"INFO All photos have been downloaded!", self._caplog.text
)

# check files
for file_name in files:
assert os.path.exists(os.path.join(base_dir, file_name)), f"{file_name} expected, but missing"

result = runner.invoke(
main,
[
"--username",
"[email protected]",
"--password",
"password1",
"--recent",
"0",
"--auto-delete",
"-d",
base_dir,
],
)
print_result_exception(result)

self.assertIn("DEBUG Looking up all photos and videos from album All Photos...", self._caplog.text)
self.assertIn(
f"INFO Downloading 0 original photos and videos to {base_dir} ...",
self._caplog.text,
)
self.assertIn(
f"INFO All photos have been downloaded!", self._caplog.text
)
self.assertIn(
f"INFO Deleting any files found in 'Recently Deleted'...",
self._caplog.text,
)

self.assertIn(
f"INFO Deleting {os.path.join(base_dir, os.path.normpath('2018/01/01/IMG_3589.JPG'))}",
self._caplog.text,
)

for file_name in files:
assert not os.path.exists(
os.path.join(base_dir, file_name)), f"{file_name} not expected, but present"

def test_download_autodelete_photos(self):
base_dir = os.path.normpath(f"tests/fixtures/Photos/{inspect.stack()[0][3]}")
if os.path.exists(base_dir):
shutil.rmtree(base_dir)
os.makedirs(base_dir)

files = [
f"{'{:%Y/%m/%d}'.format(datetime.datetime.fromtimestamp(1686106167436.0 / 1000.0, tz=pytz.utc).astimezone(get_localzone()))}/IMG_3589.JPG"
]

with vcr.use_cassette("tests/vcr_cassettes/download_autodelete_photos.yml"):
# Pass fixed client ID via environment variable
runner = CliRunner(env={
"CLIENT_ID": "DE309E26-942E-11E8-92F5-14109FE0B321"
})
result = runner.invoke(
main,
[
"--username",
"[email protected]",
"--password",
"password1",
"--recent",
"1",
"--delete-after-download",
"-d",
base_dir,
],
)

self.assertIn("DEBUG Looking up all photos and videos from album All Photos...", self._caplog.text)
self.assertIn(
f"INFO Downloading the first original photo or video to {base_dir} ...",
self._caplog.text,
)
self.assertIn(
f"INFO Downloading {os.path.join(base_dir, os.path.normpath(files[0]))}",
self._caplog.text,
)
self.assertIn(
f"INFO Deleting IMG_3589.JPG",
self._caplog.text,
)
self.assertIn(
"INFO All photos have been downloaded!", self._caplog.text
)

#check files
for file_name in files:
assert os.path.exists(os.path.join(base_dir, file_name)), f"{file_name} expected, but missing"

result = runner.invoke(
main,
[
"--username",
"[email protected]",
"--password",
"password1",
"--recent",
"0",
"--auto-delete",
"-d",
base_dir,
],
)

self.assertIn("DEBUG Looking up all photos and videos from album All Photos...", self._caplog.text)
self.assertIn(
f"INFO Downloading 0 original photos and videos to {base_dir} ...",
self._caplog.text,
)
self.assertIn(
f"INFO All photos have been downloaded!", self._caplog.text
)
self.assertIn(
f"INFO Deleting any files found in 'Recently Deleted'...",
self._caplog.text,
)

self.assertIn(
f"INFO Deleting {os.path.join(base_dir, os.path.normpath(files[0]))}",
self._caplog.text,
)

for file_name in files:
assert not os.path.exists(os.path.join(base_dir, file_name)), f"{file_name} not expected, but present"

def test_autodelete_photos(self):
base_dir = os.path.normpath(f"tests/fixtures/Photos/{inspect.stack()[0][3]}")
if os.path.exists(base_dir):
Expand All @@ -26,16 +220,17 @@ def test_autodelete_photos(self):
"2018/07/30/IMG_7407.JPG",
"2018/07/30/IMG_7407-original.JPG"
]

files_to_delete = [
"2018/07/30/IMG_7406.MOV",
"2018/07/26/IMG_7383.PNG",
"2018/07/12/IMG_7190.JPG",
"2018/07/12/IMG_7190-medium.JPG"
f"{'{:%Y/%m/%d}'.format(datetime.datetime.fromtimestamp(1532940539000.0 / 1000.0, tz=pytz.utc).astimezone(get_localzone()))}/IMG_7406.MOV",
f"{'{:%Y/%m/%d}'.format(datetime.datetime.fromtimestamp(1532618424000.0 / 1000.0, tz=pytz.utc).astimezone(get_localzone()))}/IMG_7383.PNG",
f"{'{:%Y/%m/%d}'.format(datetime.datetime.fromtimestamp(1531371164630.0 / 1000.0, tz=pytz.utc).astimezone(get_localzone()))}/IMG_7190.JPG",
f"{'{:%Y/%m/%d}'.format(datetime.datetime.fromtimestamp(1531371164630.0 / 1000.0, tz=pytz.utc).astimezone(get_localzone()))}/IMG_7190-medium.JPG"
]

os.makedirs(os.path.join(base_dir, "2018/07/30/"))
os.makedirs(os.path.join(base_dir, "2018/07/26/"))
os.makedirs(os.path.join(base_dir, "2018/07/12/"))
os.makedirs(os.path.join(base_dir, f"{'{:%Y/%m/%d}'.format(datetime.datetime.fromtimestamp(1532940539000.0 / 1000.0, tz=pytz.utc).astimezone(get_localzone()))}/"))
os.makedirs(os.path.join(base_dir, f"{'{:%Y/%m/%d}'.format(datetime.datetime.fromtimestamp(1532618424000.0 / 1000.0, tz=pytz.utc).astimezone(get_localzone()))}/"))
os.makedirs(os.path.join(base_dir, f"{'{:%Y/%m/%d}'.format(datetime.datetime.fromtimestamp(1531371164630.0 / 1000.0, tz=pytz.utc).astimezone(get_localzone()))}/"))

# create some empty files
for file_name in files_to_create + files_to_delete:
Expand Down Expand Up @@ -80,19 +275,19 @@ def test_autodelete_photos(self):
)

self.assertIn(
f"INFO Deleting {os.path.join(base_dir, os.path.normpath('2018/07/30/IMG_7406.MOV'))}",
f"INFO Deleting {os.path.join(base_dir, os.path.normpath(files_to_delete[0]))}",
self._caplog.text,
)
self.assertIn(
f"INFO Deleting {os.path.join(base_dir, os.path.normpath('2018/07/26/IMG_7383.PNG'))}",
f"INFO Deleting {os.path.join(base_dir, os.path.normpath(files_to_delete[1]))}",
self._caplog.text,
)
self.assertIn(
f"INFO Deleting {os.path.join(base_dir, os.path.normpath('2018/07/12/IMG_7190.JPG'))}",
f"INFO Deleting {os.path.join(base_dir, os.path.normpath(files_to_delete[2]))}",
self._caplog.text,
)
self.assertIn(
f"INFO Deleting {os.path.join(base_dir, os.path.normpath('2018/07/12/IMG_7190-medium.JPG'))}",
f"INFO Deleting {os.path.join(base_dir, os.path.normpath(files_to_delete[3]))}",
self._caplog.text,
)

Expand Down
Loading