使用google photos API(python)进行基于过滤器的媒体搜索

2024-10-01 09:42:23 发布

您现在位置: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中有解决这个问题的正确方法吗


Tags: 方法tokensearchifpagebodyenumelse
1条回答
网友
1楼 · 发布于 2024-10-01 09:42:23

修改点:

  • 当使用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
    }
}

参考资料:

相关问题 更多 >