首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用twitter V2从用户tweet时间线获取源视频URL

如何使用twitter V2从用户tweet时间线获取源视频URL
EN

Stack Overflow用户
提问于 2022-02-04 05:24:30
回答 2查看 2K关注 0票数 2

我使用下面的url来获取用户的tweet。

代码语言:javascript
运行
复制
https://api.twitter.com/2/users/{user_twitter_id}/tweets

以下是我的请求参数

代码语言:javascript
运行
复制
query_params = {
    'max_results': max_results,
    'expansions': 'attachments.media_keys',
    'tweet.fields': 'id,created_at,text,author_id,in_reply_to_user_id,referenced_tweets,attachments,withheld,geo,entities,public_metrics,possibly_sensitive,source,lang,context_annotations,conversation_id,reply_settings',
    'media.fields': 'media_key,duration_ms,height,preview_image_url,type,url,width,public_metrics,non_public_metrics,organic_metrics,promoted_metrics,alt_text'
}

下面是我在“包括”媒体列表中得到的回应

代码语言:javascript
运行
复制
"includes": {
    "media": [
        {
            "height": 750,
            "media_key": "3_1489397927281840131",
            "type": "photo",
            "url": "https://pbs.twimg.com/media/FKtnhhDWUAMtyBi.jpg",
            "width": 1125
        },
        {
            "height": 750,
            "media_key": "3_1489397930452783110",
            "type": "photo",
            "url": "https://pbs.twimg.com/media/FKtnhs3XEAYeMkP.jpg",
            "width": 1125
        },
        {
            "height": 750,
            "media_key": "3_1489397944214302727",
            "type": "photo",
            "url": "https://pbs.twimg.com/media/FKtnigIXMAcTO6t.jpg",
            "width": 1125
        },
        {
            "duration_ms": 242784,
            "height": 1080,
            "media_key": "13_1489018359819771906",
            "preview_image_url": "https://pbs.twimg.com/media/FKoOePDWYAA1ZZB.jpg",
            "public_metrics": {
                "view_count": 275300
            },
            "type": "video",
            "width": 1920
        },
        {
            "height": 2400,
            "media_key": "3_1488933061307809794",
            "type": "photo",
            "url": "https://pbs.twimg.com/media/FKnAuwWWQAIZZ8J.jpg",
            "width": 3000
        },
        {
            "height": 2000,
            "media_key": "3_1488640905187938304",
            "type": "photo",
            "url": "https://pbs.twimg.com/media/FKi3BB_X0AA47_h.jpg",
            "width": 3000
        },
        {
            "duration_ms": 41374,
            "height": 1080,
            "media_key": "13_1488623384250527746",
            "preview_image_url": "https://pbs.twimg.com/amplify_video_thumb/1488623384250527746/img/K2fiO7GwjmxL0H89.jpg",
            "public_metrics": {
                "view_count": 239341
            },
            "type": "video",
            "width": 1080
        },
        {
            "height": 2000,
            "media_key": "3_1488548514921603078",
            "type": "photo",
            "url": "https://pbs.twimg.com/media/FKhi_NbWYAYqP04.jpg",
            "width": 3000
        },
        {
            "height": 750,
            "media_key": "3_1488336416732028931",
            "type": "photo",
            "url": "https://pbs.twimg.com/media/FKeiFeGXIAMgHQx.jpg",
            "width": 1125
        },
        {
            "duration_ms": 53136,
            "height": 1080,
            "media_key": "13_1488316251667582978",
            "preview_image_url": "https://pbs.twimg.com/amplify_video_thumb/1488316251667582978/img/DE2q07gtwoARK76r.jpg",
            "public_metrics": {
                "view_count": 214984
            },
            "type": "video",
            "width": 1080
        },
        {
            "duration_ms": 40248,
            "height": 1080,
            "media_key": "13_1488154727544152064",
            "preview_image_url": "https://pbs.twimg.com/media/FKb85iAXoAou4ED.jpg",
            "public_metrics": {
                "view_count": 242329
            },
            "type": "video",
            "width": 1080
        },
        {
            "height": 913,
            "media_key": "3_1487927712761229314",
            "type": "photo",
            "url": "https://pbs.twimg.com/media/FKYuXxKXsAIbXd2.jpg",
            "width": 1200
        },
        {
            "duration_ms": 35785,
            "height": 1080,
            "media_key": "13_1487538546948939777",
            "preview_image_url": "https://pbs.twimg.com/amplify_video_thumb/1487538546948939777/img/qzUmEZKmD6ii_0dM.jpg",
            "public_metrics": {
                "view_count": 290603
            },
            "type": "video",
            "width": 1080
        }
    ]
}

正如您在媒体内部所看到的,我们有类型的视频和媒体键,但是没有实际的视频url,我们所能看到的就是视频缩略图。所以,请告诉我如何获取视频url和预览图片url。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-02-04 13:32:55

这里有几件事要注意:

  • 在Twitter v2中,在2022年2月编写此答案时,视频URL目前不可用。这是Twitter正在做的一个已知的遗漏。
  • 在Twitter v1.1中,您通常应该能够获得视频URL。还有两件事要知道.
代码语言:javascript
运行
复制
- if the Tweet is a longer (more than 140 characters) Tweet, then you will need to add `tweet_mode=extended` to your API call to ensure that the whole Tweet data and extended entities are included. You can tell if you have not retrieved the whole Tweet if the `truncated` field value is `true`. This is the case in your example Tweet ID above. Using `tweet_mode=extended` you will see the video URLs.
- if the Tweet is from the Ads platform, the video may be part of a Ads card format, and these are not served via the API. You can tell this by checking the `source` field for evidence that the Tweet was posted as an ad. The advertiser is able to mark the media as not for wider syndication / API access in that case, and the video URL is not returned in the API.
票数 3
EN

Stack Overflow用户

发布于 2022-04-14 05:52:11

除了回答之外,还有一个解决办法是,还包括--视频的直接URL :只需获取一个tweet,并使用https://cdn.syndication.twimg.com/tweet?id=<tweet_id> URL (示例)获取所需的数据。

Pros

  • 它不需要任何API密钥,所以很容易集成
  • 它为您提供.mp4.hls格式的视频直接链接。

Cons

  • 这是一个内部API调用,对于外部使用❗️❗️❗️不可靠
  • 还需要一个额外的请求来获取数据。
  • 它需要一个代理来消除CORS策略。
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70981790

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档