Skip to content

Commit

Permalink
Updated requirements and application
Browse files Browse the repository at this point in the history
  • Loading branch information
Javinator9889 committed Jun 20, 2018
1 parent cd74249 commit c3dd46e
Show file tree
Hide file tree
Showing 5 changed files with 195 additions and 61 deletions.
118 changes: 96 additions & 22 deletions App/.idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

108 changes: 69 additions & 39 deletions 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__':
Expand All @@ -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",
Expand Down
26 changes: 26 additions & 0 deletions 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"])
3 changes: 3 additions & 0 deletions App/database/__init__.py
@@ -0,0 +1,3 @@
class DatabaseOperations:
def __init__(self):

1 change: 1 addition & 0 deletions App/requirements.txt
@@ -1,2 +1,3 @@
telegram
pip
python-telegram-bot

0 comments on commit c3dd46e

Please sign in to comment.