skip to Main Content

When getting tweet information using the twitter API, the returned text or full_text field has a URL appended at the end of the text. For example:

    "full_text": "Just another Extended Tweet with more than 140 characters, generated as a documentation example, showing that ["truncated": true] and the presence of an "extended_tweet" object with complete text and "entities" #documentation #parsingJSON #GeoTagged https://twitter.com/FloodSocial/status/994633657141813248"

https://twitter.com/FloodSocial/status/994633657141813248 is appended at the end(The appended url is acutally a shortened url but stackoverflow does not allow shortened url in the body so I just replace it with the full URL). Why does the API add this and is there a way to get the text without the added URL?

2

Answers


  1. Are you using the correct twitter gem? using gem install twitter and setting up a client according to the docs, you should be able to just get the tweet/status by it’s ID. But whatever example you are using doesn’t show how you got the full text

    text = client.status('994633657141813248').text
    =>"Just another Extended Tweet with more than 140 characters, generated as a documentation example, showing that https://twitter.com/FloodSocial/status/994633657141813248"
    

    The url is truncated as a plain string so not sure what you even do to get the string you formulated.

    But if you have some long string somehow with the url embedded, you could do

    text.split(/shttp?s/).first
    
    Login or Signup to reply.
  2. That looks like a quote Tweet where the original Tweet URL is included?

    [edit – I was wrong with the above statement]

    I see what is happening. The original Tweet links to an image on Twitter (https://twitter.com/FloodSocial/status/994633657141813248/photo/1, via a shortened tco link). Twitter hides the image URL in the rendered Tweet, but returns it in the body of the text. That’s the expected behaviour in this case. You can also see the link parsed out in the extended_entities segment of the Tweet data, as well as the image data itself in the same area of the Tweet. If you want to omit the URL from the text data, you’ll need to trim it yourself.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search