mirror of
https://github.com/polyjitter/searchbot-discord.git
synced 2024-08-14 22:46:55 +00:00
Exit cmd + search logic refinement
This commit is contained in:
parent
5e9c3ce568
commit
d9fc49e66d
1 changed files with 42 additions and 15 deletions
57
main.py
57
main.py
|
@ -25,6 +25,8 @@ class Bot(commands.Bot):
|
||||||
self.prefix = self.config.get('PREFIX')
|
self.prefix = self.config.get('PREFIX')
|
||||||
self.version = self.config.get('VERSION')
|
self.version = self.config.get('VERSION')
|
||||||
self.maintenance = self.config.get('MAINTENANCE')
|
self.maintenance = self.config.get('MAINTENANCE')
|
||||||
|
with open('searxes.txt') as instances:
|
||||||
|
self.instances = instances
|
||||||
print('Initialization complete.\n\n')
|
print('Initialization complete.\n\n')
|
||||||
|
|
||||||
async def get_prefix_new(self, bot, msg):
|
async def get_prefix_new(self, bot, msg):
|
||||||
|
@ -59,41 +61,58 @@ bot = Bot(
|
||||||
@bot.command()
|
@bot.command()
|
||||||
async def search(ctx, *, query: str):
|
async def search(ctx, *, query: str):
|
||||||
"""Search online for results."""
|
"""Search online for results."""
|
||||||
|
|
||||||
|
print(f"\n\nNEW CALL: {ctx.user} from {ctx.server}.\n")
|
||||||
|
|
||||||
async with ctx.typing():
|
async with ctx.typing():
|
||||||
msg = search_logic(query)
|
msg = search_logic(query)
|
||||||
ctx.send(msg)
|
ctx.send(msg)
|
||||||
|
|
||||||
|
@bot.command(aliases=['exit', 'reboot'])
|
||||||
|
@commands.is_owner()
|
||||||
|
async def restart(ctx):
|
||||||
|
exit()
|
||||||
|
|
||||||
@bot.command()
|
@bot.command()
|
||||||
@commands.is_owner()
|
@commands.is_owner()
|
||||||
async def rejson(ctx):
|
async def rejson(ctx):
|
||||||
'''Refreshes the list of instances for searx.'''
|
'''Refreshes the list of instances for searx.'''
|
||||||
|
|
||||||
msg = await ctx.send('Refreshing instance list...\n\n(Due to extensive quality checks, this may take a bit.)')
|
msg = await ctx.send('<a:updating:403035325242540032> Refreshing instance list...\n\n'
|
||||||
|
'(Due to extensive quality checks, this may take a bit.)')
|
||||||
plausible = []
|
plausible = []
|
||||||
|
|
||||||
|
# Get, parse, and quality check all instances
|
||||||
async with bot.session.get('https://searx.space/data/instances.json') as r:
|
async with bot.session.get('https://searx.space/data/instances.json') as r:
|
||||||
|
|
||||||
|
# Parsing
|
||||||
searx_json = await r.json()
|
searx_json = await r.json()
|
||||||
instances = searx_json['instances']
|
instances = searx_json['instances']
|
||||||
|
|
||||||
|
# Quality Check
|
||||||
for i in instances:
|
for i in instances:
|
||||||
info = instances.get(i)
|
info = instances.get(i)
|
||||||
is_good = await instance_check(i, info)
|
is_good = await instance_check(i, info)
|
||||||
if is_good:
|
if is_good:
|
||||||
plausible.append(i)
|
plausible.append(i)
|
||||||
|
|
||||||
|
# Save new list
|
||||||
with open('searxes.txt', 'w') as f:
|
with open('searxes.txt', 'w') as f:
|
||||||
f.write('\n'.join(plausible))
|
f.write('\n'.join(plausible))
|
||||||
|
|
||||||
await msg.edit(content='Instances refreshed!')
|
await msg.edit(content='Instances refreshed!')
|
||||||
|
|
||||||
async def search_logic(query: str):
|
async def search_logic(query: str, type: str = None):
|
||||||
|
'''Provides search logic for all search commands.'''
|
||||||
|
|
||||||
# Choose an instance
|
# Choose an instance & distribute load
|
||||||
with open('searxes.txt') as instances:
|
if bot.instances == []:
|
||||||
instance = random.sample(instances.read().split('\n'), k=1)
|
bot.instances = open('searxes.txt')
|
||||||
print(instance)
|
instance = random.sample(bot.instances.read().split('\n'), k=1)
|
||||||
|
bot.instances
|
||||||
|
print(f"Attempting to use {instance[0]}")
|
||||||
|
|
||||||
|
# Error Template
|
||||||
error_msg = ("**An error occured!**\n\n"
|
error_msg = ("**An error occured!**\n\n"
|
||||||
f"There was a problem with `{instance[0]}`. Please try again later.\n"
|
f"There was a problem with `{instance[0]}`. Please try again later.\n"
|
||||||
f"_If problems with this instance persist, contact`{bot.appinfo.owner}` to have it removed._")
|
f"_If problems with this instance persist, contact`{bot.appinfo.owner}` to have it removed._")
|
||||||
|
@ -120,9 +139,10 @@ async def search_logic(query: str):
|
||||||
msg += "\n".join(
|
msg += "\n".join(
|
||||||
[f"**{entry['title']}** <{entry['url']}>" for entry in results[1:5]])
|
[f"**{entry['title']}** <{entry['url']}>" for entry in results[1:5]])
|
||||||
msg += f"\n\n_Results retrieved from instance `{instance[0]}`._"
|
msg += f"\n\n_Results retrieved from instance `{instance[0]}`._"
|
||||||
except (KeyError, IndexError):
|
except (KeyError, IndexError) as e:
|
||||||
print(response['results'])
|
# Reached if error with returned results
|
||||||
return error_msg
|
print(f"{e} with instance {instance[0]}, trying again.")
|
||||||
|
return search_logic(query) # Recurse until good response
|
||||||
|
|
||||||
# Send message
|
# Send message
|
||||||
return msg
|
return msg
|
||||||
|
@ -130,28 +150,29 @@ async def search_logic(query: str):
|
||||||
async def instance_check(instance, info):
|
async def instance_check(instance, info):
|
||||||
'''Checks the quality of an instance.'''
|
'''Checks the quality of an instance.'''
|
||||||
|
|
||||||
|
# Makes sure proper values exist
|
||||||
if 'error' in info:
|
if 'error' in info:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
if not ('engines' in info and 'initial' in info['timing']):
|
if not ('engines' in info and 'initial' in info['timing']):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
if not ('google' in info['engines'] and 'enabled' in info['engines']['google']):
|
if not ('google' in info['engines'] and 'enabled' in info['engines']['google']):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
# Makes sure google is enabled
|
||||||
if not info['engines']['google']['enabled']:
|
if not info['engines']['google']['enabled']:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
# Makes sure is not Tor
|
||||||
if info['network_type'] != 'normal':
|
if info['network_type'] != 'normal':
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
# Only picks instances that are fast enough
|
||||||
timing = int(info['timing']['initial'])
|
timing = int(info['timing']['initial'])
|
||||||
if timing > 0.25:
|
if timing > 0.45:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
test_search = f'{instance}/search?q=test&format=json&language=en-US'
|
|
||||||
|
|
||||||
# Check for Google captcha
|
# Check for Google captcha
|
||||||
|
test_search = f'{instance}/search?q=test&format=json&language=en-US'
|
||||||
try:
|
try:
|
||||||
async with bot.session.get(test_search) as resp:
|
async with bot.session.get(test_search) as resp:
|
||||||
response = await resp.json()
|
response = await resp.json()
|
||||||
|
@ -159,17 +180,23 @@ async def instance_check(instance, info):
|
||||||
except (aiohttp.ClientError, KeyError, IndexError):
|
except (aiohttp.ClientError, KeyError, IndexError):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
# Reached if passes all checks
|
||||||
return True
|
return True
|
||||||
|
|
||||||
@bot.listen("on_command_error")
|
@bot.listen("on_command_error")
|
||||||
async def on_command_error(ctx, error):
|
async def on_command_error(ctx, error):
|
||||||
|
|
||||||
if isinstance(error, commands.CommandNotFound):
|
if isinstance(error, commands.CommandNotFound):
|
||||||
print(ctx.args)
|
|
||||||
|
print(f"\n\nNEW CALL: {ctx.user} from {ctx.server}.\n")
|
||||||
|
|
||||||
async with ctx.typing():
|
async with ctx.typing():
|
||||||
|
# Prepares term
|
||||||
term = ctx.message.content.replace(ctx.prefix, '', 1)
|
term = ctx.message.content.replace(ctx.prefix, '', 1)
|
||||||
term = term.lstrip(' ')
|
term = term.lstrip(' ')
|
||||||
|
# Does search
|
||||||
msg = await search_logic(term)
|
msg = await search_logic(term)
|
||||||
|
# Sends result
|
||||||
await ctx.send(msg)
|
await ctx.send(msg)
|
||||||
|
|
||||||
# elif isinstance(error, commands.CommandInvokeError):
|
# elif isinstance(error, commands.CommandInvokeError):
|
||||||
|
|
Loading…
Reference in a new issue