2023-10-12 21:29:09 -04:00
from random import choice
2023-10-16 14:32:57 -04:00
from re import compile
2020-10-03 18:05:08 -04:00
from asyncbooru import Danbooru , Gelbooru , Konachan , Sankaku , Yandere
from telethon import Button
2023-02-05 11:49:51 -05:00
2020-10-03 18:05:08 -04:00
from ubot import ldr
help_str = " Fetches images from Danbooru, Gelbooru, Konachan, Sankaku Complex and Yandere, takes tags as arguments. "
2023-09-17 13:45:27 -04:00
ext_regex = compile ( r " \ .(jpg|jpeg|png|mp4|gif)($| \ ?) " )
2020-10-03 18:05:08 -04:00
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 = { }
2021-02-11 10:47:40 -05:00
normal_commands = {
" dan " : dan_api ,
" gel " : gel_api ,
" kon " : kon_api ,
" san " : san_api ,
" yan " : yan_api
2020-10-03 18:05:08 -04:00
}
2021-02-11 10:47:40 -05:00
button_commands = {
" 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 " ]
2020-10-03 18:05:08 -04:00
}
2021-02-11 10:47:40 -05:00
@ldr.add_dict ( normal_commands , pattern_extra = " (s)(f|) " , help = help_str , userlocking = True )
2021-02-12 11:42:59 -05:00
@ldr.add_dict ( normal_commands , pattern_extra = " (x|q|)(f|) " , userlocking = True , nsfw = True , nsfw_warning = " NSFW commands are disabled in this chat, add ' s ' to the end of the command for SFW images. " , hide_help = True )
2020-10-03 18:05:08 -04:00
async def booru ( event ) :
2021-02-12 12:23:21 -05:00
posts = await event . extra . get_random_posts ( event . args , 3 , event . other_args [ 0 ] )
2020-10-03 18:05:08 -04:00
if not posts :
await event . reply ( f " No results for query: { event . args } " )
return
2023-09-17 13:45:27 -04:00
images = [ [ post . file_url , post . sauce , post . source , post . rating ] for post in posts if post . file_url and ext_regex . search ( post . file_url ) ]
2020-10-03 18:05:08 -04:00
if not images :
await event . reply ( f " Failed to find URLs for query: { event . args } " )
return
for image in images :
try :
2021-07-11 13:19:11 -04:00
await event . reply (
gen_source_string ( image [ 1 ] , image [ 2 ] ) ,
2023-04-26 10:37:38 -04:00
file = image [ 0 ] ,
force_document = bool ( event . other_args [ 1 ] ) ,
2023-09-17 13:45:27 -04:00
spoiler = event . chat_db . spoiler_nsfw and event . extra . _get_rating ( " x " ) == image [ 3 ]
2021-07-11 13:19:11 -04:00
)
2020-10-03 18:05:08 -04:00
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 ] )
2023-02-05 11:49:51 -05:00
return [ [ post . file_url , gen_source_string ( post . sauce , post . source ) ] for post in posts if post . file_url and ext_regex . search ( post . file_url ) ] if posts else None
2020-10-03 18:05:08 -04:00
2021-02-11 10:47:40 -05:00
@ldr.add_dict ( button_commands , pattern_extra = " (s) " , help = help_str , userlocking = True )
2021-02-12 11:42:59 -05:00
@ldr.add_dict ( button_commands , pattern_extra = " (x|q|) " , userlocking = True , nsfw = True , nsfw_warning = " NSFW commands are disabled in this chat, add ' s ' to the end of the command for SFW images. " , hide_help = True )
2020-10-03 18:05:08 -04:00
async def booru_buttons ( event ) :
2021-02-12 12:23:21 -05:00
posts = await event . extra [ 0 ] . get_random_posts ( event . args , 30 , event . other_args [ 0 ] )
2020-10-03 18:05:08 -04:00
if not posts :
await event . reply ( f " No results for query: { event . args } " )
return
2023-10-12 17:21:23 -04:00
images = [ [ post . file_url , post . sauce , post . source , post . rating ] for post in posts if post . file_url and ext_regex . search ( post . file_url ) ]
2020-10-03 18:05:08 -04:00
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 (
2021-07-11 13:19:11 -04:00
gen_source_string ( images [ 0 ] [ 1 ] , images [ 0 ] [ 2 ] ) ,
2023-04-26 10:37:38 -04:00
file = images [ 0 ] [ 0 ] ,
buttons = [ Button . inline ( ' ➡️ ' , f ' { event . extra [ 2 ] } * { event . chat . id } _ { event . id } *r ' ) ] ,
2023-10-12 17:21:23 -04:00
spoiler = event . chat_db . spoiler_nsfw and event . extra [ 0 ] . _get_rating ( " x " ) == images [ 0 ] [ 3 ]
2020-10-03 18:05:08 -04:00
)
2023-02-05 11:49:51 -05:00
@ldr.add_callback_query ( " dan " , extra = [ dan_butt , dan_api ] )
@ldr.add_callback_query ( " gel " , extra = [ gel_butt , gel_api ] )
@ldr.add_callback_query ( " kon " , extra = [ kon_butt , kon_api ] )
@ldr.add_callback_query ( " san " , extra = [ san_butt , san_api ] )
@ldr.add_callback_query ( " yan " , extra = [ yan_butt , yan_api ] )
2020-10-03 18:05:08 -04:00
async def booru_buttons_callback ( event ) :
args_split = event . args . split ( " * " )
dict_id = args_split [ 0 ]
direction = args_split [ 1 ]
2023-02-05 11:49:51 -05:00
this_image = None
2020-10-03 18:05:08 -04:00
2023-02-05 11:49:51 -05:00
if dict_id in event . extra [ 0 ] :
this_dict = event . extra [ 0 ] [ dict_id ]
2020-10-03 18:05:08 -04:00
else :
2023-10-12 21:29:09 -04:00
await event . answer ( " The bot was restarted and the buttons on this message can no longer be used. " , alert = True )
2020-10-03 18:05:08 -04:00
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 (
2021-07-11 13:19:11 -04:00
gen_source_string ( this_image [ 1 ] , this_image [ 2 ] ) ,
2023-04-26 10:37:38 -04:00
file = this_image [ 0 ] ,
buttons = buttons ,
2023-10-12 17:21:23 -04:00
spoiler = event . chat_db . spoiler_nsfw and event . extra [ 1 ] . _get_rating ( " x " ) == this_image [ 3 ]
2020-10-03 18:05:08 -04:00
)
except :
2023-10-12 21:29:09 -04:00
await event . answer (
choice (
[
" I shidded myself on that one. " ,
" Oopsie poopsies. " ,
" Something went wrong, I don ' t know what it was though. " ,
" Yikes, something broke! " ,
" Nick can ' t code and something went wrong. " ,
" Well, this is embarrassing. " ,
" I tripped and fell trying to handle that button press. " ,
" I ' ve failed you for the last time! *dies* " ,
" That shouldn ' t happen. " ,
" Try again maybe? :( " ,
" I broke. " ,
" I ' ve fallen and I can ' t handle that request! "
]
)
)
2021-07-11 13:19:11 -04:00
def gen_source_string ( source : str , orig_source : str ) - > str :
if orig_source and orig_source . startswith ( " http " ) :
2021-07-12 08:30:19 -04:00
return f " [source]( { source } ) - [original source]( { orig_source . split ( ) [ 0 ] } ) "
2021-07-11 13:19:11 -04:00
else :
return f " [source]( { source } ) "