使用状态/ user_timeline API从推文中获取图像和外部URL

时间:2019-07-17 16:35:58

标签: twitter

我希望提取Tweet中存在的外部URL 的值。加上该URL生成的缩略图

示例推文:

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实体是一个空白数组。如果该链接不存在于Tweet文本本身中,则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,但没有返回我的问题前面提到的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

1 个答案:

答案 0 :(得分:1)

您提供的两个示例中的第一个示例是由Twitter Ads平台发布的,因此随附的卡片不属于Tweet。没有办法通过API来实现。在第二种情况下,URL是Tweet文本的一部分,因此它也是URL实体对象的一部分。