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