1
0
Fork 0

Feature: Unarchive assets in CLEANUP or DELETE_ALL modes

If --mode is set to CLEANUP or DELETE_ALL and --archive is set as well, unarchives all assets of all deleted albums to revert their state
This commit is contained in:
Salvoxia 2024-09-27 20:50:24 +02:00
parent 2f0a7c5223
commit 8ba71a43f1

View file

@ -89,7 +89,7 @@ parser.add_argument("-O", "--album-order", default=False, type=str, choices=[Fal
parser.add_argument("-A", "--find-assets-in-albums", action="store_true", help="By default, the script only finds assets that are not assigned to any album yet. Set this option to make the script discover assets that are already part of an album and handle them as usual. If --find-archived-assets is set as well, both options apply.")
parser.add_argument("-f", "--path-filter", action="append", help="Use either literals or glob-like patterns to filter assets before album name creation. This filter is evaluated before any values passed with --ignore. May be specified multiple times.")
parser.add_argument("--set-album-thumbnail", choices=ALBUM_THUMBNAIL_SETTINGS, help="Set first/last/random image as thumbnail for newly created albums or albums assets have been added to. If set to "+ALBUM_THUMBNAIL_RANDOM_FILTERED+", thumbnails are shuffled for all albums whose assets would not be filtered out or ignored by the ignore or path-filter options, even if no assets were added during the run. If set to "+ALBUM_THUMBNAIL_RANDOM_ALL+", the thumbnails for ALL albums will be shuffled on every run.")
parser.add_argument("-v", "--archive", action="store_true", help="Set this option to automatically archive all assets that were newly added to albums. Archiving hides the assets from Immich's timeline.")
parser.add_argument("-v", "--archive", action="store_true", help="Set this option to automatically archive all assets that were newly added to albums. If this option is set in combination with --mode = CLEANUP or DELETE_ALL, archived images of deleted albums will be unarchived. Archiving hides the assets from Immich's timeline.")
parser.add_argument("--find-archived-assets", action="store_true", help="By default, the script only finds assets that are not archived in Immich. Set this option to make the script discover assets that are already archived. If -A/--find-assets-in-albums is set as well, both options apply.")
@ -665,14 +665,16 @@ def setAlbumThumbnail(albumId: str, assetId: str):
r = requests.patch(root_url+apiEndpoint, json=data, **requests_kwargs)
r.raise_for_status()
def archiveAssets(assetIds: list[str]):
def setAssetsArchived(assetIds: list[str], isArchived: bool):
"""
Archives the assets identified by the passed list of UUIDs.
(Un-)Archives the assets identified by the passed list of UUIDs.
Parameters
----------
assetIds : list
A list of asset IDs to archive
isArchived : bool
Flag indicating whether to archive or unarchive the passed assets
Raises
----------
@ -682,11 +684,13 @@ def archiveAssets(assetIds: list[str]):
data = {
"ids": assetIds,
"isArchived": True
"isArchived": isArchived
}
r = requests.put(root_url+apiEndpoint, json=data, **requests_kwargs)
r.raise_for_status()
if r.status_code != 204:
logging.error("Error setting assets archived: %s", r.json())
r.raise_for_status()
if insecure:
@ -769,8 +773,16 @@ if mode == SCRIPT_MODE_DELETE_ALL:
cpt = 0
for album in albums:
if deleteAlbum(album):
# If the archived flag is set it means we need to unarchived all images of deleted albums;
# In order to do so, we need to fetch all assets of the album we're going to delete
assets_in_album = []
if archive:
assets_in_album = fetchAlbumAssets(album['id'])
logging.info("Deleted album %s", album['albumName'])
cpt += 1
if len(assets_in_album) > 0 and archive:
setAssetsArchived([asset['id'] for asset in assets_in_album], False)
logging.info("Unarchived %d assets", len(assets_in_album))
logging.info("Deleted %d/%d albums", cpt, len(albums))
exit(0)
@ -854,9 +866,17 @@ if mode == SCRIPT_MODE_CLEANUP:
else:
cpt = 0
for album_to_delete in albums_to_delete:
# If the archived flag is set it means we need to unarchived all images of deleted albums;
# In order to do so, we need to fetch all assets of the album we're going to delete
assets_in_album = []
if archive:
assets_in_album = fetchAlbumAssets(album_to_delete['id'])
if deleteAlbum(album_to_delete):
logging.info("Deleted album %s", album_to_delete['albumName'])
cpt += 1
if len(assets_in_album) > 0 and archive:
setAssetsArchived([asset['id'] for asset in assets_in_album], False)
logging.info("Unarchived %d assets", len(assets_in_album))
logging.info("Deleted %d/%d albums", cpt, len(album_to_assets))
exit(0)
@ -949,7 +969,7 @@ for album, assets in album_to_assets.items():
# Archive assets
if archive and len(asset_uuids_added) > 0:
archiveAssets(asset_uuids_added)
setAssetsArchived(asset_uuids_added, True)
logging.info("Archived %d assets", len(asset_uuids_added))