John Yuki
John Yuki

Reputation: 310

Getting a tweet id from a tweet link using Tweepy

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.

Upvotes: 3

Views: 4170

Answers (4)

Nisuga Jayawardana
Nisuga Jayawardana

Reputation: 432

  let u = new URL(twitterUrl)
  let split = u.pathname.split('status/')
  let idContainer = split[split.length-1]
  let idSplit = idContainer.split('/')
  let id = idSplit[0]

Upvotes: -1

jorkata
jorkata

Reputation: 51

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

Upvotes: 0

Daksh Shah
Daksh Shah

Reputation: 3113

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.

Upvotes: 2

Filippos Ser
Filippos Ser

Reputation: 386

Simple

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

and you get what you want

Upvotes: 3

Related Questions