(home)

Learning About Digital NZ

Code

Project Log

( 12 September 2009 )

As part of the DigitalNZ HackFest Christchurch I took a look at the existing DigitalNZ Python binding.

( Update 2009-09-14: Now fixed in the repository ) Unfortunately on the Python 2.5 which is default on Mac OS X 10.5 it breaks with:

 File "build/bdist.macosx-10.5-i386/egg/digitalnz/request.py", line 39, in search
 AttributeError: 'str' object has no attribute 'format'

It turns out this is because the library uses the format method of string objects which was introduced in Python 2.6.

The following patch fixes it enough that the search method works but the other methods need to be fixed as well:

--- request.py~	2009-05-13 16:12:28.000000000 +1200
+++ request.py	2009-09-12 15:36:53.000000000 +1200
@@ -36,7 +36,7 @@

     def search(self, **kwargs):
         args = kwargs
-        req_url = '{0}/records/v{1}.{2}?api_key={3}&{4}'.format(\
+        req_url = '%s/records/v%s.%s?api_key=%s&%s' % (\
             self.base_url,
             self.version,
             self.format,

This patch should fix all the methods (tested once each :) ):

--- request.py~	2009-05-13 16:12:28.000000000 +1200
+++ request.py	2009-09-12 16:18:06.000000000 +1200
@@ -36,7 +36,7 @@

     def search(self, **kwargs):
         args = kwargs
-        req_url = '{0}/records/v{1}.{2}?api_key={3}&{4}'.format(\
+        req_url = '%s/records/v%s.%s?api_key=%s&%s' % (\
             self.base_url,
             self.version,
             self.format,
@@ -50,7 +50,7 @@
         args = kwargs
         if title is None:
             raise
-        req_url = '{0}/custom_searches/v{1}/{2}.{3}?api_key={4}&{5}'.format(\
+        req_url = '%s/custom_searches/v%s/%s.%s?api_key=%s&%s' % (\
             self.base_url,
             self.version,
             title,
@@ -64,7 +64,7 @@
     def metadata(self, rec_num=None):
         if rec_num is None:
             raise
-        req_url = '{0}/records/v{1}/{2}.{3}?api_key={4}'.format(\
+        req_url = '%s/records/v%s/%s.%s?api_key=%s' % (\
             self.base_url,
             self.version,
             rec_num,
@@ -75,7 +75,7 @@

         
     def partners(self):
-        req_url = '{0}/content_partners/v{1}.{2}?api_key={3}'.format(\
+        req_url = '%s/content_partners/v%s.%s?api_key=%s' % (\
             self.base_url,
             self.version,
             self.format,

Usage

Basic usage of the API:

import digitalnz
dnz = digitalnz.request.DigitalNZAPI(api_key="<insert_your_api_key_here>")
result = dnz.search(search_text="hacker")
print result.data.keys()
print result.data['result_count']
print result.data['results'][0]

which produces output like:

['start', 'num_results_requested', 'api_call', 'results', 'result_count']
17
{'category': 'Manuscripts',
'description': 'Trooper Arthur Hacker.World War I, 1914-1918.Auckland Mounted Rifles.Gallipoli, Turkey',
'title': 'Trooper Arthur Hacker',
'metadata_url': 'http://api.digitalnz.org/records/v1/151818',
'display_url': 'http://muse.aucklandmuseum.com/databases/Cenotaph/6052.detail',
'source_url': 'http://api.digitalnz.org/records/v1/151818/source',
'thumbnail_url': '',
'content_provider': 'Auckland War Memorial Museum Tamaki Paenga Hira',
'date': '',
'syndication_date': '2009-03-25T06:40:19.932Z',
'id': '151818'}

Example code

Does a search from the command line: digital_search.py (Needs an API key.) (Updated 13 September 2009)

code@rancidbacon.com