diff --git a/.gitignore b/.gitignore index 6dc7536..8304bd8 100644 --- a/.gitignore +++ b/.gitignore @@ -4,5 +4,5 @@ __pycache__/ .idea config/config.ini config/*.log* -config/sqlite.db +config/sqlite* .DS_STORE \ No newline at end of file diff --git a/main.py b/main.py index 3131670..d19fa56 100644 --- a/main.py +++ b/main.py @@ -1,4 +1,4 @@ -from src.bot import run +from src import run if __name__ == '__main__': diff --git a/src/bot/database.py b/src/bot/database.py index ec6beea..e3fc60e 100644 --- a/src/bot/database.py +++ b/src/bot/database.py @@ -6,7 +6,7 @@ from alembic import command from alembic.config import Config from dateutil import tz from sqlalchemy import func -from sqlalchemy.orm import Session +from sqlalchemy.orm import Session, joinedload from sqlalchemy_utils import database_exists from .log import get_logger @@ -17,11 +17,7 @@ engine = sqlalchemy.create_engine(f"{os.getenv('BOT_DB_URL', 'sqlite:///./config engine.connect() -def create_engine(db_url: str): - return engine - - -def run_migrations(script_location: str, db_url: str) -> None: +def run_db_migrations(script_location: str, db_url: str) -> None: logger.debug('Running DB migrations in %r on %r', script_location, db_url) alembic_cfg = Config() alembic_cfg.set_main_option('script_location', script_location) @@ -30,10 +26,9 @@ def run_migrations(script_location: str, db_url: str) -> None: if not database_exists(db_url): logger.debug(f"'{db_url}' does not exist. Running normal migration to create db and tables." ) command.upgrade(alembic_cfg, 'head') - if database_exists(db_url): - logger.debug(f"'{db_url} exists.") - e = create_engine(db_url) - insp = sqlalchemy.inspect(e) + elif database_exists(db_url): + logger.debug(f"'{db_url}' exists.") + insp = sqlalchemy.inspect(engine) alembic_version_table_name = 'alembic_version' has_alembic_table = insp.has_table(alembic_version_table_name) if has_alembic_table: @@ -49,6 +44,11 @@ def run_migrations(script_location: str, db_url: str) -> None: class NotificationHelper: + @classmethod + def get(cls): + with Session(engine) as session: + return session.query(TableNotification).order_by(TableNotification.created_at.desc()).all() + @classmethod def insert(cls, type_of_error, message, medium, success): with Session(engine) as session: @@ -89,6 +89,11 @@ class NotificationHelper: class GiveawayHelper: + @classmethod + def get(cls): + with Session(engine) as session: + return session.query(TableGiveaway).options(joinedload('steam_item')).order_by(TableGiveaway.giveaway_ended_at.desc()).all() + @classmethod def unix_timestamp_to_utc_datetime(cls, timestamp): return datetime.utcfromtimestamp(timestamp) diff --git a/src/bot/run.py b/src/run.py similarity index 86% rename from src/bot/run.py rename to src/run.py index c5993fb..2375878 100644 --- a/src/bot/run.py +++ b/src/run.py @@ -1,13 +1,13 @@ import os from time import sleep -from .log import get_logger -from .config_reader import ConfigReader, ConfigException -from .enter_giveaways import SteamGiftsException -from .giveaway_thread import GiveawayThread -from .notification import Notification -from .database import run_migrations, create_engine -from .webserver_thread import WebServerThread +from src.bot.log import get_logger +from src.bot.config_reader import ConfigReader, ConfigException +from src.bot.enter_giveaways import SteamGiftsException +from src.bot.giveaway_thread import GiveawayThread +from src.bot.notification import Notification +from src.bot.database import run_db_migrations +from src.web.webserver_thread import WebServerThread logger = get_logger(__name__) config_file_name = f"{os.getenv('BOT_CONFIG_DIR', './config')}/config.ini" @@ -73,9 +73,9 @@ def entry(): |___/ ------------------------------------------------------------------------------------- """) - run_migrations(alembic_migration_files, db_url) - create_engine(db_url) + run_db_migrations(alembic_migration_files, db_url) run() + if __name__ == '__main__': entry() diff --git a/src/bot/static/css/main.css b/src/web/static/css/main.css similarity index 100% rename from src/bot/static/css/main.css rename to src/web/static/css/main.css diff --git a/src/bot/templates/configuration.html b/src/web/templates/configuration.html similarity index 73% rename from src/bot/templates/configuration.html rename to src/web/templates/configuration.html index 7268e62..670abb9 100644 --- a/src/bot/templates/configuration.html +++ b/src/web/templates/configuration.html @@ -1,7 +1,7 @@ - Steamgifts Bot Configuration + {{name}} Steamgifts Bot Configuration @@ -13,6 +13,8 @@
  • Config
  • Info Logs
  • Debug Logs
  • +
  • Giveaways
  • +
  • Notifications
  • diff --git a/src/web/templates/giveaways.html b/src/web/templates/giveaways.html new file mode 100644 index 0000000..e35376d --- /dev/null +++ b/src/web/templates/giveaways.html @@ -0,0 +1,63 @@ + + + + {{name}} Steamgifts Bot DB View + + + +
    +
    +

    Steamgifts Bot

    + + + +
    +
    +
    + + + + + + + + + + + + + + + + + + + {% for row in content %} + + + + + + + + + + + + + + + {% endfor %} + +
    Giveaway IDSteam IDGiveaway URINameSteam URLUserGiveaway Created AtGiveaway Ended AtCostCopiesContributor LevelEntered
    {{row.giveaway_id}}{{row.steam_id}}{{row.giveaway_uri}}{{row.steam_item.game_name}}{{row.steam_item.steam_url}}{{row.user}}{{row.giveaway_created_at}}{{row.giveaway_ended_at}}{{row.cost}}{{row.copies}}{{row.contributor_level}}{{row.entered}}
    +
    + + \ No newline at end of file diff --git a/src/bot/templates/log.html b/src/web/templates/log.html similarity index 73% rename from src/bot/templates/log.html rename to src/web/templates/log.html index 9b486de..8a7408f 100644 --- a/src/bot/templates/log.html +++ b/src/web/templates/log.html @@ -1,7 +1,7 @@ - Steamgifts Bot Logs + {{name}} Steamgifts Bot {{log_type}} Logs @@ -13,6 +13,8 @@
  • Config
  • Info Logs
  • Debug Logs
  • +
  • Giveaways
  • +
  • Notifications
  • diff --git a/src/web/templates/notifications.html b/src/web/templates/notifications.html new file mode 100644 index 0000000..b11fe88 --- /dev/null +++ b/src/web/templates/notifications.html @@ -0,0 +1,51 @@ + + + + {{name}} Steamgifts Bot DB View + + + +
    +
    +

    Steamgifts Bot

    + + + +
    +
    +
    + + + + + + + + + + + + + {% for row in content %} + + + + + + + + + {% endfor %} + +
    IDMediumTypeSuccessCreated AtMessage
    {{row.id}}{{row.medium}}{{row.type}}{{row.success}}{{row.created_at}}{{row.message}}
    +
    + + \ No newline at end of file diff --git a/src/bot/webserver_thread.py b/src/web/webserver_thread.py similarity index 76% rename from src/bot/webserver_thread.py rename to src/web/webserver_thread.py index 6f6aeba..4456801 100644 --- a/src/bot/webserver_thread.py +++ b/src/web/webserver_thread.py @@ -4,7 +4,8 @@ from threading import Thread from flask_basicauth import BasicAuth -from .log import get_logger +from src.bot.database import NotificationHelper, GiveawayHelper +from src.bot.log import get_logger logger = get_logger(__name__) @@ -15,6 +16,7 @@ class WebServerThread(threading.Thread): Thread.__init__(self) self.exc = None self.config = config + self.name = config['NOTIFICATIONS'].get('notification.prefix') self.host = config['WEB'].get('web.host') self.port = config['WEB'].getint('web.port') self.ssl = config['WEB'].getboolean('web.ssl') @@ -41,24 +43,32 @@ class WebServerThread(threading.Thread): def config(): with open(f"{os.getenv('BOT_CONFIG_DIR', './config')}/config.ini", 'r') as f: content = f.read() - return render_template('configuration.html', content=content) + return render_template('configuration.html', name=self.name, content=content) @app.route(f"{self.app_root}log_info") def log_info(): with open(f"{os.getenv('BOT_CONFIG_DIR', './config')}/info.log", 'r') as f: content = f.read() - return render_template('log.html', content=content) + return render_template('log.html', name=self.name, log_type='info', content=content) @app.route(f"{self.app_root}log_debug") def log_debug(): with open(f"{os.getenv('BOT_CONFIG_DIR', './config')}/debug.log", 'r') as f: content = f.read() - return render_template('log.html', content=content) + return render_template('log.html', name=self.name, log_type='debug', content=content) @app.route(f"{self.app_root}alive") def alive(): return 'OK' + @app.route(f"{self.app_root}notifications") + def db_notifications(): + return render_template('notifications.html', content=NotificationHelper.get()) + + @app.route(f"{self.app_root}giveaways") + def db_giveaways(): + return render_template('giveaways.html', content=GiveawayHelper.get()) + if self.enabled: logger.info("Webserver Enabled. Running") if self.ssl: