From d47a90b338e3062a4607fb0a7e60f60fe9d08da4 Mon Sep 17 00:00:00 2001 From: Hammy Date: Fri, 29 Jan 2021 22:40:12 +0000 Subject: [PATCH] Refactor Code --- bot/commands.py | 15 ++++++--------- bot/handler.py | 14 +++++++++++++- bot/winston.py | 24 ++++++++---------------- 3 files changed, 27 insertions(+), 26 deletions(-) diff --git a/bot/commands.py b/bot/commands.py index d5d942d..682d6dc 100644 --- a/bot/commands.py +++ b/bot/commands.py @@ -1,19 +1,16 @@ -from bot.winston import Winston - - -def send_tweet(tweet_text): +def send_tweet(bot, tweet_text): """Sends a tweet to Twitter""" - Winston.bot.update_status(status=tweet_text) + bot.update_status(status=tweet_text) -def follow_someone(username): +def follow_someone(bot, username): """Follows someone based on given username""" - Winston.bot.create_friendship(username=username) + bot.create_friendship(username=username) -def like_tweet(tweet_id): +def like_tweet(bot, tweet_id): """Likes a tweet based on it's ID""" - Winston.bot.create_favorite(id=tweet_id) + bot.create_favorite(id=tweet_id) diff --git a/bot/handler.py b/bot/handler.py index 15af678..589a3fe 100644 --- a/bot/handler.py +++ b/bot/handler.py @@ -1,6 +1,18 @@ +import logging +import random + +from bot.commands import send_tweet from bot.winston import Winston +logger = logging.getLogger() +logger.setLevel(logging.INFO) + def event_handler(event, context): """Sends random tweet from list of potential tweets""" - Winston.request_handler() + winston = Winston() + + random_tweet = random.choice(winston.potential_tweets) + send_tweet(winston.bot, random_tweet) + + logger.info(f"Random Tweet Sent: {random_tweet}") diff --git a/bot/winston.py b/bot/winston.py index 774714e..7618902 100644 --- a/bot/winston.py +++ b/bot/winston.py @@ -1,26 +1,18 @@ -import random from twython import Twython from bot.aws_secrets import get_secret -from bot.commands import send_tweet class Winston: - bot = Twython( - get_secret("CONSUMER_KEY"), - get_secret("CONSUMER_SECRET"), - get_secret("ACCESS_TOKEN_KEY"), - get_secret("ACCESS_TOKEN_SECRET") - ) - - @staticmethod - def potential_tweets(): - return [ + def __init__(self): + self.bot = Twython( + get_secret("CONSUMER_KEY"), + get_secret("CONSUMER_SECRET"), + get_secret("ACCESS_TOKEN_KEY"), + get_secret("ACCESS_TOKEN_SECRET") + ) + self.potential_tweets = [ "Hello! I'm Winston From Overwatch!", "Winston! From! Overwatch!", "Winston? From Overwatch?" ] - - @staticmethod - def request_handler(): - send_tweet(random.choice(Winston.potential_tweets()))