diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fc98374 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +build +crpapi.pyc +test.py \ No newline at end of file diff --git a/crpapi.py b/crpapi.py index bc6963c..f974ece 100644 --- a/crpapi.py +++ b/crpapi.py @@ -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 @@ -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 """ @@ -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') @@ -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 \ No newline at end of file + 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 diff --git a/setup.py b/setup.py index ab223f2..6fe0bbe 100644 --- a/setup.py +++ b/setup.py @@ -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__, @@ -22,6 +22,6 @@ "Programming Language :: Python", "Topic :: Software Development :: Libraries :: Python Modules", ], - install_requires=["simplejson >= 1.8"] + install_requires=["requests >= 2.9"] )