From bf0fff0da2841ee0706adbe113cf48c623f9dc6c Mon Sep 17 00:00:00 2001 From: Igor Brigadir Date: Wed, 16 Mar 2022 02:11:43 +0000 Subject: [PATCH 1/4] add quote tweet api --- twarc/client2.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/twarc/client2.py b/twarc/client2.py index ed9ce21b..ba4b6695 100644 --- a/twarc/client2.py +++ b/twarc/client2.py @@ -1228,6 +1228,37 @@ 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): From 46fc178501e4b2cfec887383eb2cd7f6a149ade7 Mon Sep 17 00:00:00 2001 From: Igor Brigadir Date: Wed, 16 Mar 2022 02:18:07 +0000 Subject: [PATCH 2/4] add quotes command --- twarc/command2.py | 49 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/twarc/command2.py b/twarc/command2.py index e7534af5..3418a0f1 100644 --- a/twarc/command2.py +++ b/twarc/command2.py @@ -978,6 +978,55 @@ 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 + + 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 == "") 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", From 455dae13cd3129cc67dad212035f82c7a0a39b4c Mon Sep 17 00:00:00 2001 From: Igor Brigadir Date: Wed, 16 Mar 2022 15:46:16 +0000 Subject: [PATCH 3/4] fix expansions --- twarc/command2.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/twarc/command2.py b/twarc/command2.py index 3418a0f1..e9e5401c 100644 --- a/twarc/command2.py +++ b/twarc/command2.py @@ -1006,6 +1006,11 @@ def quotes(T, tweet_id, outfile, limit, max_results, hide_progress, **kwargs): """ 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) From 9f8195415a0387416d3007e302dab83d5015a042 Mon Sep 17 00:00:00 2001 From: Igor Brigadir Date: Wed, 16 Mar 2022 17:34:15 +0000 Subject: [PATCH 4/4] black format --- twarc/client2.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/twarc/client2.py b/twarc/client2.py index ba4b6695..afa1a613 100644 --- a/twarc/client2.py +++ b/twarc/client2.py @@ -1255,9 +1255,7 @@ def quotes( if "data" in page: yield page else: - log.info( - f"Retrieved an empty page of results for quotes of {tweet_id}" - ) + log.info(f"Retrieved an empty page of results for quotes of {tweet_id}") @catch_request_exceptions @rate_limit