DDG search

This commit is contained in:
osmarks 2021-03-25 18:39:45 +00:00
parent 9936827f4d
commit bf49617568
3 changed files with 49 additions and 4 deletions

44
src/duckduckgo.py Normal file
View File

@ -0,0 +1,44 @@
import aiohttp
import discord
import asyncio
import logging
import discord.ext.commands as commands
import html.parser
class Parser(html.parser.HTMLParser):
def __init__(self):
self.links = []
super().__init__()
def handle_starttag(self, tag, attrs):
attrs = dict(attrs)
if tag == "a" and attrs.get("class") == "result__a" and "https://duckduckgo.com/y.js?ad_provider" not in attrs["href"]:
self.links.append(attrs["href"])
class DuckDuckGo(commands.Cog):
def __init__(self, bot):
self.bot = bot
self.session = aiohttp.ClientSession()
@commands.command()
async def search(self, ctx, *, query):
async with ctx.typing():
async with self.session.post("https://html.duckduckgo.com/html/", data={ "q": query, "d": "" }) as resp:
if resp.history:
await ctx.send(resp.url, reference=ctx.message)
else:
p = Parser()
txt = await resp.text()
p.feed(txt)
p.close()
try:
return await ctx.send(p.links[0], reference=ctx.message)
except IndexError:
return await ctx.send("No results.", reference=ctx.message)
def cog_unload(self):
asyncio.create_task(self.session.close())
def setup(bot):
cog = DuckDuckGo(bot)
bot.add_cog(cog)

View File

@ -25,11 +25,11 @@ config = util.config
logging.basicConfig(level=logging.INFO, format="%(levelname)s %(asctime)s %(message)s", datefmt="%H:%M:%S %d/%m/%Y")
#intents = discord.Intents.default()
#intents.members = True
intents = discord.Intents.default()
intents.members = True
bot = commands.Bot(command_prefix=config["prefix"], description="AutoBotRobot, the most useless bot in the known universe." + util.config.get("description_suffix", ""),
case_insensitive=True, allowed_mentions=discord.AllowedMentions(everyone=False, users=True, roles=True))
case_insensitive=True, allowed_mentions=discord.AllowedMentions(everyone=False, users=True, roles=True), intents=intents)
bot._skip_check = lambda x, y: False
messages = prometheus_client.Counter("abr_messages", "Messages seen/handled by bot")

View File

@ -265,7 +265,8 @@ extensions = (
"commands",
"userdata",
"irc_link",
"discord_link"
"discord_link",
"duckduckgo"
)
# https://github.com/SawdustSoftware/simpleflake/blob/master/simpleflake/simpleflake.py