From 5fa6e73bbe677cf2527af7187012f665b825639d Mon Sep 17 00:00:00 2001 From: Harmon Date: Sat, 30 Oct 2021 12:23:57 -0500 Subject: [PATCH] Replace Code Snippets page in documentation with Examples --- docs/code_snippet.rst | 64 ------------------------------------------- docs/examples.rst | 27 ++++++++++++++++++ docs/index.rst | 2 +- 3 files changed, 28 insertions(+), 65 deletions(-) delete mode 100644 docs/code_snippet.rst create mode 100644 docs/examples.rst diff --git a/docs/code_snippet.rst b/docs/code_snippet.rst deleted file mode 100644 index bc57ab4..0000000 --- a/docs/code_snippet.rst +++ /dev/null @@ -1,64 +0,0 @@ -.. _code_snippet: - - -************* -Code Snippets -************* - -Introduction -============ - -Here are some code snippets to help you out with using Tweepy. Feel -free to contribute your own snippets or improve the ones here! - -OAuth -===== - -.. code-block :: python - - auth = tweepy.OAuthHandler("consumer_key", "consumer_secret") - - # Redirect user to Twitter to authorize - redirect_user(auth.get_authorization_url()) - - # Get access token - auth.get_access_token("verifier_value") - - # Construct the API instance - api = tweepy.API(auth) - -FollowAll -========= - -This snippet will follow every follower of the authenticated user. - -.. code-block :: python - - for follower in tweepy.Cursor(api.followers).items(): - follower.follow() - -Handling the rate limit using cursors -===================================== - -Since cursors raise ``RateLimitError``\ s while iterating, -handling them can be done by wrapping the cursor in an iterator. - -Running this snippet will print all users you follow that themselves follow -less than 300 people total - to exclude obvious spambots, for example - and -will wait for 15 minutes each time it hits the rate limit. - -.. code-block :: python - - # In this example, the handler is time.sleep(15 * 60), - # but you can of course handle it in any way you want. - - def limit_handled(cursor): - while True: - try: - yield next(cursor) - except tweepy.RateLimitError: - time.sleep(15 * 60) - - for follower in limit_handled(tweepy.Cursor(api.followers).items()): - if follower.friends_count < 300: - print(follower.screen_name) diff --git a/docs/examples.rst b/docs/examples.rst new file mode 100644 index 0000000..5adefcc --- /dev/null +++ b/docs/examples.rst @@ -0,0 +1,27 @@ +.. _examples: + +******** +Examples +******** + +.. tabs:: + + .. tab:: Authentication + + .. literalinclude:: ../examples/authentication.py + + .. tab:: PIN-based Authorization + + .. literalinclude:: ../examples/pin-based_authorization.py + + .. tab:: Tweet / Update Status + + .. literalinclude:: ../examples/update_status.py + + .. tab:: Follow Followers + + .. literalinclude:: ../examples/follow_followers.py + + .. tab:: Handle Rate Limits + + .. literalinclude:: ../examples/rate_limit_handling.py diff --git a/docs/index.rst b/docs/index.rst index 040ffe0..baee10a 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -14,7 +14,6 @@ Contents: install.rst getting_started.rst auth_tutorial.rst - code_snippet.rst api.rst client.rst models.rst @@ -26,6 +25,7 @@ Contents: streaming.rst running_tests.rst changelog.md + examples.rst faq.rst Indices and tables -- 2.25.1