diff --git a/.env.example b/.env.example index a609d93..67e3e61 100644 --- a/.env.example +++ b/.env.example @@ -1,2 +1,3 @@ # Bot API -BOT_API_KEY = \ No newline at end of file +BOT_API_KEY= +DATABASE_CONNECTION=mysql://scott:tiger@localhost/foo' \ No newline at end of file diff --git a/.gitignore b/.gitignore index b6e4761..ec41a00 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ +# IntelliJ / PyCharm +.idea/ + # Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] diff --git a/requirements.txt b/requirements.txt index b78a2fe..e55bf96 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ pyTelegramBotAPI~=4.5.0 python-dotenv~=0.20.0 -APScheduler~=3.9.1 \ No newline at end of file +APScheduler~=3.9.1 +SQLAlchemy~=1.4.36 \ No newline at end of file diff --git a/source/bot.py b/source/bot.py index 04c3dbd..596905f 100644 --- a/source/bot.py +++ b/source/bot.py @@ -3,29 +3,31 @@ script for telegram bot and its functions """ __author__ = "Florian Kellermann, Linus Eickhoff, Florian Kaiser" __date__ = "02.05.2022" -__version__ = "0.0.1" +__version__ = "0.0.1" __license__ = "None" # main bot at http://t.me/guess_the_price_bot # debug bot at http://t.me/amazondebug_bot -from dotenv import load_dotenv -import telebot -from telebot import types -from apscheduler.schedulers.background import BackgroundScheduler import logging import os import sys -load_dotenv(dotenv_path='.env') # load environment variables +import telebot +from dotenv import load_dotenv +from telebot import types -bot_version = "0.0.1" # version of bot +from source.db import User, session + +load_dotenv(dotenv_path='.env') # load environment variables + +bot_version = "0.0.1" # version of bot bot = telebot.TeleBot(os.getenv('BOT_API_KEY')) -@bot.message_handler(commands=['start', 'Start']) + +@bot.message_handler(commands=['start', 'Start']) def send_start(message): - """ Sending welcome message to new user :type message: message object bot :param message: message that was reacted to, in this case always containing '/start' @@ -35,12 +37,13 @@ def send_start(message): :rtype: none """ bot.reply_to(message, "Welcome to this amazon prices guesser bot") - + + telebot.logger.setLevel(logging.DEBUG) - -@bot.inline_handler(lambda query: query.query == 'text') # inline prints for debugging + + +@bot.inline_handler(lambda query: query.query == 'text') # inline prints for debugging def query_text(inline_query): - """ Output in the console about current user actions and status of bot :type inline_query: :param inline_query: @@ -56,8 +59,8 @@ def query_text(inline_query): except Exception as e: print(e) + def main_loop(): - """ Start bot :raises: none @@ -65,9 +68,10 @@ def main_loop(): """ bot.infinity_polling() + if __name__ == '__main__': try: main_loop() except KeyboardInterrupt: print('\nExiting by user request.\n') - sys.exit(0) \ No newline at end of file + sys.exit(0) diff --git a/source/db.py b/source/db.py new file mode 100644 index 0000000..3e466ed --- /dev/null +++ b/source/db.py @@ -0,0 +1,59 @@ +__author__ = "Florian Kaiser" +__copyright__ = "Copyright 2022, GuessThePrice" +__credits__ = ["Florian Kaiser", "Florian Kellermann", "Linus Eickhof"] +__license__ = "GPL 3.0" +__version__ = "1.0.0" + +import os + +from sqlalchemy import Column, Integer, String, ForeignKey, create_engine, Boolean, DateTime, Float +from sqlalchemy.ext.declarative import declarative_base +from sqlalchemy.orm import sessionmaker + +engine = create_engine(os.getenv("DATABASE_CONNECTION"), echo=True) + +Base = declarative_base() + + +class User(Base): + __tablename__ = 'user' + telegram_id = Column(Integer, primary_key=True) + username = Column(String) + admin = Column(Boolean) + + def __repr__(self): + return "" % (self.telegram_id, self.username, self.admin) + + +class Score(Base): + __tablename__ = 'score' + telegram_id = Column(Integer, ForeignKey('user.telegram_id'), primary_key=True) + date = Column(DateTime, primary_key=True) + product_id = Column(String, ForeignKey('product.product_id')) + guess = Column(Float) + score = Column(Integer) + + def __repr__(self): + return "" % (self.telegram_id, self.date, self.product_id, self.guess, self.score) + + +class Product(Base): + __tablename__ = 'product' + product_id = Column(String, primary_key=True) + price = Column(Float) + image_link = Column(String) + title = Column(String) + description = Column(String) + date = Column(DateTime) + + def __repr__(self): + return "" % ( + self.product_id, self.price, self.image_link, self.title, self.description, self.date) + + +# Create all tables by issuing CREATE TABLE commands to the DB. +Base.metadata.create_all(engine) + +# Creates a new session to the database by using the engine we described. +Session = sessionmaker(bind=engine) +session = Session() diff --git a/source/db_handler.py b/source/db_handler.py deleted file mode 100644 index e69de29..0000000