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
async def _old_search_logic(self, query: str, is_nsfw: bool = False,
category: str = None) -> str:
"""Provides search logic for all search commands."""
# # This is the old search logic for reference purposes
# NSFW Filtering
# WARNING - This list includes slurs.
nono_words = [
'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'
]
# async def _old_search_logic(self, query: str, is_nsfw: bool = False,
# category: str = None) -> str:
# """Provides search logic for all search commands."""
if not is_nsfw:
for i in nono_words:
if i in query.replace(" ", ""):
return (
"**Sorry!** That query included language "
"we cannot accept in a non-NSFW channel. "
"Please try again in an NSFW channel."
)
# # NSFW Filtering
# # WARNING - This list includes slurs.
# nono_words = [
# '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'
# ]
# Choose an instance
if self.instances == []:
with open('searxes.txt') as f:
self.instances = f.read().split('\n')
instance = random.sample(self.instances, k=1)[0]
# if not is_nsfw:
# for i in nono_words:
# if i in query.replace(" ", ""):
# return (
# "**Sorry!** That query included language "
# "we cannot accept in a non-NSFW channel. "
# "Please try again in an NSFW channel."
# )
# Error Template
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._"
)
# # Choose an instance
# if self.instances == []:
# with open('searxes.txt') as f:
# self.instances = f.read().split('\n')
# instance = random.sample(self.instances, k=1)[0]
# Create the URL to make an API call to
call = f'{instance}search?q={query}&format=json&language=en-US'
# # Error Template
# 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
if category:
call += f'&categories={category}'
# # Create the URL to make an API call to
# call = f'{instance}search?q={query}&format=json&language=en-US'
if is_nsfw:
call += '&safesearch=0'
else:
call += '&safesearch=1'
# # If a type is provided, add that type to the call URL
# if category:
# call += f'&categories={category}'
# Figure out engines for different categories to get decent results.
if category == 'videos':
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
# if is_nsfw:
# call += '&safesearch=0'
# else:
# call += '&safesearch=1'
# Split our response data up for parsing
# infoboxes = response['infoboxes']
results = response['results']
# # Figure out engines for different categories to get decent results.
# if category == 'videos':
# 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
try:
# Handle tiny result count
if len(results) > 5:
amt = 5
else:
amt = len(results)
# # Split our response data up for parsing
# # infoboxes = response['infoboxes']
# results = response['results']
# Remove no-no sites
if not is_nsfw:
for r in results[0:7]:
for n in nono_sites:
if n in r['url']:
results.remove(r)
# # Create message with results
# try:
# # Handle tiny result count
# if len(results) > 5:
# amt = 5
# else:
# amt = len(results)
# Escape stuff
query = discord.utils.escape_mentions(query)
query = discord.utils.escape_markdown(query)
# # Remove no-no sites
# if not is_nsfw:
# for r in results[0:7]:
# for n in nono_sites:
# if n in r['url']:
# results.remove(r)
# 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}`._"
# # Escape stuff
# query = discord.utils.escape_mentions(query)
# query = discord.utils.escape_markdown(query)
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
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"
)
# return msg
self.instances.remove(instance) # Weed the instance out
# Recurse until good response
return await self._old_search_logic(query, is_nsfw)
# # Reached if error with returned results
# 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"
# )
async def _instance_check(self, instance: str, content: dict) -> bool:
"""Checks the quality of an instance."""
# self.instances.remove(instance) # Weed the instance out
# # Recurse until good response
# return await self._old_search_logic(query, is_nsfw)
# Makes sure proper values exist
if 'error' in content:
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
# async def _instance_check(self, instance: str, content: dict) -> bool:
# """Checks the quality of an instance."""
# Makes sure google is enabled
if not content['engines']['google']['enabled']:
return False
# # Makes sure proper values exist
# if 'error' in content:
# 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
if content['network_type'] != 'normal':
return False
# # Makes sure google is enabled
# if not content['engines']['google']['enabled']:
# return False
# Only picks instances that are fast enough
timing = int(content['timing']['initial'])
if timing > 0.20:
return False
# # Makes sure is not Tor
# if content['network_type'] != 'normal':
# return False
# Check for Google captcha
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
# # Only picks instances that are fast enough
# timing = int(content['timing']['initial'])
# if timing > 0.20:
# return False
# Reached if passes all checks
return True
# # Check for Google captcha
# 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()
@commands.is_owner()
async def rejson(self, ctx):
"""Refreshes the list of instances for searx."""
# # Reached if passes all checks
# return True
msg = await ctx.send('<a:updating:403035325242540032> Refreshing instance list...\n\n'
'(Due to extensive quality checks, this may take a bit.)')
plausible: List[str] = []
# @commands.command()
# @commands.is_owner()
# async def rejson(self, ctx):
# """Refreshes the list of instances for searx."""
# Get, parse, and quality check all instances
async with self.request.get('https://searx.space/data/instances.json') as r:
# Parsing
searx_json = await r.json()
instances = searx_json['instances']
# msg = await ctx.send('<a:updating:403035325242540032> Refreshing instance list...\n\n'
# '(Due to extensive quality checks, this may take a bit.)')
# plausible: List[str] = []
# Quality Check
for i in instances:
content = instances.get(i)
is_good: bool = await self._instance_check(i, content)
if is_good:
plausible.append(i)
# # Get, parse, and quality check all instances
# async with self.request.get('https://searx.space/data/instances.json') as r:
# # Parsing
# searx_json = await r.json()
# instances = searx_json['instances']
# Save new list
self.instances = plausible
with open('searxes.txt', 'w') as f:
f.write('\n'.join(plausible))
# # Quality Check
# for i in instances:
# content = instances.get(i)
# 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,
category: str = None):
"""Base search message generation."""
# await msg.edit(content='Instances refreshed!')
async with ctx.typing():
is_nsfw = (
ctx.channel.is_nsfw() if hasattr(ctx.channel, 'is_nsfw')
else False
)
# async def _old_basic_search(self, ctx, query: str,
# category: str = None):
# """Base search message generation."""
msg = await self._old_search_logic(query, is_nsfw, category)
await ctx.send(msg)
# async with ctx.typing():
# is_nsfw = (
# ctx.channel.is_nsfw() if hasattr(ctx.channel, 'is_nsfw')
# else False
# )
self.info(
content=(
f"**{ctx.author}** searched for `{query}` "
f"in \"{ctx.guild}\" and got this:"
f"\n\n{msg}"
),
name="Search Results"
)
# msg = await self._old_search_logic(query, is_nsfw, category)
# await ctx.send(msg)
# self.info(
# content=(
# f"**{ctx.author}** searched for `{query}` "
# f"in \"{ctx.guild}\" and got this:"
# f"\n\n{msg}"
# ),
# name="Search Results"
# )