fixed ttbt
Marco Andronaco andronacomarco@gmail.com
Tue, 06 Sep 2022 00:50:59 +0200
3 files changed,
41 insertions(+),
32 deletions(-)
M
Api.py
→
Api.py
@@ -1,7 +1,7 @@
from PIL import Image, UnidentifiedImageError from io import BytesIO -import requests, random, time +import requests, random, time, logging base_url = "https://danbooru.donmai.us/" base_url_test = "https://testbooru.donmai.us/"@@ -20,9 +20,21 @@ rating_normal = "g,s"
rating_lewd = "q" limit = 100 -max_pages = 700 +max_pages = 1000 sleep_seconds = 3 max_tries = 5 + +supported_file_types = [ + ".jpg", + ".jpeg", + ".png" +] + +def _valid_extension(fname: str): + for t in supported_file_types: + if fname.lower().endswith(t): + return True + return False def get_random_image(rating=rating_normal): params = {@@ -33,35 +45,27 @@ }
count = 0 while count < max_tries: params['page'] = random.randint(1, max_pages) - r = requests.get(base_url + page_suffix, params) - page = r.json() + page = requests.get(base_url + page_suffix, params).json() + n = random.randint(0, limit - 1) + #print("Page: " + str(params['page'])) - - if 'success' in page: - if not page['success']: - print("Error: " + page['error']) - print("Message: " + page['message']) - else: - print(page) - else: - n = random.randint(0, limit - 1) - #print("File: " + str(n)) - try: - file_url = page[n]['file_url'] - r = requests.get(file_url) - try: - img = Image.open(BytesIO(r.content)) - return img, base_url + post_suffix + str(page[n]['id']) - except UnidentifiedImageError: - print("Unidentified image: " + file_url) - except KeyError: - print("Image has no file_url. post: " + base_url + post_suffix + str(page[n]['id'])) - print(str(page[n])) - except IndexError: - print("Page does not exist. " + str(page)) + #print("File: " + str(n)) + try: + file_url = page[n]['file_url'] + if not _valid_extension(file_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 + + except (KeyError, IndexError, Exception): + logging.warning("Can't display image.") + except UnidentifiedImageError: + logging.warning("Unidentified image: " + file_url) count += 1 - print(f"Try #{count} failed. Retrying in {sleep_seconds} seconds...\n") + logging.warning(f"Try #{count} failed.\n") #time.sleep(sleep_seconds) - print(f"Reached {count} tries. Giving up.") + logging.error(f"Reached {count} tries. Giving up.") return None, None
M
Constants.py
→
Constants.py
@@ -7,7 +7,7 @@ 'lewd_toggle' : "Lewd content was {} for this chat.",
'enabled' : "enabled", 'disabled' : "disabled", 'unknown' : "Sorry, I didn't understand that command.", - 'error': "Something bad happened. Please retry.", + 'error': "An error has occurred. Please retry.", } }
M
main.py
→
main.py
@@ -46,7 +46,8 @@
if input is None: return None, fallback - if input.photo is not None: + image = None + if len(input.photo) > 0: image = input.photo[-1].get_file() #image = context.bot.get_file(input.photo[-1].file_id) image = Image.open(BytesIO(image.download_as_bytearray()))@@ -146,8 +147,12 @@ def ttbt(update: Update, context: CallbackContext):
message = update.message image, reply = _get_reply(message.reply_to_message, context) - content = message.text[6:] # /ttbt[space] + content = message.text.split(" ") + content.pop(0) + content = " ".join(content) + input_text = f"{reply}\n{content}" + print(message.text.split(" ")) image, markup =_ttbt_general(context, input_text, image)