Improve API v2 examples
authorHarmon <Harmon758@gmail.com>
Wed, 16 Mar 2022 18:09:51 +0000 (13:09 -0500)
committerHarmon <Harmon758@gmail.com>
Wed, 16 Mar 2022 18:09:51 +0000 (13:09 -0500)
15 files changed:
docs/examples.rst
examples/API_v2/get_liked_tweets.py
examples/API_v2/get_liking_users.py
examples/API_v2/get_recent_tweets_count.py [new file with mode: 0644]
examples/API_v2/get_retweeters.py
examples/API_v2/get_tweets.py [new file with mode: 0644]
examples/API_v2/get_users.py [new file with mode: 0644]
examples/API_v2/get_users_followers.py
examples/API_v2/get_users_mentions.py
examples/API_v2/get_users_timeline.py [deleted file]
examples/API_v2/get_users_tweets.py [new file with mode: 0644]
examples/API_v2/lookup_tweets_using_tweet_ids.py [deleted file]
examples/API_v2/lookup_users_using_user_ids.py [deleted file]
examples/API_v2/recent_tweets_count.py [deleted file]
examples/API_v2/search_recent_tweets.py

index 30c788467c08a7fa5e040a3ccb2dd81a8bde6a47..d9157d444ab79eb8b5a2818342f1da447f0a9acc 100644 (file)
@@ -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 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
index 021d4b60f901882657c71719d0e885c14d978ff3..7c71ce6c94eba164430b6f4a21e5410b54036287 100644 (file)
@@ -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)
index 97214782f32ed9d0d5239e25a71c5e697cd32f9d..518043dce2c4715c14f16f0e04f29e08355e7cf9 100644 (file)
@@ -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 (file)
index 0000000..173496d
--- /dev/null
@@ -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)
index ade644c1eeaed251e9efa689703e3b362d762bda..9e6c29140f190806ab71f4d7c8218a675b7300f9 100644 (file)
@@ -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 (file)
index 0000000..cfc74b4
--- /dev/null
@@ -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 (file)
index 0000000..c0785b5
--- /dev/null
@@ -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)
index cbdee6afc9b2ec4ece7d5a0e4f89335d15b1d9f6..f5f2f7f63b8e5dbc0a67655fcb5ea57d4c6df136 100644 (file)
@@ -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)
index 2835e3976a0bc268d0ffe047042d1ca8feb156d6..689633c1107ae8bf613084fd8609dd7a17b44e94 100644 (file)
@@ -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 (file)
index 856c380..0000000
+++ /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 (file)
index 0000000..f7708e3
--- /dev/null
@@ -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 (file)
index 295ac0c..0000000
+++ /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 (file)
index 78e50a5..0000000
+++ /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 (file)
index 5b08871..0000000
+++ /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
index 377d2f6059180f5cb47910682286ca2a91a6084d..97da5a1946f66f6bb0f5dd396cf80d71bf4ff48e 100644 (file)
@@ -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