基于过滤器的媒体搜索 google 照片 API (python)

filter based media search with google photos API (python)

我正在尝试使用 mediaItems().search() 方法,使用以下正文:

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

但问题是集合 {"LANDSCAPES","CITYSCAPES"} 实际上应该是一组枚举(如 Java 枚举),而不是我写的字符串。这是在 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)
  ]
}

在python中有解决这个问题的正确方法吗?

修改点:

  • 当使用albumIdfilters时,会出现The album ID cannot be set if filters are used.的错误。所以当你想使用filters时,请去掉albumId.
  • includedContentCategories的值是一个数组如下。
    • "includedContentCategories": ["LANDSCAPES","CITYSCAPES"]
  • includeArchiveMediaincludeArchivedMedia.
  • 请在filters中包含includeArchivedMedia

当以上几点反映到你的脚本中,就会变成下面这样。

修改后的脚本:

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

参考: