True changes to logic and searx list

This commit is contained in:
Adriene Hutchins 2020-02-19 19:08:41 -05:00
parent 7d9b8c0083
commit 893b66c8dc
2 changed files with 16 additions and 18 deletions

31
main.py
View file

@ -25,8 +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: with open('searxes.txt') as f:
self.instances = instances self.instances = f.read().split('\n')
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):
@ -66,7 +66,7 @@ async def search(ctx, *, query: str):
async with ctx.typing(): async with ctx.typing():
msg = search_logic(query) msg = search_logic(query)
ctx.send(msg) await ctx.send(msg)
@bot.command(aliases=['exit', 'reboot']) @bot.command(aliases=['exit', 'reboot'])
@commands.is_owner() @commands.is_owner()
@ -107,18 +107,19 @@ async def search_logic(query: str, type: str = None):
# Choose an instance & distribute load # Choose an instance & distribute load
if bot.instances == []: if bot.instances == []:
bot.instances = open('searxes.txt') with open('searxes.txt') as f:
instance = random.sample(bot.instances.read().split('\n'), k=1) bot.instances = f.read().split('\n')
bot.instances instance = random.sample(bot.instances, k=1)[0]
print(f"Attempting to use {instance[0]}") bot.instances.remove(instance)
print(f"Attempting to use {instance}")
# Error Template # 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}`. 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._")
# Create the URL to make an API call to # Create the URL to make an API call to
call = f'{instance[0]}/search?q={query}&format=json&language=en-US' call = f'{instance}/search?q={query}&format=json&language=en-US'
# Make said API call # Make said API call
try: try:
@ -138,11 +139,11 @@ async def search_logic(query: str, type: str = None):
f"{results[0]['content']}\n\n") f"{results[0]['content']}\n\n")
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}`._"
except (KeyError, IndexError) as e: except (KeyError, IndexError) as e:
# Reached if error with returned results # Reached if error with returned results
print(f"{e} with instance {instance[0]}, trying again.") print(f"{e} with instance {instance}, trying again.")
return search_logic(query) # Recurse until good response return await search_logic(query) # Recurse until good response
# Send message # Send message
return msg return msg
@ -168,11 +169,11 @@ async def instance_check(instance, info):
# Only picks instances that are fast enough # Only picks instances that are fast enough
timing = int(info['timing']['initial']) timing = int(info['timing']['initial'])
if timing > 0.45: if timing > 0.20:
return False return False
# Check for Google captcha # Check for Google captcha
test_search = f'{instance}/search?q=test&format=json&language=en-US' test_search = f'{instance}/search?q=test&format=json&lang=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()
@ -188,7 +189,7 @@ async def on_command_error(ctx, error):
if isinstance(error, commands.CommandNotFound): if isinstance(error, commands.CommandNotFound):
print(f"\n\nNEW CALL: {ctx.user} from {ctx.server}.\n") print(f"\n\nNEW CALL: {ctx.author} from {ctx.guild}.\n")
async with ctx.typing(): async with ctx.typing():
# Prepares term # Prepares term

View file

@ -1,8 +1,5 @@
https://anyonething.de/
https://haku.ahmia.fi/ https://haku.ahmia.fi/
https://roteserver.de/searx/
https://search.nebulacentre.net/ https://search.nebulacentre.net/
https://searx.ch/
https://searx.nakhan.net/ https://searx.nakhan.net/
https://searx.orcadian.net/ https://searx.orcadian.net/
https://searx.prvcy.eu/ https://searx.prvcy.eu/