From: Harmon Date: Sun, 2 Jan 2022 00:29:34 +0000 (-0600) Subject: Add API v2 examples X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=bbdbb7bbd7f3eb0d3c46d970aa14098d37857053;p=tweepy.git Add API v2 examples --- diff --git a/docs/examples.rst b/docs/examples.rst index 541a3ca..c564066 100644 --- a/docs/examples.rst +++ b/docs/examples.rst @@ -6,26 +6,54 @@ Examples .. tabs:: - .. tab:: Authentication + .. tab:: API v1.1 - .. literalinclude:: ../examples/authentication.py + .. tabs:: - .. tab:: PIN-based Authorization + .. tab:: Authentication - .. literalinclude:: ../examples/pin-based_authorization.py + .. literalinclude:: ../examples/API_v1/authentication.py - .. tab:: Tweet / Update Status + .. tab:: PIN-based Authorization - .. literalinclude:: ../examples/update_status.py + .. literalinclude:: ../examples/API_v1/pin-based_authorization.py - .. tab:: Follow Followers + .. tab:: Tweet / Update Status - .. literalinclude:: ../examples/follow_followers.py + .. literalinclude:: ../examples/API_v1/update_status.py - .. tab:: Handle Rate Limits + .. tab:: Follow Followers - .. literalinclude:: ../examples/rate_limit_handling.py + .. literalinclude:: ../examples/API_v1/follow_followers.py - .. tab:: Streaming + .. tab:: Handle Rate Limits - .. literalinclude:: ../examples/streaming.py + .. literalinclude:: ../examples/API_v1/rate_limit_handling.py + + .. tab:: Streaming + + .. literalinclude:: ../examples/API_v1/streaming.py + + .. tab:: API v2 + + .. tabs:: + + .. tab:: Authentication + + .. literalinclude:: ../examples/API_v2/authentication.py + + .. tab:: Search Recent Tweets + + .. literalinclude:: ../examples/API_v2/search_recent_tweets.py + + .. tab:: Tweet Fields + + .. literalinclude:: ../examples/API_v2/tweet_fields.py + + .. tab:: Expansions + + .. literalinclude:: ../examples/API_v2/expansions.py + + .. tab:: Create Tweet + + .. literalinclude:: ../examples/API_v2/create_tweet.py diff --git a/examples/authentication.py b/examples/API_v1/authentication.py similarity index 100% rename from examples/authentication.py rename to examples/API_v1/authentication.py diff --git a/examples/follow_followers.py b/examples/API_v1/follow_followers.py similarity index 100% rename from examples/follow_followers.py rename to examples/API_v1/follow_followers.py diff --git a/examples/pin-based_authorization.py b/examples/API_v1/pin-based_authorization.py similarity index 100% rename from examples/pin-based_authorization.py rename to examples/API_v1/pin-based_authorization.py diff --git a/examples/rate_limit_handling.py b/examples/API_v1/rate_limit_handling.py similarity index 100% rename from examples/rate_limit_handling.py rename to examples/API_v1/rate_limit_handling.py diff --git a/examples/streaming.py b/examples/API_v1/streaming.py similarity index 100% rename from examples/streaming.py rename to examples/API_v1/streaming.py diff --git a/examples/update_status.py b/examples/API_v1/update_status.py similarity index 100% rename from examples/update_status.py rename to examples/API_v1/update_status.py diff --git a/examples/API_v2/authentication.py b/examples/API_v2/authentication.py new file mode 100644 index 0000000..f352cfe --- /dev/null +++ b/examples/API_v2/authentication.py @@ -0,0 +1,37 @@ +import tweepy + +# Your app's bearer token can be found under the Authentication Tokens section +# of the Keys and Tokens tab of your app, under the +# Twitter Developer Portal Projects & Apps page at +# https://developer.twitter.com/en/portal/projects-and-apps +bearer_token = "" + +# Your app's API/consumer key and secret can be found under the Consumer Keys +# section of the Keys and Tokens tab of your app, under the +# Twitter Developer Portal Projects & Apps page at +# https://developer.twitter.com/en/portal/projects-and-apps +consumer_key = "" +consumer_secret = "" + +# Your account's (the app owner's account's) access token and secret for your +# app can be found under the Authentication Tokens section of the +# Keys and Tokens tab of your app, under the +# Twitter Developer Portal Projects & Apps page at +# https://developer.twitter.com/en/portal/projects-and-apps +access_token = "" +access_token_secret = "" + +# You can authenticate as your app with just your bearer token +client = tweepy.Client(bearer_token=bearer_token) + +# Alternatively, you can provide the consumer key and secret +client = tweepy.Client( + consumer_key=consumer_key, consumer_secret=consumer_secret +) + +# You can provide the consumer key and secret with the access token and access +# token secret to authenticate as a user +client = tweepy.Client( + consumer_key=consumer_key, consumer_secret=consumer_secret, + access_token=access_token, access_token_secret=access_token_secret +) diff --git a/examples/API_v2/create_tweet.py b/examples/API_v2/create_tweet.py new file mode 100644 index 0000000..bf671da --- /dev/null +++ b/examples/API_v2/create_tweet.py @@ -0,0 +1,28 @@ +import tweepy + + +consumer_key = "" +consumer_secret = "" +access_token = "" +access_token_secret = "" + +client = tweepy.Client( + consumer_key=consumer_key, consumer_secret=consumer_secret, + access_token=access_token, access_token_secret=access_token_secret +) + +# Create Tweet + +# The app and the corresponding credentials must have the Write permission + +# Check the App permissions section of the Settings tab of your app, under the +# Twitter Developer Portal Projects & Apps page at +# https://developer.twitter.com/en/portal/projects-and-apps + +# Make sure to reauthorize your app / regenerate your access token and secret +# after setting the Write permission + +response = client.create_tweet( + text="This Tweet was Tweeted using Tweepy and Twitter API v2!" +) +print(f"https://twitter.com/user/status/{response.data['id']}") diff --git a/examples/API_v2/expansions.py b/examples/API_v2/expansions.py new file mode 100644 index 0000000..358ad00 --- /dev/null +++ b/examples/API_v2/expansions.py @@ -0,0 +1,28 @@ +import tweepy + + +bearer_token = "" + +client = tweepy.Client(bearer_token) + +# You can specify expansions to retrieve additional objects that relate to the +# returned results +response = client.search_recent_tweets( + "Tweepy", expansions=["attachments.media_keys", "author_id"] +) +tweets = response.data + +# You can then access those objects in the includes Response field +includes = response.includes +users = includes["users"] + +# The IDs that represent the expanded objects are included directly in the +# returned data objects +for tweet in tweets: + print(tweet.author_id) + +# An efficient way of matching expanded objects to each data object is to +# create a dictionary of each type of expanded object, with IDs as keys +users = {user["id"]: user for user in users} +for tweet in tweets: + print(tweet.id, users[tweet.author_id].username) diff --git a/examples/API_v2/search_recent_tweets.py b/examples/API_v2/search_recent_tweets.py new file mode 100644 index 0000000..377d2f6 --- /dev/null +++ b/examples/API_v2/search_recent_tweets.py @@ -0,0 +1,27 @@ +import tweepy + + +bearer_token = "" + +client = tweepy.Client(bearer_token) + +# Search Recent Tweets + +# This endpoint/method returns Tweets from the last seven days + +response = client.search_recent_tweets("Tweepy") +# The method returns a Response object, a named tuple with data, includes, +# errors, and meta fields +print(response.meta) + +# In this case, the data field of the Response returned is a list of Tweet +# objects +tweets = response.data + +# Each Tweet object has default id and text fields +for tweet in tweets: + print(tweet.id) + +# By default, this endpoint/method returns 10 results +# You can retrieve up to 100 Tweets by specifying max_results +response = client.search_recent_tweets("Tweepy", max_results=100) diff --git a/examples/API_v2/tweet_fields.py b/examples/API_v2/tweet_fields.py new file mode 100644 index 0000000..f8d1ef2 --- /dev/null +++ b/examples/API_v2/tweet_fields.py @@ -0,0 +1,24 @@ +import tweepy + + +bearer_token = "" + +client = tweepy.Client(bearer_token) + +# You can specify additional Tweet fields to retrieve using tweet_fields +response = client.search_recent_tweets( + "Tweepy", tweet_fields=["created_at", "lang"] +) +tweets = response.data + +# You can then access those fields as attributes of the Tweet objects +for tweet in tweets: + print(tweet.id, tweet.lang) + +# Alternatively, you can also access fields as keys, like a dictionary +for tweet in tweets: + print(tweet["id"], tweet["lang"]) + +# There’s also a data attribute/key that provides the entire data dictionary +for tweet in tweets: + print(tweet.data)