Skip to content

Commit

Permalink
scrapers: Add DuckDuckGo
Browse files Browse the repository at this point in the history
Change-Id: I10901eedbf9c213b8bf080c3ab38fb63c4255b45
Signed-off-by: KenHV <[email protected]>
  • Loading branch information
FrosT2k5 authored and KenHV committed Apr 29, 2022
1 parent 632c712 commit 4b4bc01
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ CairoSVG
cffi
cowpy
dnspython
duckduckgo_search
emoji
gitpython
google-api-python-client
Expand Down
38 changes: 38 additions & 0 deletions userbot/modules/scrapers.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import asyncurban
from bs4 import BeautifulSoup
from duckduckgo_search import ddg
from emoji import get_emoji_regexp
from googletrans import LANGUAGES, Translator
from gtts import gTTS
Expand Down Expand Up @@ -237,6 +238,43 @@ async def gsearch(event):
)


@register(outgoing=True, pattern=r"^.ddg(?: |$)(.*)")
async def gsearch(q_event):
"""For .ddg command, do a DuckDuckGo search."""
textx = await q_event.get_reply_message()
query = q_event.pattern_match.group(1)

if query:
pass
elif textx:
query = textx.text
else:
await q_event.edit(
"`Pass a query as an argument or reply "
"to a message for DuckDuckGo search!`"
)
return

msg = ""
await q_event.edit("`Searching...`")
try:
rst = ddg(query)
i = 1
while i <= 5:
result = rst[i]
msg += f"{i}: [{result['title']}]({result['href']})\n"
msg += f"{result['body']}\n\n"
i += 1
await q_event.edit(msg)
if BOTLOG:
await q_event.client.send_message(
BOTLOG_CHATID,
" Search query `" + query + "` was executed successfully",
)
except Exception as e:
await q_event.edit(f"An error: {e} occured, report it to support group")


@register(outgoing=True, pattern=r"^\.wiki(?: |$)(.*)")
async def wiki(wiki_q):
"""For .wiki command, fetch content from Wikipedia."""
Expand Down

0 comments on commit 4b4bc01

Please sign in to comment.