|
| 1 | +#!/usr/bin/env python |
| 2 | +# encoding: utf-8 |
| 3 | +""" |
| 4 | +Python interface for NYPL's What's on The Menu API |
| 5 | +https://github.com/NYPL/menus-api |
| 6 | +""" |
| 7 | +from __future__ import print_function, unicode_literals |
| 8 | +import requests |
| 9 | + |
| 10 | + |
| 11 | +class WhatsOnTheMenu(object): |
| 12 | + """ Python interface for NYPL's What's on The Menu API """ |
| 13 | + def __init__(self, token): |
| 14 | + self.token = token |
| 15 | + self.base_url = "http://api.menus.nypl.org/" |
| 16 | + self.ratelimit_limit = None |
| 17 | + self.ratelimit_remaining = None |
| 18 | + |
| 19 | + def nypl_menus_api(self, method, params=None): |
| 20 | + """ Call the API and return JSON """ |
| 21 | + if not params: |
| 22 | + params = "" |
| 23 | + url = self.base_url + method + "?token=" + self.token + params |
| 24 | + r = requests.get(url) |
| 25 | + |
| 26 | + self.ratelimit_limit = r.headers['X-Ratelimit-Limit'] |
| 27 | + self.ratelimit_remaining = r.headers['X-Ratelimit-Remaining'] |
| 28 | + |
| 29 | + if (r.status_code) == 200: |
| 30 | + return r.json() |
| 31 | + |
| 32 | + return None |
| 33 | + |
| 34 | + def _paramify(self, params, param_name, param_value): |
| 35 | + """ If param_value, append ¶m_name=param_value to params """ |
| 36 | + if param_value: |
| 37 | + params += "&" + param_name + "=" + str(param_value) |
| 38 | + return params |
| 39 | + |
| 40 | + def rate_limit(self): |
| 41 | + """ |
| 42 | + Return the daily rate limit, and how many your API token has remaining |
| 43 | + """ |
| 44 | + if not self.ratelimit_limit or not self.ratelimit_remaining: |
| 45 | + # Not cached so make a dummy call to fetch them |
| 46 | + self.get_menus(min_year=9999) |
| 47 | + return self.ratelimit_limit, self.ratelimit_remaining |
| 48 | + |
| 49 | + def rate_limit_remaining(self): |
| 50 | + """ |
| 51 | + Return how many calls your API token has remaining today |
| 52 | + """ |
| 53 | + _, remaining = self.rate_limit() |
| 54 | + return remaining |
| 55 | + |
| 56 | + def get_menus(self, min_year=None, max_year=None, sort_by=None, |
| 57 | + status=None): |
| 58 | + """ GET /menus """ |
| 59 | + params = "" |
| 60 | + params = self._paramify(params, "min_year", min_year) |
| 61 | + params = self._paramify(params, "max_year", max_year) |
| 62 | + params = self._paramify(params, "sort_by", sort_by) |
| 63 | + params = self._paramify(params, "status", status) |
| 64 | + |
| 65 | + method = "menus" |
| 66 | + return self.nypl_menus_api(method, params) |
| 67 | + |
| 68 | + def get_menus_id(self, id): |
| 69 | + """ GET /menus/{id} """ |
| 70 | + method = "menus/" + str(id) |
| 71 | + return self.nypl_menus_api(method) |
| 72 | + |
| 73 | + def get_menus_id_pages(self, id): |
| 74 | + """ GET /menus/{id}/pages """ |
| 75 | + method = "menus/" + str(id) + "/pages" |
| 76 | + return self.nypl_menus_api(method) |
| 77 | + |
| 78 | + def get_menus_id_dishes(self, id): |
| 79 | + """ GET /menus/{id}/dishes """ |
| 80 | + method = "menus/" + str(id) + "/dishes" |
| 81 | + return self.nypl_menus_api(method) |
| 82 | + |
| 83 | + def get_dishes_search(self, query): |
| 84 | + """ GET /dishes/search """ |
| 85 | + params = "" |
| 86 | + params = self._paramify(params, "query", query) |
| 87 | + |
| 88 | + method = "dishes/search" |
| 89 | + return self.nypl_menus_api(method, params) |
| 90 | + |
| 91 | + |
| 92 | +if __name__ == "__main__": |
| 93 | + import argparse |
| 94 | + from pprint import pprint |
| 95 | + |
| 96 | + parser = argparse.ArgumentParser( |
| 97 | + description="Python interface for NYPL's What's on The Menu API.", |
| 98 | + formatter_class=argparse.ArgumentDefaultsHelpFormatter) |
| 99 | + parser.add_argument( |
| 100 | + 'token', |
| 101 | + help="Your API token, ask NYPL for one: " |
| 102 | + "https://github.com/NYPL/menus-api#tokens") |
| 103 | + args = parser.parse_args() |
| 104 | + |
| 105 | + # Example use |
| 106 | + |
| 107 | + # Initialise the API with your token |
| 108 | + api = WhatsOnTheMenu(args.token) |
| 109 | + |
| 110 | + # # Get all menus |
| 111 | + # menus = api.get_menus(min_year=1950, max_year=1951) |
| 112 | + # # Pick the first menu |
| 113 | + # print(menus) |
| 114 | + # menu = menus['menus'][0] |
| 115 | + # pprint(menu) |
| 116 | + |
| 117 | + # # Get a certain menu |
| 118 | + # menu = api.get_menus_id(30924) |
| 119 | + # pprint(menu) |
| 120 | + |
| 121 | + # # Get pages from a certain menu |
| 122 | + # pages = api.get_menus_id_pages(30924) |
| 123 | + # pprint(pages) |
| 124 | + |
| 125 | + # # Get pages from a certain menu |
| 126 | + # dishes = api.get_menus_id_dishes(30924) |
| 127 | + # pprint(dishes) |
| 128 | + |
| 129 | + # Search for meatballs |
| 130 | + dishes = api.get_dishes_search("meatballs") |
| 131 | + pprint(dishes) |
| 132 | + |
| 133 | + # Show rate limit |
| 134 | + print("Rate limit remaining: ", api.rate_limit_remaining()) |
| 135 | + |
| 136 | + |
| 137 | +# End of file |
0 commit comments