searchbot-discord/main.py

238 lines
7.4 KiB
Python
Raw Normal View History

2020-02-19 19:29:27 +00:00
# -*- coding: utf-8 -*-
2020-02-19 21:04:07 +00:00
# search - a tiny little search utility bot for discord.
2020-02-19 19:29:27 +00:00
# All original work by taciturasa, with some code by ry00001.
2020-02-19 21:04:07 +00:00
# Used and modified with permission.
# See LICENSE for license information.
2020-02-19 19:29:27 +00:00
'''Main File'''
import discord
from discord.ext import commands
import json
import aiohttp
import random
class Bot(commands.Bot):
'''Custom Bot Class that overrides the commands.ext one'''
def __init__(self, **options):
super().__init__(self.get_prefix_new, **options)
print('Performing initialization...\n')
with open('config.json') as f:
self.config = json.load(f)
self.prefix = self.config.get('PREFIX')
self.version = self.config.get('VERSION')
self.maintenance = self.config.get('MAINTENANCE')
2020-02-20 00:08:41 +00:00
with open('searxes.txt') as f:
self.instances = f.read().split('\n')
2020-02-19 19:29:27 +00:00
print('Initialization complete.\n\n')
async def get_prefix_new(self, bot, msg):
return commands.when_mentioned_or(*self.prefix)(bot, msg)
async def on_ready(self):
self.appinfo = await bot.application_info()
self.session = aiohttp.ClientSession()
msg = "CONNECTED!\n"
msg += "-----------------------------\n"
msg += f"ACCOUNT: {bot.user}\n"
msg += f"OWNER: {self.appinfo.owner}\n"
msg += "-----------------------------\n"
print(msg)
async def on_message(self, message):
if message.author.bot:
return
if message.author.id in self.config.get('BLOCKED'):
return
if (self.maintenance):
return
await self.get_context(message)
await self.process_commands(message)
bot = Bot(
description='search - a tiny little search utility bot for discord.',
case_insensitive=True)
@bot.command()
async def search(ctx, *, query: str):
"""Search online for results."""
2020-02-19 23:35:17 +00:00
2020-02-19 23:40:12 +00:00
print(f"\n\nNEW CALL: {ctx.author} from {ctx.guild}.\n")
2020-02-19 23:35:17 +00:00
2020-02-19 19:29:27 +00:00
async with ctx.typing():
msg = search_logic(query)
2020-02-20 00:08:41 +00:00
await ctx.send(msg)
2020-02-19 19:29:27 +00:00
2020-02-19 23:35:17 +00:00
@bot.command(aliases=['exit', 'reboot'])
@commands.is_owner()
async def restart(ctx):
2020-02-20 00:11:39 +00:00
await ctx.send(':zzz: **Restarting.**')
2020-02-19 23:35:17 +00:00
exit()
2020-02-19 19:29:27 +00:00
@bot.command()
@commands.is_owner()
async def rejson(ctx):
'''Refreshes the list of instances for searx.'''
2020-02-19 23:35:17 +00:00
msg = await ctx.send('<a:updating:403035325242540032> Refreshing instance list...\n\n'
'(Due to extensive quality checks, this may take a bit.)')
2020-02-19 19:29:27 +00:00
plausible = []
2020-02-19 23:35:17 +00:00
# Get, parse, and quality check all instances
2020-02-19 19:29:27 +00:00
async with bot.session.get('https://searx.space/data/instances.json') as r:
2020-02-19 23:35:17 +00:00
# Parsing
2020-02-19 19:29:27 +00:00
searx_json = await r.json()
instances = searx_json['instances']
2020-02-19 23:35:17 +00:00
# Quality Check
2020-02-19 19:29:27 +00:00
for i in instances:
info = instances.get(i)
is_good = await instance_check(i, info)
if is_good:
plausible.append(i)
2020-02-19 23:35:17 +00:00
# Save new list
2020-02-19 19:29:27 +00:00
with open('searxes.txt', 'w') as f:
f.write('\n'.join(plausible))
await msg.edit(content='Instances refreshed!')
2020-02-19 23:35:17 +00:00
async def search_logic(query: str, type: str = None):
'''Provides search logic for all search commands.'''
2020-02-19 19:29:27 +00:00
2020-02-19 23:35:17 +00:00
# Choose an instance & distribute load
if bot.instances == []:
2020-02-20 00:08:41 +00:00
with open('searxes.txt') as f:
bot.instances = f.read().split('\n')
instance = random.sample(bot.instances, k=1)[0]
print(f"Attempting to use {instance}")
2020-02-19 19:29:27 +00:00
2020-02-19 23:35:17 +00:00
# Error Template
2020-02-19 21:04:07 +00:00
error_msg = ("**An error occured!**\n\n"
2020-02-20 00:08:41 +00:00
f"There was a problem with `{instance}`. Please try again later.\n"
2020-02-19 21:04:07 +00:00
f"_If problems with this instance persist, contact`{bot.appinfo.owner}` to have it removed._")
2020-02-19 19:29:27 +00:00
# Create the URL to make an API call to
2020-02-20 00:08:41 +00:00
call = f'{instance}/search?q={query}&format=json&language=en-US'
2020-02-19 19:29:27 +00:00
# Make said API call
try:
async with bot.session.get(call) as resp:
response = await resp.json()
except aiohttp.ClientError:
return error_msg
# Split our response data up for parsing
# infoboxes = response['infoboxes']
results = response['results']
# Create message with results
try:
msg = f"Showing **5** results for `{query}`. \n\n"
msg += (f"**{results[0]['title']}** <{results[0]['url']}>\n"
f"{results[0]['content']}\n\n")
msg += "\n".join(
[f"**{entry['title']}** <{entry['url']}>" for entry in results[1:5]])
2020-02-20 00:08:41 +00:00
msg += f"\n\n_Results retrieved from instance `{instance}`._"
2020-02-19 23:35:17 +00:00
except (KeyError, IndexError) as e:
# Reached if error with returned results
2020-02-20 00:08:41 +00:00
print(f"{e} with instance {instance}, trying again.")
2020-02-20 00:11:39 +00:00
bot.instances.remove(instance)
2020-02-20 00:08:41 +00:00
return await search_logic(query) # Recurse until good response
2020-02-19 19:29:27 +00:00
# Send message
return msg
async def instance_check(instance, info):
'''Checks the quality of an instance.'''
2020-02-19 23:35:17 +00:00
# Makes sure proper values exist
2020-02-19 19:29:27 +00:00
if 'error' in info:
return False
if not ('engines' in info and 'initial' in info['timing']):
return False
if not ('google' in info['engines'] and 'enabled' in info['engines']['google']):
return False
2020-02-19 23:35:17 +00:00
# Makes sure google is enabled
2020-02-19 19:29:27 +00:00
if not info['engines']['google']['enabled']:
return False
2020-02-19 23:35:17 +00:00
# Makes sure is not Tor
2020-02-19 19:29:27 +00:00
if info['network_type'] != 'normal':
return False
2020-02-19 23:35:17 +00:00
# Only picks instances that are fast enough
2020-02-19 19:29:27 +00:00
timing = int(info['timing']['initial'])
2020-02-20 00:08:41 +00:00
if timing > 0.20:
2020-02-19 19:29:27 +00:00
return False
# Check for Google captcha
2020-02-20 00:08:41 +00:00
test_search = f'{instance}/search?q=test&format=json&lang=en-US'
2020-02-19 19:29:27 +00:00
try:
async with bot.session.get(test_search) as resp:
response = await resp.json()
response['results'][0]['content']
except (aiohttp.ClientError, KeyError, IndexError):
return False
2020-02-19 23:35:17 +00:00
# Reached if passes all checks
2020-02-19 19:29:27 +00:00
return True
@bot.listen("on_command_error")
async def on_command_error(ctx, error):
if isinstance(error, commands.CommandNotFound):
2020-02-19 23:35:17 +00:00
2020-02-20 00:08:41 +00:00
print(f"\n\nNEW CALL: {ctx.author} from {ctx.guild}.\n")
2020-02-19 23:35:17 +00:00
2020-02-19 19:29:27 +00:00
async with ctx.typing():
2020-02-19 23:35:17 +00:00
# Prepares term
2020-02-19 19:29:27 +00:00
term = ctx.message.content.replace(ctx.prefix, '', 1)
term = term.lstrip(' ')
2020-02-19 23:35:17 +00:00
# Does search
2020-02-19 19:29:27 +00:00
msg = await search_logic(term)
2020-02-19 23:35:17 +00:00
# Sends result
2020-02-19 19:29:27 +00:00
await ctx.send(msg)
# elif isinstance(error, commands.CommandInvokeError):
# error = error.original
# _traceback = traceback.format_tb(error.__traceback__)
# _traceback = ''.join(_traceback)
# embed_fallback = "**ERROR: <@97788939196182528>**"
# error_embed = discord.Embed(
# title="An error has occurred.",
# color=0xFF0000,
# description=(
# "This is (probably) a bug. This has been automatically "
# f"reported, but give **{bot.appinfo.owner}** a heads-up in DMs.")
# )
# trace_content = (
# "```py\n\nTraceback (most recent call last):"
# "\n{}{}: {}```").format(
# _traceback,
# type(error).__name__,
# error)
# error_embed.add_field(
# name="`{}` in command `{}`".format(
# type(error).__name__, ctx.command.qualified_name),
# value=(trace_content[:1018] + '...```')
# if len(trace_content) > 1024
# else trace_content)
# await ctx.send(embed_fallback, embed=error_embed)
else:
await ctx.send(error)
bot.run(bot.config['TOKEN'])