示例#1
0
def authenticate(fname_api='twitter_api.yml'):
    """Generate a requests.Session object authorized via OAuth-2.

    Twitter API urls:
     - url_api = 'https://api.twitter.com'
     - url_request_oauth1_token = url_api + '/oauth/request_token'
     - url_request_oauth2_token = url_api + '/oauth2/token'
     - url_authorize = url_api + '/oauth/authorize'
     - url_access_token = url_api + '/oauth/access_token'
    """
    # Load application's Twitter API details.
    info_twitter = yaml_io.read(fname_api)

    client_key = info_twitter['consumer_key']
    # client_secret = info_twitter['consumer_secret']
    access_token = info_twitter['access_token']

    # Backend client / desktop client application workflow.
    client = oauthlib.oauth2.BackendApplicationClient(client_key)
    token = {'access_token': access_token, 'token_type': 'Bearer'}

    # Generate requests.Session object.
    session = requests_oauthlib.OAuth2Session(client, token=token)

    return session
def main():
    """
    ---------------------
    Twitter Search Update
    ---------------------

    Usage:
        pdate.py <fname_config> [options]

    Options:
        -z             Enable Z-profile in second window
        -h --help      Show this help message.

    """

    # Parse command line arguments.
    args = docopt.docopt(main.__doc__)

    # Load data from file.
    fname = args['<fname_config>']

    info = yaml_io.read(fname)

    print(info['queries'])
    session = utilities.authenticate()

    path_query = os.path.join(os.path.curdir, 'tweets')

    manager = twitter.Search_Manager(session, query, path_query)

    print('\nStatus')
    print('------')
    print('client_id:  {:s}'.format(session.client_id.client_id))
    print('Limit:      {:d}'.format(manager.api_limit))
    print('Remaining:  {:d}'.format(manager.api_remaining))
    print('Minutes:  {:5.2f}'.format(manager.api_minutes))

    print()
    print('Count: {:d}'.format(manager.count))

    manager.search_continuous()

    print('\nStatus')
    print('------')
    print('client_id:  {:s}'.format(session.client_id.client_id))
    print('Limit:      {:d}'.format(manager.api_limit))
    print('Remaining:  {:d}'.format(manager.api_remaining))
    print('Minutes:  {:5.2f}'.format(manager.api_minutes))

    print()
    print('Count: {:d}'.format(manager.count))
def main():
    """
    ---------------------
    Twitter Search Update
    ---------------------

    Usage:
        pdate.py <fname_config> [options]

    Options:
        -z             Enable Z-profile in second window
        -h --help      Show this help message.

    """

    # Parse command line arguments.
    args = docopt.docopt(main.__doc__)

    # Load data from file.
    fname = args['<fname_config>']

    info = yaml_io.read(fname)

    print(info['queries'])
    session = utilities.authenticate()

    path_query = os.path.join(os.path.curdir, 'tweets')

    manager = twitter.Search_Manager(session, query, path_query)

    print('\nStatus')
    print('------')
    print('client_id:  {:s}'.format(session.client_id.client_id))
    print('Limit:      {:d}'.format(manager.api_limit))
    print('Remaining:  {:d}'.format(manager.api_remaining))
    print('Minutes:  {:5.2f}'.format(manager.api_minutes))

    print()
    print('Count: {:d}'.format(manager.count))

    manager.search_continuous()

    print('\nStatus')
    print('------')
    print('client_id:  {:s}'.format(session.client_id.client_id))
    print('Limit:      {:d}'.format(manager.api_limit))
    print('Remaining:  {:d}'.format(manager.api_remaining))
    print('Minutes:  {:5.2f}'.format(manager.api_minutes))

    print()
    print('Count: {:d}'.format(manager.count))
    def load_config(self):
        try:
            info = yaml_io.read(self.fname_config)
        except IOError:
            info = None

        if info:
            self._limit = int(info['limit'])
            self._remaining = int(info['remaining'])
            self._reset = int(info['reset'])
        else:
            # Set safe default values.
            self._limit = 15 * 60
            self._remaining = 0
            self._reset = None