""" Python library for interacting with the Sunlight Labs API.
The Sunlight Labs API (http://services.sunlightlabs.com/api/) provides basic
legislator data, conversion between ids, and zipcode-district lookups.
"""
__author__ = "James Turk (jturk@sunlightfoundation.com)"
__version__ = "0.6.0"
__copyright__ = "Copyright (c) 2009 Sunlight Labs"
__license__ = "BSD"
import sys
import warnings
if sys.version_info[0] == 3:
from urllib.parse import urlencode
from urllib.request import urlopen
from urllib.error import HTTPError
else:
from urllib import urlencode
from urllib2 import urlopen
from urllib2 import HTTPError
try:
import json
except ImportError:
import simplejson as json
class SunlightApiError(Exception):
""" Exception for Sunlight API errors """
# results #
class SunlightApiObject(object):
def __init__(self, d):
self.__dict__ = d
def __repr__(self):
return '%s(%r)' % (self.__class__.__name__, self.__dict__)
class Legislator(SunlightApiObject):
def __str__(self):
if self.nickname:
fname = self.nickname
else:
fname = self.firstname
return '%s. %s %s (%s-%s)' % (self.title, fname, self.lastname,
self.party, self.state)
class LegislatorSearchResult(SunlightApiObject):
def __init__(self, d):
self.legislator = Legislator(d['legislator'])
self.score = d['score']
def __str__(self):
return '%s %s' % (self.score, self.legislator)
class Committee(SunlightApiObject):
def __init__(self, d):
self.__dict__ = d
self.subcommittees = [Committee(sc['committee']) for sc in getattr(self, 'subcommittees', [])]
self.members = [Legislator(m['legislator']) for m in getattr(self, 'members', [])]
def __str__(self):
return '%s' % (self.name)
class District(SunlightApiObject):
def __str__(self):
return '%s-%s' % (self.state, self.number)
# namespaces #
class sunlight(object):
apikey = None
@staticmethod
def _apicall(func, params):
if sunlight.apikey is None:
raise SunlightApiError('Missing sunlight apikey')
url = 'http://services.sunlightlabs.com/api/%s.json?apikey=%s&%s' % \
(func, sunlight.apikey, urlencode(params))
try:
response = urlopen(url).read().decode()
return json.loads(response)['response']
except HTTPError, e:
raise SunlightApiError(e.read())
except (ValueError, KeyError), e:
raise SunlightApiError('Invalid Response')
class legislators(object):
@staticmethod
def get(**kwargs):
result = sunlight._apicall('legislators.get', kwargs)['legislator']
return Legislator(result)
@staticmethod
def getList(**kwargs):
results = sunlight._apicall('legislators.getList', kwargs)
return [Legislator(l['legislator']) for l in results['legislators']]
@staticmethod
def search(name, threshold=0.9, all_legislators=False):
params = {'name':name, 'threshold': threshold}
if all_legislators:
params['all_legislators'] = 1
results = sunlight._apicall('legislators.search', params)['results']
return [LegislatorSearchResult(r['result']) for r in results]
@staticmethod
def allForZip(zipcode):
results = sunlight._apicall('legislators.allForZip', {'zip':zipcode})
return [Legislator(l['legislator']) for l in results['legislators']]
@staticmethod
def allForLatLong(latitude, longitude):
params = {'latitude':latitude, 'longitude':longitude}
results = sunlight._apicall('legislators.allForLatLong', params)
return [Legislator(l['legislator']) for l in results['legislators']]
class committees(object):
@staticmethod
def get(committee_id):
results = sunlight._apicall('committees.get', {'id':committee_id})
return Committee(results['committee'])
@staticmethod
def getList(chamber):
results = sunlight._apicall('committees.getList', {'chamber':chamber})
return [Committee(c['committee']) for c in results['committees']]
@staticmethod
def allForLegislator(bioguide_id):
results = sunlight._apicall('committees.allForLegislator',
{'bioguide_id': bioguide_id})
return [Committee(c['committee']) for c in results['committees']]
class districts(object):
@staticmethod
def getDistrictsFromZip(zipcode):
results = sunlight._apicall('districts.getDistrictsFromZip', {'zip':zipcode})
return [District(r['district']) for r in results['districts']]
@staticmethod
def getZipsFromDistrict(state, district):
params = {'state':state, 'district':district}
results = sunlight._apicall('districts.getZipsFromDistrict', params)
return results['zips']
@staticmethod
def getDistrictFromLatLong(latitude, longitude):
params = {'latitude':latitude, 'longitude':longitude}
result = sunlight._apicall('districts.getDistrictFromLatLong', params)
return District(result['districts'][0]['district'])
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
python-sunlightapi-0.6.0.tar.gz (6个子文件)
python-sunlightapi-0.6.0
PKG-INFO 15KB
README.rst 12KB
test_sunlightapi.py 116B
sunlightapi.py 5KB
LICENSE 2KB
setup.py 955B
共 6 条
- 1
资源评论
挣扎的蓝藻
- 粉丝: 14w+
- 资源: 15万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功