From ef958366b99b83a8307b99cc9d0b84138f1ef104 Mon Sep 17 00:00:00 2001 From: Klaus-Uwe Mitterer Date: Tue, 21 Mar 2017 22:26:16 +0100 Subject: [PATCH] Move strings to bottools.strings --- bottools/__init__.py | 0 strings.py => bottools/strings.py | 0 telegrambot.py | 85 ++++++++++++++----------------- 3 files changed, 39 insertions(+), 46 deletions(-) create mode 100644 bottools/__init__.py rename strings.py => bottools/strings.py (100%) diff --git a/bottools/__init__.py b/bottools/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/strings.py b/bottools/strings.py similarity index 100% rename from strings.py rename to bottools/strings.py diff --git a/telegrambot.py b/telegrambot.py index 8b0f69b..4e2611d 100755 --- a/telegrambot.py +++ b/telegrambot.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 -import ast, dbtools, html, io, logging, moviepy.editor, PIL.Image, random, setuptools, string, strings, telegram.ext, twitools, urllib.request, tweepy +import ast, dbtools, html, io, logging, moviepy.editor, PIL.Image, random, setuptools, string, bottools.strings, telegram.ext, twitools, urllib.request, tweepy logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO) logger = logging.getLogger(__name__) @@ -10,42 +10,39 @@ def log(bot, update, error): def twoExceptions(e, message): text = { - 32: strings.badToken, - 36: strings.selfSpam, - 64: strings.accountSuspended, - 88: strings.rateLimit, - 89: strings.badToken, - 99: strings.badToken, - 130: strings.overload, - 131: strings.twitterError, - 161: strings.followLimit, - 185: strings.tweetLimit, - 186: strings.tweetLimit, - 187: strings.dupTweet, - 205: strings.rateLimit, - 226: strings.automatedTweet, - 271: strings.selfMute, - 272: strings.notMuted, - 323: strings.multipleGIFs, - 326: strings.accountLocked, - 354: strings.longTweet + 32: bottools.strings.badToken, + 36: bottools.strings.selfSpam, + 64: bottools.strings.accountSuspended, + 88: bottools.strings.rateLimit, + 89: bottools.strings.badToken, + 99: bottools.strings.badToken, + 130: bottools.strings.overload, + 131: bottools.strings.twitterError, + 161: bottools.strings.followLimit, + 185: bottools.strings.tweetLimit, + 186: bottools.strings.tweetLimit, + 187: bottools.strings.dupTweet, + 205: bottools.strings.rateLimit, + 226: bottools.strings.automatedTweet, + 271: bottools.strings.selfMute, + 272: bottools.strings.notMuted, + 323: bottools.strings.multipleGIFs, + 326: bottools.strings.accountLocked, + 354: bottools.strings.longTweet }.get(e.api_code) - message.reply_text(text or strings.twoFail) + message.reply_text(text or bottools.strings.twoFail) def getTwo(message): try: return twitools.twoBotHelper(message.chat_id) except ValueError: - noauth(message) + message.reply_text(bottools.strings.noauth) except tweepy.error.TweepError as e: raise -def noauth(message): - message.reply_text(strings.noauth) - def start(bot, update): - update.message.reply_text(strings.start % (setuptools.botname(), setuptools.botname())) + update.message.reply_text(bottools.strings.start % (setuptools.botname(), setuptools.botname())) def auth(bot, update): db = dbtools.dbHelper() @@ -53,11 +50,11 @@ def auth(bot, update): if not (db.ato(cid) or db.ase(cid)): auth = tweepy.OAuthHandler(setuptools.cke(), setuptools.cse()) - update.message.reply_text(strings.auth % auth.get_authorization_url()) + update.message.reply_text(bottools.strings.auth % auth.get_authorization_url()) dbtools.dbHelper().storeToken(cid, auth.request_token) else: - update.message.reply_text(strings.authimp) + update.message.reply_text(bottools.strings.authimp) def verify(bot, update, args): db = dbtools.dbHelper() @@ -70,22 +67,22 @@ def verify(bot, update, args): try: auth.get_access_token(args[0]) dbtools.dbHelper().storeUser(cid, auth.access_token, auth.access_token_secret) - update.message.reply_text(strings.verify) + update.message.reply_text(bottools.strings.verify) except Exception as e: dbtools.dbHelper().deleteUser(update.message.chat_id) - update.message.reply_text(strings.verifyfail) + update.message.reply_text(bottools.strings.verifyfail) else: - update.message.reply_text(strings.verifyimp) + update.message.reply_text(bottools.strings.verifyimp) def unauth(bot, update): dbtools.dbHelper().deleteUser(update.message.chat_id) - update.message.reply_text(strings.unauth % setuptools.url()) + update.message.reply_text(bottools.strings.unauth % setuptools.url()) def fish(bot, update): dbtools.dbHelper().addFish(update.message.chat_id) - update.message.reply_text(strings.fishThanks) + update.message.reply_text(bottools.strings.fishThanks) def follow(bot, update, args): try: @@ -149,17 +146,13 @@ def explicitTweet(bot, update, args, reply = None): two.tweet(' '.join(args), reply) except tweepy.error.TweepError as e: - logging.exception("Meh.") twoExceptions(e, update.message) - 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]) + update.message.reply_text(bottools.strings.cantfind % args[0]) explicitTweet(bot, update, args[1:], reply) @@ -169,7 +162,7 @@ def retweet(bot, update, args): tid = getTweetID(tweet, update.message.chat_id) getTwo(update.message).api.retweet(tid) except ValueError: - update.message.reply_text(strings.cantfind % tweet) + update.message.reply_text(bottools.strings.cantfind % tweet) except tweepy.error.TweepError as e: twoExceptions(e, update.message) @@ -179,7 +172,7 @@ def like(bot, update, args): tid = getTweetID(tweet, update.message.chat_id) getTwo(update.message).api.create_favorite(tid) except ValueError: - update.message.reply_text(strings.cantfind % tweet) + update.message.reply_text(bottools.strings.cantfind % tweet) except tweepy.error.TweepError as e: twoExceptions(e, update.message) @@ -187,8 +180,8 @@ def tweet(bot, update): try: if dbtools.dbHelper().getTStatus(update.message.chat_id): explicitTweet(bot, update, [update.message.text]) - except: - noauth(update.message) + except twepy.error.TweepError as e: + twoExceptions(e, update.message) def timeline(bot, update, args = [10]): try: @@ -215,12 +208,12 @@ def timeline(bot, update, args = [10]): def toggleTweet(bot, update): try: - update.message.reply_text(strings.toggleTweet % (strings.toggleTweetOn if dbtools.dbHelper().toggleTweet(update.message.chat_id) else strings.toggleTweetOff)) - except: - noauth(update.message) + update.message.reply_text(bottools.strings.toggleTweet % (bottools.strings.toggleTweetOn if dbtools.dbHelper().toggleTweet(update.message.chat_id) else bottools.strings.toggleTweetOff)) + except tweepy.error.TweepError as e: + twoExceptions(e, update.message) def unknown(bot, update): - update.message.reply_text(strings.unknownCommand) + update.message.reply_text(bottools.strings.unknownCommand) def test(bot, update, args): print(args)