More stuff regarding multiple accounts

This commit is contained in:
Klaus-Uwe Mitterer 2017-03-27 19:08:02 +02:00
parent b2da5a1f85
commit 0edc96940b
4 changed files with 62 additions and 37 deletions

View file

@ -1,4 +1,4 @@
import ast, dbtools, html, io, logging, moviepy.editor, os, PIL.Image, random, re, setuptools, string, bottools.strings, sys, telegram.ext, telegram, time, twitools, twitools.streaming, urllib.request, tweepy import ast, dbtools, html, io, logging, moviepy.editor, os, PIL.Image, random, re, setuptools, string, bottools.strings, sys, telegram.ext, telegram, time, twitools, bottools.streaming, urllib.request, tweepy
def getTwo(message): def getTwo(message):
try: try:
@ -99,8 +99,13 @@ def toggleConfirmations(bot, update):
def unknown(bot, update): def unknown(bot, update):
update.message.reply_text(bottools.strings.unknownCommand) update.message.reply_text(bottools.strings.unknownCommand)
def makeMenu(buttons, columns = 2): def makeMenu(buttons, columns = 2, header = None, footer = None):
return [buttons[i:i + columns] for i in range(0, len(buttons), columns)] menu = [buttons[i:i + columns] for i in range(0, len(buttons), columns)]
if header:
menu.insert(0, header)
if footer:
menu.append(footer)
return menu
# Authentication process # Authentication process
@ -157,6 +162,13 @@ def verify(bot, update, args):
update.message.reply_text(bottools.strings.verifyimp) update.message.reply_text(bottools.strings.verifyimp)
def unauth(bot, update): def unauth(bot, update):
global mentionstreams
ato = dbtools.dbHelper().ato(update.message.chat_id)
if ato in mentionstreams:
mentionstreams.pop(ato).disconnect()
dbtools.dbHelper().deleteUser(update.message.chat_id) dbtools.dbHelper().deleteUser(update.message.chat_id)
update.message.reply_text(bottools.strings.unauth % setuptools.url()) update.message.reply_text(bottools.strings.unauth % setuptools.url())
@ -167,7 +179,7 @@ def switch(bot, update, args):
for a in dbtools.dbHelper().accounts(cid): for a in dbtools.dbHelper().accounts(cid):
try: try:
if twitools.twObject(ato=a[0], ase=a[1]).whoami().strip("@") == args[0].strip("@"): if twitools.twObject(ato=a[0], ase=a[1]).whoami().strip("@").lower() == args[0].strip("@").lower():
found = a found = a
break break
except: except:
@ -397,7 +409,7 @@ def tweet(bot, update):
# Timelines # Timelines
def tweetMessage(status, cid, bot, force = False, callback = None): def tweetMessage(status, cid, bot, force = False, callback = None, notified = None):
db = dbtools.dbHelper() db = dbtools.dbHelper()
if not (force or callback): if not (force or callback):
@ -424,6 +436,13 @@ def tweetMessage(status, cid, bot, force = False, callback = None):
i = int(callback.message.text.split()[1].strip(":")) i = int(callback.message.text.split()[1].strip(":"))
buttons = [] buttons = []
header = None
if notified and notified != dbtools.dbHelper().ato(cid):
ase = db.aseByAto(notified)
oac = twitools.twObject(ato=notified, ase=ase).whoami()
header = [telegram.InlineKeyboardButton(bottools.strings.messageSwitch % oac, callback_data = "/switch %s" % oac)]
if status.favorited: if status.favorited:
buttons += [telegram.InlineKeyboardButton("Unlike", callback_data = "/unlike %i" % i)] buttons += [telegram.InlineKeyboardButton("Unlike", callback_data = "/unlike %i" % i)]
@ -443,11 +462,12 @@ def tweetMessage(status, cid, bot, force = False, callback = None):
if status.in_reply_to_status_id: if status.in_reply_to_status_id:
buttons += [telegram.InlineKeyboardButton("View Thread", callback_data = "/thread %i" % i)] buttons += [telegram.InlineKeyboardButton("View Thread", callback_data = "/thread %i" % i)]
rmu = telegram.InlineKeyboardMarkup(makeMenu(buttons)) rmu = telegram.InlineKeyboardMarkup(makeMenu(buttons, header = header))
if callback: if callback:
bot.editMessageReplyMarkup(chat_id=callback.message.chat_id, message_id=callback.message.message_id, reply_markup=rmu) bot.editMessageReplyMarkup(chat_id=callback.message.chat_id, message_id=callback.message.message_id, reply_markup=rmu)
else: else:
bot.sendMessage(chat_id = 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)), reply_markup=rmu) bot.sendMessage(chat_id = 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)), reply_markup=rmu)
def trends(bot, update, args): def trends(bot, update, args):
@ -469,17 +489,21 @@ def trends(bot, update, args):
trends = two.api.trends_place(woeid)[0]['trends'] trends = two.api.trends_place(woeid)[0]['trends']
outtext = "%s\n" % bottools.strings.trends buttons = []
for trend in trends: for trend in trends[:count]:
outtext += "\n%s" % trend['name'] buttons += [telegram.InlineKeyboardButton(trend['name'], callback_data = "/search %s" % trend['name'])]
update.message.reply_text(outtext) rmo = telegram.InlineKeyboardMarkup(makeMenu(buttons))
update.message.reply_text(bottools.strings.trends, reply_markup = rmo)
except tweepy.error.TweepError as e: except tweepy.error.TweepError as e:
twoExceptions(e, update.message) twoExceptions(e, update.message)
def search(bot, update, args): def search(bot, update, args):
message = update.message or update.callback_query.message
try: try:
count = int(args[0]) count = int(args[0])
query = ' '.join(args[1:]) query = ' '.join(args[1:])
@ -488,7 +512,7 @@ def search(bot, update, args):
query = ' '.join(args) query = ' '.join(args)
try: try:
two = bottools.methods.getTwo(update.message) two = bottools.methods.getTwo(message)
lt = [] lt = []
@ -498,10 +522,10 @@ def search(bot, update, args):
lt.reverse() lt.reverse()
for tweet in lt: for tweet in lt:
tweetMessage(tweet, update.message.chat_id, bot) tweetMessage(tweet, message.chat_id, bot)
except tweepy.error.TweepError as e: except tweepy.error.TweepError as e:
bottools.methods.twoExceptions(e, update.message) bottools.methods.twoExceptions(e, message)
def user(bot, update, args): def user(bot, update, args):
try: try:
@ -562,15 +586,15 @@ def timeline(bot, update, args = [10]):
mentionstreams = {} mentionstreams = {}
def makeStream(bot, cid): def makeStream(bot, cid, ato, ase):
two = twitools.twoBotHelper(cid) two = twitools.twObject(ato=ato, ase=ase)
stream = tweepy.Stream(auth = two.auth, listener = twitools.streaming.BotStreamListener(bot, cid)) stream = tweepy.Stream(auth = two.auth, listener = bottools.streaming.BotStreamListener(bot, cid, ato))
stream.filter(track=["@%s" % two.whoami().strip("@")], async=True) stream.filter(track=["@%s" % two.whoami().strip("@")], async=True)
return stream return stream
try: try:
for u in dbtools.dbHelper().mentionsOn(): for u in dbtools.dbHelper().mentionsOn():
mentionstreams[u] = makeStream(telegram.Bot(token=setuptools.token()), u) mentionstreams[u[1]] = makeStream(telegram.Bot(token=setuptools.token()), u[0], u[1], u[2])
except Exception as e: except Exception as e:
print(e) print(e)
@ -578,13 +602,19 @@ def mentionstream(bot, update):
global mentionstreams global mentionstreams
try: try:
dbtools.dbHelper().toggleMentions(update.message.chat_id) message = update.message or update.callback_query.message
db = dbtools.dbHelper()
cid = message.chat_id
if update.message.chat_id in mentionstreams: db.toggleMentions(cid)
mentionstreams.pop(update.message.chat_id).disconnect() ato = db.ato(cid)
ase = db.ase(cid)
if ato in mentionstreams:
mentionstreams.pop(ato).disconnect()
update.message.reply_text(bottools.strings.toggleMentions % bottools.strings.toggleTweetOff) update.message.reply_text(bottools.strings.toggleMentions % bottools.strings.toggleTweetOff)
else: else:
mentionstreams[update.message.chat_id] = makeStream(bot, update.message.chat_id) mentionstreams[ato] = makeStream(bot, cid, ato, ase)
update.message.reply_text(bottools.strings.toggleMentions % bottools.strings.toggleTweetOn) update.message.reply_text(bottools.strings.toggleMentions % bottools.strings.toggleTweetOn)
except tweepy.error.TweepError as e: except tweepy.error.TweepError as e:
bottools.methods.twoExceptions(e, update.message) bottools.methods.twoExceptions(e, update.message)

