From 6d5234260e0bb55a0459e08be00b67fbcec49d98 Mon Sep 17 00:00:00 2001 From: asonnenschein Date: Tue, 18 Aug 2015 11:22:25 -0700 Subject: [PATCH 1/4] update new domain and replace urllib w/ requests --- .gitignore | 3 +++ crpapi.py | 30 ++++++++++++++---------------- setup.py | 2 +- 3 files changed, 18 insertions(+), 17 deletions(-) create mode 100644 .gitignore 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..8b49f8f 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,14 @@ 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)) + 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 +93,15 @@ 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 diff --git a/setup.py b/setup.py index ab223f2..918c28f 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__, From e82e0a4cc720d3a2257e9c438d47f1b9f49b213e Mon Sep 17 00:00:00 2001 From: asonnenschein Date: Tue, 17 Nov 2015 17:02:37 -0700 Subject: [PATCH 2/4] add prescandContrib call --- crpapi.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/crpapi.py b/crpapi.py index 8b49f8f..cce0930 100644 --- a/crpapi.py +++ b/crpapi.py @@ -38,6 +38,8 @@ def _apicall(func, params): '?method=%s&output=json&apikey=%s&%s' % \ (func, CRP.apikey, urllib.urlencode(params)) + print url + try: response = requests.get(url) return response.json()['response'] @@ -105,3 +107,9 @@ class congCmteIndus(object): def get(**kwargs): results = CRP._apicall('congCmteIndus', kwargs)['committee']['member'] return results + + class presCandContrib(object): + @staticmethod + def get(**kwargs): + results = CRP._apicall('presCandContrib', kwargs) + return results \ No newline at end of file From bf53c4f11c2b734f14c07c0c27fd8b3e476d5aa8 Mon Sep 17 00:00:00 2001 From: Adrian Sonnenschein Date: Wed, 3 Feb 2016 16:55:45 -0700 Subject: [PATCH 3/4] updated w/ new routes --- crpapi.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/crpapi.py b/crpapi.py index cce0930..f974ece 100644 --- a/crpapi.py +++ b/crpapi.py @@ -111,5 +111,17 @@ def get(**kwargs): class presCandContrib(object): @staticmethod def get(**kwargs): - results = CRP._apicall('presCandContrib', kwargs) - return results \ No newline at end of file + 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 From d088fe95a027067f95382533af24226f2b3b39c9 Mon Sep 17 00:00:00 2001 From: Mike Shultz Date: Mon, 18 Apr 2016 11:54:12 -0600 Subject: [PATCH 4/4] Changed dependency from simplejson to requests. --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 918c28f..6fe0bbe 100644 --- a/setup.py +++ b/setup.py @@ -22,6 +22,6 @@ "Programming Language :: Python", "Topic :: Software Development :: Libraries :: Python Modules", ], - install_requires=["simplejson >= 1.8"] + install_requires=["requests >= 2.9"] )