Getting a tweet id from a tweet link using Tweepy

Question:

I am creating a bot for Reddit that posts a tweet’s text, author, and a couple of other small things on submissions that directly link to the tweet (example – https://twitter.com/John_Yuki_Bot/status/889453450664304641).

However, I can’t find anything in the Tweepy documentation that lets me extract a tweet’s id from a tweet link so that I can use api.get_status(status_id) in order to get the status text, status author, and so on.

How can I get the status id just using links like that?

EDIT – The code at the end of the link (889453450664304641) is the status ID. I need to have this in its own variable so that Tweepy can use it.

Asked By: John Yuki

||

Answers:

Simple

url.split('/')[-1]

and you get what you want

Answered By: Filippos Ser
url.split('/')[-1].split('?')[0]

the first part (.split('/')[-1]) splits the string based on the / character and chooses the last element. On top of that, we do .split('?')[0], which splits the result based on ? character and chooses the first part. This will remove any arguments being passed in the URL.

Answered By: Daksh Shah
let twitterLink = 'https://twitter.com/i/status/1565542250158837761';
      let tweetId = "";
            let arr = twitterLink.split("/");
            for (let i = 0; i < arr.length; i++) {
              let el = arr[i];
              if (el === "status") {
                tweetId = arr[i + 1];
              }
            }

Therefore you get the element of array that comes after status and for twetter that is always ID

Answered By: jorkata
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.