Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
d60 authored Apr 18, 2024
1 parent 6c3675b commit 3318202
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 8 deletions.
2 changes: 1 addition & 1 deletion twikit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
A Python library for interacting with the Twitter API.
"""

__version__ = '1.5.2'
__version__ = '1.5.3'

from .client import Client
from .community import (Community, CommunityCreator, CommunityMember,
Expand Down
44 changes: 43 additions & 1 deletion twikit/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1246,7 +1246,7 @@ def _get_tweet_detail(self, tweet_id: str, cursor: str | None):

def _get_more_replies(self, tweet_id: str, cursor: str) -> Result[Tweet]:
response = self._get_tweet_detail(tweet_id, cursor)
entries = find_dict(response, 'entries')[0]
entries = find_dict(response, 'entries')

results = []
for entry in entries:
Expand Down Expand Up @@ -1274,6 +1274,20 @@ def _get_more_replies(self, tweet_id: str, cursor: str) -> Result[Tweet]:
next_cursor
)

def _show_more_replies(self, tweet_id: str, cursor: str) -> Result[Tweet]:
response = self._get_tweet_detail(tweet_id, cursor)
items = find_dict(response, 'moduleItems')[0]
results = []
for item in items:
if 'tweet' not in item['entryId']:
continue
tweet_data = find_dict(item, 'result')[0]
if 'tweet' in tweet_data:
tweet_data = tweet_data['tweet']
user_data = tweet_data['core']['user_results']['result']
results.append(Tweet(self, tweet_data, User(self, user_data)))
return Result(results)

def get_tweet_by_id(
self, tweet_id: str, cursor: str | None = None
) -> Tweet:
Expand Down Expand Up @@ -1335,6 +1349,34 @@ def get_tweet_by_id(
if tweet is None:
reply_to.append(tweet_object)
else:
replies = []
sr_cursor = None
show_replies = None
# Reply to reply
for reply in entry['content']['items'][1:]:
if 'tweet' in find_dict(reply, 'result'):
reply = reply['tweet']
if 'tweet' in reply.get('entryId'):
rpl_data = find_dict(reply, 'result')[0]
if rpl_data.get('__typename') == 'TweetTombstone':
continue
usr_data = find_dict(
rpl_data, 'user_results')[0]['result']
replies.append(
Tweet(self, rpl_data, User(self, usr_data))
)
if 'cursor' in reply.get('entryId'):
sr_cursor = reply['item']['itemContent']['value']
show_replies = partial(
self._show_more_replies,
tweet_id,
sr_cursor
)
tweet_object.replies = Result(
replies,
show_replies,
sr_cursor
)
replies_list.append(tweet_object)

if entries[-1]['entryId'].startswith('cursor'):
Expand Down
8 changes: 5 additions & 3 deletions twikit/tweet.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,11 @@ def __init__(self, client: Client, data: dict, user: User = None) -> None:
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']
'url' in binding_values['thumbnail_image_original'
]['image_value']
):
self.thumbnail_url = binding_values['thumbnail_image_original']['image_value']['url']
self.thumbnail_url = binding_values['thumbnail_image_original'
]['image_value']['url']

@property
def created_at_datetime(self) -> datetime:
Expand Down Expand Up @@ -663,4 +665,4 @@ def __eq__(self, __value: object) -> bool:
return isinstance(__value, CommunityNote) and self.id == __value.id

def __ne__(self, __value: object) -> bool:
return not self == __value
return not self == __value
44 changes: 44 additions & 0 deletions twikit/twikit_async/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1290,6 +1290,22 @@ async def _get_more_replies(
next_cursor
)

async def _show_more_replies(
self, tweet_id: str, cursor: str
) -> Result[Tweet]:
response = await self._get_tweet_detail(tweet_id, cursor)
items = find_dict(response, 'moduleItems')[0]
results = []
for item in items:
if 'tweet' not in item['entryId']:
continue
tweet_data = find_dict(item, 'result')[0]
if 'tweet' in tweet_data:
tweet_data = tweet_data['tweet']
user_data = tweet_data['core']['user_results']['result']
results.append(Tweet(self, tweet_data, User(self, user_data)))
return Result(results)

async def get_tweet_by_id(
self, tweet_id: str, cursor: str | None = None
) -> Tweet:
Expand Down Expand Up @@ -1351,6 +1367,34 @@ async def get_tweet_by_id(
if tweet is None:
reply_to.append(tweet_object)
else:
replies = []
sr_cursor = None
show_replies = None
# Reply to reply
for reply in entry['content']['items'][1:]:
if 'tweet' in find_dict(reply, 'result'):
reply = reply['tweet']
if 'tweet' in reply.get('entryId'):
rpl_data = find_dict(reply, 'result')[0]
if rpl_data.get('__typename') == 'TweetTombstone':
continue
usr_data = find_dict(
rpl_data, 'user_results')[0]['result']
replies.append(
Tweet(self, rpl_data, User(self, usr_data))
)
if 'cursor' in reply.get('entryId'):
sr_cursor = reply['item']['itemContent']['value']
show_replies = partial(
self._show_more_replies,
tweet_id,
sr_cursor
)
tweet_object.replies = Result(
replies,
show_replies,
sr_cursor
)
replies_list.append(tweet_object)

if entries[-1]['entryId'].startswith('cursor'):
Expand Down
8 changes: 5 additions & 3 deletions twikit/twikit_async/tweet.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,11 @@ def __init__(self, client: Client, data: dict, user: User = None) -> None:
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']
'url' in binding_values['thumbnail_image_original'
]['image_value']
):
self.thumbnail_url = binding_values['thumbnail_image_original']['image_value']['url']
self.thumbnail_url = binding_values['thumbnail_image_original'
]['image_value']['url']

@property
def created_at_datetime(self) -> datetime:
Expand Down Expand Up @@ -651,4 +653,4 @@ def __eq__(self, __value: object) -> bool:
return isinstance(__value, CommunityNote) and self.id == __value.id

def __ne__(self, __value: object) -> bool:
return not self == __value
return not self == __value

0 comments on commit 3318202

Please sign in to comment.