def __init__(self): self._log = logging.getLogger(__name__) self._api_key = config.get('SauceNao', 'api_key', fallback=None) self._re_api_key = re.compile(r"^[a-zA-Z0-9]{40}$") self.tracemoe = None bot.loop.create_task(self.purge_cache()) self.ready_tracemoe()
async def _get_sauce(self, ctx: commands.context.Context, url: str) -> typing.Optional[GenericSource]: """ Perform a SauceNao lookup on the supplied URL Args: ctx (commands.context.Context): url (str): Returns: typing.Optional[GenericSource] """ # Get the API key for this server api_key = Servers.lookup_guild(ctx.guild) if not api_key: api_key = self._api_key # Log the query SauceQueries.log(ctx, url) cache = SauceCache.fetch(url) # type: SauceCache if cache: container = getattr(pysaucenao.containers, cache.result_class) sauce = container(cache.header, cache.result) # type: GenericSource self._log.info(f'Cache entry found: {sauce.title}') else: # Initialize SauceNao and execute a search query saucenao = SauceNao(api_key=api_key, min_similarity=float( config.get('SauceNao', 'min_similarity', fallback=50.0)), priority=[21, 22, 5, 37, 25]) search = await saucenao.from_url(url) sauce = search.results[0] if search.results else None # Log output rep = reprlib.Repr() rep.maxstring = 16 self._log.debug( f"[{ctx.guild.name}] {search.short_remaining} short API queries remaining for {rep.repr(api_key)}" ) self._log.info( f"[{ctx.guild.name}] {search.long_remaining} daily API queries remaining for {rep.repr(api_key)}" ) # Cache the search result if sauce: SauceCache.add_or_update(url, sauce) return sauce
import typing from time import time import discord from discord.ext.commands import Context from pony.orm import * from pysaucenao import GenericSource from saucebot.config import config from saucebot.log import log db = Database() if config.has_section('MySQL'): db.bind(provider='mysql', host=config.get('MySQL', 'hostname'), user=config.get('MySQL', 'username'), passwd=config.get('MySQL', 'password'), db=config.get('MySQL', 'database'), charset='utf8mb4') else: db.bind(provider='sqlite', filename='database.sqlite', create_db=True) # noinspection PyMethodParameters class Servers(db.Entity): server_id = Required(int, size=64, unique=True) api_key = Optional(str, 40) @db_session def lookup_guild(guild: discord.Guild) -> typing.Optional[str]:
from discord.ext import commands from saucebot.config import config bot = commands.AutoShardedBot(command_prefix=[p.strip() for p in str(config.get('Bot', 'command_prefixes', fallback='?')).split(',')], case_insensitive=True)
from saucebot.config import config from saucebot.saucebot import bot bot.run(config.get('Discord', 'token'))
def ready_tracemoe(self): token = config.get('TraceMoe', 'token', fallback=None) if token: self.tracemoe = ATraceMoe(bot.loop, token)
from discord.ext import commands from saucebot.config import config bot = commands.AutoShardedBot( command_prefix=[p.strip() for p in str(config.get('Bot', 'command_prefixes', fallback='?')).split(',')], case_insensitive=True, )
# Set up logging import logging from saucebot.config import config logLevel = getattr( logging, str(config.get('Bot', 'log_level', fallback='ERROR')).upper()) logFormat = logging.Formatter("[%(asctime)s] %(levelname)s: %(message)s", "%Y-%m-%d %H:%M:%S") log = logging.getLogger('saucebot') log.setLevel(logLevel) # logging.basicConfig(level=logging.DEBUG) query_log = logging.getLogger('pony.orm.sql') query_log.setLevel(logLevel) ch = logging.StreamHandler() ch.setLevel(logLevel) ch.setFormatter(logFormat) log.addHandler(ch) query_log.addHandler(ch)
# Set up logging import logging import sentry_sdk from saucebot.config import config logLevel = getattr( logging, str(config.get('Bot', 'log_level', fallback='ERROR')).upper()) logFormat = logging.Formatter("[%(asctime)s] %(levelname)s: %(message)s", "%Y-%m-%d %H:%M:%S") log = logging.getLogger('saucebot') log.setLevel(logLevel) # logging.basicConfig(level=logging.DEBUG) query_log = logging.getLogger('pony.orm.sql') query_log.setLevel(logLevel) ch = logging.StreamHandler() ch.setLevel(logLevel) ch.setFormatter(logFormat) log.addHandler(ch) query_log.addHandler(ch) # Unless you're running your own custom fork of saucebot, you probably don't need this. if config.has_option('Bot', 'sentry_logging') and config.getboolean( 'Bot', 'sentry_logging'):