Implement remove hashtag method.

Can´t explain signal handling problem yet :s
Couldnt simplify and reproduce yet either
This commit is contained in:
Julien Lengrand-Lambert
2013-01-10 17:12:36 +01:00
parent fb888ebb16
commit b2b120d81c
4 changed files with 36 additions and 11 deletions

3
.gitignore vendored
View File

@@ -2,4 +2,5 @@ oauth.keys
basic.keys
*.log
lib_tests/tweepy
*.pyc
*.pyc
test.py

Binary file not shown.

View File

@@ -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()
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()

View File

@@ -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")
h.add_hashtag("#WTF")
sleep(2)
h.add_hashtag("#fuck")
h.remove_hashtag("#WTF")