Post

How to Create Albums on Nextcloud Using Metadata from Google Takeout

How to Create Albums on Nextcloud Using Metadata from Google Takeout

In this blog post, I’ll walk you through how to create albums on Nextcloud using metadata extracted from a Google Takeout after you managed to migrate your photos from Google to Nextcloud. This is particularly useful if you have a large collection of photos and metadata exported from Google, and you want to organize and upload them to Nextcloud for easier access. The solution uses a simple bash script that automates the process of creating albums and associating photos with the corresponding album titles.


🧠 Understanding the Process

Before diving into the script itself, it’s important to understand the workflow and how the different components interact.

🗃️ Google Takeout and Metadata

Google Takeout allows users to export their data from various Google services, including Google Photos. When you export your photos from Google, the metadata for each photo, such as the title, creation date, and file information, is included in JSON files. These JSON files contain information about the photos that can be useful for organizing them into albums.

📦 Nextcloud and Docker Setup

Nextcloud is an open-source platform for file synchronization and sharing. In this case, Nextcloud is used to store and organize your photos, while Docker is used to run the necessary commands within the Nextcloud container. The Nextcloud Photos app allows you to organize images into albums, and this is what we’ll be using to automate album creation.


📝 The Bash Script

Here’s the bash script that helps automate the process of creating albums on Nextcloud using metadata from Google Takeout.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/bin/bash

USER=$2

# Function to process each directory
process_directory() {
  local directory="$1"

  # Check if a Metadaten.json exists
  if [[ -f "$directory/Metadaten.json" ]]; then
    # Read the "title" from the Metadaten.json
    local title=$(jq -r '.title' "$directory/Metadaten.json")
    
    if [[ -z "$title" ]]; then
      #echo "No title found in Metadaten.json in $directory. Skipping..."
      return
    fi

    # Execute Docker command to create the album
    echo "Creating album with title: $title"
    
    docker exec -u www-data -it nextcloud-aio-nextcloud php occ photos:album:create $USER "$title"

    # Process all JSON files in the directory
    for json_file in "$directory"/*.json; do
      # Skip Metadaten.json
      if [[ "$json_file" == "$directory/Metadaten.json" ]]; then
        continue
      fi
      
      # Remove the postfix ".supplemental-metadata.json"
      local filename=$(basename "$json_file")
      local year_month=$(jq -r '.photoTakenTime.formatted | strptime("%d.%m.%Y, %H:%M:%S UTC") | strftime("%Y/%m")' "$json_file")
      local cleaned_filename="${filename%.supp*.json}"

      # Search for the relative path of the file under /srv/nextcloud/data/manuel/files/Photos/
      local file_path=$(find "/srv/nextcloud/data/$USER/files/Photos/$year_month" -name "$cleaned_filename")
      
      # Check if the file exists
      if [[ -f "$file_path" ]]; then
        # Execute Docker command to add the photo to the album
        local trimmed_path="${file_path#"/srv/nextcloud/data/$USER/files/"}"  
        #echo "Adding photo with title: $title and path: $trimmed_path to the album."
        docker exec -u www-data -it nextcloud-aio-nextcloud php occ photos:album:add $USER "$title" "$trimmed_path"
      else
        echo "File $cleaned_filename not found. Skipping..."
      fi
    done
  fi
}

# Recursively go through all directories and process them
export -f process_directory

find $1 -type d -exec bash -c 'process_directory "$0"' {} \;

🔍 Breakdown of the Script

1. Processing the Directory

The script starts by iterating over the directories in the provided directory path. Each directory should contain a Metadaten.json file that contains metadata about the photos in that directory.

If you do have an account language other than German, your JSON file is probably named differently!

1
2
process_directory() {
  local directory="$1"

2. Extracting Metadata

If the Metadaten.json file is found, it extracts the album title using the jq command:

1
local title=$(jq -r '.title' "$directory/Metadaten.json")

If no title is found, the script skips that directory.

3. Creating the Album

The script then creates an album in Nextcloud using the photos:album:create command:

1
docker exec -u www-data -it nextcloud-aio-nextcloud php occ photos:album:create $USER "$title"

This command uses the occ (OwnCloud Console) to create the album for the specified user.

4. Adding Photos to the Album

The script then processes each .json file in the directory (excluding Metadaten.json). For each .json file, it extracts the timestamp for when the photo was taken and searches for the corresponding photo file in the Nextcloud server.

You need to check whether the given date format from the script applies to your JSON files too!

1
local file_path=$(find "/srv/nextcloud/data/$USER/files/Photos/$year_month" -name "$cleaned_filename")

If the photo file is found, it adds the photo to the album using the photos:album:add command.


⚙️ Setting Up the Script

Before running the script, you need to have a working Nextcloud setup with the Photos app enabled. You also need Docker to run Nextcloud commands inside the container. Here’s how you can set up your environment:

  1. Install Nextcloud (if you don’t have it already): Follow the Nextcloud installation guide.
  2. Enable the Photos/Memories app: Ensure that the Nextcloud Photos app is enabled. You can alternatively also use the Memories app which is probably superior to the Photos app.
  3. Install jq: The script uses jq to parse JSON files. Install it using your package manager (e.g., sudo apt install jq).
  4. Run the script: Once everything is set up, run the script by providing the path to the directory containing your Google Takeout metadata.

🧪 Run the Script

After setting up everything, you just need to copy the JSON files from the Google takeout onto your server/container hosting Nextcloud and run the script:

1
./create_albums.sh manuel takeout

In this example, the script is executed for the user manuel and the Google takeout is stored in a local folder called takeout.


🧠 Final Thoughts

Using automation to organize and upload your photos to Nextcloud can save you a lot of time, especially if you have a large collection of photos and metadata. The script provided here uses metadata from Google Takeout to create albums and add photos automatically. This is a great way to take control of your photo organization while keeping your data secure and accessible on Nextcloud.

If you encounter any issues with the script, ensure that your Nextcloud instance is running correctly and that the paths to the photos are accurate. You may also need to adjust the script to fit your particular Nextcloud setup, depending on the version or specific configuration you’re using. Happy organizing!

This post is licensed under CC BY 4.0 by the author.