Skip to content

Commit 2488720

Browse files
committed
Initial commit
1 parent d7dfe5a commit 2488720

File tree

4 files changed

+215
-1
lines changed

4 files changed

+215
-1
lines changed

.editorconfig

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Top-most EditorConfig file
2+
root = true
3+
4+
# Unix-style newlines with a newline ending every file
5+
[*]
6+
end_of_line = lf
7+
insert_final_newline = true
8+
charset = utf-8
9+
10+
# Four-space indentation
11+
[*.py]
12+
indent_size = 4
13+
indent_style = space

README.md

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,56 @@
11
# whatsonthemenu
2-
Python wrapper around the API for NYPL's What's on The Menu
2+
3+
Python interface to NYPL's What's on The Menu API.
4+
5+
## Installation
6+
7+
### From PyPI
8+
9+
```bash
10+
pip install whatsonthemenu
11+
```
12+
13+
### From source
14+
15+
```bash
16+
git clone https://github.com/hugovk/whatsonthemenu.git
17+
python setup.py install
18+
```
19+
20+
## Example use
21+
22+
First, you need to ask NYPL for a token: https://github.com/NYPL/menus-api#tokens
23+
24+
```python
25+
from whatsonthemenu import WhatsOnTheMenu
26+
from pprint import pprint
27+
28+
# Initialise the API with your token
29+
api = WhatsOnTheMenu(token)
30+
31+
# # Get all menus
32+
menus = api.get_menus(min_year=1950, max_year=1951)
33+
# # Pick the first menu
34+
print(menus)
35+
menu = menus['menus'][0]
36+
pprint(menu)
37+
38+
# Get a certain menu
39+
menu = api.get_menus_id(30924)
40+
pprint(menu)
41+
42+
# Get pages from a certain menu
43+
pages = api.get_menus_id_pages(30924)
44+
pprint(pages)
45+
46+
# Get pages from a certain menu
47+
dishes = api.get_menus_id_dishes(30924)
48+
pprint(dishes)
49+
50+
# Search for meatballs
51+
dishes = api.get_dishes_search("meatballs")
52+
pprint(dishes)
53+
54+
# Show rate limit
55+
print("Rate limit remaining: ", api.rate_limit_remaining())
56+
```

setup.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from setuptools import setup
2+
3+
setup(name='whatsonthemenu',
4+
version='0.0.1',
5+
description="Python interface to NYPL's What's on The Menu API",
6+
author='hugovk',
7+
url='https://github.com/hugovk/whatsonthemenu',
8+
packages=['whatsonthemenu'])
9+
10+
# End of file

whatsonthemenu/__init__.py

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
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 &param_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

Comments
 (0)