Implement untested stream handler

This commit is contained in:
Klaus-Uwe Mitterer 2017-03-23 14:18:52 +01:00
parent 5fb84de363
commit 3af38cc3d9
2 changed files with 36 additions and 0 deletions

View file

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

22
twitools/streaming.py Normal file
View file

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