From 6a6ef98efa58e13147220cde1b475bfc6fac3116 Mon Sep 17 00:00:00 2001 From: Harmon Date: Wed, 16 Mar 2022 13:09:51 -0500 Subject: [PATCH] Improve API v2 examples --- docs/examples.rst | 38 +++++++++---------- examples/API_v2/get_liked_tweets.py | 23 ++++++----- examples/API_v2/get_liking_users.py | 25 ++++++------ examples/API_v2/get_recent_tweets_count.py | 21 ++++++++++ examples/API_v2/get_retweeters.py | 25 ++++++------ examples/API_v2/get_tweets.py | 20 ++++++++++ examples/API_v2/get_users.py | 21 ++++++++++ examples/API_v2/get_users_followers.py | 31 ++++++++------- examples/API_v2/get_users_mentions.py | 23 ++++++----- examples/API_v2/get_users_timeline.py | 19 ---------- examples/API_v2/get_users_tweets.py | 24 ++++++++++++ .../API_v2/lookup_tweets_using_tweet_ids.py | 21 ---------- .../API_v2/lookup_users_using_user_ids.py | 22 ----------- examples/API_v2/recent_tweets_count.py | 19 ---------- examples/API_v2/search_recent_tweets.py | 3 +- 15 files changed, 174 insertions(+), 161 deletions(-) create mode 100644 examples/API_v2/get_recent_tweets_count.py create mode 100644 examples/API_v2/get_tweets.py create mode 100644 examples/API_v2/get_users.py delete mode 100644 examples/API_v2/get_users_timeline.py create mode 100644 examples/API_v2/get_users_tweets.py delete mode 100644 examples/API_v2/lookup_tweets_using_tweet_ids.py delete mode 100644 examples/API_v2/lookup_users_using_user_ids.py delete mode 100644 examples/API_v2/recent_tweets_count.py diff --git a/docs/examples.rst b/docs/examples.rst index 30c7884..d9157d4 100644 --- a/docs/examples.rst +++ b/docs/examples.rst @@ -58,38 +58,38 @@ Examples .. literalinclude:: ../examples/API_v2/create_tweet.py - .. tab:: Get User's Timeline + .. tab:: Get User's Tweets - .. literalinclude:: ../examples/API_v2/get_users_timeline.py + .. literalinclude:: ../examples/API_v2/get_users_tweets.py .. tab:: Get User's Mentions .. literalinclude:: ../examples/API_v2/get_users_mentions.py - - .. tab:: Get Tweets liked by a User + + .. tab:: Get User's Liked Tweets .. literalinclude:: ../examples/API_v2/get_liked_tweets.py - - .. tab:: Get Retweeters of a Tweet + + .. tab:: Get Retweeters .. literalinclude:: ../examples/API_v2/get_retweeters.py - - .. tab:: Get Users liking a Tweet + + .. tab:: Get Liking Users .. literalinclude:: ../examples/API_v2/get_liking_users.py - - .. tab:: Get a User's Followers + + .. tab:: Get User's Followers .. literalinclude:: ../examples/API_v2/get_users_followers.py - - .. tab:: Lookup Tweets using Tweet IDs - .. literalinclude:: ../examples/API_v2/lookup_tweets_using_tweet_ids.py - - .. tab:: Lookup Users using User IDs + .. tab:: Get Tweets + + .. literalinclude:: ../examples/API_v2/get_tweets.py + + .. tab:: Get Users + + .. literalinclude:: ../examples/API_v2/get_users.py - .. literalinclude:: ../examples/API_v2/lookup_users_using_user_ids.py - - .. tab:: Recent Tweet Counts + .. tab:: Get Recent Tweets Count - .. literalinclude:: ../examples/API_v2/recent_tweets_count.py + .. literalinclude:: ../examples/API_v2/get_recent_tweets_count.py diff --git a/examples/API_v2/get_liked_tweets.py b/examples/API_v2/get_liked_tweets.py index 021d4b6..7c71ce6 100644 --- a/examples/API_v2/get_liked_tweets.py +++ b/examples/API_v2/get_liked_tweets.py @@ -1,21 +1,20 @@ import tweepy -# Replace bearer token value with your own bearer_token = "" -# Initializing the Tweepy client client = tweepy.Client(bearer_token) -# Replace User ID -id = 2244994945 +# Get User's Liked Tweets -# By default the Tweet ID and text are returned. If you want additional fields, -# request them using the tweet_fields parameter -tweets = client.get_liked_tweets(id, tweet_fields=["created_at"]) +# This endpoint/method allows you to get information about a user’s liked +# Tweets -# Print the Tweet id and the time the Tweet was created -for tweet in tweets.data: - print(tweet.id) - print(tweet.created_at) - \ No newline at end of file +user_id = 2244994945 + +# By default, only the ID and text fields of each Tweet will be returned +# Additional fields can be retrieved using the tweet_fields parameter +response = client.get_liked_tweets(user_id, tweet_fields=["created_at"]) + +for tweet in response.data: + print(tweet.id, tweet.created_at) diff --git a/examples/API_v2/get_liking_users.py b/examples/API_v2/get_liking_users.py index 9721478..518043d 100644 --- a/examples/API_v2/get_liking_users.py +++ b/examples/API_v2/get_liking_users.py @@ -1,22 +1,21 @@ import tweepy -# Replace bearer token value with your own bearer_token = "" -# Initializing the Tweepy client client = tweepy.Client(bearer_token) -# Replace Tweet ID -id = 1441054496931541004 +# Get Liking Users -# By default the user ID, name and username are returned. user_fields can be -# used to specify the additional user data that you want returned for each user -# e.g. profile_image_url -users = client.get_liking_users(id, user_fields=["profile_image_url"]) +# This endpoint/method allows you to get information about a Tweet’s liking +# users -# Print the username and the user's profile image url -for user in users.data: - print(user.username) - print(user.profile_image_url) - \ No newline at end of file +tweet_id = 1460323737035677698 + +# By default, only the ID, name, and username fields of each user will be +# returned +# Additional fields can be retrieved using the user_fields parameter +response = client.get_liking_users(tweet_id, user_fields=["profile_image_url"]) + +for user in response.data: + print(user.username, user.profile_image_url) diff --git a/examples/API_v2/get_recent_tweets_count.py b/examples/API_v2/get_recent_tweets_count.py new file mode 100644 index 0000000..173496d --- /dev/null +++ b/examples/API_v2/get_recent_tweets_count.py @@ -0,0 +1,21 @@ +import tweepy + + +bearer_token = "" + +client = tweepy.Client(bearer_token) + +# Get Recent Tweets Count + +# This endpoint/method returns count of Tweets from the last seven days that +# match a search query + +query = "Tweepy -is:retweet" + +# Granularity is what you want the timeseries count data to be grouped by +# You can request minute, hour, or day granularity +# The default granularity, if not specified is hour +response = client.get_recent_tweets_count(query, granularity="day") + +for count in response.data: + print(count) diff --git a/examples/API_v2/get_retweeters.py b/examples/API_v2/get_retweeters.py index ade644c..9e6c291 100644 --- a/examples/API_v2/get_retweeters.py +++ b/examples/API_v2/get_retweeters.py @@ -1,22 +1,21 @@ import tweepy -# Replace bearer token value with your own bearer_token = "" -# Initializing the Tweepy client client = tweepy.Client(bearer_token) -# Replace Tweet ID -id = 1441054496931541004 +# Get Retweeters -# By default the user ID, name and username are returned. user_fields can be -# used to specify the additional user data that you want returned for each user -# e.g. profile_image_url -users = client.get_retweeters(id, user_fields=["profile_image_url"]) +# This endpoint/method allows you to get information about who has Retweeted a +# Tweet -# Print the username and the user's profile image url -for user in users.data: - print(user.username) - print(user.profile_image_url) - \ No newline at end of file +tweet_id = 1460323737035677698 + +# By default, only the ID, name, and username fields of each user will be +# returned +# Additional fields can be retrieved using the user_fields parameter +response = client.get_retweeters(tweet_id, user_fields=["profile_image_url"]) + +for user in response.data: + print(user.username, user.profile_image_url) diff --git a/examples/API_v2/get_tweets.py b/examples/API_v2/get_tweets.py new file mode 100644 index 0000000..cfc74b4 --- /dev/null +++ b/examples/API_v2/get_tweets.py @@ -0,0 +1,20 @@ +import tweepy + + +bearer_token = "" + +client = tweepy.Client(bearer_token) + +# Get Tweets + +# This endpoint/method returns a variety of information about the Tweet(s) +# specified by the requested ID or list of IDs + +tweet_ids = [1460323737035677698, 1293593516040269825, 1293595870563381249] + +# By default, only the ID and text fields of each Tweet will be returned +# Additional fields can be retrieved using the tweet_fields parameter +response = client.get_tweets(tweet_ids, tweet_fields=["created_at"]) + +for tweet in response.data: + print(tweet.id, tweet.created_at) diff --git a/examples/API_v2/get_users.py b/examples/API_v2/get_users.py new file mode 100644 index 0000000..c0785b5 --- /dev/null +++ b/examples/API_v2/get_users.py @@ -0,0 +1,21 @@ +import tweepy + + +bearer_token = "" + +client = tweepy.Client(bearer_token) + +# Get Users + +# This endpoint/method returns a variety of information about one or more users +# specified by the requested IDs or usernames + +user_ids = [2244994945, 6253282] + +# By default, only the ID, name, and username fields of each user will be +# returned +# Additional fields can be retrieved using the user_fields parameter +response = client.get_users(ids=user_ids, user_fields=["profile_image_url"]) + +for user in response.data: + print(user.username, user.profile_image_url) diff --git a/examples/API_v2/get_users_followers.py b/examples/API_v2/get_users_followers.py index cbdee6a..f5f2f7f 100644 --- a/examples/API_v2/get_users_followers.py +++ b/examples/API_v2/get_users_followers.py @@ -1,22 +1,27 @@ import tweepy -# Replace bearer token value with your own bearer_token = "" -# Initializing the Tweepy client client = tweepy.Client(bearer_token) -# Replace user ID -id = 2244994945 +# Get User's Followers -# By default the user ID, name and username are returned. user_fields can be -# used to specify the additional user data that you want returned for each user -# e.g. profile_image_url -users = client.get_users_followers(id, user_fields=["profile_image_url"]) +# This endpoint/method returns a list of users who are followers of the +# specified user ID -# Print the username and the user's profile image url -for user in users.data: - print(user.username) - print(user.profile_image_url) - \ No newline at end of file +user_id = 2244994945 + +# By default, only the ID, name, and username fields of each user will be +# returned +# Additional fields can be retrieved using the user_fields parameter +response = client.get_users_followers( + user_id, user_fields=["profile_image_url"] +) + +for user in response.data: + print(user.username, user.profile_image_url) + +# By default, this endpoint/method returns 100 results +# You can retrieve up to 1000 users by specifying max_results +response = client.get_users_followers(user_id, max_results=1000) diff --git a/examples/API_v2/get_users_mentions.py b/examples/API_v2/get_users_mentions.py index 2835e39..689633c 100644 --- a/examples/API_v2/get_users_mentions.py +++ b/examples/API_v2/get_users_mentions.py @@ -1,19 +1,24 @@ import tweepy -# Replace bearer token value with your own bearer_token = "" -# Initializing the Tweepy client client = tweepy.Client(bearer_token) -# Replace user ID -id = 2244994945 +# Get User's Mentions -# By default the Tweet ID and Tweet text will be returned. -tweets = client.get_users_mentions(id) +# This endpoint/method returns Tweets mentioning a single user specified by the +# requested user ID -# Print the Tweet id -for tweet in tweets.data: +user_id = 2244994945 + +response = client.get_users_mentions(user_id) + +# By default, only the ID and text fields of each Tweet will be returned +for tweet in response.data: print(tweet.id) - \ No newline at end of file + print(tweet.text) + +# By default, the 10 most recent Tweets will be returned +# You can retrieve up to 100 Tweets by specifying max_results +response = client.get_users_mentions(user_id, max_results=100) diff --git a/examples/API_v2/get_users_timeline.py b/examples/API_v2/get_users_timeline.py deleted file mode 100644 index 856c380..0000000 --- a/examples/API_v2/get_users_timeline.py +++ /dev/null @@ -1,19 +0,0 @@ -import tweepy - - -# Replace bearer token value with your own -bearer_token = "" - -# Initializing the Tweepy client -client = tweepy.Client(bearer_token) - -# Replace user ID -id = 2244994945 - -# By default the Tweet ID and Tweet text will be returned. -tweets = client.get_users_tweets(id) - -# Print the Tweet id -for tweet in tweets.data: - print(tweet.id) - \ No newline at end of file diff --git a/examples/API_v2/get_users_tweets.py b/examples/API_v2/get_users_tweets.py new file mode 100644 index 0000000..f7708e3 --- /dev/null +++ b/examples/API_v2/get_users_tweets.py @@ -0,0 +1,24 @@ +import tweepy + + +bearer_token = "" + +client = tweepy.Client(bearer_token) + +# Get User's Tweets + +# This endpoint/method returns Tweets composed by a single user, specified by +# the requested user ID + +user_id = 2244994945 + +response = client.get_users_tweets(user_id) + +# By default, only the ID and text fields of each Tweet will be returned +for tweet in response.data: + print(tweet.id) + print(tweet.text) + +# By default, the 10 most recent Tweets will be returned +# You can retrieve up to 100 Tweets by specifying max_results +response = client.get_users_tweets(user_id, max_results=100) diff --git a/examples/API_v2/lookup_tweets_using_tweet_ids.py b/examples/API_v2/lookup_tweets_using_tweet_ids.py deleted file mode 100644 index 295ac0c..0000000 --- a/examples/API_v2/lookup_tweets_using_tweet_ids.py +++ /dev/null @@ -1,21 +0,0 @@ -import tweepy - - -# Replace bearer token value with your own -bearer_token = "" - -# Initializing the Tweepy client -client = tweepy.Client(bearer_token) - -# Replace Tweet IDs -ids = [1409935014725177344, 1409931481552543749, 1441054496931541004] - -# By default the Tweet ID and Tweet text are returned. If you want additional -# Tweet fields, specify those in tweet_fields -tweets = client.get_tweets(ids=ids, tweet_fields=["created_at"]) - -# Print the Tweet id and the time the Tweet was created -for tweet in tweets.data: - print(tweet.id) - print(tweet.created_at) - \ No newline at end of file diff --git a/examples/API_v2/lookup_users_using_user_ids.py b/examples/API_v2/lookup_users_using_user_ids.py deleted file mode 100644 index 78e50a5..0000000 --- a/examples/API_v2/lookup_users_using_user_ids.py +++ /dev/null @@ -1,22 +0,0 @@ -import tweepy - - -# Replace bearer token value with your own -bearer_token = "" - -# Initializing the Tweepy client -client = tweepy.Client(bearer_token) - -# Replace User IDs -ids = [2244994945, 6253282] - -# By default the user ID, name and username are returned. user_fields can be -# used to specify the additional user data that you want returned for each user -# e.g. profile_image_url -users = client.get_users(ids=ids, user_fields=["profile_image_url"]) - -# Print the username and the user's profile image url -for user in users.data: - print(user.username) - print(user.profile_image_url) - \ No newline at end of file diff --git a/examples/API_v2/recent_tweets_count.py b/examples/API_v2/recent_tweets_count.py deleted file mode 100644 index 5b08871..0000000 --- a/examples/API_v2/recent_tweets_count.py +++ /dev/null @@ -1,19 +0,0 @@ -import tweepy - - -# Replace bearer token value with your own -bearer_token = "" - -# Initializing the Tweepy client -client = tweepy.Client(bearer_token) - -# Replace with your own search query -query = "tweepy -is:retweet" - -# Granularity can be day, hour or minute -counts = client.get_recent_tweets_count(query, granularity="day") - -# Print the daily count of Tweets for your query -for count in counts.data: - print(count) - \ No newline at end of file diff --git a/examples/API_v2/search_recent_tweets.py b/examples/API_v2/search_recent_tweets.py index 377d2f6..97da5a1 100644 --- a/examples/API_v2/search_recent_tweets.py +++ b/examples/API_v2/search_recent_tweets.py @@ -18,9 +18,10 @@ print(response.meta) # objects tweets = response.data -# Each Tweet object has default id and text fields +# Each Tweet object has default ID and text fields for tweet in tweets: print(tweet.id) + print(tweet.text) # By default, this endpoint/method returns 10 results # You can retrieve up to 100 Tweets by specifying max_results -- 2.25.1