Skip to content

Commit 901463d

Browse files
authored
Merge pull request #43 from Salvoxia/feat-FindAssetsInAlbum
Feat find assets in album
2 parents 4d6443e + 11c6a47 commit 901463d

File tree

3 files changed

+30
-10
lines changed

3 files changed

+30
-10
lines changed

README.md

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ This script is mostly based on the following original script: [REDVM/immich_auto
2121
4. [How It Works (with Examples)](#how-it-works)
2222
5. [Automatic Album Sharing](#automatic-album-sharing)
2323
6. [Cleaning Up Albums](#cleaning-up-albums)
24-
7. [Dealing with External Library Changes](#dealing-with-external-library-changes)
24+
7. [Assets in Multiple Albums](#assets-in-multiple-albums)
25+
8. [Dealing with External Library Changes](#dealing-with-external-library-changes)
2526

2627
## Usage
2728
### Bare Python Script
@@ -37,7 +38,7 @@ This script is mostly based on the following original script: [REDVM/immich_auto
3738
3. Run the script
3839
```
3940
usage: immich_auto_album.py [-h] [-r ROOT_PATH] [-u] [-a ALBUM_LEVELS] [-s ALBUM_SEPARATOR] [-c CHUNK_SIZE] [-C FETCH_CHUNK_SIZE] [-l {CRITICAL,ERROR,WARNING,INFO,DEBUG}] [-k] [-i IGNORE] [-m {CREATE,CLEANUP,DELETE_ALL}] [-d] [-x SHARE_WITH] [-o {viewer,editor}]
40-
[-S {0,1,2}] [-O {False,asc,desc}]
41+
[-S {0,1,2}] [-O {False,asc,desc}] [-A]
4142
root_path api_url api_key
4243

4344
Create Immich Albums from an external library path based on the top level folders
@@ -80,6 +81,8 @@ This script is mostly based on the following original script: [REDVM/immich_auto
8081
offline asset removal (REQUIRES API KEY OF AN ADMIN USER!) (default: 0)
8182
-O {False,asc,desc}, --album-order {False,asc,desc}
8283
Set sorting order for newly created albums to newest or oldest file first, Immich defaults to newest file first (default: False)
84+
-A, --find-assets-in-albums
85+
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. (default: False)
8386
```
8487
8588
__Plain example without optional arguments:__
@@ -113,6 +116,7 @@ The environment variables are analoguous to the script's command line arguments.
113116
| SHARE_ROLE | no | The role for users newly created albums are shared with. Only effective if `SHARE_WITH` is not empty and no explicit share role was specified for at least one user. (default: viewer), allowed values: `viewer`, `editor` |
114117
| SYNC_MODE | no | Synchronization mode to use. Synchronization mode helps synchronizing changes in external libraries structures to Immich after albums have already been created. Possible Modes: <br>`0` = do nothing<br>`1` = Delete any empty albums<br>`2` = Trigger offline asset removal (REQUIRES API KEY OF AN ADMIN USER!)<br>(default: `0`)<br>Refer to [Dealing with External Library Changes](#dealing-with-external-library-changes). |
115118
| ALBUM_ORDER | no | Set sorting order for newly created albums to newest (`desc`) or oldest (`asc`) file first, Immich defaults to newest file first, allowed values: `asc`, `desc` |
119+
| FIND_ASSETS_IN_ALBUMS | no | 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. (default: `False`)<br>Refer to [Assets in Multiple Albums](#assets-in-multiple-albums). |
116120

117121
#### Run the container with Docker
118122

@@ -188,13 +192,16 @@ Suppose you provide an external library to Immich under the path `/external_libs
188192
The folder structure of `photos` might look like this:
189193
190194
```
191-
/external_libs/photos/2020
192-
/external_libs/photos/2020/02 Feb
193-
/external_libs/photos/2020/02 Feb/Vacation
194-
/external_libs/photos/2020/08 Aug/Vacation
195-
/external_libs/photos/Birthdays/John
196-
/external_libs/photos/Birthdays/Jane
197-
/external_libs/photos/Skiing 2023
195+
/external_libs/photos/
196+
├── 2020/
197+
│ ├── 02 Feb/
198+
│ │ └── Vacation/
199+
│ ├── 08 Aug/
200+
│ │ └── Vacation/
201+
├── Birthdays/
202+
│ ├── John/
203+
│ └── Jane/
204+
└── Skiing 2023/
198205
```
199206
200207
Albums created for `root_path = /external_libs/photos` (`--album-levels` is implicitly set to `1`):
@@ -324,6 +331,12 @@ The script will generate album names using the script's arguments and the assets
324331
> As the name suggests, this mode blindly deletes **ALL** albums from Immich. Use with caution!
325332
326333
334+
## Assets in Multiple Albums
335+
336+
By default, the script only fetches assets from Immich that are not assigned to any album yet. This makes querying assets in large libraries very fast. However, if assets should be part of either manually created albums as well as albums based on the folder structure, or if multiple script passes with different album level settings should create differently named albums with overlapping contents, the option `--find-assets-in-albums` (bare Python) or environment variable `FIND_ASSETS_IN_ALBUMS` (Docker) may be set.
337+
In that case, the script will request all assets from Immich and add them to their corresponding folders, even if the also are part of other albums.
338+
339+
327340
## Dealing with External Library Changes
328341
329342
Due to their nature, external libraries may be changed by the user without Immich having any say in it.

docker/immich_auto_album.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,5 +91,9 @@ if [ ! -z "$ALBUM_ORDER" ]; then
9191
args="-O $ALBUM_ORDER $args"
9292
fi
9393

94+
if [ ! -z "$FIND_ASSETS_IN_ALBUMS" ]; then
95+
args="-A $args"
96+
fi
97+
9498
BASEDIR=$(dirname "$0")
9599
echo $args | xargs python3 -u $BASEDIR/immich_auto_album.py

immich_auto_album.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ def is_integer(str):
5252
parser.add_argument("-o", "--share-role", default="viewer", choices=['viewer', 'editor'], help="The default share role for users newly created albums are shared with. Only effective if --share-with is specified at least once and the share role is not specified within --share-with.")
5353
parser.add_argument("-S", "--sync-mode", default=0, type=int, choices=[0, 1, 2], help="Synchronization mode to use. Synchronization mode helps synchronizing changes in external libraries structures to Immich after albums have already been created. Possible Modes: 0 = do nothing; 1 = Delete any empty albums; 2 = Trigger offline asset removal (REQUIRES API KEY OF AN ADMIN USER!)")
5454
parser.add_argument("-O", "--album-order", default=False, type=str, choices=[False, 'asc', 'desc'], help="Set sorting order for newly created albums to newest or oldest file first, Immich defaults to newest file first")
55+
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.")
5556

5657
args = vars(parser.parse_args())
5758
# set up logger to log in logfmt format
@@ -77,6 +78,7 @@ def is_integer(str):
7778
share_with = args["share_with"]
7879
share_role = args["share_role"]
7980
sync_mode = args["sync_mode"]
81+
find_assets_in_albums = args["find_assets_in_albums"]
8082

8183
# Override unattended if we're running in destructive mode
8284
if mode != SCRIPT_MODE_CREATE:
@@ -102,6 +104,7 @@ def is_integer(str):
102104
logging.debug("share_with = %s", share_with)
103105
logging.debug("share_role = %s", share_role)
104106
logging.debug("sync_mode = %d", sync_mode)
107+
logging.debug("find_assets_in_albums = %s", find_assets_in_albums)
105108

106109
# Verify album levels
107110
if is_integer(album_levels) and album_levels == 0:
@@ -566,7 +569,7 @@ def triggerOfflineAssetRemoval(libraryId: str):
566569
logging.info("Requesting all assets")
567570
# only request images that are not in any album if we are running in CREATE mode,
568571
# otherwise we need all images, even if they are part of an album
569-
if mode == SCRIPT_MODE_CREATE:
572+
if mode == SCRIPT_MODE_CREATE and not find_assets_in_albums:
570573
assets = fetchAssets(True)
571574
else:
572575
assets = fetchAssets(False)

0 commit comments

Comments
 (0)