Navigation Menu

Skip to content

Commit

Permalink
DB integration and handlers defined
Browse files Browse the repository at this point in the history
  • Loading branch information
Javinator9889 committed Jun 26, 2018
1 parent b174dbe commit bff4dca
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 33 deletions.
138 changes: 117 additions & 21 deletions App/.idea/workspace.xml

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

26 changes: 23 additions & 3 deletions App/application.py
Expand Up @@ -5,6 +5,8 @@

from out import cPrint, Colors
from upgrader import PiPUpgrader
from database import DatabaseOperationsBase
from commands import *


def main(arguments: Namespace):
Expand All @@ -15,7 +17,8 @@ def main(arguments: Namespace):
import string

from os import path
from telegram.ext import Updater, CommandHandler
from telegram import MessageEntity
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
except (ImportError, ModuleNotFoundError) as import_error:
print("Modules needed not found: " + str(import_error))
exit(-1)
Expand Down Expand Up @@ -58,13 +61,30 @@ def main(arguments: Namespace):
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)
cPrint("Starting database system...", Colors.GREEN)
db_manager = DatabaseOperationsBase(username=database_user, password=database_password)
cPrint("Defining handlers...", Colors.GREEN)
start_handler = CommandHandler("start", )
help_handler = CommandHandler("help", )
develop_handler = CommandHandler("develop", )
video_id_handler = MessageHandler(Filters.command, )
url_handler = MessageHandler(Filters.text & (Filters.entity(MessageEntity.URL) |
Filters.entity(MessageEntity.TEXT_LINK)), )
message_handler = MessageHandler(Filters.text, )
unknown_handler = MessageHandler(Filters.all, )
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)
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)


if __name__ == '__main__':
Expand Down
46 changes: 37 additions & 9 deletions App/commands/__init__.py
@@ -1,26 +1,54 @@
from telegram import Bot, Update
from telegram.ext import run_async

from utils import Constants
from database import InsertOperations

class StartHandler:
def __init__(self, start_messages: dict):
self.__messages = start_messages

class Handler:
def __init__(self, handler_messages: dict):
self.__messages = handler_messages

class StartHandler(Handler):
@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"])
import os
import pickle

effective_user = update.effective_user
user_id = effective_user.id
username = effective_user.username
name = effective_user.first_name
user_path = Constants.P_USERS_PATH + user_id + '/'
db_insert: InsertOperations = InsertOperations()
if not os.path.exists(user_path):
os.mkdir(user_path)
with open(user_path + Constants.P_USERS_FILE, "wb") as user_info:
pickle.dump(Constants.D_USER_DICT, user_info)
db_insert.registerNewUser(user_id=user_id, username=username, name=name)


class HelpHandler:
def __init__(self, help_messages: dict):
self.__messages = help_messages
# bot.sendMessage(chat_id, self.__messages["welcome"])


class HelpHandler(Handler):
@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"])


class DeveloperHandler(Handler):
@run_async
def develop(self, bot: Bot, update: Update):
# to do


class VideoIDHandler(Handler):
@run_async
def video_handler(self, bot: Bot, update: Update):
video_id = update.message.text

7 changes: 7 additions & 0 deletions App/utils/__init__.py
@@ -0,0 +1,7 @@
class Constants:
P_USERS_PATH = "user_data/"
P_USERS_FILE = "user_info.json"

D_USER_DICT = {"state": 0,
"is_downloading_video": False,
"pending_videos": 0}

0 comments on commit bff4dca

Please sign in to comment.