google-photosgoogle-photos-api

Programmatically set a Google Photos caption


We have a big batch of photos that I want to put in Google Photos and I want them to all have their respective captions. By caption I mean that upper-right input in the slide-out Info (i) sidebar when viewing Google Photos on desktop web. By caption I also mean that white text that overlays images as you flip through them when the Info sidebar is closed. The captions are in json files from a big Flickr download, but that's not where the challenge lies. Here's what we've tried:

  1. Written various fields into the EXIF of the .jpgs themselves (using exiftool) before manual upload. I swear this used to work a couple of years ago. But now I only see the description in the "Other" field in the Info bar, not captions. Funny, I don't even remember this field from before.
  2. Used Google Photos API for updating a mediaItem from a Google Apps Script, after manually uploading EXIF-tagged .jpgs, to pluck the data in pre-existing photos from the "Other" field and tried to set the "description" field using that value. (I was surprised to see that what I was seeing in when viewed on desktop web as Other was already the description as seen in the API!) Worth a shot, but this was not allowed, because, quoting the reference link, "The media item must have been created by the developer via the API." Okay, fine, I got this, so I took a side trip...
  3. If it's got to be manual, I can do manual! I started coding some Javascript to run in the browser's developer console that would find the Other div, get the text, insert that text into the caption field, then find the Next button and send a click event to it. Everything worked, I even saw my text in the caption field but it would not save! when I switched to the next image like it does when you actually click it with a mouse. Never figured it out but I guess I'm not mad at Google for being hack-proof. Back on track...
  4. Thinking about that quote from the docs, I decided to tackle actually doing an API upload and set the description all in one go. I used Powershell on Windows, mostly because most of the work this script had to do was on my file system. I created Albums, uploaded files, and created mediaItems successfully, making sure to set the description field to what I wanted it to be and ... these descriptions still showed up as Other!

We're not excited about retyping all these. I'm beginning to think the Other field is "any description defined by anything other than fingers on your keyboard." How do we set the caption with code?!?!?!?!!?!?!?

What I don't want: What I don't want

What I do want: What I do want


Solution

  • I have had success setting the media item's description using Tanaike-san's library here: https://github.com/tanaikech/GPhotoApp I added "description" to the items list:

        const resource = {
        albumId: albumId,
        albumPosition: {
          "properties": {
            "position": {
              "enum": [
                "FIRST_IN_ALBUM",
              ]}}},
        items: [
          {
            blob: DriveApp.getFileById(fileId).getBlob(),
            description: descrip,      <---- "your text goes here"
            filename: filname,
          },
          {
            blob: UrlFetchApp.fetch(url).getBlob(),
            description: descrip,      <---- "your text goes here"
            filename: filname,
          },
        ],
      };