Salvoxia / immich-folder-album-creator

Automatically create and populate albums in Immich from a folder structure in external libraries
https://hub.docker.com/r/salvoxia/immich-folder-album-creator
222 stars 13 forks source link

Add option to name album by deepest folder only #7

Closed tobi-dub closed 7 months ago

tobi-dub commented 7 months ago

Very nice script, saved me a lot of manual work :)

I modified it a little bit to name the albums only by the deepest folder name. My album structure is like: 2020/2020-01/2020-01-01 2020/2020-03/2020-03-27

Could be nicer but this works fine:

logging.info("Sorting assets to corresponding albums using folder name")
album_to_assets = defaultdict(list)
deepest_folders = set()

for asset in assets:
    asset_path = asset['originalPath']
    if root_path not in asset_path:
        continue
    # Chunks of the asset's path below root_path
    path_chunks = asset_path.replace(root_path, '').split('/') 
    # A single chunk means it's just the image file in no sub folder, ignore
    if len(path_chunks) == 1:
        continue
    deepest_folder = path_chunks[-2]  # Extracting the deepest folder
    deepest_folders.add(deepest_folder)
    album_to_assets[deepest_folder].append(asset['id'])

logging.info("%d albums identified", len(deepest_folders))
logging.info("Album list: %s", list(deepest_folders))
if not unattended:
    print("Press Enter to continue, Ctrl+C to abort")
    input()

Could be integrated as second option to your already implemented album level naming.

Salvoxia commented 7 months ago

Thanks for the suggestion! I think with minimal tweaking the script could accept negative album_levels to achieve exactly that. Just played around with that and looks like it's working, but need to do proper validation first.

Salvoxia commented 7 months ago

@tobi-dub I just pushed some modifications to the script that will make it accept negative album_level values. By setting it to -1 you should be able to achieve the same result as with your modification. Would you be willing to test the current script in main in interactive mode?

tobi-dub commented 7 months ago

Thanks @Salvoxia ! I just tested with two different libraries and it works pretty fine!

Here an example output for the year 2003. The pics are stored in a folder structure like: years/2003/2003-06/2003-06-04 years/2003/2003-06/2003-06-11 .....

The output shows all deepest folders as expected :)

$ python3 ./immich_auto_album_new_main.py  -a -1 /usr/src/app/external/years http://127.0.0.1:2283/api xxxxxxxxxxx
time=2024-04-12T08:28:07.942+02:00 level=INFO msg=Requesting all assets
time=2024-04-12T08:28:16.981+02:00 level=INFO msg=69273 photos found
time=2024-04-12T08:28:16.981+02:00 level=INFO msg=Sorting assets to corresponding albums using folder name
time=2024-04-12T08:28:17.072+02:00 level=INFO msg=1016 albums identified
time=2024-04-12T08:28:17.072+02:00 level=INFO msg=Album list: ['2003-06-04', '2003-06-11', '2003-06-15', '2003-06-18', '2003-06-24', '2003-06-27', '2003-06-30', '2003-07-03', '2003-07-07', '2003-07-08', '2003-07-12', '2003-07-18', '2003-07-25', '2003-08-01', '2003-08-08', '2003-08-13', '2003-08-15', '2003-08-16', '2003-08-20', '2003-08-21', '2003-08-25', '2003-08-27', '2003-09-02', '2003-09-05']
Press Enter to continue, Ctrl+C to abort
Salvoxia commented 7 months ago

Great, thanks for testing and your idea! I'll release a new version for this during the weekend.

Salvoxia commented 7 months ago

Implemented in v0.4.0