def add_or_update(url: str, result: GenericSource): """ Cache a SauceNao result for 24-hours Args: url (str): Url to query result (GenericSource): Result to cache Returns: SauceQueries """ now = int(time()) h = hashlib.new('md5') h.update(url.encode()) log.debug(f"Looking up cache entry {h.hexdigest()}") cache = SauceCache.get(url_hash=h.hexdigest()) if cache: log.debug(f"Refreshing cache entry for {h.hexdigest()}") cache.delete() return SauceCache(url_hash=h.hexdigest(), created_at=now, header=result.header, result=result.data, result_class=type(result).__name__)
def count_total() -> int: """ Get a count of the total number of sauce requests the bot has ever received Returns: int """ total = count(q for q in SauceQueries) log.debug(f"Counted {total} total number of saucenao queries") return total
def validate_url(url: str) -> bool: """ Validates URL's for security Args: url (str): The URL to validate Returns: bool """ if not URL_REGEX.match(url): log.debug("URL failed to match: " + url) return False return True
def user_count(user: discord.User, minutes: int = 5) -> int: """ Get a count of how many requests the specified discord user has made in the specified timespan Args: user (discord.User): minutes (int): Returns: int """ log.debug( f"Counting how many queries {user} has made in the last {minutes} minute(s)" ) cutoff = int(time()) - (minutes * 60) return count(q for q in SauceQueries if q.queried > cutoff)
def log(ctx: Context, url: str): """ Gets the SauceNao API key for the specified guild Args: ctx (Context): url (str): URL to the image that was queries. Will be md5 hashed and stored in the database. Returns: SauceQueries """ now = int(time()) h = hashlib.new('md5') h.update(url.encode()) log.debug( f"Logging query from user {ctx.author} with URL hash {h.hexdigest()}" ) return SauceQueries(server_id=ctx.guild.id, user_id=ctx.author.id, url_hash=h.hexdigest(), queried=now)