Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add get_tweet_by_url function to the client #275

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions twikit/client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
<Tweet id="...">
"""
# 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]:
"""
Expand Down