nltk.twitter.api module

This module provides an interface for TweetHandlers, and support for timezone handling.

class nltk.twitter.api.BasicTweetHandler[source]

Bases: object

Minimal implementation of TweetHandler.

Counts the number of Tweets and decides when the client should stop fetching them.

__init__(limit=20)[source]
counter

A flag to indicate to the client whether to stop fetching data given some condition (e.g., reaching a date limit).

do_continue()[source]

Returns False if the client should stop fetching Tweets.

do_stop

Stores the id of the last fetched Tweet to handle pagination.

class nltk.twitter.api.LocalTimezoneOffsetWithUTC[source]

Bases: tzinfo

This is not intended to be a general purpose class for dealing with the local timezone. In particular:

  • it assumes that the date passed has been created using datetime(…, tzinfo=Local), where Local is an instance of the object LocalTimezoneOffsetWithUTC;

  • for such an object, it returns the offset with UTC, used for date comparisons.

Reference: https://docs.python.org/3/library/datetime.html

DSTOFFSET = datetime.timedelta(seconds=7200)
STDOFFSET = datetime.timedelta(seconds=3600)
utcoffset(dt)[source]

Access the relevant time offset.

class nltk.twitter.api.TweetHandlerI[source]

Bases: BasicTweetHandler

Interface class whose subclasses should implement a handle method that Twitter clients can delegate to.

__init__(limit=20, upper_date_limit=None, lower_date_limit=None)[source]
Parameters
  • limit (int) – The number of data items to process in the current round of processing.

  • upper_date_limit (tuple) – The date at which to stop collecting new data. This should be entered as a tuple which can serve as the argument to datetime.datetime. E.g. date_limit=(2015, 4, 1, 12, 40) for 12:30 pm on April 1 2015.

  • lower_date_limit (tuple) – The date at which to stop collecting new data. See upper_data_limit for formatting.

check_date_limit(data, verbose=False)[source]

Validate date limits.

abstract handle(data)[source]

Deal appropriately with data returned by the Twitter API

abstract on_finish()[source]

Actions when the tweet limit has been reached