Skip to content

Commit

Permalink
Merge pull request #9 from v411e/develop
Browse files Browse the repository at this point in the history
Add feature to migrate all albums at once (#8 by @ValentinVie)
  • Loading branch information
v411e authored Jul 14, 2024
2 parents d6adbc6 + 473adea commit 2552bb8
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 5 deletions.
3 changes: 2 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
"request": "launch",
"module": "ppim-migrator",
"args": [
//"migrate-album", "aqm7nj52giix3wq2"
"migrate-album",
"as6zq323u22kxvzb"
//"migrate-favorites"
// "migrate-all-albums",
// "--count=2"
],
},
]
Expand Down
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ This tool can migrate albums and favorites from Photoprism to Immich. It does no
3. It connects to immich, tries to find the photo by `filename` and validates potential candidates on the immich side by comparing the file `path`
4. If a match was found, it creates a new `album` in immich and adds the matched photos

### Migrate all albums
1. The tool fetches all albums from your photoprism instance (1000 albums by default)
2. For each album found, the migration is launched as described per the previous paragraph.

## Installation
```
pip install ppim-migrator
Expand Down Expand Up @@ -59,3 +63,12 @@ Example:
```
python -m ppim-migrator migrate-album <album-id-here>
```

### Migrate all albums from photoprism to immich
```
python -m ppim-migrator migrate-all-albums
```
You can overwrite the default 1000 albums cap, by adding a `--count=` option:
```
python -m ppim-migrator migrate-all-albums --count=5000
```
5 changes: 5 additions & 0 deletions ppim-migrator/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ def migrate_favorites():
migrator = Migrator()
migrator.migrate_favorites()

@cli.command()
@click.option('--count', default=1000, help='Number of albums to migrate.')
def migrate_all_albums(count):
migrator = Migrator()
migrator.migrate_all_albums(count)

if __name__ == "__main__":
cli()
5 changes: 3 additions & 2 deletions ppim-migrator/api/immich_api.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import requests
import json
import datetime


class ImmichApi:
Expand Down Expand Up @@ -28,8 +29,8 @@ def search_metadata(self, originalFileName: str) -> dict:
def create_album(
self,
albumName: str,
assetIds: list = [],
description: str = "Imported from photoprism",
assetIds = [],
description = f"Imported from photoprism ({datetime.datetime.now().strftime("%I:%M%p on %B %d, %Y")})",
) -> str:
url = f"{self.base_url}/api/albums"
payload = json.dumps(
Expand Down
12 changes: 12 additions & 0 deletions ppim-migrator/api/photoprism_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ def _get_photo_data(self, uid: str) -> dict:
json_response = response.json()
return json_response

# GET /api/v1/albums?count=100&type=album
def _get_all_albums_data(self, count: int = 1000) -> dict:
album_url = f"{self.base_url}/api/v1/albums?count={count}&type=album"
headers = {"X-Session-ID": self._get_session_id()}
response = requests.get(album_url, headers=headers)
json_response = response.json()
return json_response

# GET /api/v1/albums/:uid
def _get_album_data(self, uid: str) -> dict:
album_url = f"{self.base_url}/api/v1/albums/{uid}"
Expand Down Expand Up @@ -63,6 +71,10 @@ def _map_file_name(photos: list) -> list:
def _filter_sidecar_files(self, photo_files: list) -> list:
return [file for file in photo_files if not file["FileRoot"] == "sidecar"]

def get_all_albums_data(self, count: int = 1000) -> list:
albums_data = self._get_all_albums_data(count)
return albums_data

def get_photo_files_in_album(self, uid: str, count: int = 100000) -> list:
photos: list = self._get_photos_in_album(uid, count)
photos = self._filter_sidecar_files(photos)
Expand Down
23 changes: 21 additions & 2 deletions ppim-migrator/migrator.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,32 @@ def __init__(self) -> None:
config.config["immich"]["base_url"], config.config["immich"]["api_key"]
)

def migrate_all_albums(self, count: int = 1000):
click.echo(f"Migrating all albums (limit: {count})...")
data = self.pp_api.get_all_albums_data(count)

# Migrate one by one...
album_num = len(data)
for index, album in enumerate(data):
click.echo(f"-------------- ({index + 1}/{album_num})")
self._migrate_album(uid=album["UID"], album_title=album["Title"])

click.echo("--------------")
click.echo("Done.")

def migrate_album(self, uid):
click.echo(f"Migrating album with id {uid}")

album_title = self.pp_api.get_album_title(uid)
click.echo(f"Album title: {album_title}")

self._migrate_album(uid=uid, album_title=album_title)
click.echo("Done.")

def _migrate_album(self, uid: str, album_title: str):
click.echo(f"Migrating album with id {uid}")
click.echo(f"Album title: {album_title}")

photo_file_list = self.pp_api.get_photo_files_in_album(uid=uid)
click.echo(f"Photo file list: {photo_file_list}")

Expand All @@ -33,8 +54,6 @@ def migrate_album(self, uid):

self.im_api.create_album(albumName=album_title, assetIds=matches_uids)

click.echo("Done.")

def migrate_favorites(self):
click.echo("Migrating favorites")
photo_file_list = self.pp_api.get_photo_files_in_favorites()
Expand Down

0 comments on commit 2552bb8

Please sign in to comment.