filter based media search with google photos API (python)

388 views Asked by At

I'm trying to use the mediaItems().search() method, using the following body:

body = {
        "pageToken": page_token if page_token != "" else "",
        "pageSize": 100,
        "filters": {
            "contentFilter": {
                "includedContentCategories": {"LANDSCAPES","CITYSCAPES"}
            }
        },
        "includeArchiveMedia": include_archive
    }

but the problem is that the set {"LANDSCAPES","CITYSCAPES"} should actually be a set of enums (as in Java enums), and not strings as ive written. this is specified in the API: (https://developers.google.com/photos/library/reference/rest/v1/albums)

ContentFilter - This filter allows you to return media items based on the content type.

JSON representation

{
  "includedContentCategories": [
    enum (ContentCategory)
  ],
  "excludedContentCategories": [
    enum (ContentCategory)
  ]
}

is there a proper way of solving this in python?

1

There are 1 answers

1
Tanaike On BEST ANSWER

Modification points:

  • When albumId and filters are used, an error of The album ID cannot be set if filters are used. occurs. So when you want to use filters, please remove albumId.
  • The value of includedContentCategories is an array as follows.
    • "includedContentCategories": ["LANDSCAPES","CITYSCAPES"]
  • includeArchiveMedia is includeArchivedMedia.
  • Please include includeArchivedMedia in filters.

When above points are reflected to your script, it becomes as follows.

Modified script:

body = {
    # "albumId": album_id,  # <--- removed
    "pageToken": page_token if page_token != "" else "",
    "pageSize": 100,
    "filters": {
        "contentFilter": {
            "includedContentCategories": ["LANDSCAPES", "CITYSCAPES"]
        },
        "includeArchivedMedia": include_archive
    }
}

Reference: