Use built-in logger

This commit is contained in:
Luke Murphy 2021-02-27 10:45:19 +01:00
parent 9625c991c6
commit 36eed56661
No known key found for this signature in database
GPG Key ID: 5E2EF5A63E3718CC

View File

@ -25,7 +25,7 @@ def del_from_db(self, message, key):
del self.db[message.room]["messages"][key]
def write_log(self, message):
def write_log(self, message, logger):
template = jinja2.Template(open("template.html").read())
roomname = re.sub(r"@.*", "", message.room)
log_path = os.path.join(self.output, roomname, "index.html")
@ -34,10 +34,10 @@ def write_log(self, message):
title=self.db[message.room]["title"], db=self.db[message.room]["messages"]
)
out.write(html)
print("writing to: ", log_path)
logger.info("writing to: ", log_path)
def download(self, message):
def download(self, message, logger):
# define media_type
if message.url.lower().endswith(
(".jpg", "jpeg", "png", ".gif", ".bmp", ".svg", "eps")
@ -73,12 +73,12 @@ def download(self, message):
# download file
data = urllib.request.urlopen(message.url).read()
if data:
print("downloading: ", message.url)
logger.info("downloading: ", message.url)
parsed_url = urlparse(message.url)
filename = (
os.path.basename(parsed_url.path).replace(" ", "_").replace("%20", "_")
) # safe url's
print("as the file: ", filename)
self.log.info("as the file: ", filename)
roomname = re.sub(r"@.*", "", message.room)
path = os.path.join(self.output, roomname, media_type)
if not os.path.isdir(path):
@ -147,39 +147,39 @@ logbot @uptime: To check how long @logbot has been around
"""
def setup(self):
print("INFO ", f"Output folder is set to: { self.output }")
self.log.info(f"Output folder is set to: { self.output }")
for room in self.rooms:
roomname = str(re.match(r".*@", room).group()).replace("@", "")
self.room_path = os.path.join(self.output, roomname)
print("INFO ", f"Room path is set to: { self.room_path }")
self.log.info(f"Room path is set to: { self.room_path }")
# Check if the room is in the database already
if not room in self.db.keys():
self.db[room] = {}
self.db[room]["messages"] = {}
self.db[room]["title"] = room
print("INFO ", f"Added to the database: { room }")
self.log.info(f"Added to the database: { room }")
# Check if the room has an output folder already
if not os.path.exists(self.room_path):
os.mkdir(self.room_path)
shutil.copy("stylesheet.css", self.room_path)
print("INFO ", f"Created a folder for: { room }")
print("INFO ", f"Copied stylesheet.css to: { room }")
self.log.info(f"Created a folder for: { room }")
self.log.info(f"Copied stylesheet.css to: { room }")
def group(self, message):
# to debug in the terminal
print("------------------")
print("message: ", message.text)
print("room: ", message.room)
print("sender: ", message.sender)
self.log.info("------------------")
self.log.info("message: ", message.text)
self.log.info("room: ", message.room)
self.log.info("sender: ", message.sender)
# image / PDF / audio / video
if message.url:
media_post, media_type = download(self, message)
media_post, media_type = download(self, message, self.log)
if media_post:
add_to_db(self, message, media_post=media_post)
media_type = media_type.replace("images", "image") # linguistic hack!
@ -196,7 +196,7 @@ logbot @uptime: To check how long @logbot has been around
key = str(match.replace("@delete ", ""))
if key in self.db[message.room]["messages"]:
print("To be deleted:", self.db[message.room]["messages"][key])
self.log.info("To be deleted:", self.db[message.room]["messages"][key])
reply = f"This message is deleted: { self.db[message.room]['messages'][key] }"
del_from_db(self, message, key)
else:
@ -214,7 +214,7 @@ logbot @uptime: To check how long @logbot has been around
else:
reply = "Hmm ... not sure what you want to do?"
write_log(self, message)
write_log(self, message, self.log)
return self.reply(reply, room=message.room)