Skip to content

Commit

Permalink
Feature: Unarchive assets in CLEANUP or DELETE_ALL modes
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Salvoxia committed Sep 27, 2024
1 parent 2f0a7c5 commit 8ba71a4
Showing 1 changed file with 26 additions and 6 deletions.
32 changes: 26 additions & 6 deletions immich_auto_album.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def glob_to_re(pattern):
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.")


Expand Down Expand Up @@ -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
----------
Expand All @@ -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:
Expand Down Expand Up @@ -769,8 +773,16 @@ def archiveAssets(assetIds: list[str]):
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)

Expand Down Expand Up @@ -854,9 +866,17 @@ def archiveAssets(assetIds: list[str]):
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)

Expand Down Expand Up @@ -949,7 +969,7 @@ def archiveAssets(assetIds: list[str]):

# 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))


Expand Down

0 comments on commit 8ba71a4

Please sign in to comment.