Comment out old search

This commit is contained in:
Adriene Hutchins 2020-04-08 18:31:08 -04:00
parent fb93adecbe
commit 13e64461be

View file

@ -1,211 +1,211 @@
# This is the old search logic for reference purposes # # This is the old search logic for reference purposes
async def _old_search_logic(self, query: str, is_nsfw: bool = False, # async def _old_search_logic(self, query: str, is_nsfw: bool = False,
category: str = None) -> str: # category: str = None) -> str:
"""Provides search logic for all search commands.""" # """Provides search logic for all search commands."""
# NSFW Filtering # # NSFW Filtering
# WARNING - This list includes slurs. # # WARNING - This list includes slurs.
nono_words = [ # nono_words = [
'tranny', 'faggot', 'fag', # 'tranny', 'faggot', 'fag',
'porn', 'cock', 'dick', # 'porn', 'cock', 'dick',
'titty', 'boob', 'penis', # 'titty', 'boob', 'penis',
'slut', 'cum', 'jizz', # 'slut', 'cum', 'jizz',
'semen', 'cooch', 'coochie', # 'semen', 'cooch', 'coochie',
'pussy', 'penis', 'fetish', # 'pussy', 'penis', 'fetish',
'bdsm', 'sexy', 'xxx', # 'bdsm', 'sexy', 'xxx',
'orgasm', 'masturbation', # 'orgasm', 'masturbation',
'erotic', 'creampie', # 'erotic', 'creampie',
'fap', 'nude', 'orgasm', # 'fap', 'nude', 'orgasm',
'squirting', 'yiff', # 'squirting', 'yiff',
'e621' # 'e621'
] # ]
nono_sites = [ # nono_sites = [
'xvideos', 'pornhub', # 'xvideos', 'pornhub',
'xhamster', 'xnxx', # 'xhamster', 'xnxx',
'youporn', 'xxx', # 'youporn', 'xxx',
'freexcafe', 'sex.com', # 'freexcafe', 'sex.com',
'e621', 'nhentai' # 'e621', 'nhentai'
] # ]
if not is_nsfw: # if not is_nsfw:
for i in nono_words: # for i in nono_words:
if i in query.replace(" ", ""): # if i in query.replace(" ", ""):
return ( # return (
"**Sorry!** That query included language " # "**Sorry!** That query included language "
"we cannot accept in a non-NSFW channel. " # "we cannot accept in a non-NSFW channel. "
"Please try again in an NSFW channel." # "Please try again in an NSFW channel."
) # )
# Choose an instance # # Choose an instance
if self.instances == []: # if self.instances == []:
with open('searxes.txt') as f: # with open('searxes.txt') as f:
self.instances = f.read().split('\n') # self.instances = f.read().split('\n')
instance = random.sample(self.instances, k=1)[0] # instance = random.sample(self.instances, k=1)[0]
# Error Template # # Error Template
error_msg = ( # error_msg = (
"**An error occured!**\n\n" # "**An error occured!**\n\n"
f"There was a problem with `{instance}`. Please try again later.\n" # f"There was a problem with `{instance}`. Please try again later.\n"
f"_If problems with this instance persist, " # f"_If problems with this instance persist, "
f"contact`{self.bot.appinfo.owner}` to have it removed._" # f"contact`{self.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}search?q={query}&format=json&language=en-US' # call = f'{instance}search?q={query}&format=json&language=en-US'
# If a type is provided, add that type to the call URL # # If a type is provided, add that type to the call URL
if category: # if category:
call += f'&categories={category}' # call += f'&categories={category}'
if is_nsfw: # if is_nsfw:
call += '&safesearch=0' # call += '&safesearch=0'
else: # else:
call += '&safesearch=1' # call += '&safesearch=1'
# Figure out engines for different categories to get decent results. # # Figure out engines for different categories to get decent results.
if category == 'videos': # if category == 'videos':
call += '&engines=bing+videos,google+videos' # call += '&engines=bing+videos,google+videos'
# Make said API call # # Make said API call
try: # try:
async with self.request.get(call) as resp: # async with self.request.get(call) as resp:
response = await resp.json() # response = await resp.json()
except aiohttp.ClientError: # except aiohttp.ClientError:
return error_msg # return error_msg
# Split our response data up for parsing # # Split our response data up for parsing
# infoboxes = response['infoboxes'] # # infoboxes = response['infoboxes']
results = response['results'] # results = response['results']
# Create message with results # # Create message with results
try: # try:
# Handle tiny result count # # Handle tiny result count
if len(results) > 5: # if len(results) > 5:
amt = 5 # amt = 5
else: # else:
amt = len(results) # amt = len(results)
# Remove no-no sites # # Remove no-no sites
if not is_nsfw: # if not is_nsfw:
for r in results[0:7]: # for r in results[0:7]:
for n in nono_sites: # for n in nono_sites:
if n in r['url']: # if n in r['url']:
results.remove(r) # results.remove(r)
# Escape stuff # # Escape stuff
query = discord.utils.escape_mentions(query) # query = discord.utils.escape_mentions(query)
query = discord.utils.escape_markdown(query) # query = discord.utils.escape_markdown(query)
# Header # # Header
msg = f"Showing **{amt}** results for `{query}`. \n\n" # msg = f"Showing **{amt}** results for `{query}`. \n\n"
# Expanded Result # # Expanded Result
msg += ( # msg += (
f"**{results[0]['title']}** <{results[0]['url']}>\n" # f"**{results[0]['title']}** <{results[0]['url']}>\n"
f"{results[0]['content']}\n\n") # f"{results[0]['content']}\n\n")
# Other Results # # Other Results
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]])
# Instance Info # # Instance Info
msg += f"\n\n_Results retrieved from instance `{instance}`._" # msg += f"\n\n_Results retrieved from instance `{instance}`._"
return msg # return msg
# Reached if error with returned results # # Reached if error with returned results
except (KeyError, IndexError) as e: # except (KeyError, IndexError) as e:
# Logging # # Logging
self.warn( # self.warn(
f"A user encountered a(n) `{e}` with <{instance}> when searching for `{query}`. " # f"A user encountered a(n) `{e}` with <{instance}> when searching for `{query}`. "
"Consider removing it or looking into it.", # "Consider removing it or looking into it.",
name="Failed Instance" # name="Failed Instance"
) # )
self.instances.remove(instance) # Weed the instance out # self.instances.remove(instance) # Weed the instance out
# Recurse until good response # # Recurse until good response
return await self._old_search_logic(query, is_nsfw) # return await self._old_search_logic(query, is_nsfw)
async def _instance_check(self, instance: str, content: dict) -> bool: # async def _instance_check(self, instance: str, content: dict) -> bool:
"""Checks the quality of an instance.""" # """Checks the quality of an instance."""
# Makes sure proper values exist # # Makes sure proper values exist
if 'error' in content: # if 'error' in content:
return False # return False
if not ('engines' in content and 'initial' in content['timing']): # if not ('engines' in content and 'initial' in content['timing']):
return False # return False
if not ('google' in content['engines'] and 'enabled' in content['engines']['google']): # if not ('google' in content['engines'] and 'enabled' in content['engines']['google']):
return False # return False
# Makes sure google is enabled # # Makes sure google is enabled
if not content['engines']['google']['enabled']: # if not content['engines']['google']['enabled']:
return False # return False
# Makes sure is not Tor # # Makes sure is not Tor
if content['network_type'] != 'normal': # if content['network_type'] != 'normal':
return False # return False
# Only picks instances that are fast enough # # Only picks instances that are fast enough
timing = int(content['timing']['initial']) # timing = int(content['timing']['initial'])
if timing > 0.20: # 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&lang=en-US' # test_search = f'{instance}/search?q=test&format=json&lang=en-US'
try: # try:
async with self.request.get(test_search) as resp: # async with self.request.get(test_search) as resp:
response = await resp.json() # response = await resp.json()
response['results'][0]['content'] # response['results'][0]['content']
except (aiohttp.ClientError, KeyError, IndexError): # except (aiohttp.ClientError, KeyError, IndexError):
return False # return False
# Reached if passes all checks # # Reached if passes all checks
return True # return True
@commands.command() # @commands.command()
@commands.is_owner() # @commands.is_owner()
async def rejson(self, ctx): # async def rejson(self, ctx):
"""Refreshes the list of instances for searx.""" # """Refreshes the list of instances for searx."""
msg = await ctx.send('<a:updating:403035325242540032> Refreshing instance list...\n\n' # msg = await ctx.send('<a:updating:403035325242540032> Refreshing instance list...\n\n'
'(Due to extensive quality checks, this may take a bit.)') # '(Due to extensive quality checks, this may take a bit.)')
plausible: List[str] = [] # plausible: List[str] = []
# Get, parse, and quality check all instances # # Get, parse, and quality check all instances
async with self.request.get('https://searx.space/data/instances.json') as r: # async with self.request.get('https://searx.space/data/instances.json') as r:
# Parsing # # Parsing
searx_json = await r.json() # searx_json = await r.json()
instances = searx_json['instances'] # instances = searx_json['instances']
# Quality Check # # Quality Check
for i in instances: # for i in instances:
content = instances.get(i) # content = instances.get(i)
is_good: bool = await self._instance_check(i, content) # is_good: bool = await self._instance_check(i, content)
if is_good: # if is_good:
plausible.append(i) # plausible.append(i)
# Save new list # # Save new list
self.instances = plausible # self.instances = plausible
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 _old_basic_search(self, ctx, query: str, # async def _old_basic_search(self, ctx, query: str,
category: str = None): # category: str = None):
"""Base search message generation.""" # """Base search message generation."""
async with ctx.typing(): # async with ctx.typing():
is_nsfw = ( # is_nsfw = (
ctx.channel.is_nsfw() if hasattr(ctx.channel, 'is_nsfw') # ctx.channel.is_nsfw() if hasattr(ctx.channel, 'is_nsfw')
else False # else False
) # )
msg = await self._old_search_logic(query, is_nsfw, category) # msg = await self._old_search_logic(query, is_nsfw, category)
await ctx.send(msg) # await ctx.send(msg)
self.info( # self.info(
content=( # content=(
f"**{ctx.author}** searched for `{query}` " # f"**{ctx.author}** searched for `{query}` "
f"in \"{ctx.guild}\" and got this:" # f"in \"{ctx.guild}\" and got this:"
f"\n\n{msg}" # f"\n\n{msg}"
), # ),
name="Search Results" # name="Search Results"
) # )