53 lines
1.6 KiB
Python
Executable File
53 lines
1.6 KiB
Python
Executable File
import requests
|
|
import help_methods
|
|
|
|
import discord
|
|
|
|
|
|
def get_definition(word):
|
|
"""
|
|
get_definition(word)
|
|
|
|
Tries to return the highest voted definition from urbandictionary.
|
|
If that fails, it reaches out to pearson for an actual defintion
|
|
"""
|
|
try:
|
|
definition = requests.get(
|
|
"https://api.urbandictionary.com/v0/define?term={}".format(word)
|
|
).json()["list"]
|
|
# UD returns all the results in a json blob. Calculate which result has the highest upvotes to downvotes ratio
|
|
# as this is the result that shows up at the top on urbandictionary.com
|
|
ratios = {}
|
|
for result in definition:
|
|
upvotes = result["thumbs_up"]
|
|
downvotes = result["thumbs_down"]
|
|
if int(downvotes) > 0:
|
|
ratio = int(upvotes) / int(downvotes)
|
|
ratios[definition.index(result)] = ratio
|
|
definition = definition[(max(ratios, key=ratios.get))]["definition"]
|
|
site = "Urban Dictionary"
|
|
|
|
except IndexError:
|
|
try:
|
|
# Try another dictionary
|
|
definition = requests.get(
|
|
"http://api.pearson.com/v2/dictionaries/ldoce5/entries?headword={}".format(
|
|
"%20".join(word)
|
|
)
|
|
).json()["results"][0]["senses"][0]["definition"][0]
|
|
|
|
site = "Pearson"
|
|
except IndexError:
|
|
definition = "No definition found"
|
|
|
|
embed = discord.Embed(
|
|
description="%s:\n%s" % (word, definition),
|
|
color=0x428BCA,
|
|
type="rich",
|
|
)
|
|
embed.set_author(name="From %s" % site)
|
|
|
|
return embed
|
|
|
|
return help_methods.get_help_message("define")
|