Example #1
0
import requests
import pause
from typing import Union
from datetime import datetime
from requests_oauthlib import OAuth1Session
import osometweet.utils as o_util

logger = o_util.get_logger(__name__)


#########################################################
#########################################################
# Authentication
#########################################################
class OAuthHandler:
    def __init__(self):
        pass

    def make_request(self):
        pass


class OAuth1a(OAuthHandler):
    def __init__(
        self,
        api_key: str = "",
        api_key_secret: str = "",
        access_token: str = "",
        access_token_secret="",
    ) -> None:
        self._api_key = api_key
"""
This module handles Twitter rate limiting automatically by relying on the
the response objects `x-rate-limit*` parameters as well as HTTP errors.
"""
from datetime import datetime
from osometweet.utils import get_logger, pause_until

logger = get_logger(__name__)

def manage_rate_limits(response):
    """Manage Twitter V2 Rate Limits

    This method takes in a `requests` response object after querying
    Twitter and uses the headers["x-rate-limit-remaining"] and
    headers["x-rate-limit-reset"] headers objects to manage Twitter's
    most common, time-dependent HTTP errors.

    Wiki Reference: https://github.com/osome-iu/osometweet/wiki/Info:-HTTP-Status-Codes-and-Errors
    Twitter Reference: https://developer.twitter.com/en/support/twitter-api/error-troubleshooting
    """

    # The x-rate-limit-remaining parameter is not always present.
    #    If it is, we want to use it.
    try:
        # Get number of requests left with our tokens
        remaining_requests = int(response.headers["x-rate-limit-remaining"])

        # If that number is below 3, we try to get the reset-time
        #   and wait until then, plus 15 seconds (your welcome Twitter).
        # The regular 429 exception is caught below as well,
        #   however, we want to program defensively, where possible.