-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from ivybowman/dev
Dev Merge - Use new log library
- Loading branch information
Showing
8 changed files
with
342 additions
and
323 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,4 +31,5 @@ MANIFEST | |
autobooks.conf | ||
web_chromeprofile/ | ||
web_edgeprofile/ | ||
venv/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import glob | ||
import os | ||
import platform | ||
import sys | ||
|
||
import discord | ||
import pandas as pd | ||
from discord.ext import commands | ||
|
||
from AutoBooks import web_run, main_run, version, script_dir, parser, csv_path, LOG_FILENAME, logger | ||
|
||
# Bot Settings | ||
try: | ||
token = parser.get("DEFAULT", "discord_bot_token") | ||
except KeyError: | ||
logger.critical("Bot token field not found in config file, exiting.") | ||
bot = commands.Bot(command_prefix='?') | ||
|
||
|
||
@bot.event | ||
async def on_ready(): | ||
logger.info(f'{bot.user} has connected to Discord!') | ||
|
||
|
||
@bot.command(name='web') | ||
async def hello(ctx): | ||
# Send starting embed and running web | ||
embed_start = discord.Embed(title="Running AutoBooks Web. This may take awhile....", | ||
description=f"V.{version} \n Logfile: {LOG_FILENAME}", color=0xFFAFCC) | ||
embed_start.set_image(url="https://raw.githubusercontent.com/ivybowman/AutoBooks/main/img/logo/small_pink.png") | ||
embed_start.set_footer(text="OS: " + platform.platform() + " Host: " + platform.node()) | ||
await ctx.channel.send(embed=embed_start) | ||
web_info = ["test-data", "test-data2"] | ||
web_run() | ||
|
||
# Ending Embed | ||
embed_end = discord.Embed(title="AutoBooks Web Finished", | ||
description="See log info below for details. ErrorCount: " + str(web_info[1]), | ||
color=0xFFAFCC) | ||
embed_end.set_thumbnail(url="https://raw.githubusercontent.com/ivybowman/AutoBooks/main/img/icon_pink.png") | ||
if web_info[0] != "": | ||
embed_end.add_field(name="Book List", value=str(web_info[0]), inline=False) | ||
await ctx.channel.send(embed=embed_end) | ||
# Logfile fetching | ||
files = glob.glob(os.path.join(script_dir, "log", "*.log")) | ||
files2 = sorted(files, key=os.path.getmtime, reverse=True) | ||
print(files2[0]) | ||
await ctx.channel.send(file=discord.File(files2[0])) | ||
|
||
|
||
@bot.command(name='main') | ||
async def hello(ctx): | ||
embed_var = discord.Embed(title="Title", description="Desc", color=0xFFAFCC) | ||
embed_var.add_field(name="Field1", value="hi", inline=False) | ||
embed_var.add_field(name="Field2", value="hi2", inline=False) | ||
|
||
await ctx.channel.send(embed=embed_var) | ||
main_run() | ||
|
||
|
||
@bot.command(name='log') | ||
async def hello(ctx): | ||
files = glob.glob(os.path.join(script_dir, "log", "*.log")) | ||
max_file = max(files, key=os.path.getmtime) | ||
print(max_file) | ||
await ctx.channel.send("Fetched latest AutoBooks logfile: \n" + max_file) | ||
await ctx.channel.send(file=discord.File(max_file)) | ||
|
||
|
||
@bot.command(name='csv') | ||
async def hello(ctx): | ||
try: | ||
df = pd.read_csv(csv_path, sep=",") | ||
embed_var = discord.Embed(title="Autobooks Known Books", | ||
description=df['audiobook_title'].to_string(index=False), color=0xFFAFCC) | ||
embed_var.set_footer(text="OS: " + platform.platform() + " Host: " + os.uname()) | ||
await ctx.channel.send(embed=embed_var) | ||
except FileNotFoundError: | ||
await ctx.channel.send("Known Books CSV not found.") | ||
# await ctx.channel.send(file=discord.File(max_file)) | ||
|
||
|
||
def discord_run(): | ||
if token == "": | ||
logger.critical("Bot token not found in config file, exiting.") | ||
else: | ||
bot.run(token) | ||
|
||
|
||
if __name__ == "__main__": | ||
try: | ||
discord_run() | ||
except KeyboardInterrupt: | ||
sys.exit(1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# from AutoBooks import web_run, main_run | ||
from DiscordBot import discord_run | ||
import argparse | ||
|
||
if __name__ == "__main__": | ||
discord_run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import logging | ||
|
||
from loguru import logger | ||
|
||
|
||
# Formatter to remove patterns from log output | ||
class RedactingFormatter: | ||
def __init__(self, patterns=None, source_fmt=None): | ||
super().__init__() | ||
self.patterns = patterns | ||
self.fmt = source_fmt | ||
|
||
def format(self, record): | ||
scrubbed = record["message"] | ||
for pattern in self.patterns: | ||
scrubbed = scrubbed.replace(pattern, "") | ||
record["extra"]["scrubbed"] = scrubbed | ||
return self.fmt | ||
|
||
|
||
# Handler to intercept logging messages for loguru | ||
class InterceptHandler(logging.Handler): | ||
def emit(self, record): | ||
try: | ||
level = logger.level(record.levelname).name | ||
except ValueError: | ||
level = record.levelno | ||
|
||
frame, depth = logging.currentframe(), 2 | ||
while frame.f_code.co_filename == logging.__file__: | ||
frame = frame.f_back | ||
depth += 1 | ||
|
||
logger.opt(depth=depth, exception=record.exc_info).log( | ||
level, record.getMessage() | ||
) | ||
|
||
|
||
# Process log file for Cronitor | ||
def process_logfile(logfile, terms=None): | ||
with open(logfile) as logs: | ||
lines = logs.readlines() | ||
log_list = [] | ||
for line in lines: | ||
if any(term in line for term in terms): | ||
log_list.append(line) | ||
return "".join(log_list) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,37 @@ | ||
from setuptools import setup | ||
|
||
VERSION = '0.2.1' | ||
VERSION = '0.3' | ||
DESCRIPTION = 'Python tool to automate processing a batch of OverDrive audiobooks.' | ||
LONG_DESCRIPTION = 'Python tool to automate processing a batch of OverDrive audiobooks.' | ||
|
||
# Setting up | ||
setup( | ||
name="AutoBooks", | ||
version=VERSION, | ||
author="Ivy Bowman", | ||
license="GPL", | ||
url="https://github.com/ivybowman/AutoBooks", | ||
description=DESCRIPTION, | ||
long_description=LONG_DESCRIPTION, | ||
packages=["autobooks"], | ||
entry_points={ | ||
name="AutoBooks", | ||
version=VERSION, | ||
author="Ivy Bowman", | ||
license="GPL", | ||
url="https://github.com/ivybowman/AutoBooks", | ||
description=DESCRIPTION, | ||
long_description=LONG_DESCRIPTION, | ||
packages=["autobooks"], | ||
entry_points={ | ||
"console_scripts": [ | ||
"autobooks = autobooks.AutoBooks:main_run", | ||
"autobooks-web = autobooks.AutoBooks:web_run", | ||
"autobooks-discord = autobooks.AutoBooksDiscord:run" | ||
] | ||
}, | ||
install_requires=['odmpy @ git+https://git@github.com/ping/odmpy.git', "cronitor", "pandas", "discord.py", "selenium", "requests"], | ||
include_package_data=True, | ||
platforms="any", | ||
keywords=['python', 'AutoBooks'], | ||
classifiers= [ | ||
"Development Status :: 4 - Beta", | ||
"License :: OSI Approved :: GNU General Public License v3 (GPLv3)", | ||
"Environment :: Console", | ||
"Intended Audience :: End Users/Desktop", | ||
"Programming Language :: Python :: 3.5", | ||
"Programming Language :: Python :: 3.6", | ||
] | ||
) | ||
install_requires=['odmpy @ git+https://git@github.com/ping/odmpy.git', "cronitor", "pandas", "discord.py", | ||
"selenium", "requests", "loguru"], | ||
include_package_data=True, | ||
platforms="any", | ||
keywords=['python', 'AutoBooks'], | ||
classifiers=[ | ||
"Development Status :: 4 - Beta", | ||
"License :: OSI Approved :: GNU General Public License v3 (GPLv3)", | ||
"Environment :: Console", | ||
"Intended Audience :: End Users/Desktop", | ||
"Programming Language :: Python :: 3.5", | ||
"Programming Language :: Python :: 3.6", | ||
] | ||
) |