twitools/twitools/streaming.py

25 lines
762 B
Python
Raw Normal View History

2017-03-23 16:01:05 +00:00
import dbtools, html, tweepy
2017-03-23 13:18:52 +00:00
class BotStreamListener(tweepy.StreamListener):
2017-03-23 16:01:05 +00:00
def __init__(self, bot, cid, *args, **kwargs):
tweepy.StreamListener.__init__(self, *args, **kwargs)
2017-03-23 13:18:52 +00:00
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))
2017-03-23 13:27:38 +00:00
db.commit()
2017-03-23 13:18:52 +00:00
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