Skip to content

Commit dbf7bc4

Browse files
remove unnecessary scanning of albums (#1093)
1 parent b7d877d commit dbf7bc4

25 files changed

+19
-12019
lines changed

src/icloudpd/autodelete.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def autodelete_photos(
4343
"""
4444
logger.info("Deleting any files found in 'Recently Deleted'...")
4545

46-
recently_deleted = library_object.albums["Recently Deleted"]
46+
recently_deleted = library_object.recently_deleted
4747

4848
for media in recently_deleted:
4949
try:

src/pyicloud_ipd/services/photos.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ class PhotoLibrary(object):
4242
"direction": "ASCENDING",
4343
"query_filter": None
4444
}
45+
RECENTLY_DELETED = {
46+
"obj_type": "CPLAssetDeletedByExpungedDate",
47+
"list_type": "CPLAssetAndMasterDeletedByExpungedDate",
48+
"direction": "ASCENDING",
49+
"query_filter": None
50+
}
4551
SMART_FOLDERS = {
4652
"Time-lapse": {
4753
"obj_type": "CPLAssetInSmartAlbumByAssetDate:Timelapse",
@@ -140,12 +146,7 @@ class PhotoLibrary(object):
140146
}
141147
}]
142148
},
143-
"Recently Deleted": {
144-
"obj_type": "CPLAssetDeletedByExpungedDate",
145-
"list_type": "CPLAssetAndMasterDeletedByExpungedDate",
146-
"direction": "ASCENDING",
147-
"query_filter": None
148-
},
149+
"Recently Deleted": RECENTLY_DELETED,
149150
"Hidden": {
150151
"obj_type": "CPLAssetHiddenByAssetDate",
151152
"list_type": "CPLAssetAndMasterHiddenByAssetDate",
@@ -162,6 +163,7 @@ def __init__(self, service: "PhotosService", zone_id: Dict[str, Any], library_ty
162163

163164
self._albums: Optional[Dict[str, PhotoAlbum]] = None
164165
self._all: Optional[PhotoAlbum] = None
166+
self._recently_deleted: Optional[PhotoAlbum] = None
165167

166168
url = ('%s/records/query?%s' %
167169
(self.service_endpoint, urlencode(self.service.params)))
@@ -243,10 +245,13 @@ def _fetch_folders(self) -> Sequence[Dict[str, Any]]:
243245
def all(self) -> "PhotoAlbum":
244246
if not self._all:
245247
self._all = PhotoAlbum(self.service, self.service_endpoint, "", zone_id=self.zone_id, **self.WHOLE_COLLECTION) # type: ignore[arg-type] # dynamically builing params
246-
# need to pull all albums because existing recorded tests expect all albums to be pulled...
247-
self.albums
248248
return self._all
249249

250+
@property
251+
def recently_deleted(self) -> "PhotoAlbum":
252+
if not self._recently_deleted:
253+
self._recently_deleted = PhotoAlbum(self.service, self.service_endpoint, "", zone_id=self.zone_id, **self.RECENTLY_DELETED) # type: ignore[arg-type] # dynamically builing params
254+
return self._recently_deleted
250255

251256
class PhotosService(PhotoLibrary):
252257
"""The 'Photos' iCloud service.

tests/test_download_photos.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,7 @@ def mocked_authenticate(self: PyiCloudService) -> None:
622622
self.assertEqual,
623623
self.root_path,
624624
base_dir,
625-
"listing_photos.yml",
625+
"listing_albums.yml",
626626
[],
627627
[],
628628
[

tests/test_download_photos_id.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,7 @@ def mocked_authenticate(self: PyiCloudService) -> None:
623623
self.assertEqual,
624624
self.root_path,
625625
base_dir,
626-
"listing_photos.yml",
626+
"listing_albums.yml",
627627
[],
628628
[],
629629
[

tests/test_listing_recent_photos.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -269,10 +269,10 @@ def test_listing_recent_photos_with_missing_downloadURL(self) -> None:
269269
mock_open.assert_called_once_with(
270270
file="icloudpd-photo-error.json", mode="w", encoding="utf8"
271271
)
272-
# Multiple JSON "dumps" occur with the new pyicloud 1.0.0 implementation
273-
# mock_json.assert_called_once()
272+
mock_json.assert_called()
273+
self.assertEqual(len(mock_json.call_args_list), 7)
274274
# Check a few keys in the dict
275-
first_arg = mock_json.call_args_list[7][0][0]
275+
first_arg = mock_json.call_args_list[6][0][0]
276276
self.assertEqual(
277277
first_arg["master_record"]["recordName"], "AY6c+BsE0jjaXx9tmVGJM1D2VcEO"
278278
)

tests/vcr_cassettes/autodelete_photos.yml

Lines changed: 0 additions & 638 deletions
Large diffs are not rendered by default.

tests/vcr_cassettes/autodelete_photos_heic.yml

Lines changed: 0 additions & 638 deletions
Large diffs are not rendered by default.

tests/vcr_cassettes/download_autodelete_photos.yml

Lines changed: 0 additions & 592 deletions
Large diffs are not rendered by default.

tests/vcr_cassettes/download_live_photos.yml

Lines changed: 0 additions & 616 deletions
Large diffs are not rendered by default.

tests/vcr_cassettes/listing_photos.yml

Lines changed: 0 additions & 595 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)