From c3dd46e62f49d8028ab35aa870af6553b1c44aad Mon Sep 17 00:00:00 2001 From: Javinator9889 Date: Wed, 20 Jun 2018 17:55:33 +0200 Subject: [PATCH 01/19] Updated requirements and application --- App/.idea/workspace.xml | 118 +++++++++++++++++++++++++++++++-------- App/application.py | 108 ++++++++++++++++++++++------------- App/commands/__init__.py | 26 +++++++++ App/database/__init__.py | 3 + App/requirements.txt | 1 + 5 files changed, 195 insertions(+), 61 deletions(-) create mode 100644 App/commands/__init__.py create mode 100644 App/database/__init__.py 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 From abc06a8ab7e2606f9387ee7202dda1f484c0f8c2 Mon Sep 17 00:00:00 2001 From: Javinator9889 Date: Wed, 20 Jun 2018 18:37:34 +0200 Subject: [PATCH 02/19] updated cql script --- Design/DB_STRUCTURE/db_script.cql | 66 +++++++++++++++---------------- 1 file changed, 31 insertions(+), 35 deletions(-) diff --git a/Design/DB_STRUCTURE/db_script.cql b/Design/DB_STRUCTURE/db_script.cql index 4aa0547..6310f30 100644 --- a/Design/DB_STRUCTURE/db_script.cql +++ b/Design/DB_STRUCTURE/db_script.cql @@ -1,59 +1,55 @@ +CREATE KEYSPACE IF NOT EXISTS YouTubeMDApp + WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 3} + + CREATE TABLE IF NOT EXISTS history ( - user_id INT NOT NULL, - file_id VARCHAR2(256 CHAR) NOT NULL + user_id int NOT NULL, + file_id varchar NOT NULL ); CREATE TABLE IF NOT EXISTS metadata ( - title VARCHAR2(256 CHAR), - artist VARCHAR2(256 CHAR), - cover TEXT, - duration VARCHAR2(20 CHAR), - music_file_id VARCHAR2(256 CHAR) NOT NULL, + title varchar, + artist varchar, + cover text, + duration varchar, + music_file_id varchar NOT NULL, PRIMARY KEY (music_file_id) ); CREATE TABLE IF NOT EXISTS music ( - file_id VARCHAR2(256 CHAR) NOT NULL, - video_id VARCHAR2(20 CHAR), - audio_quality INT, - audio_format INT, - times_requested INT, - is_metadata_by_user CHAR(1), + file_id varchar NOT NULL, + video_id varchar, + audio_quality int, + audio_format int, + times_requested int, + is_metadata_by_user boolean, PRIMARY KEY (file_id) ); CREATE TABLE IF NOT EXISTS playlist ( - playlist_id VARCHAR2(256 CHAR) NOT NULL, - number_elements INT, - times_requested INT, + playlist_id varchar NOT NULL, + number_elements int, + times_requested int, PRIMARY KEY (playlist_id) ); CREATE TABLE IF NOT EXISTS playlist_has_music ( - playlist_playlist_id VARCHAR2(256 CHAR) NOT NULL, - music_file_id VARCHAR2(256 CHAR) NOT NULL + playlist_playlist_id varchar NOT NULL, + music_file_id varchar NOT NULL ); CREATE TABLE IF NOT EXISTS preferences ( - audio_quality VARCHAR2(5 CHAR), - audio_format VARCHAR2(5 CHAR), - os VARCHAR2(10 CHAR), - should_ask_metadata CHAR(1), - user_id INT NOT NULL, + audio_quality varchar, + audio_format varchar, + os varchar, + should_ask_metadata boolean, + user_id int NOT NULL, PRIMARY KEY (user_id) ); CREATE TABLE IF NOT EXISTS "User" ( - user_id INT NOT NULL, - username VARCHAR2(45 CHAR), - name TEXT, - PRIMARY KEY (user_id) -); - -CREATE TABLE IF NOT EXISTS Statistics ( - lang VARCHAR2(5 CHAR), - downloads INT, - last_time_active DATE, - user_id INT NOT NULL, + user_id int NOT NULL, + username varchar, + name text, PRIMARY KEY (user_id) -) \ No newline at end of file +); \ No newline at end of file From 0ba1de161fcbaceaa5f77844822b18e0e0056803 Mon Sep 17 00:00:00 2001 From: Javinator9889 Date: Wed, 20 Jun 2018 20:10:21 +0200 Subject: [PATCH 03/19] Created class for database managing - files for database creation adapted to Cassandra model --- App/.idea/sqlDataSources.xml | 18 +++ App/.idea/workspace.xml | 178 +++++++++++++++++++----------- App/database/__init__.py | 52 ++++++++- App/db_script_pycharm.ddl | 10 ++ App/requirements.txt | 1 + Design/DB_STRUCTURE/db_script.cql | 65 ++--------- 6 files changed, 200 insertions(+), 124 deletions(-) create mode 100644 App/.idea/sqlDataSources.xml create mode 100644 App/db_script_pycharm.ddl diff --git a/App/.idea/sqlDataSources.xml b/App/.idea/sqlDataSources.xml new file mode 100644 index 0000000..b125209 --- /dev/null +++ b/App/.idea/sqlDataSources.xml @@ -0,0 +1,18 @@ + + + + + + \ No newline at end of file diff --git a/App/.idea/workspace.xml b/App/.idea/workspace.xml index 3d10e82..b30117d 100644 --- a/App/.idea/workspace.xml +++ b/App/.idea/workspace.xml @@ -2,11 +2,11 @@ - - + - + +