Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk committed Oct 29, 2015
1 parent d7dfe5a commit 2488720
Show file tree
Hide file tree
Showing 4 changed files with 215 additions and 1 deletion.
13 changes: 13 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Top-most EditorConfig file
root = true

# Unix-style newlines with a newline ending every file
[*]
end_of_line = lf
insert_final_newline = true
charset = utf-8

# Four-space indentation
[*.py]
indent_size = 4
indent_style = space
56 changes: 55 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,56 @@
# whatsonthemenu
Python wrapper around the API for NYPL's What's on The Menu

Python interface to NYPL's What's on The Menu API.

## Installation

### From PyPI

```bash
pip install whatsonthemenu
```

### From source

```bash
git clone https://github.com/hugovk/whatsonthemenu.git
python setup.py install
```

## Example use

First, you need to ask NYPL for a token: https://github.com/NYPL/menus-api#tokens

```python
from whatsonthemenu import WhatsOnTheMenu
from pprint import pprint

# Initialise the API with your token
api = WhatsOnTheMenu(token)

# # Get all menus
menus = api.get_menus(min_year=1950, max_year=1951)
# # Pick the first menu
print(menus)
menu = menus['menus'][0]
pprint(menu)

# Get a certain menu
menu = api.get_menus_id(30924)
pprint(menu)

# Get pages from a certain menu
pages = api.get_menus_id_pages(30924)
pprint(pages)

# Get pages from a certain menu
dishes = api.get_menus_id_dishes(30924)
pprint(dishes)

# Search for meatballs
dishes = api.get_dishes_search("meatballs")
pprint(dishes)

# Show rate limit
print("Rate limit remaining: ", api.rate_limit_remaining())
```
10 changes: 10 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from setuptools import setup

setup(name='whatsonthemenu',
version='0.0.1',
description="Python interface to NYPL's What's on The Menu API",
author='hugovk',
url='https://github.com/hugovk/whatsonthemenu',
packages=['whatsonthemenu'])

# End of file
137 changes: 137 additions & 0 deletions whatsonthemenu/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
#!/usr/bin/env python
# encoding: utf-8
"""
Python interface for NYPL's What's on The Menu API
https://github.com/NYPL/menus-api
"""
from __future__ import print_function, unicode_literals
import requests


class WhatsOnTheMenu(object):
""" Python interface for NYPL's What's on The Menu API """
def __init__(self, token):
self.token = token
self.base_url = "http://api.menus.nypl.org/"
self.ratelimit_limit = None
self.ratelimit_remaining = None

def nypl_menus_api(self, method, params=None):
""" Call the API and return JSON """
if not params:
params = ""
url = self.base_url + method + "?token=" + self.token + params
r = requests.get(url)

self.ratelimit_limit = r.headers['X-Ratelimit-Limit']
self.ratelimit_remaining = r.headers['X-Ratelimit-Remaining']

if (r.status_code) == 200:
return r.json()

return None

def _paramify(self, params, param_name, param_value):
""" If param_value, append &param_name=param_value to params """
if param_value:
params += "&" + param_name + "=" + str(param_value)
return params

def rate_limit(self):
"""
Return the daily rate limit, and how many your API token has remaining
"""
if not self.ratelimit_limit or not self.ratelimit_remaining:
# Not cached so make a dummy call to fetch them
self.get_menus(min_year=9999)
return self.ratelimit_limit, self.ratelimit_remaining

def rate_limit_remaining(self):
"""
Return how many calls your API token has remaining today
"""
_, remaining = self.rate_limit()
return remaining

def get_menus(self, min_year=None, max_year=None, sort_by=None,
status=None):
""" GET /menus """
params = ""
params = self._paramify(params, "min_year", min_year)
params = self._paramify(params, "max_year", max_year)
params = self._paramify(params, "sort_by", sort_by)
params = self._paramify(params, "status", status)

method = "menus"
return self.nypl_menus_api(method, params)

def get_menus_id(self, id):
""" GET /menus/{id} """
method = "menus/" + str(id)
return self.nypl_menus_api(method)

def get_menus_id_pages(self, id):
""" GET /menus/{id}/pages """
method = "menus/" + str(id) + "/pages"
return self.nypl_menus_api(method)

def get_menus_id_dishes(self, id):
""" GET /menus/{id}/dishes """
method = "menus/" + str(id) + "/dishes"
return self.nypl_menus_api(method)

def get_dishes_search(self, query):
""" GET /dishes/search """
params = ""
params = self._paramify(params, "query", query)

method = "dishes/search"
return self.nypl_menus_api(method, params)


if __name__ == "__main__":
import argparse
from pprint import pprint

parser = argparse.ArgumentParser(
description="Python interface for NYPL's What's on The Menu API.",
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument(
'token',
help="Your API token, ask NYPL for one: "
"https://github.com/NYPL/menus-api#tokens")
args = parser.parse_args()

# Example use

# Initialise the API with your token
api = WhatsOnTheMenu(args.token)

# # Get all menus
# menus = api.get_menus(min_year=1950, max_year=1951)
# # Pick the first menu
# print(menus)
# menu = menus['menus'][0]
# pprint(menu)

# # Get a certain menu
# menu = api.get_menus_id(30924)
# pprint(menu)

# # Get pages from a certain menu
# pages = api.get_menus_id_pages(30924)
# pprint(pages)

# # Get pages from a certain menu
# dishes = api.get_menus_id_dishes(30924)
# pprint(dishes)

# Search for meatballs
dishes = api.get_dishes_search("meatballs")
pprint(dishes)

# Show rate limit
print("Rate limit remaining: ", api.rate_limit_remaining())


# End of file

0 comments on commit 2488720

Please sign in to comment.