Alexis King
Alexis King

Reputation: 43852

Is there any way to get links, hashtags, and @names from Twitter API requests?

I'm building a website, and I'm using the Twitter API to display data from a user's tweets. It works fine, but all the tweets are retrieved in plain text. This means that, unlike on the Twitter website, all links are simply plain text, no @names are links, and hashtags are completely static.

I would assume that Twitter pulls out these elements using regular expressions, but not only am I fairly poor at regexes, I want the result to be as close to Twitter's implementation as possible. Is there any way to pull these from the Twitter API itself? If not, how could I get parsing as close to Twitter's as possible?

Upvotes: 1

Views: 1525

Answers (2)

Chamilyan
Chamilyan

Reputation: 9423

Look at Tweet Entities . You can add the parameter &tweet_entities=1 to the end of some REST calls. The JSON response will include the extra attributes your looking for attributed to the tweet.

I.E

The urls entity

An array of URLs extracted from the Tweet text. Each URL entity comes with the following attributes: url , display_url, expanded_url, indices

 "text": "Twitter for Mac is now easier and faster, and you can open multiple windows at once http://t.co/0JG5Mcq",
    "entities": {
      "media": [
      ],
      "urls": [
        {
          "url": "http://t.co/0JG5Mcq",
          "display_url": "blog.twitter.com/2011/05/twitte…",
          "expanded_url": "http://blog.twitter.com/2011/05/twitter-for-mac-update.html",
          "indices": [
            84,
            103
          ]
        }
      ],
      "user_mentions": [
      ],
      "hashtags": [
      ]
    }

The hashtags entity

An array of hashtags extracted from the Tweet text. Each Hashtag entity comes with the following attributes:

text
The Hashtag text indices
The character positions the Hashtag was extracted from

    "text": "Loved #devnestSF"
>     "entities": {
>       "media": [
>       ],
>       "urls": [
>       ],
>       "user_mentions": [
>       ],
>       "hashtags": [
>         "text": "devnestSF"
>         "indices": [
>           6,
>           16
>         ]
>       ]
>     }

The user_mentions entity

An array of Twitter screen names extracted from the Tweet text. Each User entity comes with the following attributes:

id
The User ID (int format) id_str The User ID (string format) screen_name
The User screen name name
The User's full name indices
The character positions the User mention was extracted from

"text": "@rno Et demi!"
    "entities": {
      "media": [
      ],
      "urls": [
      ],
      "user_mentions": [
        {
          "id": 22548447,
          "id_str": "22548447",
          "screen_name": "rno",
          "name": "Arnaud Meunier",
          "indices": [
            0,
            4
          ]
        }
      ],
      "hashtags": [
      ]
    }

more Tweet Entities at this link:

https://dev.twitter.com/docs/tweet-entities

Upvotes: 4

DaveRandom
DaveRandom

Reputation: 88657

I believe this is what you are looking for:

https://dev.twitter.com/docs/api/1/get/statuses/oembed

Upvotes: 0

Related Questions