Add methods for trends
This commit is contained in:
parent
2921c10fb6
commit
f0ed4f552a
5 changed files with 137 additions and 24 deletions
|
@ -33,17 +33,24 @@ def twoExceptions(e, message):
|
||||||
|
|
||||||
message.reply_text(text or bottools.strings.twoFail)
|
message.reply_text(text or bottools.strings.twoFail)
|
||||||
|
|
||||||
|
def silence(bot, update):
|
||||||
|
pass
|
||||||
|
|
||||||
def callback(bot, update):
|
def callback(bot, update):
|
||||||
args = update.callback_query.data.split()
|
args = update.callback_query.data.split()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
feature = commands[args[0][1:]]
|
feature = commands[args[0][1:]]
|
||||||
try:
|
try:
|
||||||
feature(bot, update, args[1:])
|
status = feature(bot, update, args[1:])
|
||||||
except:
|
except:
|
||||||
feature(bot, update)
|
status = feature(bot, update)
|
||||||
except:
|
except:
|
||||||
update.callback_query.reply_text(bottools.strings.unknownCommand)
|
update.callback_query.reply_text(bottools.strings.unknownTweet)
|
||||||
|
return
|
||||||
|
|
||||||
|
if status:
|
||||||
|
tweetMessage(status, None, bot, callback = update.callback_query)
|
||||||
|
|
||||||
def mentionHelper(bot, update):
|
def mentionHelper(bot, update):
|
||||||
args = update.message.text.split()
|
args = update.message.text.split()
|
||||||
|
@ -97,6 +104,17 @@ def makeMenu(buttons, columns = 2):
|
||||||
|
|
||||||
# Authentication process
|
# Authentication process
|
||||||
|
|
||||||
|
def shareLocation(bot, update):
|
||||||
|
rmo = telegram.ReplyKeyboardMarkup([[telegram.KeyboardButton(text = "Share location", request_location = True)]])
|
||||||
|
update.message.reply_text(bottools.strings.shareLocation, reply_markup = rmo)
|
||||||
|
|
||||||
|
def storeLocation(bot, update):
|
||||||
|
cid = update.message.chat_id
|
||||||
|
lat = update.message.location.latitude
|
||||||
|
lon = update.message.location.longitude
|
||||||
|
|
||||||
|
dbtools.dbHelper().storeLocation(cid, lat, lon)
|
||||||
|
|
||||||
def auth(bot, update):
|
def auth(bot, update):
|
||||||
db = dbtools.dbHelper()
|
db = dbtools.dbHelper()
|
||||||
cid = update.message.chat_id
|
cid = update.message.chat_id
|
||||||
|
@ -190,8 +208,7 @@ def explicitTweet(bot, update, args, reply = None):
|
||||||
else:
|
else:
|
||||||
status = two.tweet(' '.join(args), reply)
|
status = two.tweet(' '.join(args), reply)
|
||||||
|
|
||||||
if dbtools.dbHelper().getCStatus(update.message.chat_id):
|
bottools.methods.tweetMessage(status, update.message.chat_id, bot)
|
||||||
bottools.methods.tweetMessage(status, update.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, update.message)
|
||||||
|
@ -253,15 +270,18 @@ def quote(bot, update, args):
|
||||||
|
|
||||||
def retweet(bot, update, args):
|
def retweet(bot, update, args):
|
||||||
message = update.message or update.callback_query.message
|
message = update.message or update.callback_query.message
|
||||||
|
two = bottools.methods.getTwo(message)
|
||||||
for tweet in args:
|
for tweet in args:
|
||||||
try:
|
try:
|
||||||
tid = bottools.methods.getTweetID(tweet, message.chat_id)
|
tid = bottools.methods.getTweetID(tweet, message.chat_id)
|
||||||
bottools.methods.getTwo(message).api.retweet(tid)
|
two.api.retweet(tid)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
message.reply_text(bottools.strings.cantfind % tweet)
|
message.reply_text(bottools.strings.cantfind % tweet)
|
||||||
except tweepy.error.TweepError as e:
|
except tweepy.error.TweepError as e:
|
||||||
bottools.methods.twoExceptions(e, message)
|
bottools.methods.twoExceptions(e, message)
|
||||||
|
|
||||||
|
return twitools.getTweet(tid)
|
||||||
|
|
||||||
def thread(bot, update, args):
|
def thread(bot, update, args):
|
||||||
message = update.message or update.callback_query.message
|
message = update.message or update.callback_query.message
|
||||||
tid = bottools.methods.getTweetID(args[0], message.chat_id)
|
tid = bottools.methods.getTweetID(args[0], message.chat_id)
|
||||||
|
@ -290,39 +310,80 @@ def thread(bot, update, args):
|
||||||
|
|
||||||
def like(bot, update, args):
|
def like(bot, update, args):
|
||||||
message = update.message or update.callback_query.message
|
message = update.message or update.callback_query.message
|
||||||
|
two = bottools.methods.getTwo(message)
|
||||||
for tweet in args:
|
for tweet in args:
|
||||||
try:
|
try:
|
||||||
tid = bottools.methods.getTweetID(tweet, message.chat_id)
|
tid = bottools.methods.getTweetID(tweet, message.chat_id)
|
||||||
bottools.methods.getTwo(message).api.create_favorite(tid)
|
two.api.create_favorite(tid)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
message.reply_text(bottools.strings.cantfind % tweet)
|
message.reply_text(bottools.strings.cantfind % tweet)
|
||||||
except tweepy.error.TweepError as e:
|
except tweepy.error.TweepError as e:
|
||||||
bottools.methods.twoExceptions(e, message)
|
bottools.methods.twoExceptions(e, message)
|
||||||
|
|
||||||
|
return twitools.getTweet(tid)
|
||||||
|
|
||||||
|
def unlike(bot, update, args):
|
||||||
|
message = update.message or update.callback_query.message
|
||||||
|
two = bottools.methods.getTwo(message)
|
||||||
|
for tweet in args:
|
||||||
|
try:
|
||||||
|
tid = bottools.methods.getTweetID(tweet, message.chat_id)
|
||||||
|
two.api.destroy_favorite(tid)
|
||||||
|
except ValueError:
|
||||||
|
message.reply_text(bottools.strings.cantfind % tweet)
|
||||||
|
except tweepy.error.TweepError as e:
|
||||||
|
bottools.methods.twoExceptions(e, message)
|
||||||
|
|
||||||
|
return twitools.getTweet(tid)
|
||||||
|
|
||||||
def tweet(bot, update):
|
def tweet(bot, update):
|
||||||
try:
|
try:
|
||||||
if dbtools.dbHelper().getTStatus(update.message.chat_id):
|
if dbtools.dbHelper().getTStatus(update.message.chat_id):
|
||||||
bottools.methods.explicitTweet(bot, update, [update.message.text])
|
bottools.methods.explicitTweet(bot, update, [update.message.text])
|
||||||
except twepy.error.TweepError as e:
|
except tweepy.error.TweepError as e:
|
||||||
bottools.methods.twoExceptions(e, update.message)
|
bottools.methods.twoExceptions(e, update.message)
|
||||||
|
|
||||||
# Timelines
|
# Timelines
|
||||||
|
|
||||||
def tweetMessage(status, cid, bot):
|
def tweetMessage(status, cid, bot, force = False, callback = None):
|
||||||
db = dbtools.dbHelper()
|
db = dbtools.dbHelper()
|
||||||
|
|
||||||
try:
|
if not (force or callback):
|
||||||
db.executeQuery("SELECT MAX(nr) FROM timelines WHERE cid = %i;" % int(cid))
|
try:
|
||||||
i = int(db.getNext()[0]) + 1
|
two = twitools.twoBotHelper(cid)
|
||||||
except:
|
except tweepy.error.TweepError as e:
|
||||||
i = 1
|
logging.exception("I really don't see how this could possibly happen.")
|
||||||
|
|
||||||
db.executeQuery("INSERT INTO timelines VALUES(%i, %i, %i);" % (cid, i, status.id))
|
if not callback:
|
||||||
db.commit()
|
if status.user.screen_name.strip("@") == two.whoami().strip("@") and not db.getCStatus(cid):
|
||||||
|
return
|
||||||
|
|
||||||
buttons = [
|
if not callback:
|
||||||
telegram.InlineKeyboardButton("Like", callback_data = "/like %i" % i),
|
try:
|
||||||
telegram.InlineKeyboardButton("Retweet", callback_data = "/retweet %i" % i),
|
db.executeQuery("SELECT MAX(nr) FROM timelines WHERE cid = %i;" % int(cid))
|
||||||
|
i = int(db.getNext()[0]) + 1
|
||||||
|
except:
|
||||||
|
i = 1
|
||||||
|
|
||||||
|
db.executeQuery("INSERT INTO timelines VALUES(%i, %i, %i);" % (cid, i, status.id))
|
||||||
|
db.commit()
|
||||||
|
|
||||||
|
else:
|
||||||
|
i = int(callback.message.text.split()[1].strip(":"))
|
||||||
|
|
||||||
|
buttons = []
|
||||||
|
|
||||||
|
if status.favorited:
|
||||||
|
buttons += [telegram.InlineKeyboardButton("Unlike", callback_data = "/unlike %i" % i)]
|
||||||
|
else:
|
||||||
|
buttons += [telegram.InlineKeyboardButton("Like", callback_data = "/like %i" % i)]
|
||||||
|
|
||||||
|
if status.retweeted:
|
||||||
|
buttons += [telegram.InlineKeyboardButton("Retweeted", callback_data = "/silence")]
|
||||||
|
else:
|
||||||
|
buttons += [telegram.InlineKeyboardButton("Retweet", callback_data = "/retweet %i" % i)]
|
||||||
|
|
||||||
|
buttons += [
|
||||||
telegram.InlineKeyboardButton("Reply", switch_inline_query_current_chat = "/reply %i " % i),
|
telegram.InlineKeyboardButton("Reply", switch_inline_query_current_chat = "/reply %i " % i),
|
||||||
telegram.InlineKeyboardButton("Quote",switch_inline_query_current_chat = "/quote %i " % i)
|
telegram.InlineKeyboardButton("Quote",switch_inline_query_current_chat = "/quote %i " % i)
|
||||||
]
|
]
|
||||||
|
@ -332,7 +393,39 @@ def tweetMessage(status, cid, bot):
|
||||||
|
|
||||||
rmu = telegram.InlineKeyboardMarkup(makeMenu(buttons))
|
rmu = telegram.InlineKeyboardMarkup(makeMenu(buttons))
|
||||||
|
|
||||||
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)
|
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):
|
||||||
|
try:
|
||||||
|
count = int(args[0])
|
||||||
|
except:
|
||||||
|
count = 5
|
||||||
|
|
||||||
|
two = getTwo(update.message)
|
||||||
|
lat, lon = dbtools.dbHelper().getLocation(update.message.chat_id)
|
||||||
|
|
||||||
|
lt = []
|
||||||
|
|
||||||
|
try:
|
||||||
|
if lat:
|
||||||
|
woeid = two.api.trends_closest(lat, lon)[0]["woeid"]
|
||||||
|
else:
|
||||||
|
woeid = 1
|
||||||
|
|
||||||
|
trends = two.api.trends_place(woeid)[0]['trends']
|
||||||
|
|
||||||
|
outtext = "%s\n" % bottools.strings.trends
|
||||||
|
|
||||||
|
for trend in trends:
|
||||||
|
outtext += "\n%s" % trend['name']
|
||||||
|
|
||||||
|
update.message.reply_text(outtext)
|
||||||
|
|
||||||
|
except tweepy.error.TweepError as e:
|
||||||
|
twoExceptions(e, update.message)
|
||||||
|
|
||||||
def search(bot, update, args):
|
def search(bot, update, args):
|
||||||
try:
|
try:
|
||||||
|
@ -375,7 +468,7 @@ def user(bot, update, args):
|
||||||
lt.reverse()
|
lt.reverse()
|
||||||
|
|
||||||
for status in lt:
|
for status in lt:
|
||||||
tweetMessage(status, update.message.chat_id, bot)
|
tweetMessage(status, update.message.chat_id, bot, True)
|
||||||
|
|
||||||
except tweepy.error.TweepError as e:
|
except tweepy.error.TweepError as e:
|
||||||
bottools.methods.twoExceptions(e, update.message)
|
bottools.methods.twoExceptions(e, update.message)
|
||||||
|
@ -408,7 +501,7 @@ def timeline(bot, update, args = [10]):
|
||||||
lt.reverse()
|
lt.reverse()
|
||||||
|
|
||||||
for status in lt:
|
for status in lt:
|
||||||
tweetMessage(status, update.message.chat_id, bot)
|
tweetMessage(status, update.message.chat_id, bot, True)
|
||||||
|
|
||||||
except tweepy.error.TweepError as e:
|
except tweepy.error.TweepError as e:
|
||||||
bottools.methods.twoExceptions(e, update.message)
|
bottools.methods.twoExceptions(e, update.message)
|
||||||
|
@ -507,6 +600,8 @@ commands = {
|
||||||
"retweet": retweet,
|
"retweet": retweet,
|
||||||
"search": search,
|
"search": search,
|
||||||
"self": selfTweets,
|
"self": selfTweets,
|
||||||
|
"sharelocation": shareLocation,
|
||||||
|
"silence": silence,
|
||||||
"start": start,
|
"start": start,
|
||||||
"thread": thread,
|
"thread": thread,
|
||||||
"timeline": timeline,
|
"timeline": timeline,
|
||||||
|
@ -514,9 +609,11 @@ commands = {
|
||||||
"toggleconfirmations": toggleConfirmations,
|
"toggleconfirmations": toggleConfirmations,
|
||||||
"togglementions": mentionstream,
|
"togglementions": mentionstream,
|
||||||
"toggletweet": toggleTweet,
|
"toggletweet": toggleTweet,
|
||||||
|
"trends": trends,
|
||||||
"tweet": explicitTweet,
|
"tweet": explicitTweet,
|
||||||
"unauth": unauth,
|
"unauth": unauth,
|
||||||
"unfollow": unfollow,
|
"unfollow": unfollow,
|
||||||
|
"unlike": unlike,
|
||||||
"user": user,
|
"user": user,
|
||||||
"verify": verify
|
"verify": verify
|
||||||
}
|
}
|
||||||
|
@ -534,6 +631,8 @@ pargs = [
|
||||||
thread,
|
thread,
|
||||||
timeline,
|
timeline,
|
||||||
toggleTweet,
|
toggleTweet,
|
||||||
|
trends,
|
||||||
|
unlike,
|
||||||
user,
|
user,
|
||||||
explicitTweet,
|
explicitTweet,
|
||||||
unfollow,
|
unfollow,
|
||||||
|
|
|
@ -132,3 +132,7 @@ longTweet = '''Sorry, but this tweet is too long.'''
|
||||||
cantfind = '''Sorry, I can't find a tweet with ID %s. Please check your timeline and try again.'''
|
cantfind = '''Sorry, I can't find a tweet with ID %s. Please check your timeline and try again.'''
|
||||||
|
|
||||||
restart = '''Restarting. Be right back. Hopefully.'''
|
restart = '''Restarting. Be right back. Hopefully.'''
|
||||||
|
|
||||||
|
shareLocation = '''Would you like to share your location with me?'''
|
||||||
|
|
||||||
|
trends = '''The current trends are:'''
|
||||||
|
|
|
@ -117,6 +117,15 @@ class dbObject:
|
||||||
self.executeQuery("INSERT INTO tokens(cid, ato, ase) VALUES(%i, '%s', '%s');" % (int(cid), ato, ase))
|
self.executeQuery("INSERT INTO tokens(cid, ato, ase) VALUES(%i, '%s', '%s');" % (int(cid), ato, ase))
|
||||||
self.commit()
|
self.commit()
|
||||||
|
|
||||||
|
def storeLocation(self, cid, lat, lon):
|
||||||
|
self.executeQuery("UPDATE tokens SET lat = %s, lon = %s WHERE cid = %i;" % (lat, lon, int(cid)))
|
||||||
|
self.commit()
|
||||||
|
|
||||||
|
def getLocation(self, cid):
|
||||||
|
self.executeQuery("SELECT lat, lon FROM tokens WHERE cid = %i;" % int(cid))
|
||||||
|
re = self.getNext()
|
||||||
|
return re[0], re[1]
|
||||||
|
|
||||||
def ato(self, cid):
|
def ato(self, cid):
|
||||||
try:
|
try:
|
||||||
self.executeQuery("SELECT ato FROM tokens WHERE cid = %i;" % int(cid))
|
self.executeQuery("SELECT ato FROM tokens WHERE cid = %i;" % int(cid))
|
||||||
|
|
2
setup.py
2
setup.py
|
@ -65,7 +65,7 @@ if not db.isInitialized():
|
||||||
db.executeQuery("CREATE TABLE names(`id` TEXT NOT NULL, `name` TEXT NOT NULL, `since` INTEGER NOT NULL, `until` INTEGER, PRIMARY KEY(id, until));")
|
db.executeQuery("CREATE TABLE names(`id` TEXT NOT NULL, `name` TEXT NOT NULL, `since` INTEGER NOT NULL, `until` INTEGER, PRIMARY KEY(id, until));")
|
||||||
db.executeQuery("CREATE TABLE retweets(id INT PRIMARY KEY, author VARCHAR(30), created_at VARCHAR(30), text TEXT);")
|
db.executeQuery("CREATE TABLE retweets(id INT PRIMARY KEY, author VARCHAR(30), created_at VARCHAR(30), text TEXT);")
|
||||||
db.executeQuery("CREATE TABLE lyrics(id INTEGER PRIMARY KEY AUTOINCREMENT, text VARCHAR(140) NOT NULL, ref INT NOT NULL default '0', tweet_id INT, active BOOLEAN default '0');")
|
db.executeQuery("CREATE TABLE lyrics(id INTEGER PRIMARY KEY AUTOINCREMENT, text VARCHAR(140) NOT NULL, ref INT NOT NULL default '0', tweet_id INT, active BOOLEAN default '0');")
|
||||||
db.executeQuery("CREATE TABLE tokens(`cid` INT PRIMARY KEY, `ato` TEXT, `ase` TEXT, `tweet` BOOLEAN DEFAULT 1, `fish` INT DEFAULT 0, `mentions` BOOLEAN DEFAULT 0, `broadcast` BOOLEAN DEFAULT 1, `confirmations` BOOLEAN DEFAULT 1);")
|
db.executeQuery("CREATE TABLE tokens(`cid` INT PRIMARY KEY, `ato` TEXT, `ase` TEXT, `tweet` BOOLEAN DEFAULT 1, `fish` INT DEFAULT 0, `mentions` BOOLEAN DEFAULT 0, `broadcast` BOOLEAN DEFAULT 1, `confirmations` BOOLEAN DEFAULT 1, `lat` REAL DEFAULT NULL, `lon` REAL DEFAULT NULL);")
|
||||||
db.executeQuery("CREATE TABLE timelines(`cid` INT, `nr` INT, `tid` INT, PRIMARY KEY(cid, nr));")
|
db.executeQuery("CREATE TABLE timelines(`cid` INT, `nr` INT, `tid` INT, PRIMARY KEY(cid, nr));")
|
||||||
db.commit()
|
db.commit()
|
||||||
|
|
||||||
|
|
|
@ -20,8 +20,9 @@ if __name__ == "__main__":
|
||||||
updater.dispatcher.add_handler(telegram.ext.MessageHandler(telegram.ext.Filters.photo, bottools.methods.tweet))
|
updater.dispatcher.add_handler(telegram.ext.MessageHandler(telegram.ext.Filters.photo, bottools.methods.tweet))
|
||||||
updater.dispatcher.add_handler(telegram.ext.MessageHandler(telegram.ext.Filters.sticker, bottools.methods.tweet))
|
updater.dispatcher.add_handler(telegram.ext.MessageHandler(telegram.ext.Filters.sticker, bottools.methods.tweet))
|
||||||
updater.dispatcher.add_handler(telegram.ext.MessageHandler(telegram.ext.Filters.video, bottools.methods.tweet))
|
updater.dispatcher.add_handler(telegram.ext.MessageHandler(telegram.ext.Filters.video, bottools.methods.tweet))
|
||||||
updater.dispatcher.add_handler(telegram.ext.MessageHandler(telegram.ext.Filters.command, bottools.methods.unknown))
|
|
||||||
updater.dispatcher.add_handler(telegram.ext.MessageHandler(telegram.ext.Filters.document, bottools.methods.tweet))
|
updater.dispatcher.add_handler(telegram.ext.MessageHandler(telegram.ext.Filters.document, bottools.methods.tweet))
|
||||||
|
updater.dispatcher.add_handler(telegram.ext.MessageHandler(telegram.ext.Filters.location, bottools.methods.storeLocation))
|
||||||
|
updater.dispatcher.add_handler(telegram.ext.MessageHandler(telegram.ext.Filters.command, bottools.methods.unknown))
|
||||||
|
|
||||||
updater.dispatcher.add_handler(telegram.ext.CallbackQueryHandler(bottools.methods.callback))
|
updater.dispatcher.add_handler(telegram.ext.CallbackQueryHandler(bottools.methods.callback))
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue