Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
build
crpapi.pyc
test.py
50 changes: 34 additions & 16 deletions crpapi.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
"""
Python library for interacting with the CRP API.

The CRP API (http://www.opensecrets.org/action/api_doc.php) provides campaign
The CRP API (http://www.opensecrets.org/action/api_doc.php) provides campaign
finance and other data from the Center for Responsive Politics.

See README.rst for methods and usage
Expand All @@ -12,11 +12,8 @@
__copyright__ = "Copyright (c) 2009 Sunlight Labs"
__license__ = "BSD"

import urllib, urllib2
try:
import json
except ImportError:
import simplejson as json
import urllib
import requests

class CRPApiError(Exception):
""" Exception for CRP API errors """
Expand All @@ -37,13 +34,16 @@ def _apicall(func, params):
if CRP.apikey is None:
raise CRPApiError('Missing CRP apikey')

url = 'http://api.opensecrets.org/?method=%s&output=json&apikey=%s&%s' % \
(func, CRP.apikey, urllib.urlencode(params))

url = 'http://www.opensecrets.org/api/' \
'?method=%s&output=json&apikey=%s&%s' % \
(func, CRP.apikey, urllib.urlencode(params))

print url

try:
response = urllib2.urlopen(url).read()
return json.loads(response)['response']
except urllib2.HTTPError, e:
response = requests.get(url)
return response.json()['response']
except requests.HTTPError, e:
raise CRPApiError(e.read())
except (ValueError, KeyError), e:
raise CRPApiError('Invalid Response')
Expand Down Expand Up @@ -95,15 +95,33 @@ class getOrgs(object):
def get(**kwargs):
results = CRP._apicall('getOrgs', kwargs)['organization']
return results

class orgSummary(object):
@staticmethod
def get(**kwargs):
results = CRP._apicall('orgSummary', kwargs)['organization']
return results

class congCmteIndus(object):
@staticmethod
def get(**kwargs):
results = CRP._apicall('congCmteIndus', kwargs)['committee']['member']
return results
return results

class presCandContrib(object):
@staticmethod
def get(**kwargs):
results = CRP._apicall('prescandContrib', kwargs)
return results

class presCandIndustry(object):
@staticmethod
def get(**kwargs):
results = CRP._apicall('prescandIndustry', kwargs)
return results

class presCandSector(object):
@staticmethod
def get(**kwargs):
results = CRP._apicall('prescandSector', kwargs)
return results
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from crpapi import __version__,__license__,__doc__

license_text = open('LICENSE').read()
long_description = open('README.rst').read()
long_description = open('README.md').read()

setup(name="python-crpapi",
version=__version__,
Expand All @@ -22,6 +22,6 @@
"Programming Language :: Python",
"Topic :: Software Development :: Libraries :: Python Modules",
],
install_requires=["simplejson >= 1.8"]
install_requires=["requests >= 2.9"]
)