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)
|
||||
|
||||
def silence(bot, update):
|
||||
pass
|
||||
|
||||
def callback(bot, update):
|
||||
args = update.callback_query.data.split()
|
||||
|
||||
try:
|
||||
feature = commands[args[0][1:]]
|
||||
try:
|
||||
feature(bot, update, args[1:])
|
||||
status = feature(bot, update, args[1:])
|
||||
except:
|
||||
feature(bot, update)
|
||||
status = feature(bot, update)
|
||||
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):
|
||||
args = update.message.text.split()
|
||||
|
@ -97,6 +104,17 @@ def makeMenu(buttons, columns = 2):
|
|||
|
||||
# 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):
|
||||
db = dbtools.dbHelper()
|
||||
cid = update.message.chat_id
|
||||
|
@ -190,8 +208,7 @@ def explicitTweet(bot, update, args, reply = None):
|
|||
else:
|
||||
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:
|
||||
bottools.methods.twoExceptions(e, update.message)
|
||||
|
@ -253,15 +270,18 @@ def quote(bot, update, args):
|
|||
|
||||
def retweet(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)
|
||||
bottools.methods.getTwo(message).api.retweet(tid)
|
||||
two.api.retweet(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 thread(bot, update, args):
|
||||
message = update.message or update.callback_query.message
|
||||
tid = bottools.methods.getTweetID(args[0], message.chat_id)
|
||||
|
@ -290,39 +310,80 @@ def thread(bot, update, args):
|
|||
|
||||
def like(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)
|
||||
bottools.methods.getTwo(message).api.create_favorite(tid)
|
||||
two.api.create_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 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):
|
||||
try:
|
||||
if dbtools.dbHelper().getTStatus(update.message.chat_id):
|
||||
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)
|
||||
|
||||
# Timelines
|
||||
|
||||
def tweetMessage(status, cid, bot):
|
||||
def tweetMessage(status, cid, bot, force = False, callback = None):
|
||||
db = dbtools.dbHelper()
|
||||
|
||||
try:
|
||||
db.executeQuery("SELECT MAX(nr) FROM timelines WHERE cid = %i;" % int(cid))
|
||||
i = int(db.getNext()[0]) + 1
|
||||
except:
|
||||
i = 1
|
||||
if not (force or callback):
|
||||
try:
|
||||
two = twitools.twoBotHelper(cid)
|
||||
except tweepy.error.TweepError as e:
|
||||
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))
|
||||
db.commit()
|
||||
if not callback:
|
||||
if status.user.screen_name.strip("@") == two.whoami().strip("@") and not db.getCStatus(cid):
|
||||
return
|
||||
|
||||
buttons = [
|
||||
telegram.InlineKeyboardButton("Like", callback_data = "/like %i" % i),
|
||||
telegram.InlineKeyboardButton("Retweet", callback_data = "/retweet %i" % i),
|
||||
if not callback:
|
||||
try:
|
||||
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("Quote",switch_inline_query_current_chat = "/quote %i " % i)
|
||||
]
|
||||
|
@ -332,7 +393,39 @@ def tweetMessage(status, cid, bot):
|
|||
|
||||
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):
|
||||
try:
|
||||
|
@ -375,7 +468,7 @@ def user(bot, update, args):
|
|||
lt.reverse()
|
||||
|
||||
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:
|
||||
bottools.methods.twoExceptions(e, update.message)
|
||||
|
@ -408,7 +501,7 @@ def timeline(bot, update, args = [10]):
|
|||
lt.reverse()
|
||||
|
||||
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:
|
||||
bottools.methods.twoExceptions(e, update.message)
|
||||
|
@ -507,6 +600,8 @@ commands = {
|
|||
"retweet": retweet,
|
||||
"search": search,
|
||||
"self": selfTweets,
|
||||
"sharelocation": shareLocation,
|
||||
"silence": silence,
|
||||
"start": start,
|
||||
"thread": thread,
|
||||
"timeline": timeline,
|
||||
|
@ -514,9 +609,11 @@ commands = {
|
|||
"toggleconfirmations": toggleConfirmations,
|
||||
"togglementions": mentionstream,
|
||||
"toggletweet": toggleTweet,
|
||||
"trends": trends,
|
||||
"tweet": explicitTweet,
|
||||
"unauth": unauth,
|
||||
"unfollow": unfollow,
|
||||
"unlike": unlike,
|
||||
"user": user,
|
||||
"verify": verify
|
||||
}
|
||||
|
@ -534,6 +631,8 @@ pargs = [
|
|||
thread,
|
||||
timeline,
|
||||
toggleTweet,
|
||||
trends,
|
||||
unlike,
|
||||
user,
|
||||
explicitTweet,
|
||||
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.'''
|
||||
|
||||
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.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):
|
||||
try:
|
||||
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 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 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.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.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.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.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))
|
||||
|
||||
|
|
Loading…
Reference in a new issue