owlrandomshitbot/tgbot/shit/hentai.py
2025-01-05 00:00:42 +07:00

47 lines
1.2 KiB
Python

import re
def combine_whitespaces(s: str) -> str:
return re.compile(r"\s+").sub(" ", s).strip()
class QueryParams:
def __init__(self, booru: str, tags: list[str], random: bool = True, limit: int = 10):
self.booru = booru
self.tags = tags
self.random = random
self.limit = limit
@classmethod
def from_query_str(cls, query: str):
query = query.replace("hentai ", "")
query = combine_whitespaces(query)
list_ = query.split(" ")
booru = list_[0]
list_.remove(booru)
tags = []
random = False
limit = 10
for e in list_:
if "random=" in e: # this is 'random' argument
random = e.replace("random=", "").lower() == "true"
continue
if "limit=" in e: # this is 'limit' argument
limit = int(e.replace("limit=", ""))
continue
tags.append(e)
return cls(booru, tags, random, limit)
def __str__(self):
return "QueryParams(booru=" + self.booru + ", tags=" + str(self.tags) + ", random=" + str(self.random) + \
", limit=" + str(self.limit) + ")"