From b51712cb8750fcd42debafc19584460f4a521cf8 Mon Sep 17 00:00:00 2001 From: mcinj <98779161+mcinj@users.noreply.github.com> Date: Sun, 15 May 2022 13:56:28 -0400 Subject: [PATCH] rename all the things --- src/{ConfigReader.py => config_reader.py} | 0 src/{SteamGifts.py => enter_giveaways.py} | 2 +- src/giveaway_thread.py | 72 ++++++++++ src/run.py | 154 +--------------------- src/templates/configuration.html | 2 +- src/templates/log.html | 2 +- src/webserver_thread.py | 86 ++++++++++++ 7 files changed, 168 insertions(+), 150 deletions(-) rename src/{ConfigReader.py => config_reader.py} (100%) rename src/{SteamGifts.py => enter_giveaways.py} (97%) create mode 100644 src/giveaway_thread.py create mode 100644 src/webserver_thread.py diff --git a/src/ConfigReader.py b/src/config_reader.py similarity index 100% rename from src/ConfigReader.py rename to src/config_reader.py diff --git a/src/SteamGifts.py b/src/enter_giveaways.py similarity index 97% rename from src/SteamGifts.py rename to src/enter_giveaways.py index 875b62f..878af86 100644 --- a/src/SteamGifts.py +++ b/src/enter_giveaways.py @@ -18,7 +18,7 @@ class SteamGiftsException(Exception): pass -class SteamGifts: +class EnterGiveaways: def __init__(self, cookie, user_agent, gifts_type, pinned, min_points, max_entries, max_time_left, minimum_game_points, blacklist, notification): self.contributor_level = None diff --git a/src/giveaway_thread.py b/src/giveaway_thread.py new file mode 100644 index 0000000..bf12294 --- /dev/null +++ b/src/giveaway_thread.py @@ -0,0 +1,72 @@ +import threading +from random import randint +from threading import Thread +from time import sleep + +import log +from enter_giveaways import EnterGiveaways + +logger = log.get_logger(__name__) + + +class GiveawayThread(threading.Thread): + + def __init__(self, config, notification): + Thread.__init__(self) + self.exc = None + self.config = config + self.notification = notification + + def run_steam_gifts(self, config, notification): + cookie = config['DEFAULT'].get('cookie') + user_agent = config['DEFAULT'].get('user_agent') + main_page_enabled = config['DEFAULT'].getboolean('enabled') + minimum_points = config['DEFAULT'].getint('minimum_points') + max_entries = config['DEFAULT'].getint('max_entries') + max_time_left = config['DEFAULT'].getint('max_time_left') + minimum_game_points = config['DEFAULT'].getint('minimum_game_points') + blacklist = config['DEFAULT'].get('blacklist_keywords') + + all_page = EnterGiveaways(cookie, user_agent, 'All', False, minimum_points, max_entries, + max_time_left, minimum_game_points, blacklist, notification) + + wishlist_page_enabled = config['WISHLIST'].getboolean('wishlist.enabled') + wishlist_minimum_points = config['WISHLIST'].getint('wishlist.minimum_points') + wishlist_max_entries = config['WISHLIST'].getint('wishlist.max_entries') + wishlist_max_time_left = config['WISHLIST'].getint('wishlist.max_time_left') + + wishlist_page = EnterGiveaways(cookie, user_agent, 'Wishlist', False, wishlist_minimum_points, + wishlist_max_entries, wishlist_max_time_left, 0, '', notification) + + if not main_page_enabled and not wishlist_page_enabled: + logger.error("⁉️ Both 'Default' and 'Wishlist' configurations are disabled. Nothing will run. Exiting...") + sleep(10) + exit(-1) + + while True: + logger.info("🟢 Evaluating giveaways.") + if wishlist_page_enabled: + wishlist_page.start() + if main_page_enabled: + all_page.start() + + logger.info("🔴 All giveaways evaluated.") + random_seconds = randint(1740, 3540) # sometime between 29-59 minutes + logger.info(f"🛋 Going to sleep for {random_seconds / 60} minutes.") + sleep(random_seconds) + + def run(self): + # Variable that stores the exception, if raised by someFunction + self.exc = None + try: + self.run_steam_gifts(self.config, self.notification) + except BaseException as e: + self.exc = e + + def join(self): + threading.Thread.join(self) + # Since join() returns in caller thread + # we re-raise the caught exception + # if any was caught + if self.exc: + raise self.exc \ No newline at end of file diff --git a/src/run.py b/src/run.py index e01c8a6..320ca17 100644 --- a/src/run.py +++ b/src/run.py @@ -1,155 +1,15 @@ -import threading -from random import randint from time import sleep -from flask_basicauth import BasicAuth - import log -from ConfigReader import ConfigReader, ConfigException -from SteamGifts import SteamGifts, SteamGiftsException +from config_reader import ConfigReader, ConfigException +from enter_giveaways import SteamGiftsException +from giveaway_thread import GiveawayThread from notification import Notification -from threading import Thread - +from webserver_thread import WebServerThread logger = log.get_logger(__name__) -class WebServerThread(threading.Thread): - - def __init__(self, config): - Thread.__init__(self) - self.exc = None - self.config = config - self.port = config['WEB'].getint('web.port') - self.ssl = config['WEB'].getboolean('web.ssl') - self.enabled = config['WEB'].getboolean('web.enabled') - self.app_root = config['WEB'].get('web.app_root') - self.basic_auth = config['WEB'].getboolean('web.basic_auth') - self.basic_auth_username = config['WEB'].get('web.basic_auth.username') - self.basic_auth_password = config['WEB'].get('web.basic_auth.password') - - def run_webserver(self): - from flask import Flask - from flask import render_template - - app = Flask(__name__) - - if self.basic_auth: - app.config['BASIC_AUTH_USERNAME'] = self.basic_auth_username - app.config['BASIC_AUTH_PASSWORD'] = self.basic_auth_password - - app.config['BASIC_AUTH_FORCE'] = self.basic_auth - basic_auth = BasicAuth(app) - - @app.route(f"{self.app_root}") - def config(): - with open('../config/config.ini', 'r') as f: - content = f.read() - return render_template('configuration.html', config=content) - - @app.route(f"{self.app_root}log") - def logs(): - return render_template('log.html') - - @app.route(f"{self.app_root}stream") - def stream(): - def generate(): - with open('../config/info.log') as f: - while True: - yield f.read() - sleep(10) - - return app.response_class(generate(), mimetype='text/plain') - - if self.enabled: - logger.info("Webserver Enabled. Running") - if self.ssl: - app.run(port=self.port, host="0.0.0.0", ssl_context='adhoc') - else: - app.run(port=self.port, host="0.0.0.0") - else: - logger.info("Webserver NOT Enabled.") - - def run(self): - # Variable that stores the exception, if raised by someFunction - self.exc = None - try: - self.run_webserver() - except BaseException as e: - self.exc = e - - def join(self): - threading.Thread.join(self) - # Since join() returns in caller thread - # we re-raise the caught exception - # if any was caught - if self.exc: - raise self.exc - - -class GiveawayEntererThread(threading.Thread): - - def __init__(self, config, notification): - Thread.__init__(self) - self.exc = None - self.config = config - self.notification = notification - - def run_steam_gifts(self, config, notification): - cookie = config['DEFAULT'].get('cookie') - user_agent = config['DEFAULT'].get('user_agent') - main_page_enabled = config['DEFAULT'].getboolean('enabled') - minimum_points = config['DEFAULT'].getint('minimum_points') - max_entries = config['DEFAULT'].getint('max_entries') - max_time_left = config['DEFAULT'].getint('max_time_left') - minimum_game_points = config['DEFAULT'].getint('minimum_game_points') - blacklist = config['DEFAULT'].get('blacklist_keywords') - - all_page = SteamGifts(cookie, user_agent, 'All', False, minimum_points, max_entries, - max_time_left, minimum_game_points, blacklist, notification) - - wishlist_page_enabled = config['WISHLIST'].getboolean('wishlist.enabled') - wishlist_minimum_points = config['WISHLIST'].getint('wishlist.minimum_points') - wishlist_max_entries = config['WISHLIST'].getint('wishlist.max_entries') - wishlist_max_time_left = config['WISHLIST'].getint('wishlist.max_time_left') - - wishlist_page = SteamGifts(cookie, user_agent, 'Wishlist', False, wishlist_minimum_points, - wishlist_max_entries, wishlist_max_time_left, 0, '', notification) - - if not main_page_enabled and not wishlist_page_enabled: - logger.error("⁉️ Both 'Default' and 'Wishlist' configurations are disabled. Nothing will run. Exiting...") - sleep(10) - exit(-1) - - while True: - logger.info("🟢 Evaluating giveaways.") - if wishlist_page_enabled: - wishlist_page.start() - if main_page_enabled: - all_page.start() - - logger.info("🔴 All giveaways evaluated.") - random_seconds = randint(1740, 3540) # sometime between 29-59 minutes - logger.info(f"🛋 Going to sleep for {random_seconds / 60} minutes.") - sleep(random_seconds) - - def run(self): - # Variable that stores the exception, if raised by someFunction - self.exc = None - try: - self.run_steam_gifts(self.config, self.notification) - except BaseException as e: - self.exc = e - - def join(self): - threading.Thread.join(self) - # Since join() returns in caller thread - # we re-raise the caught exception - # if any was caught - if self.exc: - raise self.exc - - def run(): logger.info("Starting Steamgifts bot.") file_name = '../config/config.ini' @@ -173,13 +33,13 @@ def run(): if pushover_enabled: notification.enable_pushover(pushover_token, pushover_user_key) try: - g = GiveawayEntererThread(config, notification) + g = GiveawayThread(config, notification) g.setName("Giveaway Enterer") g.start() w = WebServerThread(config) w.setName("WebServer") - # if the giveaway thread dies then this daemon thread will die by definition + # if the giveaway thread dies then this daemon thread will die by design w.setDaemon(True) w.start() @@ -191,7 +51,7 @@ def run(): except Exception as e: logger.error(e) notification.send_error("Something happened and the bot had to quit!") - sleep(5) + sleep(10) exit(-1) diff --git a/src/templates/configuration.html b/src/templates/configuration.html index 05b6f59..320839d 100644 --- a/src/templates/configuration.html +++ b/src/templates/configuration.html @@ -1,7 +1,7 @@ - Flask app + Steamgifts Bot Configuration diff --git a/src/templates/log.html b/src/templates/log.html index e365d92..155c1f4 100644 --- a/src/templates/log.html +++ b/src/templates/log.html @@ -1,7 +1,7 @@ - Flask app + Steamgifts Bot Logs diff --git a/src/webserver_thread.py b/src/webserver_thread.py new file mode 100644 index 0000000..3e66503 --- /dev/null +++ b/src/webserver_thread.py @@ -0,0 +1,86 @@ +import threading +from threading import Thread +from time import sleep + +from flask_basicauth import BasicAuth + +import log + +logger = log.get_logger(__name__) + + +class WebServerThread(threading.Thread): + + def __init__(self, config): + Thread.__init__(self) + self.exc = None + self.config = config + self.port = config['WEB'].getint('web.port') + self.ssl = config['WEB'].getboolean('web.ssl') + self.enabled = config['WEB'].getboolean('web.enabled') + self.app_root = config['WEB'].get('web.app_root') + self.basic_auth = config['WEB'].getboolean('web.basic_auth') + self.basic_auth_username = config['WEB'].get('web.basic_auth.username') + self.basic_auth_password = config['WEB'].get('web.basic_auth.password') + + def run_webserver(self): + from flask import Flask + from flask import render_template + + app = Flask(__name__) + + if self.basic_auth: + app.config['BASIC_AUTH_USERNAME'] = self.basic_auth_username + app.config['BASIC_AUTH_PASSWORD'] = self.basic_auth_password + + app.config['BASIC_AUTH_FORCE'] = self.basic_auth + basic_auth = BasicAuth(app) + + @app.route(f"{self.app_root}") + def config(): + with open('../config/config.ini', 'r') as f: + content = f.read() + return render_template('configuration.html', config=content) + + @app.route(f"{self.app_root}log") + def logs(): + return render_template('log.html') + + @app.route(f"{self.app_root}stream") + def stream(): + def generate(): + with open('../config/info.log') as f: + while True: + yield f.read() + sleep(10) + + return app.response_class(generate(), mimetype='text/plain') + + @app.route(f"{self.app_root}alive") + def alive(): + return 'OK' + + if self.enabled: + logger.info("Webserver Enabled. Running") + if self.ssl: + app.run(port=self.port, host="0.0.0.0", ssl_context='adhoc') + else: + app.run(port=self.port, host="0.0.0.0") + else: + logger.info("Webserver NOT Enabled.") + + def run(self): + # Variable that stores the exception, if raised by someFunction + self.exc = None + try: + self.run_webserver() + except BaseException as e: + self.exc = e + + def join(self): + threading.Thread.join(self) + # Since join() returns in caller thread + # we re-raise the caught exception + # if any was caught + if self.exc: + raise self.exc \ No newline at end of file