1. Mx A. Matienzo
  2. digitalnz

Commits

Mx A. Matienzo  committed eb9b919

- Addition of specific exception raising for missing API keys
- Addition of example code and usage notes, courtesy of Philip Lindsay

  • Participants
  • Parent commits b390cd0
  • Branches default
  • Tags 0.2

Comments (0)

Files changed (4)

File Changes

View file
-v0.2 Sun Sep 13 11:26:44 EDT 2009
+v0.2 Mon Sep 14 12:01:36 EDT 2009
+- Addition of specific exception raising for missing API keys
+- Addition of example code and usage notes, courtesy of Philip Lindsay
 - Patch from Philip Lindsay to ensure Python < 2.6 compatibility
 
 v0.1.0 Wed May 13 00:17:11 EDT 2009

File README

View file
 ------------
 
 * simplejson or Python >= 2.6
-    
+
+USAGE
+-----
+
+See digitalnz/request.py and examples/digitalnz_search.py.
+
 LICENSE
 -------
 

File digitalnz/request.py

View file
 import urllib2
 from digitalnz.response import DigitalNZResponse
 
+
 class DigitalNZAPI(object):
-    """DigitalNZAPI: class to perform requests to the APIs"""
+    """DigitalNZAPI: class to perform requests to the APIs
+    
+    Example usage:
+    
+    >>> import digitalnz
+    >>> request = digitalnz.request.DigitalNZAPI(api_key=YOUR_API_KEY)
+    >>> result = request.search(search_text="hacker")
+    >>> print result.data.keys()
+    [u'start', u'num_results_requested', u'api_call', u'results', u'result_count']
+    >>> print result.data['result_count']
+    17
+    >>> print result.data['results'][0]
+    {u'category': u'Manuscripts', u'description': u'Trooper Arthur Hacker.World War I, 1914-1918.Auckland Mounted Rifles.Gallipoli, Turkey', u'title': u'Trooper Arthur Hacker', u'metadata_url': u'http://api.digitalnz.org/records/v1/151818', u'display_url': u'http://muse.aucklandmuseum.com/databases/Cenotaph/6052.detail', u'source_url': u'http://api.digitalnz.org/records/v1/151818/source', u'thumbnail_url': u'', u'content_provider': u'Auckland War Memorial Museum Tamaki Paenga Hira', u'date': u'', u'syndication_date': u'2009-03-25T06:40:19.932Z', u'id': u'151818'}
+    """
+
     def __init__(self, api_key=None, version=1, format='json', parsing=True):
         if api_key is None:
-            raise
+            raise RuntimeError, "Missing Digital NZ API key"
         else:
             self.api_key = api_key
         self.base_url = 'http://api.digitalnz.org'
         rsp = urllib2.urlopen(req_url).read()
         return DigitalNZResponse(self, rsp)
 
-        
+
     def partners(self):
         req_url = '%s/content_partners/v%s.%s?api_key=%s' % (\
             self.base_url,

File examples/digitalnz_search.py

View file
+#!/usr/bin/env python
+
+# Copyright (C) 2009 Philip Lindsay
+#
+# This file is part of the digitalnz Python module.
+#
+# digitalnz is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# digitalnz is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with digitalnz.  If not, see <http://www.gnu.org/licenses/>.
+
+# examples/digitalnz_search.py - sample commandline app to search DigitalNZ
+
+import sys
+
+import digitalnz
+
+if __name__ == "__main__":
+    try:
+        search_term = sys.argv[1]
+    except IndexError:
+        print "Usage: %s <search_term>" % sys.argv[0]
+
+    dnz = digitalnz.request.DigitalNZAPI(api_key="<insert_api_key_here>")
+    results = dnz.search(search_text=search_term)
+
+    data = results.data
+
+    print "Results found:", data['result_count']
+
+    for result in data['results']:
+        print result['title']
+        print result['display_url']
+        print