-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_download_live_photos.py
267 lines (231 loc) · 9.94 KB
/
test_download_live_photos.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
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 PyiCloudAPIResponseException
from requests.exceptions import ConnectionError
from icloudpd.base import main
from tests.helpers import path_from_project_root, print_result_exception, recreate_path
import inspect
import glob
vcr = VCR(decode_compressed_response=True)
class DownloadLivePhotoTestCase(TestCase):
@pytest.fixture(autouse=True)
def inject_fixtures(self, caplog):
self._caplog = caplog
self.root_path = path_from_project_root(__file__)
self.fixtures_path = os.path.join(self.root_path, "fixtures")
self.vcr_path = os.path.join(self.root_path, "vcr_cassettes")
def test_skip_existing_downloads_for_live_photos(self):
base_dir = os.path.join(self.fixtures_path, inspect.stack()[0][3])
cookie_dir = os.path.join(base_dir, "cookie")
data_dir = os.path.join(base_dir, "data")
for dir in [base_dir, cookie_dir, data_dir]:
recreate_path(dir)
files_to_download = [
"2020/11/04/IMG_0516.HEIC",
"2020/11/04/IMG_0514.HEIC",
"2020/11/04/IMG_0514_HEVC.MOV",
"2020/11/04/IMG_0512.HEIC",
"2020/11/04/IMG_0512_HEVC.MOV"
]
with vcr.use_cassette(os.path.join(self.vcr_path, "download_live_photos.yml")):
# Pass fixed client ID via environment variable
runner = CliRunner(env={
"CLIENT_ID": "DE309E26-942E-11E8-92F5-14109FE0B321"
})
result = runner.invoke(
main,
[
"--username",
"--password",
"password1",
"--recent",
"3",
"--no-progress-bar",
"--threads-num",
1,
"-d",
data_dir,
"--cookie-directory",
cookie_dir
],
)
print_result_exception(result)
self.assertIn(
f"DEBUG Downloading {os.path.join(data_dir, os.path.normpath('2020/11/04/IMG_0514_HEVC.MOV'))}",
self._caplog.text,
)
self.assertIn(
f"DEBUG Downloading {os.path.join(data_dir, os.path.normpath('2020/11/04/IMG_0514.HEIC'))}",
self._caplog.text,
)
self.assertIn(
f"DEBUG Downloading {os.path.join(data_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
files_in_result = glob.glob(os.path.join(data_dir, "**/*.*"), recursive=True)
assert sum(1 for _ in files_in_result) == len(files_to_download)
for file_name in files_to_download:
assert os.path.exists(os.path.join(data_dir, os.path.normpath(file_name))), f"file {file_name} expected, but not found"
def test_skip_existing_live_photodownloads(self):
base_dir = os.path.join(self.fixtures_path, inspect.stack()[0][3])
cookie_dir = os.path.join(base_dir, "cookie")
data_dir = os.path.join(base_dir, "data")
for dir in [base_dir, cookie_dir, data_dir]:
recreate_path(dir)
files_to_create = [
("2020/11/04/IMG_0516.HEIC", 1651485),
("2020/11/04/IMG_0514_HEVC.MOV", 3951774),
]
files_to_download = [
"2020/11/04/IMG_0514.HEIC",
"2020/11/04/IMG_0512.HEIC",
"2020/11/04/IMG_0512_HEVC.MOV"
]
# simulate that some expected files are there with correct sizes
os.makedirs(os.path.join(data_dir, "2020/11/04"))
# one photo and one movie are already there and should be skipped
# Create dummies with the correct size
for (file_name, file_size) in files_to_create:
with open(os.path.join(data_dir, file_name), "a") as f:
f.truncate(file_size)
with vcr.use_cassette(os.path.join(self.vcr_path, "download_live_photos.yml")):
# Pass fixed client ID via environment variable
runner = CliRunner(env={
"CLIENT_ID": "DE309E26-942E-11E8-92F5-14109FE0B321"
})
result = runner.invoke(
main,
[
"--username",
"--password",
"password1",
"--recent",
"3",
"--no-progress-bar",
"--threads-num",
1,
"-d",
data_dir,
"--cookie-directory",
cookie_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 {data_dir} ...",
self._caplog.text,
)
self.assertIn(
f"DEBUG Downloading {os.path.join(data_dir, os.path.normpath('2020/11/04/IMG_0514.HEIC'))}",
self._caplog.text,
)
self.assertIn(
f"DEBUG {os.path.join(data_dir, os.path.normpath('2020/11/04/IMG_0514_HEVC.MOV'))} already exists",
self._caplog.text,
)
self.assertIn(
f"DEBUG Downloading {os.path.join(data_dir, os.path.normpath('2020/11/04/IMG_0514.HEIC'))}",
self._caplog.text,
)
self.assertIn(
f"DEBUG {os.path.join(data_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
files_in_result = glob.glob(os.path.join(data_dir, "**/*.*"), recursive=True)
assert sum(1 for _ in files_in_result) == len(files_to_download) + len(files_to_create)
for file_name in files_to_download + ([file_name for (file_name, _) in files_to_create]):
assert os.path.exists(os.path.join(data_dir, os.path.normpath(file_name))), f"file {file_name} expected, but not found"
def test_skip_existing_live_photo_print_filenames(self):
base_dir = os.path.join(self.fixtures_path, inspect.stack()[0][3])
cookie_dir = os.path.join(base_dir, "cookie")
data_dir = os.path.join(base_dir, "data")
for dir in [base_dir, cookie_dir, data_dir]:
recreate_path(dir)
files_to_create = [
("2020/11/04/IMG_0516.HEIC", 1651485),
("2020/11/04/IMG_0514_HEVC.MOV", 3951774),
]
files_to_download = [
"2020/11/04/IMG_0514.HEIC",
"2020/11/04/IMG_0512.HEIC",
"2020/11/04/IMG_0512_HEVC.MOV"
]
# simulate that some expected files are there with correct sizes
os.makedirs(os.path.join(data_dir, "2020/11/04"))
# one photo and one movie are already there and should be skipped
# Create dummies with the correct size
for (file_name, file_size) in files_to_create:
with open(os.path.join(data_dir, file_name), "a") as f:
f.truncate(file_size)
with vcr.use_cassette(os.path.join(self.vcr_path, "download_live_photos.yml")):
# Pass fixed client ID via environment variable
runner = CliRunner(env={
"CLIENT_ID": "DE309E26-942E-11E8-92F5-14109FE0B321"
})
result = runner.invoke(
main,
[
"--username",
"--password",
"password1",
"--recent",
"3",
"--no-progress-bar",
"--threads-num",
1,
"--only-print-filenames",
"-d",
data_dir,
"--cookie-directory",
cookie_dir,
],
)
print_result_exception(result)
filenames = result.output.splitlines()
print (filenames)
assert len(filenames) == 3
self.assertEqual(
os.path.join(data_dir, os.path.normpath("2020/11/04/IMG_0514.HEIC")),
filenames[0]
)
self.assertEqual(
os.path.join(data_dir, os.path.normpath("2020/11/04/IMG_0512.HEIC")),
filenames[1]
)
self.assertEqual(
os.path.join(data_dir, os.path.normpath("2020/11/04/IMG_0512_HEVC.MOV")),
filenames[2]
)
# Double check that a mocked file does not get listed again. It's already there!
assert "2020/11/04/IMG_0514_HEVC.MOV" not in filenames
assert result.exit_code == 0
files_in_result = glob.glob(os.path.join(data_dir, "**/*.*"), recursive=True)
assert sum(1 for _ in files_in_result) == len(files_to_create)
for file_name in ([file_name for (file_name, _) in files_to_create]):
assert os.path.exists(os.path.join(data_dir, os.path.normpath(file_name))), f"file {file_name} expected, but not found"