From c217b791687a0402482100aae34ec4d2775926ce Mon Sep 17 00:00:00 2001 From: aahnik Date: Fri, 11 Jun 2021 18:56:07 +0530 Subject: [PATCH] enhance logging, remove prints --- tgcf/bot/utils.py | 13 ++++++++----- tgcf/past.py | 2 +- tgcf/plugins/filter.py | 6 +++--- tgcf/plugins/mark.py | 11 ++++++----- 4 files changed, 18 insertions(+), 14 deletions(-) diff --git a/tgcf/bot/utils.py b/tgcf/bot/utils.py index 0adb7166..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,10 +55,13 @@ 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 raise ValueError("The source does not exist") + + +def get_command_prefix(): + if config.is_bot is None: + raise ValueError("config.is_bot is not set!") + return "/" if config.is_bot else "." 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/filter.py b/tgcf/plugins/filter.py index 39a87c2e..11bd13e9 100644 --- a/tgcf/plugins/filter.py +++ b/tgcf/plugins/filter.py @@ -32,7 +32,6 @@ class TgcfFilter(TgcfPlugin): id_ = "filter" def __init__(self, data: Dict[str, Any]) -> None: - print("tgcf filter data loaded") self.filters = Filters(**data) self.case_correct() logging.info(self.filters) @@ -47,8 +46,11 @@ def case_correct(self) -> None: def modify(self, tm: TgcfMessage) -> TgcfMessage: if self.users_safe(tm): + logging.info("Message passed users filter") if self.files_safe(tm): + logging.info("Message passed files filter") if self.text_safe(tm): + logging.info("Message passed text filter") return tm def text_safe(self, tm: TgcfMessage) -> bool: @@ -76,7 +78,6 @@ def text_safe(self, tm: TgcfMessage) -> bool: def users_safe(self, tm: TgcfMessage) -> bool: flist = self.filters.users sender = str(tm.sender_id) - logging.info(f"M message from sender id {sender}") if sender in flist.blacklist: return False if not flist.whitelist: @@ -87,7 +88,6 @@ def users_safe(self, tm: TgcfMessage) -> bool: def files_safe(self, tm: TgcfMessage) -> bool: flist = self.filters.files fl_type = tm.file_type - print(fl_type) if fl_type in flist.blacklist: return False if not flist.whitelist: 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