diff --git a/.gitignore b/.gitignore index 234f01f..50e4212 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ oauth.keys basic.keys *.log lib_tests/tweepy -*.pyc \ No newline at end of file +*.pyc +test.py diff --git a/twiderboard.db b/twiderboard.db index 7f75c5c..28cb43a 100644 Binary files a/twiderboard.db and b/twiderboard.db differ diff --git a/twiderboard/streamer.py b/twiderboard/streamer.py index 389d5bf..06f6ddc 100644 --- a/twiderboard/streamer.py +++ b/twiderboard/streamer.py @@ -148,20 +148,25 @@ class Authentification(AuthHandler): #-------------------------- class HashtagLogger(): - def __init__(self, engine_url, hashtag, oauth=True): + def __init__(self, engine_url, oauth=True): self.engine_url = engine_url - self.trendy = [hashtag] + self.trendy = [] self.oauth = oauth # Boolean defining whether we use oauth or not self.stream = None + self.auth = Authentification(oauth=self.oauth) + + def start(self): + if len(self.trendy) > 0: + listener = StreamSaverListener(self.trendy, self.engine_url) - listener = StreamSaverListener(self.trendy, self.engine_url) - auth = Authentification(oauth=self.oauth) - - self.stream = Stream(auth.get_auth(), listener) - self.stream.filter(track=self.trendy, async=True) + self.stream = Stream(self.auth.get_auth(), listener) + print self.trendy + self.stream.filter(track=self.trendy, async=True) + else: + print "No hashtag to track!" def stop(self): if self.stream is not None: @@ -169,6 +174,7 @@ class HashtagLogger(): def add_hashtag(self, hashtag): """ + FIXME: Check if starts with # Adds hashtag to the list of trendy hashtag to be listened to. Hashtag is not added if already present. @@ -179,4 +185,17 @@ class HashtagLogger(): self.trendy.append(hashtag) self.stop() - self.start() \ No newline at end of file + self.start() + + def remove_hashtag(self, hashtag): + """ + FIXME: Check if starts with # + Removes hashtag to the list of trendy hashtag to be listened to. + The streaming connexion is reinitialized to take the new filter into + account. + """ + if hashtag in self.trendy: + self.trendy.remove(hashtag) + + self.stop() + self.start() diff --git a/twiderboard/streaming_trending.py b/twiderboard/streaming_trending.py index bf1cc94..5f73c1e 100755 --- a/twiderboard/streaming_trending.py +++ b/twiderboard/streaming_trending.py @@ -25,8 +25,13 @@ def stop_handler(signal, frame): # registering the signal signal.signal(signal.SIGINT, stop_handler) -h = HashtagLogger(engine_url, "#nowplaying", oauth=True) +h = HashtagLogger(engine_url, oauth=True) h.start() print "Press CTRL + C to stop application" +h.add_hashtag("#nowplaying") h.add_hashtag("#blessed") -h.add_hashtag("#WTF") \ No newline at end of file +h.add_hashtag("#WTF") +sleep(2) +h.add_hashtag("#fuck") + +h.remove_hashtag("#WTF")