From e5018dc24a63e9a076194cc508ea706f5ddea3fe Mon Sep 17 00:00:00 2001 From: Nick80835 Date: Sat, 3 Oct 2020 18:05:08 -0400 Subject: [PATCH] unify booru modules --- ubot/modules/booru.py | 152 +++++++++++++++++++++++++++++++++++++++ ubot/modules/danbooru.py | 110 ---------------------------- ubot/modules/gelbooru.py | 110 ---------------------------- ubot/modules/konachan.py | 110 ---------------------------- ubot/modules/sankaku.py | 110 ---------------------------- ubot/modules/yandere.py | 110 ---------------------------- 6 files changed, 152 insertions(+), 550 deletions(-) create mode 100644 ubot/modules/booru.py delete mode 100644 ubot/modules/danbooru.py delete mode 100644 ubot/modules/gelbooru.py delete mode 100644 ubot/modules/konachan.py delete mode 100644 ubot/modules/sankaku.py delete mode 100644 ubot/modules/yandere.py diff --git a/ubot/modules/booru.py b/ubot/modules/booru.py new file mode 100644 index 0000000..5faa72f --- /dev/null +++ b/ubot/modules/booru.py @@ -0,0 +1,152 @@ +# SPDX-License-Identifier: GPL-2.0-or-later + +from asyncbooru import Danbooru, Gelbooru, Konachan, Sankaku, Yandere +from telethon import Button +from ubot import ldr + +help_str = "Fetches images from Danbooru, Gelbooru, Konachan, Sankaku Complex and Yandere, takes tags as arguments." + +dan_api = Danbooru(ldr.aioclient) +gel_api = Gelbooru(ldr.aioclient) +kon_api = Konachan(ldr.aioclient) +san_api = Sankaku(ldr.aioclient) +yan_api = Yandere(ldr.aioclient) + +dan_butt = {} +gel_butt = {} +kon_butt = {} +san_butt = {} +yan_butt = {} + +commands_sfw = { + "dans": dan_api, + "gels": gel_api, + "kons": kon_api, + "sans": san_api, + "yans": yan_api +} + +commands_nsfw = { + ("dan", "danx", "danq"): dan_api, + ("gel", "gelx", "gelq"): gel_api, + ("kon", "konx", "konq"): kon_api, + ("san", "sanx", "sanq"): san_api, + ("yan", "yanx", "yanq"): yan_api +} + +commands_butt = { + "danb": [dan_api, dan_butt, "dan"], + "gelb": [gel_api, gel_butt, "gel"], + "konb": [kon_api, kon_butt, "kon"], + "sanb": [san_api, san_butt, "san"], + "yanb": [yan_api, yan_butt, "yan"] +} + + +@ldr.add_dict(commands_sfw, pattern_extra="(f|)", help=help_str, userlocking=True) +@ldr.add_dict(commands_nsfw, pattern_extra="(f|)", help=help_str, userlocking=True, nsfw=True) +async def booru(event): + safety_arg = event.command[-1] + as_file = bool(event.other_args[0]) + posts = await event.extra.get_random_posts(event.args, 3, safety_arg) + + if not posts: + await event.reply(f"No results for query: {event.args}") + return + + images = [[post.file_url, post.sauce] for post in posts if post.file_url] + + if not images: + await event.reply(f"Failed to find URLs for query: {event.args}") + return + + for image in images: + try: + await event.reply(f"[sauce]({image[1]})", file=image[0], force_document=as_file) + return + except: + pass + + await event.reply(f"Failed to fetch media for query: {event.args}") + + +@ldr.add_inline_photo("dan(s|x|q|)", default="dan", extra=dan_api) +@ldr.add_inline_photo("gel(s|x|q|)", default="gel", extra=gel_api) +@ldr.add_inline_photo("kon(s|x|q|)", default="kon", extra=kon_api) +@ldr.add_inline_photo("san(s|x|q|)", default="san", extra=san_api) +@ldr.add_inline_photo("yan(s|x|q|)", default="yan", extra=yan_api) +async def booru_inline(event): + posts = await event.extra.get_random_posts(event.args, 3, event.other_args[0]) + return [[post.file_url, f"[sauce]({post.sauce})"] for post in posts if post.file_url] if posts else None + + +@ldr.add_dict(commands_butt, help=help_str, userlocking=True, nsfw=True) +async def booru_buttons(event): + posts = await event.extra[0].get_random_posts(event.args, 30) + + if not posts: + await event.reply(f"No results for query: {event.args}") + return + + images = [[post.file_url, post.sauce] for post in posts if post.file_url] + + if not images: + await event.reply(f"Failed to find URLs for query: {event.args}") + return + + event.extra[1][f"{event.chat.id}_{event.id}"] = [0, images] + + await event.reply( + f"[sauce]({images[0][1]})", + file=images[0][0], + buttons=[Button.inline('➡️', f'{event.extra[2]}*{event.chat.id}_{event.id}*r')] + ) + + +@ldr.add_callback_query("dan", extra=dan_butt) +@ldr.add_callback_query("gel", extra=gel_butt) +@ldr.add_callback_query("kon", extra=kon_butt) +@ldr.add_callback_query("san", extra=san_butt) +@ldr.add_callback_query("yan", extra=yan_butt) +async def booru_buttons_callback(event): + args_split = event.args.split("*") + + dict_id = args_split[0] + direction = args_split[1] + + if dict_id in event.extra: + this_dict = event.extra[dict_id] + else: + return + + if direction == "r": + this_dict[0] += 1 + + if this_dict[0] + 1 > len(this_dict[1]): + this_dict[0] = len(this_dict[1]) - 1 + + this_image = this_dict[1][this_dict[0]] + elif direction == "l": + this_dict[0] -= 1 + + if this_dict[0] < 0: + this_dict[0] = 0 + + this_image = this_dict[1][this_dict[0]] + + buttons = [] + + if this_dict[0] > 0: + buttons += [Button.inline('⬅️', f'{event.command}*{dict_id}*l')] + + if len(this_dict[1]) - 1 > this_dict[0]: + buttons += [Button.inline('➡️', f'{event.command}*{dict_id}*r')] + + try: + await event.edit( + f"[sauce]({this_image[1]})", + file=this_image[0], + buttons=buttons + ) + except: + pass diff --git a/ubot/modules/danbooru.py b/ubot/modules/danbooru.py deleted file mode 100644 index b9bb639..0000000 --- a/ubot/modules/danbooru.py +++ /dev/null @@ -1,110 +0,0 @@ -# SPDX-License-Identifier: GPL-2.0-or-later - -from asyncbooru import Danbooru -from telethon import Button -from ubot import ldr - -danbooru_api = Danbooru(ldr.aioclient) -dan_button_dict = {} -help_string = "Fetches images from Danbooru, takes tags as arguments." - - -@ldr.add_list(["dan", "danx", "danq"], pattern_extra="(f|)", nsfw=True, userlocking=True, help=help_string) -@ldr.add("dans", pattern_extra="(f|)", userlocking=True, help=help_string) -async def danbooru(event): - safety_arg = event.command[-1] - as_file = bool(event.other_args[0]) - posts = await danbooru_api.get_random_posts(event.args, 3, safety_arg) - - if not posts: - await event.reply(f"No results for query: {event.args}") - return - - images = [[post.file_url, post.sauce] for post in posts if post.file_url] - - if not images: - await event.reply(f"Failed to find URLs for query: {event.args}") - return - - for image in images: - try: - await event.reply(f"[sauce]({image[1]})", file=image[0], force_document=as_file) - return - except: - pass - - await event.reply(f"Failed to fetch media for query: {event.args}") - - -@ldr.add_inline_photo("dan(s|x|q|)", default="dan") -async def danbooru_inline(event): - posts = await danbooru_api.get_random_posts(event.args, 3, event.other_args[0]) - return [[post.file_url, f"[sauce]({post.sauce})"] for post in posts if post.file_url] if posts else None - - -@ldr.add("danb", nsfw=True, userlocking=True) -async def danbooru_buttons(event): - posts = await danbooru_api.get_random_posts(event.args, 30) - - if not posts: - await event.reply(f"No results for query: {event.args}") - return - - images = [[post.file_url, post.sauce] for post in posts if post.file_url] - - if not images: - await event.reply(f"Failed to find URLs for query: {event.args}") - return - - dan_button_dict[f"{event.chat.id}_{event.id}"] = [0, images] - - await event.reply( - f"[sauce]({images[0][1]})", - file=images[0][0], - buttons=[Button.inline('➡️', f'dan*{event.chat.id}_{event.id}*r')] - ) - - -@ldr.add_callback_query("dan") -async def danbooru_buttons_callback(event): - args_split = event.args.split("*") - - dict_id = args_split[0] - direction = args_split[1] - - if dict_id in dan_button_dict: - this_dict = dan_button_dict[dict_id] - else: - return - - if direction == "r": - this_dict[0] += 1 - - if this_dict[0] + 1 > len(this_dict[1]): - this_dict[0] = len(this_dict[1]) - 1 - - this_image = this_dict[1][this_dict[0]] - elif direction == "l": - this_dict[0] -= 1 - - if this_dict[0] < 0: - this_dict[0] = 0 - - this_image = this_dict[1][this_dict[0]] - - buttons = [] - - if this_dict[0] > 0: - buttons += [Button.inline('⬅️', f'dan*{dict_id}*l')] - - if len(this_dict[1]) - 1 > this_dict[0]: - buttons += [Button.inline('➡️', f'dan*{dict_id}*r')] - - try: - await event.edit( - f"[sauce]({this_image[1]})", - file=this_image[0], - buttons=buttons - ) - except: - pass diff --git a/ubot/modules/gelbooru.py b/ubot/modules/gelbooru.py deleted file mode 100644 index 8d5f58f..0000000 --- a/ubot/modules/gelbooru.py +++ /dev/null @@ -1,110 +0,0 @@ -# SPDX-License-Identifier: GPL-2.0-or-later - -from asyncbooru import Gelbooru -from telethon import Button -from ubot import ldr - -gelbooru_api = Gelbooru(ldr.aioclient) -gel_button_dict = {} -help_string = "Fetches images from Gelbooru, takes tags as arguments." - - -@ldr.add_list(["gel", "gelx", "gelq"], pattern_extra="(f|)", nsfw=True, userlocking=True, help=help_string) -@ldr.add("gels", pattern_extra="(f|)", userlocking=True, help=help_string) -async def gelbooru(event): - safety_arg = event.command[-1] - as_file = bool(event.other_args[0]) - posts = await gelbooru_api.get_random_posts(event.args, 3, safety_arg) - - if not posts: - await event.reply(f"No results for query: {event.args}") - return - - images = [[post.file_url, post.sauce] for post in posts if post.file_url] - - if not images: - await event.reply(f"Failed to find URLs for query: {event.args}") - return - - for image in images: - try: - await event.reply(f"[sauce]({image[1]})", file=image[0], force_document=as_file) - return - except: - pass - - await event.reply(f"Failed to fetch media for query: {event.args}") - - -@ldr.add_inline_photo("gel(s|x|q|)", default="gel") -async def gelbooru_inline(event): - posts = await gelbooru_api.get_random_posts(event.args, 3, event.other_args[0]) - return [[post.file_url, f"[sauce]({post.sauce})"] for post in posts if post.file_url] if posts else None - - -@ldr.add("gelb", nsfw=True, userlocking=True) -async def gelbooru_buttons(event): - posts = await gelbooru_api.get_random_posts(event.args, 30) - - if not posts: - await event.reply(f"No results for query: {event.args}") - return - - images = [[post.file_url, post.sauce] for post in posts if post.file_url] - - if not images: - await event.reply(f"Failed to find URLs for query: {event.args}") - return - - gel_button_dict[f"{event.chat.id}_{event.id}"] = [0, images] - - await event.reply( - f"[sauce]({images[0][1]})", - file=images[0][0], - buttons=[Button.inline('➡️', f'gel*{event.chat.id}_{event.id}*r')] - ) - - -@ldr.add_callback_query("gel") -async def gelbooru_buttons_callback(event): - args_split = event.args.split("*") - - dict_id = args_split[0] - direction = args_split[1] - - if dict_id in gel_button_dict: - this_dict = gel_button_dict[dict_id] - else: - return - - if direction == "r": - this_dict[0] += 1 - - if this_dict[0] + 1 > len(this_dict[1]): - this_dict[0] = len(this_dict[1]) - 1 - - this_image = this_dict[1][this_dict[0]] - elif direction == "l": - this_dict[0] -= 1 - - if this_dict[0] < 0: - this_dict[0] = 0 - - this_image = this_dict[1][this_dict[0]] - - buttons = [] - - if this_dict[0] > 0: - buttons += [Button.inline('⬅️', f'gel*{dict_id}*l')] - - if len(this_dict[1]) - 1 > this_dict[0]: - buttons += [Button.inline('➡️', f'gel*{dict_id}*r')] - - try: - await event.edit( - f"[sauce]({this_image[1]})", - file=this_image[0], - buttons=buttons - ) - except: - pass diff --git a/ubot/modules/konachan.py b/ubot/modules/konachan.py deleted file mode 100644 index 7b99e15..0000000 --- a/ubot/modules/konachan.py +++ /dev/null @@ -1,110 +0,0 @@ -# SPDX-License-Identifier: GPL-2.0-or-later - -from asyncbooru import Konachan -from telethon import Button -from ubot import ldr - -konachan_api = Konachan(ldr.aioclient) -kon_button_dict = {} -help_string = "Fetches images from Konachan, takes tags as arguments." - - -@ldr.add_list(["kon", "konx", "konq"], pattern_extra="(f|)", nsfw=True, userlocking=True, help=help_string) -@ldr.add("kons", pattern_extra="(f|)", userlocking=True, help=help_string) -async def konachan(event): - safety_arg = event.command[-1] - as_file = bool(event.other_args[0]) - posts = await konachan_api.get_random_posts(event.args, 3, safety_arg) - - if not posts: - await event.reply(f"No results for query: {event.args}") - return - - images = [[post.file_url, post.sauce] for post in posts if post.file_url] - - if not images: - await event.reply(f"Failed to find URLs for query: {event.args}") - return - - for image in images: - try: - await event.reply(f"[sauce]({image[1]})", file=image[0], force_document=as_file) - return - except: - pass - - await event.reply(f"Failed to fetch media for query: {event.args}") - - -@ldr.add_inline_photo("kon(s|x|q|)", default="kon") -async def konachan_inline(event): - posts = await konachan_api.get_random_posts(event.args, 3, event.other_args[0]) - return [[post.file_url, f"[sauce]({post.sauce})"] for post in posts if post.file_url] if posts else None - - -@ldr.add("konb", nsfw=True, userlocking=True) -async def konachan_buttons(event): - posts = await konachan_api.get_random_posts(event.args, 30) - - if not posts: - await event.reply(f"No results for query: {event.args}") - return - - images = [[post.file_url, post.sauce] for post in posts if post.file_url] - - if not images: - await event.reply(f"Failed to find URLs for query: {event.args}") - return - - kon_button_dict[f"{event.chat.id}_{event.id}"] = [0, images] - - await event.reply( - f"[sauce]({images[0][1]})", - file=images[0][0], - buttons=[Button.inline('➡️', f'kon*{event.chat.id}_{event.id}*r')] - ) - - -@ldr.add_callback_query("kon") -async def konachan_buttons_callback(event): - args_split = event.args.split("*") - - dict_id = args_split[0] - direction = args_split[1] - - if dict_id in kon_button_dict: - this_dict = kon_button_dict[dict_id] - else: - return - - if direction == "r": - this_dict[0] += 1 - - if this_dict[0] + 1 > len(this_dict[1]): - this_dict[0] = len(this_dict[1]) - 1 - - this_image = this_dict[1][this_dict[0]] - elif direction == "l": - this_dict[0] -= 1 - - if this_dict[0] < 0: - this_dict[0] = 0 - - this_image = this_dict[1][this_dict[0]] - - buttons = [] - - if this_dict[0] > 0: - buttons += [Button.inline('⬅️', f'kon*{dict_id}*l')] - - if len(this_dict[1]) - 1 > this_dict[0]: - buttons += [Button.inline('➡️', f'kon*{dict_id}*r')] - - try: - await event.edit( - f"[sauce]({this_image[1]})", - file=this_image[0], - buttons=buttons - ) - except: - pass diff --git a/ubot/modules/sankaku.py b/ubot/modules/sankaku.py deleted file mode 100644 index 451e8a5..0000000 --- a/ubot/modules/sankaku.py +++ /dev/null @@ -1,110 +0,0 @@ -# SPDX-License-Identifier: GPL-2.0-or-later - -from asyncbooru import Sankaku -from telethon import Button -from ubot import ldr - -sankaku_api = Sankaku(ldr.aioclient) -san_button_dict = {} -help_string = "Fetches images from Sankaku Complex, takes tags as arguments." - - -@ldr.add_list(["san", "sanx", "sanq"], pattern_extra="(f|)", nsfw=True, userlocking=True, help=help_string) -@ldr.add("sans", pattern_extra="(f|)", userlocking=True, help=help_string) -async def sankaku(event): - safety_arg = event.command[-1] - as_file = bool(event.other_args[0]) - posts = await sankaku_api.get_random_posts(event.args, 3, safety_arg) - - if not posts: - await event.reply(f"No results for query: {event.args}") - return - - images = [[post.file_url, post.sauce] for post in posts if post.file_url] - - if not images: - await event.reply(f"Failed to find URLs for query: {event.args}") - return - - for image in images: - try: - await event.reply(f"[sauce]({image[1]})", file=image[0], force_document=as_file) - return - except: - pass - - await event.reply(f"Failed to fetch media for query: {event.args}") - - -@ldr.add_inline_photo("san(s|x|q|)", default="san") -async def sankaku_inline(event): - posts = await sankaku_api.get_random_posts(event.args, 3, event.other_args[0]) - return [[post.file_url, f"[sauce]({post.sauce})"] for post in posts if post.file_url] if posts else None - - -@ldr.add("sanb", nsfw=True, userlocking=True) -async def sankaku_buttons(event): - posts = await sankaku_api.get_random_posts(event.args, 30) - - if not posts: - await event.reply(f"No results for query: {event.args}") - return - - images = [[post.file_url, post.sauce] for post in posts if post.file_url] - - if not images: - await event.reply(f"Failed to find URLs for query: {event.args}") - return - - san_button_dict[f"{event.chat.id}_{event.id}"] = [0, images] - - await event.reply( - f"[sauce]({images[0][1]})", - file=images[0][0], - buttons=[Button.inline('➡️', f'san*{event.chat.id}_{event.id}*r')] - ) - - -@ldr.add_callback_query("san") -async def sankaku_buttons_callback(event): - args_split = event.args.split("*") - - dict_id = args_split[0] - direction = args_split[1] - - if dict_id in san_button_dict: - this_dict = san_button_dict[dict_id] - else: - return - - if direction == "r": - this_dict[0] += 1 - - if this_dict[0] + 1 > len(this_dict[1]): - this_dict[0] = len(this_dict[1]) - 1 - - this_image = this_dict[1][this_dict[0]] - elif direction == "l": - this_dict[0] -= 1 - - if this_dict[0] < 0: - this_dict[0] = 0 - - this_image = this_dict[1][this_dict[0]] - - buttons = [] - - if this_dict[0] > 0: - buttons += [Button.inline('⬅️', f'san*{dict_id}*l')] - - if len(this_dict[1]) - 1 > this_dict[0]: - buttons += [Button.inline('➡️', f'san*{dict_id}*r')] - - try: - await event.edit( - f"[sauce]({this_image[1]})", - file=this_image[0], - buttons=buttons - ) - except: - pass diff --git a/ubot/modules/yandere.py b/ubot/modules/yandere.py deleted file mode 100644 index fa998e7..0000000 --- a/ubot/modules/yandere.py +++ /dev/null @@ -1,110 +0,0 @@ -# SPDX-License-Identifier: GPL-2.0-or-later - -from asyncbooru import Yandere -from telethon import Button -from ubot import ldr - -yandere_api = Yandere(ldr.aioclient) -yan_button_dict = {} -help_string = "Fetches images from Yandere, takes tags as arguments." - - -@ldr.add_list(["yan", "yanx", "yanq"], pattern_extra="(f|)", nsfw=True, userlocking=True, help=help_string) -@ldr.add("yans", pattern_extra="(f|)", userlocking=True, help=help_string) -async def yandere(event): - safety_arg = event.command[-1] - as_file = bool(event.other_args[0]) - posts = await yandere_api.get_random_posts(event.args, 3, safety_arg) - - if not posts: - await event.reply(f"No results for query: {event.args}") - return - - images = [[post.file_url, post.sauce] for post in posts if post.file_url] - - if not images: - await event.reply(f"Failed to find URLs for query: {event.args}") - return - - for image in images: - try: - await event.reply(f"[sauce]({image[1]})", file=image[0], force_document=as_file) - return - except: - pass - - await event.reply(f"Failed to fetch media for query: {event.args}") - - -@ldr.add_inline_photo("yan(s|x|q|)", default="yan") -async def yandere_inline(event): - posts = await yandere_api.get_random_posts(event.args, 3, event.other_args[0]) - return [[post.file_url, f"[sauce]({post.sauce})"] for post in posts if post.file_url] if posts else None - - -@ldr.add("yanb", nsfw=True, userlocking=True) -async def yandere_buttons(event): - posts = await yandere_api.get_random_posts(event.args, 30) - - if not posts: - await event.reply(f"No results for query: {event.args}") - return - - images = [[post.file_url, post.sauce] for post in posts if post.file_url] - - if not images: - await event.reply(f"Failed to find URLs for query: {event.args}") - return - - yan_button_dict[f"{event.chat.id}_{event.id}"] = [0, images] - - await event.reply( - f"[sauce]({images[0][1]})", - file=images[0][0], - buttons=[Button.inline('➡️', f'yan*{event.chat.id}_{event.id}*r')] - ) - - -@ldr.add_callback_query("yan") -async def yandere_buttons_callback(event): - args_split = event.args.split("*") - - dict_id = args_split[0] - direction = args_split[1] - - if dict_id in yan_button_dict: - this_dict = yan_button_dict[dict_id] - else: - return - - if direction == "r": - this_dict[0] += 1 - - if this_dict[0] + 1 > len(this_dict[1]): - this_dict[0] = len(this_dict[1]) - 1 - - this_image = this_dict[1][this_dict[0]] - elif direction == "l": - this_dict[0] -= 1 - - if this_dict[0] < 0: - this_dict[0] = 0 - - this_image = this_dict[1][this_dict[0]] - - buttons = [] - - if this_dict[0] > 0: - buttons += [Button.inline('⬅️', f'yan*{dict_id}*l')] - - if len(this_dict[1]) - 1 > this_dict[0]: - buttons += [Button.inline('➡️', f'yan*{dict_id}*r')] - - try: - await event.edit( - f"[sauce]({this_image[1]})", - file=this_image[0], - buttons=buttons - ) - except: - pass