urbandictionary command will now return the definition with the highest ratio of upvotes to downvotes

This commit is contained in:
Luke Robles 2022-07-28 18:25:50 -06:00
parent 3f22ba9558
commit 673e89d40e

View File

@ -17,7 +17,17 @@ def get_definition(content):
"https://api.urbandictionary.com/v0/define?term={}".format( "https://api.urbandictionary.com/v0/define?term={}".format(
"%20".join(word) "%20".join(word)
) )
).json()["list"][0]["definition"] ).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" site = "Urban Dictionary"
except IndexError: except IndexError: