Skip to content

Commit

Permalink
Merge pull request #201 from menkej/fix-200
Browse files Browse the repository at this point in the history
fixes #200, --only-print-filenames (again)
  • Loading branch information
menkej authored Nov 5, 2020
2 parents 0839acd + 9c58a4e commit 60ff108
Show file tree
Hide file tree
Showing 4 changed files with 245,876 additions and 1 deletion.
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

- fix: --only-print-filenames option displays filenames (live photos) of files that have already been downloaded #200
- fix: docker works on Windows #192

## 1.7.0 (2020-11-1)
Expand Down
2 changes: 1 addition & 1 deletion icloudpd/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ def download_photo(counter, photo):

lp_file_exists = os.path.isfile(lp_download_path)

if only_print_filenames:
if only_print_filenames and not lp_file_exists:
print(lp_download_path)
else:
if lp_file_exists:
Expand Down
195 changes: 195 additions & 0 deletions tests/test_download_live_photos.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
from unittest import TestCase
from vcr import VCR
import os
import sys
import shutil
import pytest
import mock
import datetime
from mock import call, ANY
from click.testing import CliRunner
import piexif
from piexif._exceptions import InvalidImageDataError
from pyicloud_ipd.services.photos import PhotoAsset, PhotoAlbum, PhotosService
from pyicloud_ipd.base import PyiCloudService
from pyicloud_ipd.exceptions import PyiCloudAPIResponseError
from requests.exceptions import ConnectionError
from icloudpd.base import main
from tests.helpers.print_result_exception import print_result_exception

vcr = VCR(decode_compressed_response=True)

class DownloadLivePhotoTestCase(TestCase):
@pytest.fixture(autouse=True)
def inject_fixtures(self, caplog):
self._caplog = caplog

def test_skip_existing_downloads_for_live_photos(self):
base_dir = os.path.normpath("tests/fixtures/Photos")
if os.path.exists(base_dir):
shutil.rmtree(base_dir)
os.makedirs(base_dir)

with vcr.use_cassette("tests/vcr_cassettes/download_live_photos.yml"):
# Pass fixed client ID via environment variable
os.environ["CLIENT_ID"] = "DE309E26-942E-11E8-92F5-14109FE0B321"
runner = CliRunner()
result = runner.invoke(
main,
[
"--username",
"[email protected]",
"--password",
"password1",
"--recent",
"3",
"--no-progress-bar",
"--threads-num",
1,
"-d",
base_dir,
],
)
print_result_exception(result)

self.assertIn(
f"INFO Downloading {os.path.join(base_dir, os.path.normpath('2020/11/04/IMG_0514_HEVC.MOV'))}",
self._caplog.text,
)
self.assertIn(
f"INFO Downloading {os.path.join(base_dir, os.path.normpath('2020/11/04/IMG_0514.HEIC'))}",
self._caplog.text,
)
self.assertIn(
f"INFO Downloading {os.path.join(base_dir, os.path.normpath('2020/11/04/IMG_0516.HEIC'))}",
self._caplog.text,
)
self.assertIn(
"INFO All photos have been downloaded!", self._caplog.text
)
assert result.exit_code == 0

def test_skip_existing_live_photodownloads(self):
base_dir = os.path.normpath("tests/fixtures/Photos")
if os.path.exists(base_dir):
shutil.rmtree(base_dir)
os.makedirs(base_dir)

# simulate that some of the expected files are there with corret sizes
os.makedirs(os.path.join(base_dir, "2020/11/04"))
# one photo and one movie are already there and should be skipped
# Create dummies with the correct size
with open(os.path.join(base_dir, "2020/11/04/IMG_0516.HEIC"), "a") as f:
f.truncate(1651485)
with open(os.path.join(base_dir, "2020/11/04/IMG_0514_HEVC.MOV"), "a") as f:
f.truncate(3951774)

with vcr.use_cassette("tests/vcr_cassettes/download_live_photos.yml"):
# Pass fixed client ID via environment variable
os.environ["CLIENT_ID"] = "DE309E26-942E-11E8-92F5-14109FE0B321"
runner = CliRunner()
result = runner.invoke(
main,
[
"--username",
"[email protected]",
"--password",
"password1",
"--recent",
"3",
"--no-progress-bar",
"--threads-num",
1,
"-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 3 original photos and videos to tests/fixtures/Photos ...",
self._caplog.text,
)
self.assertIn(
f"INFO Downloading {os.path.join(base_dir, os.path.normpath('2020/11/04/IMG_0514.HEIC'))}",
self._caplog.text,
)
self.assertIn(
f"INFO {os.path.join(base_dir, os.path.normpath('2020/11/04/IMG_0514_HEVC.MOV'))} already exists.",
self._caplog.text,
)
self.assertIn(
f"INFO Downloading {os.path.join(base_dir, os.path.normpath('2020/11/04/IMG_0514.HEIC'))}",
self._caplog.text,
)
self.assertIn(
f"INFO {os.path.join(base_dir, os.path.normpath('2020/11/04/IMG_0516.HEIC'))} already exists.",
self._caplog.text,
)
self.assertIn(
"INFO All photos have been downloaded!", self._caplog.text
)
assert result.exit_code == 0

def test_skip_existing_live_photo_print_filenames(self):
base_dir = os.path.normpath("tests/fixtures/Photos")
if os.path.exists(base_dir):
shutil.rmtree(base_dir)
os.makedirs(base_dir)

# simulate that some of the expected files are there with corret sizes
os.makedirs(os.path.join(base_dir, "2020/11/04"))
# one photo and one movie are already there and should be skipped
# Create dummies with the correct size
with open(os.path.join(base_dir, "2020/11/04/IMG_0516.HEIC"), "a") as f:
f.truncate(1651485)
with open(os.path.join(base_dir, "2020/11/04/IMG_0514_HEVC.MOV"), "a") as f:
f.truncate(3951774)

with vcr.use_cassette("tests/vcr_cassettes/download_live_photos.yml"):
# Pass fixed client ID via environment variable
os.environ["CLIENT_ID"] = "DE309E26-942E-11E8-92F5-14109FE0B321"
runner = CliRunner()
result = runner.invoke(
main,
[
"--username",
"[email protected]",
"--password",
"password1",
"--recent",
"3",
"--no-progress-bar",
"--threads-num",
1,
"--only-print-filenames",
"-d",
base_dir,
],
)
print_result_exception(result)

filenames = result.output.splitlines()

print (filenames)

self.assertEqual(
os.path.join(base_dir, os.path.normpath("2020/11/04/IMG_0514.HEIC")),
filenames[0]
)
self.assertEqual(
os.path.join(base_dir, os.path.normpath("2020/11/04/IMG_0512.HEIC")),
filenames[1]
)
self.assertEqual(
os.path.join(base_dir, os.path.normpath("2020/11/04/IMG_0512_HEVC.MOV")),
filenames[2]
)

# Double check that a mocked file does not get listed again. Its already there!
assert "2020/11/04/IMG_0514_HEVC.MOV" not in filenames

assert result.exit_code == 0
Loading

0 comments on commit 60ff108

Please sign in to comment.