Skip to content

Commit

Permalink
Merge pull request #43 from Salvoxia/feat-FindAssetsInAlbum
Browse files Browse the repository at this point in the history
Feat find assets in album
  • Loading branch information
Salvoxia authored Sep 7, 2024
2 parents 4d6443e + 11c6a47 commit 901463d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
31 changes: 22 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ This script is mostly based on the following original script: [REDVM/immich_auto
4. [How It Works (with Examples)](#how-it-works)
5. [Automatic Album Sharing](#automatic-album-sharing)
6. [Cleaning Up Albums](#cleaning-up-albums)
7. [Dealing with External Library Changes](#dealing-with-external-library-changes)
7. [Assets in Multiple Albums](#assets-in-multiple-albums)
8. [Dealing with External Library Changes](#dealing-with-external-library-changes)

## Usage
### Bare Python Script
Expand All @@ -37,7 +38,7 @@ This script is mostly based on the following original script: [REDVM/immich_auto
3. Run the script
```
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}]
[-S {0,1,2}] [-O {False,asc,desc}]
[-S {0,1,2}] [-O {False,asc,desc}] [-A]
root_path api_url api_key

Create Immich Albums from an external library path based on the top level folders
Expand Down Expand Up @@ -80,6 +81,8 @@ This script is mostly based on the following original script: [REDVM/immich_auto
offline asset removal (REQUIRES API KEY OF AN ADMIN USER!) (default: 0)
-O {False,asc,desc}, --album-order {False,asc,desc}
Set sorting order for newly created albums to newest or oldest file first, Immich defaults to newest file first (default: False)
-A, --find-assets-in-albums
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)
```
__Plain example without optional arguments:__
Expand Down Expand Up @@ -113,6 +116,7 @@ The environment variables are analoguous to the script's command line arguments.
| 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` |
| 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). |
| 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` |
| 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). |

#### Run the container with Docker

Expand Down Expand Up @@ -188,13 +192,16 @@ Suppose you provide an external library to Immich under the path `/external_libs
The folder structure of `photos` might look like this:
```
/external_libs/photos/2020
/external_libs/photos/2020/02 Feb
/external_libs/photos/2020/02 Feb/Vacation
/external_libs/photos/2020/08 Aug/Vacation
/external_libs/photos/Birthdays/John
/external_libs/photos/Birthdays/Jane
/external_libs/photos/Skiing 2023
/external_libs/photos/
├── 2020/
│ ├── 02 Feb/
│ │ └── Vacation/
│ ├── 08 Aug/
│ │ └── Vacation/
├── Birthdays/
│ ├── John/
│ └── Jane/
└── Skiing 2023/
```
Albums created for `root_path = /external_libs/photos` (`--album-levels` is implicitly set to `1`):
Expand Down Expand Up @@ -324,6 +331,12 @@ The script will generate album names using the script's arguments and the assets
> As the name suggests, this mode blindly deletes **ALL** albums from Immich. Use with caution!
## Assets in Multiple Albums
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.
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.
## Dealing with External Library Changes
Due to their nature, external libraries may be changed by the user without Immich having any say in it.
Expand Down
4 changes: 4 additions & 0 deletions docker/immich_auto_album.sh
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,9 @@ if [ ! -z "$ALBUM_ORDER" ]; then
args="-O $ALBUM_ORDER $args"
fi

if [ ! -z "$FIND_ASSETS_IN_ALBUMS" ]; then
args="-A $args"
fi

BASEDIR=$(dirname "$0")
echo $args | xargs python3 -u $BASEDIR/immich_auto_album.py
5 changes: 4 additions & 1 deletion immich_auto_album.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def is_integer(str):
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.")
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!)")
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")
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.")

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

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

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

0 comments on commit 901463d

Please sign in to comment.