Commit e6fede70 authored by Javinator9889's avatar Javinator9889 🎼

Packages and cPrint method - pip upgrader also included

parent 79d85e32
from argparse import ArgumentParser, Namespace
from .out import cPrint, Colors
from .upgrader import PiPUpgrader
def main(arguments: Namespace):
from os import path
import pickle
token = arguments.token
youtube_api_key = arguments.youtube
creator_id = arguments.creator
......@@ -12,18 +17,32 @@ def main(arguments: Namespace):
exit(0)
if not path.exists("app_data.dict"):
if not token:
ValueError("You must add token at least the first time you execute this app")
raise ValueError("You must add token at least the first time you execute this app")
elif not youtube_api_key:
ValueError("You must include the YouTube API Key at least the first time you execute this app")
raise ValueError("You must include the YouTube API Key at least the first time you execute this app")
elif not creator_id:
ValueError("You must include the creator ID (Telegram) at least the first time you execute this app")
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:
import pickle
app_data = {"TOKEN": token,
"YT_API": youtube_api_key,
"CREATOR_ID": creator_id}
pickle.dump(app_data, app_data_file, pickle.HIGHEST_PROTOCOL)
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)
if __name__ == '__main__':
......
class Colors:
RED = "red"
BLUE = "blue"
GREEN = "green"
WARNING = "warning"
FAIL = "fail"
BOLD = "bold"
UNDERLINE = "underline"
RESET_ALL = "reset_all"
class OutputColors:
colors = {Colors.RED: '\033[95m',
Colors.BLUE: '\033[94m',
Colors.GREEN: '\033[92m',
Colors.WARNING: '\033[93m',
Colors.FAIL: '\033[91m',
Colors.BOLD: '\033[1m',
Colors.UNDERLINE: '\033[4m',
Colors.RESET_ALL: '\033[0m'}
def cPrint(text: str, color: Colors = None):
colors = OutputColors.colors
if color is None:
color = colors[Colors.RESET_ALL]
print(colors[color] + text + colors[Colors.RESET_ALL])
pip
python-telegram-bot
class PiPUpgrader:
def __init__(self, file: str):
self.__file = file
def upgradePackages(self):
import subprocess
with open(self.__file, 'r') as f:
requirements = f.read().splitlines()
for requirement in requirements:
subprocess.run(("pip install -U " + requirement).split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment