diff --git a/twikit/client/client.py b/twikit/client/client.py index ee03690e..677d7672 100644 --- a/twikit/client/client.py +++ b/twikit/client/client.py @@ -1619,6 +1619,35 @@ async def get_tweet_by_id( tweet.related_tweets = related_tweets return tweet + + async def get_tweet_by_url(self, url: str, cursor: str | None = None) -> Tweet: + """ + Fetches a tweet by tweet URL. + + Parameters + ---------- + url : :class:`str` + The URL of the tweet. + + Returns + ------- + :class:`Tweet` + A Tweet object representing the fetched tweet. + + Examples + -------- + >>> tweet_url = 'https://twitter.com/user/status/...' + >>> tweet = await client.get_tweet_by_url(tweet_url) + >>> print(tweet) + + """ + # regex to search for tweet id in the url + match = re.search(r'status/(\d+)', url) + if not match: + raise ValueError(f'Could not extract tweet ID from URL: {url}') + tweet_id = match.group(1) + # call get_tweet_by_id + return await self.get_tweet_by_id(tweet_id, cursor) async def get_scheduled_tweets(self) -> list[ScheduledTweet]: """