Skip to content

Commit

Permalink
First commit to enable callbacks (ckoepp#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
ckoepp committed Feb 15, 2015
1 parent b10b398 commit c3d0306
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
16 changes: 15 additions & 1 deletion TwitterSearch/TwitterSearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ def __init__(self, consumer_key, consumer_secret,
# statistics
self.__statistics = [0,0]

# callback
self.__callback = None

# verify
if "verify" in attr:
self.authenticate(attr["verify"])
Expand Down Expand Up @@ -181,16 +184,23 @@ def check_http_status(self, http_status):
raise TwitterSearchException(http_status,
self.exceptions[http_status])

def search_tweets_iterable(self, order):
def search_tweets_iterable(self, order, callback=None):
""" Returns itself and queries the Twitter API. Is called when using \
an instance of this class as iterable. \
See `Basic usage <basic_usage.html>`_ for examples
:param order: An instance of TwitterOrder class \
(e.g. TwitterSearchOrder or TwitterUserOrder)
:param callback: Function to be called after a new page \
is queried from the Twitter API
:returns: Itself using ``self`` keyword
"""

if callback:
if not callable(callback):
raise TwitterSearchException(1018)
self.__callback = callback

self.search_tweets(order)
return self

Expand Down Expand Up @@ -242,6 +252,10 @@ def send_search(self, url):
self.__statistics[0] += 1
self.__statistics[1] += seen_tweets

# call callback if available
if self.__callback:
self.__callback(self)

# if we've seen the correct amount of tweets there may be some more
# using IDs to request more results
# (former versions used page parameter)
Expand Down
1 change: 1 addition & 0 deletions TwitterSearch/TwitterSearchException.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class TwitterSearchException(Exception):
1015: 'No keywords given',
1016: 'Invalid dict',
1017: 'Invalid argument: need either a user ID or a screen-name',
1018: 'Not a callable function',
}

def __init__(self, code, msg=None):
Expand Down

0 comments on commit c3d0306

Please sign in to comment.