Table Of Contents

Next topic

Classes and Exceptions

This Page

Documentation for KCRW’s python NPR API library

kcrw.nprapi 1.0b3

Introduction

This is a very simple library for getting story data from the National Public Radio (NPR) API.

In order to use this API, you must register for an API key and comply with the API Terms of Use. In particular, the NPR API may only be used for personal non-commercial use or for non-commercial use by a 501(c)(3) nonprofit corporation.

If you require Python < 2.6 then you will need the simplejson package.

If you require Python < 2.5 then you will need the cElementTree package.

This package consists of two classes for retrieving data from the NPR content API, StoryMapping and StoryAPI.

The StoryMapping class provides a simple high level API for retrieving story data from NPR in the form of a data structure:

>>> from kcrw.nprapi import StoryMapping
>>> api = StoryMapping(MY_API_KEY)
>>> result = api.query(ids=1)  # The id 1 is a list of news stories
>>> type(result)
<type 'list'>

The StoryAPI class returns raw output from the NPR service in any of the available formats listed in:

kcrw.nprapi.OUTPUT_FORMATS

and described in the NPR API input reference. These include (NPRML (XML), HTML, JSON, RSS, ...). The StoryAPI class is used as follows:

>>> from kcrw.nprapi import StoryAPI
>>> api = StoryAPI(MY_API_KEY, output_format='RSS')
>>> result = api.query(ids=(1,'5',234))
>>> type(result)
<type 'string'>

The query() method of both these classes takes a number of optional parameters, which are listed in:

kcrw.nprapi.QUERY_TERMS

and described in the NPR API input reference.

Additionally, this package provides an exception NPRError which is raised whenever the NPR API responds with an error message rather than data.

Indices and tables

Credits

Created by Alec Mitchell for KCRW, an NPR station based in Santa Monica, California.