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 25, 2024
1 parent 0cf195a commit 0bc93fd
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 205 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.7'
__version__ = '1.5.8'

from .bookmark import BookmarkFolder
from .client import Client
Expand Down
158 changes: 57 additions & 101 deletions twikit/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from .message import Message
from .notification import Notification
from .trend import Trend
from .tweet import CommunityNote, Poll, ScheduledTweet, Tweet
from .tweet import CommunityNote, Poll, ScheduledTweet, Tweet, tweet_from_data
from .user import User
from .utils import (
BOOKMARK_FOLDER_TIMELINE_FEATURES,
Expand Down Expand Up @@ -487,19 +487,10 @@ def search_tweet(
previous_cursor = item['content']['value']
if not item['entryId'].startswith(('tweet', 'search-grid')):
continue
tweet_info = find_dict(item, 'result')
if not tweet_info:
continue
tweet_info = tweet_info[0]
if 'tweet' in tweet_info:
tweet_info = tweet_info['tweet']
if 'core' not in tweet_info:
continue
if 'result' not in tweet_info['core']['user_results']:
continue
user_info = tweet_info['core']['user_results']['result']
if 'legacy' in tweet_info:
results.append(Tweet(self, tweet_info, User(self, user_info)))

tweet = tweet_from_data(self, item)
if tweet is not None:
results.append(tweet)

if next_cursor is None:
if product == 'Media':
Expand Down Expand Up @@ -603,17 +594,18 @@ def get_similar_tweets(self, tweet_id: str) -> list[Tweet]:
params=params,
headers=self._base_headers
).json()

items = find_dict(response, 'entries')[0]
items_ = find_dict(response, 'entries')
results = []
for item in items:
if not items_:
return results

for item in items_[0]:
if not item['entryId'].startswith('tweet'):
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)))

tweet = tweet_from_data(self, item)
if tweet is not None:
results.append(tweet)

return results

Expand Down Expand Up @@ -1333,13 +1325,9 @@ def _get_more_replies(self, tweet_id: str, cursor: str) -> Result[Tweet]:
for entry in entries:
if entry['entryId'].startswith(('cursor', 'label')):
continue
tweet_info = find_dict(entry, 'result')[0]
if tweet_info['__typename'] == 'TweetTombstone':
continue
if tweet_info['__typename'] == 'TweetWithVisibilityResults':
tweet_info = tweet_info['tweet']
user_info = tweet_info['core']['user_results']['result']
results.append(Tweet(self, tweet_info, User(self, user_info)))
tweet = tweet_from_data(self, entry)
if tweet is not None:
results.append(tweet)

if entries[-1]['entryId'].startswith('cursor'):
next_cursor = entries[-1]['content']['itemContent']['value']
Expand All @@ -1362,11 +1350,9 @@ def _show_more_replies(self, tweet_id: str, cursor: str) -> Result[Tweet]:
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)))
tweet = tweet_from_data(self, item)
if tweet is not None:
results.append(tweet)
return Result(results)

