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):
try:
@ -99,8 +99,13 @@ def toggleConfirmations(bot, update):
def unknown(bot, update):
update.message.reply_text(bottools.strings.unknownCommand)
def makeMenu(buttons, columns = 2):
return [buttons[i:i + columns] for i in range(0, len(buttons), columns)]
def makeMenu(buttons, columns = 2, header = None, footer = None):
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
@ -157,6 +162,13 @@ def verify(bot, update, args):
update.message.reply_text(bottools.strings.verifyimp)
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)
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):
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
break
except:
@ -397,7 +409,7 @@ def tweet(bot, update):
# Timelines
def tweetMessage(status, cid, bot, force = False, callback = None):
def tweetMessage(status, cid, bot, force = False, callback = None, notified = None):
db = dbtools.dbHelper()
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(":"))
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:
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:
buttons += [telegram.InlineKeyboardButton("View Thread", callback_data = "/thread %i" % i)]
rmu = telegram.InlineKeyboardMarkup(makeMenu(buttons))
rmu = telegram.InlineKeyboardMarkup(makeMenu(buttons, header = header))
if callback:
bot.editMessageReplyMarkup(chat_id=callback.message.chat_id, message_id=callback.message.message_id, reply_markup=rmu)
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)
def trends(bot, update, args):
@ -469,17 +489,21 @@ def trends(bot, update, args):
trends = two.api.trends_place(woeid)[0]['trends']
outtext = "%s\n" % bottools.strings.trends
buttons = []
for trend in trends:
outtext += "\n%s" % trend['name']
for trend in trends[:count]:
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:
twoExceptions(e, update.message)
def search(bot, update, args):
message = update.message or update.callback_query.message
try:
count = int(args[0])
query = ' '.join(args[1:])
@ -488,7 +512,7 @@ def search(bot, update, args):
query = ' '.join(args)
try:
two = bottools.methods.getTwo(update.message)
two = bottools.methods.getTwo(message)
lt = []
@ -498,10 +522,10 @@ def search(bot, update, args):
lt.reverse()
for tweet in lt:
tweetMessage(tweet, update.message.chat_id, bot)
tweetMessage(tweet, message.chat_id, bot)
except tweepy.error.TweepError as e:
bottools.methods.twoExceptions(e, update.message)
bottools.methods.twoExceptions(e, message)
def user(bot, update, args):
try:
@ -562,15 +586,15 @@ def timeline(bot, update, args = [10]):
mentionstreams = {}
def makeStream(bot, cid):
two = twitools.twoBotHelper(cid)
stream = tweepy.Stream(auth = two.auth, listener = twitools.streaming.BotStreamListener(bot, cid))
def makeStream(bot, cid, ato, ase):
two = twitools.twObject(ato=ato, ase=ase)
stream = tweepy.Stream(auth = two.auth, listener = bottools.streaming.BotStreamListener(bot, cid, ato))
stream.filter(track=["@%s" % two.whoami().strip("@")], async=True)
return stream
try:
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:
print(e)
@ -578,13 +602,19 @@ def mentionstream(bot, update):
global mentionstreams
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:
mentionstreams.pop(update.message.chat_id).disconnect()
db.toggleMentions(cid)
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)
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)
except tweepy.error.TweepError as e:
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:'''
switch = '''You are now logged in as @%s.'''
messageSwitch = '''Switch to @%s'''

View file

@ -139,6 +139,13 @@ class dbObject:
except:
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):
try:
self.executeQuery("SELECT broadcast FROM tokens WHERE cid = %i AND active;" % int(cid))
@ -194,9 +201,9 @@ class dbObject:
return self.getMStatus(cid)
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():
yield u[0]
yield u[0], u[1], u[2]
def broadcastUsers(self):
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