all repos — python-meme-bot @ 735d49cd8cabf16eae27053532192db24cee5cbb

Telegram Bot that uses PIL to compute light image processing.

add basic tag functionality
Marco Andronaco andronacomarco@gmail.com
Tue, 06 Sep 2022 02:41:15 +0200
commit

735d49cd8cabf16eae27053532192db24cee5cbb

parent

76f731cad4e0cdd6870d5fdeae3f9dbc4b010643

2 files changed, 45 insertions(+), 15 deletions(-)

jump to
M Api.pyApi.py

@@ -16,13 +16,10 @@ 'q', # questionable

'e', # explicit ] -rating_normal = "g,s" -rating_lewd = "q" +rating_normal = "rating:g,s" +rating_lewd = "rating:q" + -limit = 100 -max_pages = 1000 -sleep_seconds = 3 -max_tries = 5 supported_file_types = [ ".jpg",

@@ -36,17 +33,26 @@ if fname.lower().endswith(t):

return True return False -def get_random_image(rating=rating_normal): +def get_random_image(rating=rating_normal, tags=""): + limit = 100 + max_pages = 1000 + sleep_seconds = 3 + max_tries = 5 + params = { "limit": limit, #"tags": "order:change_desc rating:" + rating, - "tags": "rating:" + rating, + "tags": rating + tags, } + + 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, limit - 1) + n = random.randint(0, params['limit'] - 1) #print("Page: " + str(params['page'])) #print("File: " + str(n))
M main.pymain.py

@@ -66,12 +66,12 @@ return context.chat_data["lewd"]

except KeyError: return False -def _get_image(context, bio=True): +def _get_image(context, tag="", bio=True): if context is not None: if _get_lewd(context): - image, url = get_random_image(rating_lewd) + image, url = get_random_image(rating_lewd, tag) else: - image, url = get_random_image(rating_normal) + image, url = get_random_image(rating_normal, tag) if image is None: logging.warning("Getting Image failed")

@@ -99,7 +99,31 @@

context.bot.send_message(chat_id=update.effective_chat.id, text=message) def pic(update: Update, context: CallbackContext): - image, markup = _get_image(context) + try: + tag = " " + context.args[0] + except IndexError: + tag = "" + + image, markup = _get_image(context, tag) + update.message.reply_photo(photo=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", lang), url=url)]]) + update.message.reply_photo(photo=image, parse_mode="markdown", reply_markup=markup) def pilu(update: Update, context: CallbackContext):

@@ -108,7 +132,7 @@ try:

tag = " " + context.args[0] except IndexError: tag = "" - image, url = get_random_image("e" + tag) + image, url = get_random_image("rating:e" + tag) if image is None: logging.warning("Getting Image failed") raise TelegramError("bad image")

@@ -152,7 +176,6 @@ content.pop(0)

content = " ".join(content) input_text = f"{reply}\n{content}" - print(message.text.split(" ")) image, markup =_ttbt_general(context, input_text, image)

@@ -186,6 +209,7 @@ dispatcher.add_handler(CommandHandler('start', start))

dispatcher.add_handler(CommandHandler('lewd', set_lewd)) dispatcher.add_handler(CommandHandler('caps', caps)) dispatcher.add_handler(CommandHandler('pic', pic)) + dispatcher.add_handler(CommandHandler('raw', raw)) dispatcher.add_handler(CommandHandler('pilu', pilu)) dispatcher.add_handler(CommandHandler('ttbt', ttbt)) dispatcher.add_handler(CommandHandler('tt', tt))