2. twied.twicol
— Twitter Collection¶
-
twied.twicol.
exception_repeat
(func)[source]¶ Function decorator that retrys the function if it throws an exception after a short interval. Interval grows exponentially the more times the function throws an exception. Initial time is 0.5 seconds.
Parameters: func – The function to wrap.
2.1. Collection¶
-
class
twied.twicol.collection.
TweetStreamer
(name, trackstr, db=None, callbacks=None, **kwargs)[source]¶ Threaded class which handles callbacks from the twython streamer and stores tweets in a MongoDB database.
Creates the TweetStreamer. This streamer collects tweets from the Twitter API and stores them in a MongoDB. Can also callback to functions.
Parameters: - name (str) – An indentifier for the collection.
- trackstr (str) – Search string for the twitter API.
- db – The MongoDB collection to put the tweet in. If this parameter is left as None, no database will be written to, instead only the on_add function will be called (optional).
- callbacks – Functions called when a tweet is recieved. Can also be a list of functions which will all be called. Will pass three arguments (name, data, insert_id) being the collection name, tweet data and mongo insert id respectively into the function (optional).
- **kwargs –
Settings for the Twitter API. Suggested example arguments:
- app_key
- app_secret
- oauth_token
- oauth_token_secret
(for more information see the
twython.TwythonStreamer
documentation)
Example: Following example is the usage of the
TweetStreamer
that prints out the ID of each tweet when it is recieved. As the db is None no storage of the tweets is performed. api_settings is a dictionary of the Twitter API settings.>>> def test(name, data, insertid): >>> print("Inserted with ID: %s" % insertid) >>> >>> ts = TweetStreamer("test", "twitter", db=None, callbacks=test, **api_settings) >>> ts.start()
2.2. Counter¶
-
class
twied.twicol.counters.
CounterThread
(interval_s, callbacks)[source]¶ Threaded class which calls a callback at specified interval. Used with the
TweetStreamer
class to gain a callback of the number of tweets collected in the previous interval.Example: Below is an example usage of this class with the
TweetThread
class.>>> def log(count): >>> print("Last 5 seconds: %i tweets" % count) >>> >>> counter = CounterThread(5, log) >>> ts = TweetStreamer("test", "twitter", db=None, callbacks=counter, **api_settings) >>> ts.start() >>> counter.start()
See also
TweetThread
shows the usage of the collection thread.Initialise the counter.
Parameters: - interval_s – The number of seconds between calling the callback function.
- callbacks – The function to call every interval_s seconds. Can also be a list of functions. The function should take a single parameter which is the number of tweets recieved in the last interval.