diff --git a/App/application.py b/App/application.py index a36e67e..4172f42 100644 --- a/App/application.py +++ b/App/application.py @@ -1,8 +1,13 @@ from argparse import ArgumentParser, Namespace +from .out import cPrint, Colors +from .upgrader import PiPUpgrader + def main(arguments: Namespace): from os import path + import pickle + token = arguments.token youtube_api_key = arguments.youtube creator_id = arguments.creator @@ -12,18 +17,32 @@ def main(arguments: Namespace): exit(0) if not path.exists("app_data.dict"): if not token: - ValueError("You must add token at least the first time you execute this app") + raise ValueError("You must add token at least the first time you execute this app") elif not youtube_api_key: - ValueError("You must include the YouTube API Key at least the first time you execute this app") + raise ValueError("You must include the YouTube API Key at least the first time you execute this app") elif not creator_id: - ValueError("You must include the creator ID (Telegram) at least the first time you execute this app") + raise ValueError("You must include the creator ID (Telegram) at least the first time you execute this app") else: with open("app_data.dict", "wb") as app_data_file: - import pickle app_data = {"TOKEN": token, "YT_API": youtube_api_key, "CREATOR_ID": creator_id} pickle.dump(app_data, app_data_file, pickle.HIGHEST_PROTOCOL) + else: + cPrint("Initializing bot...", Colors.GREEN) + cPrint("Looking for packages updates...", Colors.GREEN) + upgrader = PiPUpgrader("requirements.txt") + upgrader.upgradePackages() + cPrint("Obtaining values...", Colors.GREEN) + with open("app_data.dict", "rb") as app_data_file: + app_data = pickle.load(app_data_file) + from telegram.ext import Updater + updater = Updater(token=app_data["TOKEN"], workers=50) + dispatcher = updater.dispatcher + import logging + logging.basicConfig(format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", + level=logging.DEBUG) + updater.start_polling(poll_interval=5, timeout=60) if __name__ == '__main__': diff --git a/App/out/__init__.py b/App/out/__init__.py new file mode 100644 index 0000000..eb35c7e --- /dev/null +++ b/App/out/__init__.py @@ -0,0 +1,27 @@ +class Colors: + RED = "red" + BLUE = "blue" + GREEN = "green" + WARNING = "warning" + FAIL = "fail" + BOLD = "bold" + UNDERLINE = "underline" + RESET_ALL = "reset_all" + + +class OutputColors: + colors = {Colors.RED: '\033[95m', + Colors.BLUE: '\033[94m', + Colors.GREEN: '\033[92m', + Colors.WARNING: '\033[93m', + Colors.FAIL: '\033[91m', + Colors.BOLD: '\033[1m', + Colors.UNDERLINE: '\033[4m', + Colors.RESET_ALL: '\033[0m'} + + +def cPrint(text: str, color: Colors = None): + colors = OutputColors.colors + if color is None: + color = colors[Colors.RESET_ALL] + print(colors[color] + text + colors[Colors.RESET_ALL]) diff --git a/App/requirements.txt b/App/requirements.txt new file mode 100644 index 0000000..f556262 --- /dev/null +++ b/App/requirements.txt @@ -0,0 +1,2 @@ +pip +python-telegram-bot diff --git a/App/upgrader/__init__.py b/App/upgrader/__init__.py new file mode 100644 index 0000000..eb5c8b3 --- /dev/null +++ b/App/upgrader/__init__.py @@ -0,0 +1,10 @@ +class PiPUpgrader: + def __init__(self, file: str): + self.__file = file + + def upgradePackages(self): + import subprocess + with open(self.__file, 'r') as f: + requirements = f.read().splitlines() + for requirement in requirements: + subprocess.run(("pip install -U " + requirement).split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE)