Skip to content

Commit

Permalink
Merge pull request #2 from ivybowman/dev
Browse files Browse the repository at this point in the history
Dev Merge - Use new log library
  • Loading branch information
Ivy Bowman committed Mar 8, 2022
2 parents bd02cd9 + fc75856 commit 7765d6c
Show file tree
Hide file tree
Showing 8 changed files with 342 additions and 323 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ MANIFEST
autobooks.conf
web_chromeprofile/
web_edgeprofile/
venv/

6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ The intention of this script is solely to automate downloading and converting of

# Features

- AutoBooks Web: Uses selenium and chromedriver to download the odms from overdrive without user interaction.
- Uses odmpy to fulfill and convert odms to chapterized m4b audiobook files.
- AutoBooks Web: Uses selenium and chromedriver to download the odm files from overdrive without user interaction.
- Uses odmpy to fulfill and convert odm files to chapterized m4b audiobooks.
- Moves the generated audiobooks to a chosen folder.
- Backs up the download files in case you need to redownload the books.
- Backs up the download files in case you need to re-download the books.
- Logs to console and timestamped logfile.
- Reports execution status and some logs to a [Cronitor](https://cronitor.io/) monitor.
- Can be controlled via included Discord bot or terminal.
Expand Down
360 changes: 167 additions & 193 deletions autobooks/AutoBooks.py

Large diffs are not rendered by default.

104 changes: 0 additions & 104 deletions autobooks/AutoBooksDiscord.py

This file was deleted.

94 changes: 94 additions & 0 deletions autobooks/DiscordBot.py
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)
6 changes: 6 additions & 0 deletions autobooks/main.py
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()
47 changes: 47 additions & 0 deletions autobooks/utils.py
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)
47 changes: 24 additions & 23 deletions setup.py
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",
]
)

0 comments on commit 7765d6c

Please sign in to comment.