diff --git a/plugins/recipe.py b/plugins/recipe.py new file mode 100644 index 0000000..89befef --- /dev/null +++ b/plugins/recipe.py @@ -0,0 +1,28 @@ +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"] == "": + 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)) \ No newline at end of file