【问题标题】:filter based media search with google photos API (python)使用谷歌照片 API (python) 进行基于过滤器的媒体搜索
【发布时间】:2020-10-07 23:11:38
【问题描述】:

我正在尝试使用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 - 此过滤器允许您根据内容类型返回媒体项目。

JSON 表示

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

有没有合适的方法在 python 中解决这个问题?

【问题讨论】:

    标签: python google-photos-api


    【解决方案1】:

    修改点:

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

    参考:

    【讨论】:

    • @Hadar Sharvit 感谢您的回复。很高兴您的问题得到解决。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-25
    • 2020-10-28
    • 1970-01-01
    相关资源
    最近更新 更多