all repos — python-meme-bot @ 554c3158af416d7d1078cf9b80bdab056415e1f3

Telegram Bot that uses PIL to compute light image processing.

rely on a better api
Marco Andronaco andronacomarco@gmail.com
Sun, 12 Feb 2023 23:10:35 +0100
commit

554c3158af416d7d1078cf9b80bdab056415e1f3

parent

6cb1f2a6c6b0fef96d4e08b11833b2b4b55f71d9

3 files changed, 23 insertions(+), 106 deletions(-)

jump to
M Api.pyApi.py

@@ -1,74 +1,42 @@

+import requests, logging from PIL import Image, UnidentifiedImageError from io import BytesIO -import requests, random, time, logging, time - -random.seed(time.time()) -base_url = "https://danbooru.donmai.us/" -base_url_test = "https://testbooru.donmai.us/" - -ratings = [ - 'g', # general - 's', # sensitive - 'q', # questionable - 'e', # explicit -] - -rating_normal = "rating:g,s" -rating_lewd = "rating:s,q" +from anime_api.apis import WaifuPicsAPI +from anime_api.apis.waifu_pics.types import ImageCategory -supported_file_types = [ - ".jpg", - ".jpeg", - ".png" -] +max_tries = 5 +supported_file_types = [ ".jpg", ".jpeg", ".png" ] +api = WaifuPicsAPI() def _valid_extension(fname: str): - for t in [".jpg", ".jpeg", ".png"]: + for t in supported_file_types: if fname.lower().endswith(t): return True return False -def get_random_image(rating=rating_normal, tags=""): - page_suffix = "post/index.json" - post_suffix = "posts/" +def get_random_image(nsfw=False): - limit = 100 - max_pages = 1000 - sleep_seconds = 3 - max_tries = 5 - - params = { - "limit": limit, - "tags": rating + tags, - } + cat = ImageCategory.NSFW.WAIFU if nsfw else ImageCategory.SFW.WAIFU - if tags != "": - max_pages = 50 - count = 0 while count < max_tries: - params['page'] = random.randint(1, max_pages) - page = requests.get(base_url + page_suffix, params).json() - n = random.randint(0, params['limit'] - 1) - try: - file_url = page[n]['file_url'] - if not _valid_extension(file_url): + img = api.get_random_image(category=cat) + if not _valid_extension(img.url): raise Exception - r = requests.get(file_url) - img = Image.open(BytesIO(r.content)) - link = base_url + post_suffix + str(page[n]['id']) - return img, link + r = requests.get(img.url) + image = Image.open(BytesIO(r.content)) + + return image, img.url except (KeyError, IndexError, Exception): logging.warning("Can't display image.") except UnidentifiedImageError: - logging.warning("Unidentified image: " + file_url) + logging.warning("Unidentified image: " + img.url) count += 1 logging.warning(f"Try #{count} failed.\n") - #time.sleep(sleep_seconds) logging.error(f"Reached {count} tries. Giving up.") return None, None
M main.pymain.py

@@ -1,5 +1,5 @@

from PIL import Image -from Api import get_random_image, rating_normal, rating_lewd +from Api import get_random_image from Effects import img_to_bio, tt_bt_effect, bt_effect, splash_effect, wot_effect, text_effect from Constants import get_localized_string as l, format_author, format_lang, langs, get_lang, lang_markup from Slot import spin, autospin, bet, cash

@@ -57,22 +57,17 @@ return context.chat_data["lewd"]

except KeyError: return False -def _get_image(context, tag="", bio=True): +def _get_image(context): if context is not None: - if _get_lewd(context): - image, url = get_random_image(rating_lewd, tag) - else: - image, url = get_random_image(rating_normal, tag) + image, url = get_random_image(_get_lewd(context)) if image is None: logging.warning("Getting Image failed") raise TelegramError("bad image") markup = InlineKeyboardMarkup([[InlineKeyboardButton(text=l("sauce", context), url=url)]]) - - if bio: - return image, markup + return image, markup def _get_all(update, check_fn, context):

@@ -110,7 +105,7 @@ if image_content is not None:

image = image_content if image is None: - image, markup = _get_image(context, bio=False) + image, markup = _get_image(context) return content, image, markup

@@ -131,51 +126,8 @@ context.bot.send_message(chat_id=update.effective_chat.id, text=message)

def pic(update: Update, context: CallbackContext): - try: - tag = " " + context.args[0] - except IndexError: - tag = "" - - image, markup = _get_image(context, tag) + image, markup = _get_image(context) update.message.reply_photo(photo=img_to_bio(image), parse_mode="markdown", reply_markup=markup) - -def raw(update: Update, context: CallbackContext): - - tag = "" - try: - tag += " " + context.args[0] - tag += " " + context.args[1] - except IndexError: - pass - - image, url = get_random_image("", tag) - - if image is None: - logging.warning("Getting Image failed") - raise TelegramError("bad image") - - image = img_to_bio(image) - markup = InlineKeyboardMarkup([[InlineKeyboardButton(text=l("sauce", context), url=url)]]) - - update.message.reply_photo(photo=image, parse_mode="markdown", reply_markup=markup) - -def pilu(update: Update, context: CallbackContext): - - logging.warning(f"User {update.message.from_user.username} requested an explicit pic.") - try: - tag = " " + context.args[0] - except IndexError: - tag = "" - image, url = get_random_image("rating:e" + tag) - if image is None: - logging.warning("Getting Image failed") - raise TelegramError("bad image") - return - - image = img_to_bio(image) - markup = InlineKeyboardMarkup([[InlineKeyboardButton(text=l("sauce", context), url=url)]]) - - update.message.reply_photo(photo=image, parse_mode="markdown", reply_markup=markup) def _get_author(message):

@@ -436,10 +388,6 @@ # games

dispatcher.add_handler(CommandHandler('spin', spin)) dispatcher.add_handler(CommandHandler('bet', bet)) dispatcher.add_handler(CommandHandler('cash', cash)) - - # secrets - dispatcher.add_handler(CommandHandler('raw', raw)) - dispatcher.add_handler(CommandHandler('pilu', pilu)) # fallback dispatcher.add_handler(MessageHandler(Filters.command, unknown))
M requirements.txtrequirements.txt

@@ -1,3 +1,4 @@

+anime-api==0.15.4 Pillow==9.2.0 python-dotenv==0.21.0 python-telegram-bot==13.14