def get_tweet_by_id(
Expand Down Expand Up @@ -1403,28 +1389,15 @@ def get_tweet_by_id(
for entry in entries:
if entry['entryId'].startswith('cursor'):
continue
tweet_info_ = find_dict(entry, 'result')
if not tweet_info_:
continue
tweet_info = tweet_info_[0]

if tweet_info.get('__typename') == 'TweetTombstone':
tweet_object = tweet_from_data(self, entry)
if tweet_object is None:
continue

if tweet_info.get('__typename') == 'TweetWithVisibilityResults':
tweet_info = tweet_info['tweet']

user_info = find_dict(tweet_info, 'user_results')[0]['result']
tweet_object = Tweet(self, tweet_info, User(self, user_info))

if entry['entryId'].startswith('tweetdetailrelatedtweets'):
related_tweets.append(tweet_object)
continue

if entry['entryId'] == f'tweet-{tweet_id}':
if tweet_info.get('__typename') == 'TweetTombstone':
raise TweetNotAvailable('This tweet is not available.')

tweet = tweet_object
else:
if tweet is None:
Expand All @@ -1437,17 +1410,11 @@ def get_tweet_by_id(
for reply in entry['content']['items'][1:]:
if 'tweetcomposer' in reply['entryId']:
continue
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':
rpl = tweet_from_data(self, reply)
if rpl is None:
continue
usr_data = find_dict(
rpl_data, 'user_results')[0]['result']
replies.append(
Tweet(self, rpl_data, User(self, usr_data))
)
replies.append(rpl)
if 'cursor' in reply.get('entryId'):
sr_cursor = reply['item']['itemContent']['value']
show_replies = partial(
Expand Down Expand Up @@ -1809,27 +1776,19 @@ def get_user_tweets(
if entry_id.startswith('profile-conversation'):
tweets = item['content']['items']
replies = []

for reply in tweets[1:]:
tweet_info = find_dict(reply, 'result')[0]
if 'tweet' in tweet_info:
tweet_info = tweet_info['tweet']
user_info = find_dict(tweet_info, 'result')[0]
user = User(self, user_info)

replies.append(Tweet(self, tweet_info, user))

tweet_object = tweet_from_data(self, reply)
if tweet_object is None:
continue
replies.append(tweet_object)
item = tweets[0]
else:
replies = None

tweet_info = find_dict(item, 'result')[0]
if 'tweet' in tweet_info:
tweet_info = tweet_info['tweet']
user_info = find_dict(tweet_info, 'result')[0]
tweet = Tweet(self, tweet_info, User(self, user_info))
tweet = tweet_from_data(self, item)
if tweet is None:
continue
tweet.replies = replies

results.append(tweet)

return Result(
Expand Down Expand Up @@ -1912,11 +1871,10 @@ def get_timeline(
for item in items:
if 'itemContent' not in item['content']:
continue
tweet_info = find_dict(item, 'result')[0]
if tweet_info['__typename'] == 'TweetWithVisibilityResults':
tweet_info = tweet_info['tweet']
user_info = tweet_info['core']['user_results']['result']
results.append(Tweet(self, tweet_info, user_info))
tweet = tweet_from_data(self, item)
if tweet is None:
continue
results.append(tweet)

return Result(
results,
Expand Down Expand Up @@ -1994,11 +1952,10 @@ def get_latest_timeline(
for item in items:
if 'itemContent' not in item['content']:
continue
tweet_info = find_dict(item, 'result')[0]
if tweet_info['__typename'] == 'TweetWithVisibilityResults':
tweet_info = tweet_info['tweet']
user_info = tweet_info['core']['user_results']['result']
results.append(Tweet(self, tweet_info, user_info))
tweet = tweet_from_data(self, item)
if tweet is None:
continue
results.append(tweet)

return Result(
results,
Expand Down Expand Up @@ -2295,9 +2252,10 @@ def get_bookmarks(
for item in items:
if not item['entryId'].startswith('tweet'):
continue
tweet_info = find_dict(item, 'tweet_results')[0]['result']
user_info = tweet_info['core']['user_results']['result']
results.append(Tweet(self, tweet_info, User(self, user_info)))
tweet = tweet_from_data(self, item)
if tweet is None:
continue
results.append(tweet)

return Result(
results,
Expand Down Expand Up @@ -3825,11 +3783,10 @@ def get_list_tweets(
for item in items:
if not item['entryId'].startswith('tweet'):
continue
tweet_info = find_dict(item, 'result')[0]
if tweet_info['__typename'] == 'TweetWithVisibilityResults':
tweet_info = tweet_info['tweet']
user_info = find_dict(tweet_info, 'result')[0]
results.append(Tweet(self, tweet_info, User(self, user_info)))

tweet = tweet_from_data(self, item)
if tweet is not None:
results.append(tweet)

return Result(
results,
Expand Down Expand Up @@ -4273,11 +4230,10 @@ def get_community_tweets(
for item in items:
if not item['entryId'].startswith(('tweet', 'communities-grid')):
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']
tweets.append(Tweet(self, tweet_data, User(self, user_data)))

tweet = tweet_from_data(self, item)
if tweet is not None:
tweets.append(tweet)

return Result(
tweets,
Expand Down Expand Up @@ -4335,6 +4291,7 @@ def get_communities_timeline(
for item in items:
if not item['entryId'].startswith('tweet'):
continue
tweet = tweet_from_data(self, item)
tweet_data = find_dict(item, 'result')[0]
if 'tweet' in tweet_data:
tweet_data = tweet_data['tweet']
Expand Down Expand Up @@ -4602,11 +4559,10 @@ def search_community_tweet(
for item in items:
if not item['entryId'].startswith('tweet'):
continue
tweet_data = find_dict(item, 'result')[0]
if 'tweet' in tweet_data:
tweet_data = tweet_data['tweet']
user_data = find_dict(tweet_data, 'result')[0]
tweets.append(Tweet(self, tweet_data, User(self, user_data)))

tweet = tweet_from_data(self, item)
if tweet is not None:
tweets.append(tweet)

next_cursor = items[-1]['content']['value']
previous_cursor = items[-2]['content']['value']
Expand Down
21 changes: 21 additions & 0 deletions twikit/tweet.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,27 @@ def __ne__(self, __value: object) -> bool:
return not self == __value


def tweet_from_data(client: Client, data: dict) -> Tweet:
tweet_data_ = find_dict(data, 'result', True)
if not tweet_data_:
return None
tweet_data = tweet_data_[0]

if tweet_data.get('__typename') == 'TweetTombstone':
return None
if 'tweet' in tweet_data:
tweet_data = tweet_data['tweet']
if 'core' not in tweet_data:
return None
if 'result' not in tweet_data['core']['user_results']:
return None
if 'legacy' not in tweet_data:
return None

user_data = tweet_data['core']['user_results']['result']
return Tweet(client, tweet_data, User(client, user_data))


class ScheduledTweet:
def __init__(self, client: Client, data: dict) -> None:
self._client = client
Expand Down
Loading

0 comments on commit 0bc93fd

Please sign in to comment.