diff --git a/delojza.py b/delojza.py index c2a1c87..15736d2 100755 --- a/delojza.py +++ b/delojza.py @@ -88,7 +88,7 @@ class DelojzaDB: class DelojzaBot: def __init__(self, tg_api_key, out_dir, tmp_dir=None, db_path=None, protected_password=None, acoustid_key=None, tumblr_name=None, tumblr_keys=None, markov=None): - self.logger = logging.getLogger("delojza") + self._setup_logging(os.path.dirname(os.path.realpath(__file__))) self.db = DelojzaDB(db_path or os.path.join(os.path.dirname(os.path.realpath(__file__)), "delojza.db")) @@ -124,6 +124,29 @@ class DelojzaBot: self.last_downloaded = {} self.last_hashtags = {} + def _setup_logging(self, log_path): + self.logger = logging.getLogger("delojza") + self.logger.setLevel(logging.DEBUG) + + ch = logging.StreamHandler() + ch.setLevel(logging.INFO) + + dfh = logging.FileHandler(log_path + "/delojza.log") + dfh.setLevel(logging.DEBUG) + + formatter = logging.Formatter('%(asctime)s - %(name)s [%(levelname)s] %(message)s') + + ch.setFormatter(formatter) + dfh.setFormatter(formatter) + + self.logger.addHandler(ch) + self.logger.addHandler(dfh) + + def _log_msg(self, update): + from_user = update.message.from_user + self.logger.debug(f"Received from {from_user.username or (from_user.first_name + from_user.last_name)}" + f" ({update.message.chat.id}): " + (update.message.text or "")) + @staticmethod def ytdl_can(url): ies = youtube_dl.extractor.gen_extractors() @@ -460,8 +483,7 @@ class DelojzaBot: return ytdl_res or raw_res def tg_handle(self, bot, update): - self.logger.debug(f"Received from: {update.message.from_user} ({update.message.chat.id}): " + - update.message.text) + self._log_msg(update) hashtags = self._get_hashtags(update.message) if hashtags: url_res = self.handle_urls(update.message, self._get_hashtags(update.message)) @@ -491,6 +513,7 @@ class DelojzaBot: os.listdir(self.out_dir)))) def tg_stats(self, _, update): + self._log_msg(update) self.db.initialize() if update.message.chat.id not in self.db.get_protected_chats(): update.message.reply_text((self.markov.make_sentence() + "!") if self.markov and random() > .7 else "nope.") @@ -533,6 +556,7 @@ class DelojzaBot: return sorted(result, key=itemgetter(0)) def tg_orphan(self, _, update): + self._log_msg(update) self.db.initialize() if update.message.chat.id not in self.db.get_protected_chats(): update.message.reply_text((self.markov.make_sentence() + "!") if self.markov and random() > .7 else "nope.") @@ -545,6 +569,7 @@ class DelojzaBot: ", ".join(map(itemgetter(0), orphans))) def tg_orphan_full(self, _, update): + self._log_msg(update) self.db.initialize() if update.message.chat.id not in self.db.get_protected_chats(): update.message.reply_text((self.markov.make_sentence() + "!") if self.markov and random() > .7 else "nope.") @@ -564,6 +589,7 @@ class DelojzaBot: update.message.reply_text(tmp_reply) def tg_retag(self, _, update): + self._log_msg(update) if self.last_downloaded.get(update.message.chat.id) is not None and " - " in update.message.text: files, hashtags, tumblr_ids = self.last_downloaded[update.message.chat.id] mp3s = [filename for filename in files if filename.endswith("mp3")] @@ -579,6 +605,7 @@ class DelojzaBot: update.message.reply_text((self.markov.make_sentence() if self.markov and random() > .7 else "") + "???") def tg_delete(self, _, update): + self._log_msg(update) if self.last_downloaded.get(update.message.chat.id) is not None: files, hashtags, tumblr_ids = self.last_downloaded[update.message.chat.id] for file in files: @@ -604,6 +631,7 @@ class DelojzaBot: update.message.reply_text("Nothing to remove!") def tg_protect(self, _, update): + self._log_msg(update) self.db.initialize() msg_split = update.message.text.split(" ") @@ -646,12 +674,14 @@ class DelojzaBot: # noinspection PyMethodMayBeStatic def tg_version(self, _, update): + self._log_msg(update) delojza_date = datetime.fromtimestamp(os.path.getmtime(os.path.realpath(__file__))) \ .strftime('%Y/%m/%d - %H:%M:%S') update.message.reply_text("delojza modified date: {}\nyoutube-dl version: {}" .format(delojza_date, YTDL_VERSION)) def tg_start(self, _, update): + self._log_msg(update) update.message.reply_text(self.markov.make_sentence() if self.markov else "HELLO") def tg_error(self, bot, update, error):