improved logging
This commit is contained in:
parent
d19512e3eb
commit
2e416d3412
1 changed files with 33 additions and 3 deletions
36
delojza.py
36
delojza.py
|
@ -88,7 +88,7 @@ class DelojzaDB:
|
||||||
class DelojzaBot:
|
class DelojzaBot:
|
||||||
def __init__(self, tg_api_key, out_dir, tmp_dir=None, db_path=None, protected_password=None,
|
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):
|
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"))
|
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_downloaded = {}
|
||||||
self.last_hashtags = {}
|
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 "<NONE>"))
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def ytdl_can(url):
|
def ytdl_can(url):
|
||||||
ies = youtube_dl.extractor.gen_extractors()
|
ies = youtube_dl.extractor.gen_extractors()
|
||||||
|
@ -460,8 +483,7 @@ class DelojzaBot:
|
||||||
return ytdl_res or raw_res
|
return ytdl_res or raw_res
|
||||||
|
|
||||||
def tg_handle(self, bot, update):
|
def tg_handle(self, bot, update):
|
||||||
self.logger.debug(f"Received from: {update.message.from_user} ({update.message.chat.id}): " +
|
self._log_msg(update)
|
||||||
update.message.text)
|
|
||||||
hashtags = self._get_hashtags(update.message)
|
hashtags = self._get_hashtags(update.message)
|
||||||
if hashtags:
|
if hashtags:
|
||||||
url_res = self.handle_urls(update.message, self._get_hashtags(update.message))
|
url_res = self.handle_urls(update.message, self._get_hashtags(update.message))
|
||||||
|
@ -491,6 +513,7 @@ class DelojzaBot:
|
||||||
os.listdir(self.out_dir))))
|
os.listdir(self.out_dir))))
|
||||||
|
|
||||||
def tg_stats(self, _, update):
|
def tg_stats(self, _, update):
|
||||||
|
self._log_msg(update)
|
||||||
self.db.initialize()
|
self.db.initialize()
|
||||||
if update.message.chat.id not in self.db.get_protected_chats():
|
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.")
|
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))
|
return sorted(result, key=itemgetter(0))
|
||||||
|
|
||||||
def tg_orphan(self, _, update):
|
def tg_orphan(self, _, update):
|
||||||
|
self._log_msg(update)
|
||||||
self.db.initialize()
|
self.db.initialize()
|
||||||
if update.message.chat.id not in self.db.get_protected_chats():
|
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.")
|
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)))
|
", ".join(map(itemgetter(0), orphans)))
|
||||||
|
|
||||||
def tg_orphan_full(self, _, update):
|
def tg_orphan_full(self, _, update):
|
||||||
|
self._log_msg(update)
|
||||||
self.db.initialize()
|
self.db.initialize()
|
||||||
if update.message.chat.id not in self.db.get_protected_chats():
|
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.")
|
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)
|
update.message.reply_text(tmp_reply)
|
||||||
|
|
||||||
def tg_retag(self, _, update):
|
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:
|
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]
|
files, hashtags, tumblr_ids = self.last_downloaded[update.message.chat.id]
|
||||||
mp3s = [filename for filename in files if filename.endswith("mp3")]
|
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 "") + "???")
|
update.message.reply_text((self.markov.make_sentence() if self.markov and random() > .7 else "") + "???")
|
||||||
|
|
||||||
def tg_delete(self, _, update):
|
def tg_delete(self, _, update):
|
||||||
|
self._log_msg(update)
|
||||||
if self.last_downloaded.get(update.message.chat.id) is not None:
|
if self.last_downloaded.get(update.message.chat.id) is not None:
|
||||||
files, hashtags, tumblr_ids = self.last_downloaded[update.message.chat.id]
|
files, hashtags, tumblr_ids = self.last_downloaded[update.message.chat.id]
|
||||||
for file in files:
|
for file in files:
|
||||||
|
@ -604,6 +631,7 @@ class DelojzaBot:
|
||||||
update.message.reply_text("Nothing to remove!")
|
update.message.reply_text("Nothing to remove!")
|
||||||
|
|
||||||
def tg_protect(self, _, update):
|
def tg_protect(self, _, update):
|
||||||
|
self._log_msg(update)
|
||||||
self.db.initialize()
|
self.db.initialize()
|
||||||
|
|
||||||
msg_split = update.message.text.split(" ")
|
msg_split = update.message.text.split(" ")
|
||||||
|
@ -646,12 +674,14 @@ class DelojzaBot:
|
||||||
|
|
||||||
# noinspection PyMethodMayBeStatic
|
# noinspection PyMethodMayBeStatic
|
||||||
def tg_version(self, _, update):
|
def tg_version(self, _, update):
|
||||||
|
self._log_msg(update)
|
||||||
delojza_date = datetime.fromtimestamp(os.path.getmtime(os.path.realpath(__file__))) \
|
delojza_date = datetime.fromtimestamp(os.path.getmtime(os.path.realpath(__file__))) \
|
||||||
.strftime('%Y/%m/%d - %H:%M:%S')
|
.strftime('%Y/%m/%d - %H:%M:%S')
|
||||||
update.message.reply_text("delojza modified date: {}\nyoutube-dl version: {}"
|
update.message.reply_text("delojza modified date: {}\nyoutube-dl version: {}"
|
||||||
.format(delojza_date, YTDL_VERSION))
|
.format(delojza_date, YTDL_VERSION))
|
||||||
|
|
||||||
def tg_start(self, _, update):
|
def tg_start(self, _, update):
|
||||||
|
self._log_msg(update)
|
||||||
update.message.reply_text(self.markov.make_sentence() if self.markov else "HELLO")
|
update.message.reply_text(self.markov.make_sentence() if self.markov else "HELLO")
|
||||||
|
|
||||||
def tg_error(self, bot, update, error):
|
def tg_error(self, bot, update, error):
|
||||||
|
|
Loading…
Reference in a new issue