【问题标题】:Fetch Image & External URL from Tweets with statuses/user_timeline API使用 statuses/user_timeline API 从推文中获取图像和外部 URL
【发布时间】:2019-11-26 13:38:13
【问题描述】:

我希望提取推文中出现的外部 URL 的价值。加上该 URL 生成的 Thumbnail

示例推文:

http://prntscr.com/ogdqey

https://twitter.com/JarirBookstore/status/1151506848387870720

来自 Twitter statuses/user_timeline API 的输出 -

{
    "created_at": "Wed Jul 17 15:00:01 +0000 2019",
    "id": 1151506848387870720,
    "id_str": "1151506848387870720",
    "full_text": "عروض #صيف_هواوي على أجهزة التابلت والميت بوك المختلفة \nالعرض ساري الى 21 يوليو",
    "truncated": false,
    "display_text_range": [
        0,
        78
    ],
    "entities": {
        "hashtags": [
            {
                "text": "صيف_هواوي",
                "indices": [
                    5,
                    15
                ]
            }
        ],
        "symbols": [],
        "user_mentions": [],
        "urls": []
    },
    "source": "<a href=\"https:\/\/ads-api.twitter.com\" rel=\"nofollow\">Twitter Ads Composer<\/a>",
    "in_reply_to_status_id": null,
    "in_reply_to_status_id_str": null,
    "in_reply_to_user_id": null,
    "in_reply_to_user_id_str": null,
    "in_reply_to_screen_name": null,
    "user": {
        "id": 281376243,
        "id_str": "281376243"
    },
    "geo": null,
    "coordinates": null,
    "place": null,
    "contributors": null,
    "is_quote_status": false,
    "retweet_count": 0,
    "favorite_count": 3,
    "favorited": false,
    "retweeted": false,
    "lang": "ar"
},

URL 实体是一个空白数组。如果链接不存在于推文本身,API 不会在 URL 实体中返回它。我试过有无tweet_mode=extended

令人惊讶的是,Twitter 确实为少数此类推文返回 URL。下面是一个例子。

https://twitter.com/BillGates/status/1150605518291001345

API 响应:

{
    "created_at": "Mon Jul 15 03:18:27 +0000 2019",
    "id": 1150605518291001345,
    "id_str": "1150605518291001345",
    "full_text": "I recently wrote about how people with tech skills can find fascinating problems to work on in global health and development. I was excited to come across this @techreview article about African machine learning researchers who are already doing just that. https:\/\/t.co\/3e1d2QvvH4",
    "truncated": false,
    "display_text_range": [
        0,
        279
    ],
    "entities": {
        "hashtags": [],
        "symbols": [],
        "user_mentions": [
            {
                "screen_name": "techreview",
                "name": "MIT Technology Review",
                "id": 15808647,
                "id_str": "15808647",
                "indices": [
                    160,
                    171
                ]
            }
        ],
        "urls": [
            {
                "url": "https:\/\/t.co\/3e1d2QvvH4",
                "expanded_url": "https:\/\/b-gat.es\/2xMsbdh",
                "display_url": "b-gat.es\/2xMsbdh",
                "indices": [
                    256,
                    279
                ]
            }
        ]
    },
    "source": "<a href=\"https:\/\/www.sprinklr.com\" rel=\"nofollow\">Sprinklr<\/a>",
    "in_reply_to_status_id": null,
    "in_reply_to_status_id_str": null,
    "in_reply_to_user_id": null,
    "in_reply_to_user_id_str": null,
    "in_reply_to_screen_name": null,
    "user": {
        "id": 50393960,
        "id_str": "50393960"
    },
    "geo": null,
    "coordinates": null,
    "place": null,
    "contributors": null,
    "is_quote_status": false,
    "retweet_count": 1320,
    "favorite_count": 6719,
    "favorited": false,
    "retweeted": false,
    "possibly_sensitive": false,
    "lang": "en"
},

为什么响应是随机的?它确实返回比尔盖茨推文的 URL,但不返回我之前问题中提到的那个。

如何让 Twitter 显示外部 URL 和缩略图?

我的最终 API 调用 -

https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=jarirbookstore&count=100&exclude_replies=true&trim_user=true&include_rts=false&tweet_mode=extended

【问题讨论】:

    标签: twitter


    【解决方案1】:

    你提供的两个示例中的第一个是由 Twitter 广告平台发布的,因此附加的卡片不是推文的一部分。无法通过 API 获得。在第二种情况下,URL 是 Tweet 文本的一部分,因此它也是 URL 实体对象的一部分。

    【讨论】:

    • 啊.. 有道理。对于第二条推文,有没有办法提取推特生成的缩略图?我知道我可以访问链接并从 META 中提取 twitter:image 值,但我不想走那条路。我很想看到 API 本身的解决方案。
    • API 中不支持此功能。
    猜你喜欢
    • 2016-07-22
    • 1970-01-01
    • 2020-07-02
    • 1970-01-01
    • 2020-10-11
    • 2015-05-15
    • 2017-07-31
    • 2019-12-14
    • 1970-01-01
    相关资源
    最近更新 更多