Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New migrate-all-albums #8

Merged
merged 1 commit into from
Jul 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 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,11 @@ 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 1000 albums cap with the following :
```
python -m ppim-migrator migrate-all-albums --count 5000
```
7 changes: 6 additions & 1 deletion 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__":
if __name__ == "__main__":
cli()
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&offset=0&q=&category=&order=favorites&year=&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
31 changes: 31 additions & 0 deletions ppim-migrator/migrator.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,37 @@ def __init__(self) -> None:
self.im_api = ImmichApi(
config.config["immich"]["base_url"], config.config["immich"]["api_key"]
)

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


# Migrate one by one...
i = 1
album_num = len(data)
for album in data:
click.echo(f"-------------- ({i}/{album_num})")
i += 1
uid = album["UID"]
album_title = album["Title"]
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}") # Too verbose...

matching_uids = self._get_matching_uids(photo_file_list)
matches_uids = matching_uids.get("uids")
files_not_found = matching_uids.get("files_not_found")
self._summary(matches_uids=matches_uids, files_not_found=files_not_found)

click.echo("Creating album...")

self.im_api.create_album(albumName=album_title, assetIds=matches_uids)
click.echo("--------------")
click.echo("Done.")


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