From 7e5196052fd60ea92beeab9293553dd523bafd22 Mon Sep 17 00:00:00 2001 From: aahnik Date: Fri, 11 Jun 2021 17:53:39 +0530 Subject: [PATCH] update --- tgcf/bot/live_bot.py | 15 ++++++--------- tgcf/bot/utils.py | 7 ++----- tgcf/cli.py | 3 ++- tgcf/config.py | 6 +++--- tgcf/past.py | 2 +- tgcf/plugins/mark.py | 11 ++++++----- 6 files changed, 20 insertions(+), 24 deletions(-) diff --git a/tgcf/bot/live_bot.py b/tgcf/bot/live_bot.py index 3199d411..59cd19f3 100644 --- a/tgcf/bot/live_bot.py +++ b/tgcf/bot/live_bot.py @@ -1,5 +1,7 @@ """A bot to controll settings for tgcf live mode.""" +import logging + import yaml from telethon import events @@ -36,9 +38,7 @@ async def forward_command_handler(event): raise ValueError(f"{notes}\n{display_forwards(config.CONFIG.forwards)}") parsed_args = yaml.safe_load(args) - print(parsed_args) forward = config.Forward(**parsed_args) - print(forward) try: remove_source(forward.source, config.CONFIG.forwards) except: @@ -49,7 +49,7 @@ async def forward_command_handler(event): await event.respond("Success") config.write_config(config.CONFIG) except ValueError as err: - print(err) + logging.error(err) await event.respond(str(err)) finally: @@ -74,17 +74,14 @@ async def remove_command_handler(event): raise ValueError(f"{notes}\n{display_forwards(config.CONFIG.forwards)}") parsed_args = yaml.safe_load(args) - print(parsed_args) source_to_remove = parsed_args.get("source") - print(source_to_remove) config.CONFIG.forwards = remove_source(source_to_remove, config.CONFIG.forwards) - print(config.CONFIG.forwards) config.from_to = config.load_from_to(event.client, config.CONFIG.forwards) await event.respond("Success") config.write_config(config.CONFIG) except ValueError as err: - print(err) + logging.error(err) await event.respond(str(err)) finally: @@ -117,7 +114,7 @@ async def style_command_handler(event): config.write_config(config.CONFIG) except ValueError as err: - print(err) + logging.error(err) await event.respond(str(err)) finally: @@ -135,7 +132,7 @@ async def help_command_handler(event): def get_events(): - _ = get_command_prefix + _ = get_command_prefix() command_events = { "start": (start_command_handler, events.NewMessage(pattern=f"{_}start")), diff --git a/tgcf/bot/utils.py b/tgcf/bot/utils.py index 5b06ec43..bda7da1b 100644 --- a/tgcf/bot/utils.py +++ b/tgcf/bot/utils.py @@ -1,4 +1,5 @@ """helper functions for the bot.""" +import logging from typing import List from telethon import events @@ -32,9 +33,8 @@ def get_args(text: str) -> str: return "" prefix, args = splitted - print(prefix) args = args.strip() - print(args) + logging.info(f"Got command {prefix} with args {args}") return args @@ -55,9 +55,6 @@ def display_forwards(forwards: List[Forward]) -> str: def remove_source(source, forwards: List[Forward]) -> List[Forward]: """Remove a source from forwards.""" for i, forward in enumerate(forwards): - print(forward) - print(type(forward.source)) - print(type(source)) if forward.source == source: del forwards[i] return forwards diff --git a/tgcf/cli.py b/tgcf/cli.py index 2463c5a1..e6af8fc9 100644 --- a/tgcf/cli.py +++ b/tgcf/cli.py @@ -10,9 +10,10 @@ import typer from dotenv import load_dotenv +from pyfiglet import Figlet from rich import traceback from rich.logging import RichHandler -from pyfiglet import Figlet + from tgcf import __version__ load_dotenv(".env") diff --git a/tgcf/config.py b/tgcf/config.py index 35625c48..8c52dccc 100644 --- a/tgcf/config.py +++ b/tgcf/config.py @@ -106,10 +106,10 @@ def read_config() -> Config: else: config = Config() except Exception as err: - print(err) + logging.exception(err) sys.exit(1) else: - logging.info(config.dict()) + logging.info(config) return config @@ -181,5 +181,5 @@ async def _(peer): from_to = {} -is_bot: bool +is_bot: Optional[bool] = None logging.info("config.py got executed") diff --git a/tgcf/past.py b/tgcf/past.py index c090e9e6..ffeede07 100644 --- a/tgcf/past.py +++ b/tgcf/past.py @@ -66,7 +66,7 @@ async def forward_job() -> None: logging.info(f"slept for {CONFIG.past.delay} seconds") except FloodWaitError as fwe: - print(f"Sleeping for {fwe}") + logging.info(f"Sleeping for {fwe}") await asyncio.sleep(delay=fwe.seconds) except Exception as err: logging.exception(err) diff --git a/tgcf/plugins/mark.py b/tgcf/plugins/mark.py index e9e07d72..d32085a0 100644 --- a/tgcf/plugins/mark.py +++ b/tgcf/plugins/mark.py @@ -1,3 +1,4 @@ +import logging import os import shutil from typing import Any, Dict @@ -18,21 +19,21 @@ class MarkConfig(BaseModel): def download_image(url: str, filename: str = "image.png") -> bool: if filename in os.listdir(): - print("Image exists") + logging.info("Image for watermarking already exists.") return True try: - print("Downloading image ... ") + logging.info(f"Downloading image {url}") response = requests.get(url, stream=True) if response.status_code == 200: - print("Got file response") + logging.info("Got Response 200") with open(filename, "wb") as file: response.raw.decode_content = True shutil.copyfileobj(response.raw, file) except Exception as err: - print(err) + logging.error(err) return False else: - print("File created image") + logging.info("File created image") return True