diff --git a/bottools/methods.py b/bottools/methods.py index efd836f..037434b 100644 --- a/bottools/methods.py +++ b/bottools/methods.py @@ -218,6 +218,20 @@ def timeline(bot, update, args = [10]): db.commit() +# Streaming +mentionstreams = {} + +def mentionstream(bot, update): + global mentionstreams + + if update.message.chat_id in mentionstreams: + mentionstreams.pop(update.message.chat_id).disconnect() + else: + two = getTwo(update.message) + stream = tweepy.Stream(auth = two.auth, listener = twitools.streaming.BotStreamListener()) + stream.filter(track=[two.whoami()], async = True) + mentionstreams[update.message.chat_id] = stream + # Administrator def isadmin(message): diff --git a/twitools/streaming.py b/twitools/streaming.py new file mode 100644 index 0000000..645ac36 --- /dev/null +++ b/twitools/streaming.py @@ -0,0 +1,22 @@ +import dbtools, tweepy + +class BotStreamListener(tweepy.StreamListener): + def __init__(self, bot, cid): + self.bot = bot + self.cid = cid + + def on_status(self, status): + db = dbtools.dbHelper() + try: + db.executeQuery("SELECT MAX(nr) FROM timelines WHERE cid = %i;" % self.cid) + i = int(db.getNext()[0]) + 1 + except: + i = 1 + + db.executeQuery("INSERT INTO timelines VALUES(%i, %i, %i);" % (self.cid, i, status.id)) + + self.bot.sendMessage(chat_id=self.cid, text="Tweet %i:\n%s (@%s) at %s:\n%s" % (i, status.author.name, status.author.screen_name, status.created_at, html.unescape(status.text))) + + def on_error(self, status): + if status == 420: + return False