Reputation: 310
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
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
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
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