Skip to content

Commit

Permalink
Merge pull request #2 from fitnr/master
Browse files Browse the repository at this point in the history
regularize package name
  • Loading branch information
nmohoric committed Feb 19, 2015
2 parents e4d4001 + 616cade commit 20ec1de
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 83 deletions.
Empty file removed nypl-collections/__init__.py
Empty file.
70 changes: 0 additions & 70 deletions nypl-collections/nyplcollections.py

This file was deleted.

1 change: 1 addition & 0 deletions nyplcollections/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .nyplcollections import NYPLsearch
File renamed without changes.
70 changes: 70 additions & 0 deletions nyplcollections/nyplcollections.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#!/usr/bin/env python

import requests
import xmltodict


class NYPLsearch(object):
raw_results = None
results = None
error = None

def __init__(self, token, format='json', page=1, per_page=10):
self.token = token
self.page = page
self.per_page = per_page
self.format = format
self.base = "http://api.repo.nypl.org/api/v1/items"

# Return the captures for a given uuid
# optional value withTitles=yes
def captures(self, uuid, withTitles=False):
return self._get('/'.join([self.base, uuid]),
{'withTitles': 'yes' if withTitles else 'no'})

# Return the item-uuid for a identifier.
def uuid(self, type, val):
return self._get('/'.join([self.base, type, val]))

# Search across all (without field) or in specific field
# (valid fields at http://www.loc.gov/standards/mods/mods-outline.html)
def search(self, q, field=None):
params = {'q': q}
if field:
params['field'] = field

return self._get('/'.join([self.base, 'search']), params)

# Return a mods record for a given uuid
def mods(self, uuid):
return self._get('/'.join([self.base, 'mods', uuid]))

# Generic get which handles call to api and setting of results
# Return: results dict
def _get(self, url, params=None):
self.raw_results = self.results = None

headers = {"Authorization": "Token token=" + self.token}
params = params or dict()
params['page'] = self.page
params['per_page'] = self.per_page

r = requests.get(".".join([url, self.format]),
params=params,
headers=headers)

self.raw_results = r.text
self.results = self._to_dict(r)['nyplAPI']['response']

if self.results['headers']['status'] == 'error':
self.error = {
'code': self.results['headers']['code'],
'message': self.results['headers']['message']
}
else:
self.error = None

return self.results

def _to_dict(self, r):
return r.json() if self.format == 'json' else xmltodict.parse(r.text)
27 changes: 14 additions & 13 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
from distutils.core import setup
from setuptools import setup

files = ["nyplcollections.py"]

setup(name = "NYPLsearch",
version = "1",
description = "new york public library image collections api",
author = "nick mohoric",
author_email = "[email protected]",
packages = ['NYPLsearch'],
package_data = {'package' : files },
install_requires=["xmltodict"],
long_description = """Really long text here."""
)
setup(
name="nyplcollections",
version="1",
description="new york public library image collections api",
author="nick mohoric",
author_email="[email protected]",
install_requires=[
"xmltodict",
"requests"
],
packages=['nyplcollections'],
long_description="""Really long text here."""
)

0 comments on commit 20ec1de

Please sign in to comment.