View file

@ -152,3 +152,5 @@ accounts = '''You are currently logged in as @%s.
You can use the following accounts:''' You can use the following accounts:'''
switch = '''You are now logged in as @%s.''' switch = '''You are now logged in as @%s.'''
messageSwitch = '''Switch to @%s'''

View file

@ -139,6 +139,13 @@ class dbObject:
except: except:
return False return False
def aseByAto(self, ato):
try:
self.executeQuery("SELECT ase FROM tokens WHERE ato = '%s';" % ato)
return self.cur.fetchone()[0]
except:
return False
def getBStatus(self, cid): def getBStatus(self, cid):
try: try:
self.executeQuery("SELECT broadcast FROM tokens WHERE cid = %i AND active;" % int(cid)) self.executeQuery("SELECT broadcast FROM tokens WHERE cid = %i AND active;" % int(cid))
@ -194,9 +201,9 @@ class dbObject:
return self.getMStatus(cid) return self.getMStatus(cid)
def mentionsOn(self): def mentionsOn(self):
self.executeQuery("SELECT cid FROM tokens WHERE mentions AND active;") self.executeQuery("SELECT cid, ato, ase FROM tokens WHERE mentions;")
for u in self.getAll(): for u in self.getAll():
yield u[0] yield u[0], u[1], u[2]
def broadcastUsers(self): def broadcastUsers(self):
self.executeQuery("SELECT cid FROM tokens WHERE broadcast AND active;") self.executeQuery("SELECT cid FROM tokens WHERE broadcast AND active;")

View file

@ -1,14 +0,0 @@
import bottools.methods, dbtools, html, tweepy
class BotStreamListener(tweepy.StreamListener):
def __init__(self, bot, cid, *args, **kwargs):
tweepy.StreamListener.__init__(self, *args, **kwargs)
self.bot = bot
self.cid = cid
def on_status(self, status):
bottools.methods.tweetMessage(status, self.cid, self.bot)
def on_error(self, status):
if status == 420:
return False