Skip to content

Commit

Permalink
Updated "main"
Browse files Browse the repository at this point in the history
  • Loading branch information
Javinator9889 committed Jul 1, 2018
1 parent c72c222 commit c4c1d55
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 62 deletions.
130 changes: 68 additions & 62 deletions youtube_md_bot/__init__.py
Expand Up @@ -3,17 +3,17 @@
except (ImportError, ModuleNotFoundError) as e:
print("Modules needed not found: " + str(e))

from out import cPrint, Colors
from upgrader import PiPUpgrader
from database import DatabaseOperationsBase
from utils import Constants, logger
from handlers.StartHandler import StartHandler
from handlers.HelpHandler import HelpHandler
from handlers.DeveloperHandler import DeveloperHandler
from handlers.VideoIDHandler import VideoIDHandler
from handlers.URLHandler import URLHandler
from handlers.TextHandler import TextHandler
from handlers.UnexpectedHandler import UnexpectedHandler
from .out import cPrint, Colors
from .upgrader import PiPUpgrader
from .database import DatabaseOperationsBase
from .utils import Constants, logger
from .handlers.StartHandler import StartHandler
from .handlers.HelpHandler import HelpHandler
from .handlers.DeveloperHandler import DeveloperHandler
from .handlers.VideoIDHandler import VideoIDHandler
from .handlers.URLHandler import URLHandler
from .handlers.TextHandler import TextHandler
from .handlers.UnexpectedHandler import UnexpectedHandler


def handler_definer():
Expand Down Expand Up @@ -44,6 +44,7 @@ def handler_definer():


def main(arguments: Namespace):
global updater, db_manager
try:
import pickle
import logging
Expand All @@ -53,72 +54,77 @@ def main(arguments: Namespace):
from os import path
from telegram.ext import Updater
except (ImportError, ModuleNotFoundError) as import_error:
print("Modules needed not found: " + str(import_error))
cPrint("Modules needed not found" + str(import_error), Colors.FAIL)
exit(-1)
else:
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:
cPrint("Version: " + Constants.A_APP_VERSION + "-" + Constants.A_APP_TAG_R +
" (" + Constants.A_APP_TAG + ")", Colors.BOLD)
exit(0)
if not path.exists(Constants.A_APP_DATA_FILE):
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")
try:
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:
cPrint("Version: " + Constants.A_APP_VERSION + "-" + Constants.A_APP_TAG_R +
" (" + Constants.A_APP_TAG + ")", Colors.BOLD)
exit(0)
if not path.exists(Constants.A_APP_DATA_FILE):
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 YouTubeAPI 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(Constants.A_APP_DATA_FILE, "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:
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(Constants.A_APP_DATA_FILE, "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)
cPrint("Initializing bot...", Colors.GREEN)
cPrint("Looking for packages updates...", Colors.GREEN)

upgrader = PiPUpgrader(Constants.A_APP_REQ_FILE)
upgrader.upgradePackages()
upgrader = PiPUpgrader(Constants.A_APP_REQ_FILE)
upgrader.upgradePackages()

cPrint("Obtaining values...", Colors.GREEN)
with open(Constants.A_APP_DATA_FILE, "rb") as app_data_file:
app_data = pickle.load(app_data_file)
cPrint("Obtaining values...", Colors.GREEN)
with open(Constants.A_APP_DATA_FILE, "rb") as app_data_file:
app_data = pickle.load(app_data_file)

cPrint("Starting database system...", Colors.GREEN)
db_manager = DatabaseOperationsBase(username=database_user, password=database_password)
cPrint("Starting database system...", Colors.GREEN)
db_manager = DatabaseOperationsBase(username=database_user, password=database_password)

cPrint("Defining handlers...", Colors.GREEN)
handlers = handler_definer()
updater = Updater(token=app_data["TOKEN"], workers=50)
dispatcher = updater.dispatcher
for handler in handlers:
dispatcher.add_handler(handler)
cPrint("Defining handlers...", Colors.GREEN)
handlers = handler_definer()
updater = Updater(token=app_data["TOKEN"], workers=50)
dispatcher = updater.dispatcher
for handler in handlers:
dispatcher.add_handler(handler)

cPrint("Defining log...", Colors.GREEN)
logging.basicConfig(format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
level=logging.DEBUG)
cPrint("Defining log...", Colors.GREEN)
logger.setup_logging(Constants.L_PRIMARY_LOGGER_NAME, Constants.L_PRIMARY_LOGGER_FILENAME,
Constants.L_PRIMARY_LOGGER_MODE)
logger.setup_logging(Constants.L_SECONDARY_LOGGER_NAME, Constants.L_SECONDARY_LOGGER_MODE)
try:
updater.start_polling(poll_interval=5, timeout=60)
except KeyboardInterrupt:
cPrint("Exiting program... Wait while closing threads and pending petitions...", Colors.FAIL)
updater.idle()
db_manager.finishConnection()
exit(0)
except Exception as program_execution_exception:
cPrint(str(program_execution_exception), Colors.FAIL)
exit(-1)


if __name__ == '__main__':
Expand Down
9 changes: 9 additions & 0 deletions youtube_md_bot/utils/__init__.py
@@ -1,3 +1,6 @@
import logging


class Constants:
P_USERS_PATH = "user_data/"
P_USERS_FILE = "user_info.json"
Expand All @@ -12,3 +15,9 @@ class Constants:
A_APP_DATA_FILE = "app_data.dict"
A_APP_REQ_FILE = "requirements.txt"
A_APP_MESSAGES = "messages/messages.json"

L_PRIMARY_LOGGER_NAME = "pLog"
L_PRIMARY_LOGGER_MODE = logging.DEBUG
L_PRIMARY_LOGGER_FILENAME = "youtube-debug-log.log"
L_SECONDARY_LOGGER_NAME = "sLog"
L_SECONDARY_LOGGER_MODE = logging.WARNING

0 comments on commit c4c1d55

Please sign in to comment.