diff --git a/setup.py b/setup.py index f1293a9..ea9513a 100755 --- a/setup.py +++ b/setup.py @@ -65,6 +65,7 @@ if not db.isInitialized(): 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);") + db.executeQuery("CREATE TABLE timelines(`cid` INT, `nr` INT, `tid` INT, PRIMARY KEY(cid, nr));") db.commit() db.closeConnection() diff --git a/strings.py b/strings.py index ddebcef..f7bd2fa 100644 --- a/strings.py +++ b/strings.py @@ -100,3 +100,5 @@ multipleGIFs = '''I don't understand it either, but Twitter doesn't allow upload accountLocked = '''Your account was locked by Twitter for security reasons. Please log in at the Twitter website (https://twitter.com/) and follow the instructions there.''' 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.''' diff --git a/telegrambot.py b/telegrambot.py index 4d1ce04..21ebb49 100755 --- a/telegrambot.py +++ b/telegrambot.py @@ -38,7 +38,7 @@ def getTwo(message): except ValueError: noauth(message) except tweepy.error.TweepError as e: - twoExceptions(e, message) + raise def noauth(message): message.reply_text(strings.noauth) @@ -86,7 +86,15 @@ def fish(bot, update): dbtools.dbHelper().addFish(update.message.chat_id) update.message.reply_text(strings.fishThanks) -def explicitTweet(bot, update, args): +def getTweetID(tlid, cid): + try: + db = dbtools.dbHelper() + db.executeQuery("SELECT tid FROM timelines WHERE nr = %i AND cid = %i;" % (tlid, int(cid))) + return db.getNext()[0] + except: + raise ValueError("No such tweet in timeline") + +def explicitTweet(bot, update, args, reply = None): try: two = getTwo(update.message) @@ -100,11 +108,13 @@ def explicitTweet(bot, update, args): if filename.split(".")[-1].lower() == "webp": out = io.BytesIO() - PIL.Image.open(mobj).convert('RGB').save(out, "jpeg") - out.read() + PIL.Image.open(mobj).convert('RGB').save(out, format="JPEG") filename = "%s.jpg" % filename.split(".")[0] - two.api.update_with_media(filename, ' '.join(args), file=mobj) + else: + out = mobj + + two.api.update_with_media(filename, update.message.caption, file=mobj) else: two.tweet(' '.join(args)) @@ -116,6 +126,34 @@ def explicitTweet(bot, update, args): except: logging.exception("Meh.") +def reply(bot, update, args): + try: + reply = getTweetID(args[0], update.message.chat_id) + except: + update.message.reply_text(strings.cantfind % args[0]) + + explicitTweet(bot, update, args[1:], reply) + +def retweet(bot, update, args): + for tweet in args: + try: + tid = getTweetID(tweet, update.message.chat_id) + getTwo(update.message).api.retweet(tid) + except ValueError: + update.message.reply_text(strings.cantfind % tweet) + except tweepy.error.TweepError as e: + twoExceptions(e, update.message) + +def like(bot, update, args): + for tweet in args: + try: + tid = getTweetID(tweet, update.message.chat_id) + getTwo(update.message).api.create_favorite(tid) + except ValueError: + update.message.reply_text(strings.cantfind % tweet) + except tweepy.error.TweepError as e: + twoExceptions(e, update.message) + def tweet(bot, update): try: if dbtools.dbHelper().getTStatus(update.message.chat_id): @@ -129,13 +167,23 @@ def timeline(bot, update, args = [10]): except: count = 10 + db = dbtools.dbHelper() + db.executeQuery("DELETE FROM timelines WHERE cid = %i;" % int(update.message.chat_id)) + try: + i = 1 two = getTwo(update.message) + for status in two.api.home_timeline(count=count): - update.message.reply_text("%s (%s) at %s: %s" % (status.author.name, status.author.screen_name, status.created_at, status.text)) + db.executeQuery("INSERT INTO timelines VALUES(%i, %i, %i);" % (update.message.chat_id, i, status.id)) + update.message.reply_text("Tweet %i:\n%s (@%s) at %s:\n%s" % (i, status.author.name, status.author.screen_name, status.created_at, status.text)) + i += 1 + except tweepy.error.TweepError as e: twoExceptions(e, update.message) + db.commit() + def toggleTweet(bot, update): try: update.message.reply_text(strings.toggleTweet % (strings.toggleTweetOn if dbtools.dbHelper().toggleTweet(update.message.chat_id) else strings.toggleTweetOff)) @@ -155,6 +203,9 @@ if __name__ == "__main__": updater.dispatcher.add_handler(telegram.ext.CommandHandler("auth", auth)) updater.dispatcher.add_handler(telegram.ext.CommandHandler("fish", fish)) updater.dispatcher.add_handler(telegram.ext.CommandHandler("help", start)) + updater.dispatcher.add_handler(telegram.ext.CommandHandler("like", like, pass_args=True)) + updater.dispatcher.add_handler(telegram.ext.CommandHandler("reply", reply, pass_args=True)) + updater.dispatcher.add_handler(telegram.ext.CommandHandler("retweet", retweet, pass_args=True)) updater.dispatcher.add_handler(telegram.ext.CommandHandler("start", start)) updater.dispatcher.add_handler(telegram.ext.CommandHandler("test", test, pass_args=True)) updater.dispatcher.add_handler(telegram.ext.CommandHandler("timeline", timeline))