Skip to content

Commit

Permalink
Update UrbanDictionary module code.
Browse files Browse the repository at this point in the history
- Uses API instead of scraping HTML.
- We use an Async library now.
  • Loading branch information
Avinash Reddy authored Jan 4, 2020
1 parent 982d1ee commit 0b05b68
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions userbot/modules/scrapers.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from selenium.webdriver.chrome.options import Options
from wikipedia import summary
from wikipedia.exceptions import DisambiguationError, PageError
from urbandict import define
import asyncurban
from requests import get
from search_engine_parser import GoogleSearch
from google_images_download import google_images_download
Expand Down Expand Up @@ -250,21 +250,21 @@ async def urban_dict(ud_e):
""" For .ud command, fetch content from Urban Dictionary. """
await ud_e.edit("Processing...")
query = ud_e.pattern_match.group(1)
urban_dict_helper = asyncurban.UrbanDictionary()
try:
define(query)
except HTTPError:
urban_def = await urban_dict_helper.get_word(query)
except asyncurban.WordNotFoundError:
await ud_e.edit(f"Sorry, couldn't find any results for: {query}")
return
mean = define(query)
deflen = sum(len(i) for i in mean[0]["def"])
exalen = sum(len(i) for i in mean[0]["example"])
deflen = sum(len(i) for i in urban_def.definition)
exalen = sum(len(i) for i in urban_def.example)
meanlen = deflen + exalen
if int(meanlen) >= 0:
if int(meanlen) >= 4096:
await ud_e.edit("`Output too large, sending as file.`")
file = open("output.txt", "w+")
file.write("Text: " + query + "\n\nMeaning: " + mean[0]["def"] +
"\n\n" + "Example: \n" + mean[0]["example"])
file.write("Text: " + query + "\n\nMeaning: " + urban_def.definition +
"\n\n" + "Example: \n" + urban_def.example)
file.close()
await ud_e.client.send_file(
ud_e.chat_id,
Expand All @@ -275,12 +275,12 @@ async def urban_dict(ud_e):
await ud_e.delete()
return
await ud_e.edit("Text: **" + query + "**\n\nMeaning: **" +
mean[0]["def"] + "**\n\n" + "Example: \n__" +
mean[0]["example"] + "__")
urban_def.definition + "**\n\n" + "Example: \n__" +
urban_def.example + "__")
if BOTLOG:
await ud_e.client.send_message(
BOTLOG_CHATID,
"ud query `" + query + "` executed successfully.")
"UrbanDictionary query for `" + query + "` executed successfully.")
else:
await ud_e.edit("No result found for **" + query + "**")

Expand Down

0 comments on commit 0b05b68

Please sign in to comment.