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 new Quoted tweets endpoint #610

Merged
merged 4 commits into from
Mar 18, 2022
Merged
Show file tree
Hide file tree
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 twarc/client2.py
Original file line number Diff line number Diff line change
Expand Up @@ -1228,6 +1228,35 @@ def retweeted_by(
f"Retrieved an empty page of results for retweeted_by of {tweet_id}"
)

def quotes(
self,
tweet_id,
expansions=None,
tweet_fields=None,
user_fields=None,
max_results=100,
pagination_token=None,
):
"""
Retrieve the tweets that quote tweet the given tweet.

"""
url = f"https://api.twitter.com/2/tweets/{tweet_id}/quote_tweets"

params = self._prepare_params(
expansions=expansions,
tweet_fields=tweet_fields,
user_fields=user_fields,
max_results=max_results,
pagination_token=pagination_token,
)

for page in self.get_paginated(url, params=params):
if "data" in page:
yield page
else:
log.info(f"Retrieved an empty page of results for quotes of {tweet_id}")

@catch_request_exceptions
@rate_limit
def get(self, *args, **kwargs):
Expand Down
54 changes: 54 additions & 0 deletions twarc/command2.py
Original file line number Diff line number Diff line change
Expand Up @@ -978,6 +978,60 @@ def retweeted_by(T, tweet_id, outfile, limit, max_results, hide_progress):
break


@twarc2.command("quotes")
@click.option(
"--limit",
default=0,
help="Maximum number of retweeting users to retrieve. Increments of 100 or --max-results if set.",
type=int,
)
@click.option(
"--max-results",
default=100,
help="Maximum number of users (retweets) per page of results. Default and maximum is 100.",
type=int,
)
@command_line_expansions_shortcuts
@command_line_expansions_options
@command_line_progressbar_option
@click.argument("tweet_id", type=str)
@click.argument("outfile", type=click.File("w"), default="-")
@click.pass_obj
@cli_api_error
def quotes(T, tweet_id, outfile, limit, max_results, hide_progress, **kwargs):
"""
Get the tweets that quote tweet the given tweet.

Note that the progress bar is approximate.
"""
count = 0
lookup_total = 0
kwargs = _process_expansions_shortcuts(kwargs)
# Also remove media poll and place from kwargs, these are not valid for this endpoint:
kwargs.pop("media_fields", None)
kwargs.pop("poll_fields", None)
kwargs.pop("place_fields", None)

if not re.match("^\d+$", str(tweet_id)):
click.echo(click.style("Please enter a tweet ID", fg="red"), err=True)

hide_progress = True if (outfile.name == "<stdout>") else hide_progress

if not hide_progress:
target_tweet = list(T.tweet_lookup([tweet_id]))[0]
if "data" in target_tweet:
lookup_total = target_tweet["data"][0]["public_metrics"]["quote_count"]

with tqdm(disable=hide_progress, total=lookup_total) as progress:
for result in T.quotes(tweet_id, max_results=max_results, **kwargs):
_write(result, outfile)
count += len(result.get("data", []))
progress.update(len(result.get("data", [])))
if limit != 0 and count >= limit:
progress.desc = f"Set --limit of {limit} reached"
break


@twarc2.command("liked-tweets")
@click.option(
"--limit",
Expand Down