Skip to content

Commit

Permalink
Merge pull request #71 from viralerts/main
Browse files Browse the repository at this point in the history
Add some tweet card fields
  • Loading branch information
d60 authored Apr 18, 2024
2 parents 5c5799e + f7ed2c2 commit 6c3675b
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 2 deletions.
37 changes: 36 additions & 1 deletion twikit/tweet.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class Tweet:
Indicates if the tweet is a quote status.
quote : :class:`Tweet`
The Tweet being quoted (if any)
retweeted : :class:`bool`
retweeted_tweet : :class:`bool`
Whether the tweet is a retweet
possibly_sensitive : :class:`bool`
Indicates if the tweet content may be sensitive.
Expand Down Expand Up @@ -77,6 +77,12 @@ class Tweet:
Hashtags included in the tweet text.
poll : :class:`Poll`
Poll attached to the tweet.
has_card : :class:`bool`
Indicates if the tweet contains a card.
thumbnail_title : :class:`str` | None
The title of the webpage displayed inside tweet's card.
thumbnail_url : :class:`str` | None
Link to the image displayed in the tweet's card.
"""

def __init__(self, client: Client, data: dict, user: User = None) -> None:
Expand Down Expand Up @@ -183,6 +189,35 @@ def __init__(self, client: Client, data: dict, user: User = None) -> None:
else:
self._poll_data = None

self.thumbnail_url = None
self.thumbnail_title = None
self.has_card = 'card' in data
if (
'card' in data and
'legacy' in data['card'] and
'binding_values' in data['card']['legacy']
):
card_data = data['card']['legacy']['binding_values']

if isinstance(card_data, list):
binding_values = {
i.get('key'): i.get('value')
for i in card_data
}

if (
'title' in binding_values and
'string_value' in binding_values['title']
):
self.thumbnail_title = binding_values['title']['string_value']

if (
'thumbnail_image_original' in binding_values and
'image_value' in binding_values['thumbnail_image_original'] and
'url' in binding_values['thumbnail_image_original']['image_value']
):
self.thumbnail_url = binding_values['thumbnail_image_original']['image_value']['url']

@property
def created_at_datetime(self) -> datetime:
return timestamp_to_datetime(self.created_at)
Expand Down
37 changes: 36 additions & 1 deletion twikit/twikit_async/tweet.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Tweet:
Indicates if the tweet is a quote status.
quote : :class:`Tweet`
The Tweet being quoted (if any)
retweeted : :class:`bool`
retweeted_tweet : :class:`bool`
Whether the tweet is a retweet
possibly_sensitive : :class:`bool`
Indicates if the tweet content may be sensitive.
Expand Down Expand Up @@ -74,6 +74,12 @@ class Tweet:
Related tweets.
hashtags: list[:class:`str`]
Hashtags included in the tweet text.
has_card : :class:`bool`
Indicates if the tweet contains a card.
thumbnail_title : :class:`str` | None
The title of the webpage displayed inside tweet's card.
thumbnail_url : :class:`str` | None
Link to the image displayed in the tweet's card.
"""

def __init__(self, client: Client, data: dict, user: User = None) -> None:
Expand Down Expand Up @@ -180,6 +186,35 @@ def __init__(self, client: Client, data: dict, user: User = None) -> None:
else:
self._poll_data = None

self.thumbnail_url = None
self.thumbnail_title = None
self.has_card = 'card' in data
if (
'card' in data and
'legacy' in data['card'] and
'binding_values' in data['card']['legacy']
):
card_data = data['card']['legacy']['binding_values']

if isinstance(card_data, list):
binding_values = {
i.get('key'): i.get('value')
for i in card_data
}

if (
'title' in binding_values and
'string_value' in binding_values['title']
):
self.thumbnail_title = binding_values['title']['string_value']

if (
'thumbnail_image_original' in binding_values and
'image_value' in binding_values['thumbnail_image_original'] and
'url' in binding_values['thumbnail_image_original']['image_value']
):
self.thumbnail_url = binding_values['thumbnail_image_original']['image_value']['url']

@property
def created_at_datetime(self) -> datetime:
return timestamp_to_datetime(self.created_at)
Expand Down

0 comments on commit 6c3675b

Please sign in to comment.