This repository has been archived on 2023-04-13. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
CloudBot/plugins/recipe.py
2014-04-01 17:57:39 +13:00

28 lines
No EOL
900 B
Python

from util import hook, http, web
METADATA_URL = "http://omnidator.appspot.com/microdata/json/?url={}"
RANDOM_URL = "http://www.cookstr.com/searches/surprise"
clean_key = lambda i: i.split("#")[1]
def get_data(url):
""" Uses the omnidator API to parse the metadata from the provided URL """
omni = http.get_json(METADATA_URL.format(url))
schemas = omni["@"]
for d in schemas:
if d["a"] == "<http://schema.org/Recipe>":
data = {clean_key(key): value for (key, value) in d.iteritems()
if key.startswith("http://schema.org/Recipe")}
return data
@hook.command(autohelp=False)
def recipe(inp):
"""recipe - Gets a random recipe from cookstr.com!"""
page = http.open(RANDOM_URL)
url = page.geturl()
data = get_data(url)
name = data["name"]
return u"Try eating \x02{}!\x02 - {}".format(name, web.try_isgd(url))