diff --git a/App/.idea/workspace.xml b/App/.idea/workspace.xml index 69bdb49..3d10e82 100644 --- a/App/.idea/workspace.xml +++ b/App/.idea/workspace.xml @@ -1,7 +1,13 @@ - + + + + + + + - - + + - - + + - + + + + + + + + + + + + + + + + + + + + + + @@ -63,7 +90,9 @@ @@ -76,10 +105,10 @@ - - \ No newline at end of file diff --git a/App/application.py b/App/application.py index 4172f42..14ceb0e 100644 --- a/App/application.py +++ b/App/application.py @@ -1,48 +1,70 @@ -from argparse import ArgumentParser, Namespace +try: + from argparse import ArgumentParser, Namespace +except (ImportError, ModuleNotFoundError) as e: + print("Modules needed not found: " + str(e)) -from .out import cPrint, Colors -from .upgrader import PiPUpgrader +from out import cPrint, Colors +from upgrader import PiPUpgrader def main(arguments: Namespace): - from os import path - import pickle + try: + import pickle + import logging + import secrets + import string - token = arguments.token - youtube_api_key = arguments.youtube - creator_id = arguments.creator - must_show_version = arguments.version - if must_show_version: - print("Version") - exit(0) - if not path.exists("app_data.dict"): - if not token: - raise ValueError("You must add token at least the first time you execute this app") - elif not youtube_api_key: - raise ValueError("You must include the YouTube API Key at least the first time you execute this app") - elif not creator_id: - 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: - app_data = {"TOKEN": token, - "YT_API": youtube_api_key, - "CREATOR_ID": creator_id} - pickle.dump(app_data, app_data_file, pickle.HIGHEST_PROTOCOL) + from os import path + from telegram.ext import Updater, CommandHandler + except (ImportError, ModuleNotFoundError) as import_error: + print("Modules needed not found: " + str(import_error)) + exit(-1) 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) + token = arguments.token + youtube_api_key = arguments.youtube + creator_id = arguments.creator + database_user = arguments.db_user + database_password = arguments.db_password + must_show_version = arguments.version + if must_show_version: + print("Version") + exit(0) + if not path.exists("app_data.dict"): + if not token: + raise ValueError("You must add token at least the first time you execute this app") + elif not youtube_api_key: + raise ValueError("You must include the YouTube API Key at least the first time you execute this app") + elif not creator_id: + raise ValueError("You must include the creator ID (Telegram) at least the first time you execute " + "this app") + else: + if not database_user: + database_user = "youtube_md_bot" + if not database_password: + alphabet = string.ascii_letters + string.digits + database_password = ''.join(secrets.choice(alphabet) for i in range(32)) + with open("app_data.dict", "wb") as app_data_file: + app_data = {"TOKEN": token, + "YT_API": youtube_api_key, + "CREATOR_ID": creator_id, + "DB_USER": database_user, + "DB_PASSWORD:": database_password} + pickle.dump(app_data, app_data_file, pickle.HIGHEST_PROTOCOL) + main(arguments) + 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: + # unpickler = pickle.Unpickler(app_data_file) + app_data = pickle.load(app_data_file) + updater = Updater(token=app_data["TOKEN"], workers=50) + dispatcher = updater.dispatcher + 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__': @@ -59,6 +81,14 @@ def main(arguments: Namespace): "--creator", help="Telegram ID of the creator", type=int) + args.add_argument("-dbu", + "--db_user", + help="Database user (can be empty)", + type=str) + args.add_argument("-dbp", + "--db_password", + help="Database password (can be empty)", + type=str) args.add_argument("-v", "--version", help="Application version", diff --git a/App/commands/__init__.py b/App/commands/__init__.py new file mode 100644 index 0000000..a68de43 --- /dev/null +++ b/App/commands/__init__.py @@ -0,0 +1,26 @@ +from telegram import Bot, Update +from telegram.ext import run_async + + +class StartHandler: + def __init__(self, start_messages: dict): + self.__messages = start_messages + + @run_async + def start(self, bot: Bot, update: Update): + chat_id = update.message.chat_id + user_id = update.message.user_id + bot.sendMessage(chat_id, self.__messages["welcome"]) + + +class HelpHandler: + def __init__(self, help_messages: dict): + self.__messages = help_messages + + @run_async + def help(self, bot: Bot, update: Update, args: list): + chat_id = update.message.chat_id + if len(args) == 0: + print("no args") + else: + bot.sendMessage(chat_id, self.__messages["help"]) diff --git a/App/database/__init__.py b/App/database/__init__.py new file mode 100644 index 0000000..6122e79 --- /dev/null +++ b/App/database/__init__.py @@ -0,0 +1,3 @@ +class DatabaseOperations: + def __init__(self): + \ No newline at end of file diff --git a/App/requirements.txt b/App/requirements.txt index f556262..2861b8a 100644 --- a/App/requirements.txt +++ b/App/requirements.txt @@ -1,2 +1,3 @@ +telegram pip python-telegram-bot