跳转到主要内容

与Project Vote Smart API交互的库

项目描述

Python库,用于与Project Vote Smart API交互。

Project Vote Smart API提供有关政治家的详细信息,包括传记、投票和NPAT回应。(http://votesmart.org/services_api.php

python-votesmart是Sunlight Labs的项目(c)2008。由James Turk编写 <jturk@sunlightfoundation.com>。

所有代码都采用BSD风格的许可证,请参阅LICENSE获取详细信息。

主页: http://pypi.python.org/pypi/python-votesmart/

源代码: http://github.com/sunlightlabs/python-votesmart/

需求

python >= 2.4

simplejson >= 1.8(与python 2.6不兼容,将使用内置json模块)

安装

要安装,请运行

python setup.py install

这将把库安装到python的site-packages目录中。

使用

要初始化API,只需导入它并定义一个API密钥即可。

(如果您没有API密钥,请访问http://votesmart.org/services_api.php进行注册。)

从votesmart导入votesmart

>>> from votesmart import votesmart

并设置您的API密钥

>>> votesmart.apikey = '<YOUR KEY>'

地址方法

官方API文档:http://api.votesmart.org/docs/Address.html

方法 getCampaign(candidateId)getCampaignByElection(electionId)getOffice(candidateId) 都根据提供的选举或候选人ID返回一个包含 Address 对象的列表。

获取南希·佩洛西办公室的示例

>>> addr = votesmart.address.getOffice(26732)[0]
>>> print addr.street, addr.city, addr.state
235 Cannon House Office Building Washington DC

getCampaignWebAddress(candidateId)getOfficeWebAddress(candidateId) 根据提供的选举或候选人ID返回一个包含 WebAddress 对象的列表。

获取南希·佩洛西的网址示例
>>> for x in votesmart.address.getOfficeWebAddress(26732):
...     print x
https://pelosi.house.gov/contact/email-me.shtml
http://www.house.gov/pelosi/
AmericanVoices@mail.house.gov

候选人生物方法

官方API文档位于 http://api.votesmart.org/docs/CandidateBio.html

getBio(candidateId)getAddlBio(candidateId) 获取一个生物对象和一系列附加生物对象。

获取南希·佩洛西的生平示例

>>> bio = votesmart.candidatebio.getBio(26732)
>>> print 'Born', bio.birthDate, 'in', bio.birthPlace
Born 03/26/1940 in Baltimore, MD
>>> for fact in votesmart.candidatebio.getAddlBio(26732):
...     print fact
Father's Occupation: Congressman for Baltimore, Mayor of Baltimore
Number of Grandchildren: 5

候选人方法

官方API文档位于 http://api.votesmart.org/docs/Candidates.html

  • getByOfficeState(officeId, stateId=None, electionYear=None)

  • getByLastname(lastName, electionYear=None)

  • getByLevenstein(lastName, electionYear=None)

  • getByElection(electionId)

  • getByDistrict(districtId, electionYear=None)

  • getByZip(zip5, zip4=None)

所有六个方法返回一个包含一个或多个候选人对象的列表。

获取新泽西州州长选举所有候选人的示例

>>> for candidate in votesmart.candidates.getByOfficeState(3, 'NJ'):
...    print candidate
Christopher Christie
Christopher Christie
Jon Corzine
Jon Corzine
Jason Cullen
Christopher Daggett
Kenneth Kaplan
Joshua Leinsdorf
Brian Levine
Alvin Lindsay
David Meiswinkle
Richard Merkt
Gregory Pason
Kostas Petris
Gary Steele
Gary Stein

您会注意到有几个候选人出现了两次,这是由于Vote Smart API中的一个不幸问题,即具有多个政党或选举状态的候选人在数据中重复。请在使用候选人数据时小心去重,使用您认为有用的字段。

(见 http://github.com/sunlightlabs/python-votesmart/issues/closed/#issue/1)

委员会方法

官方API文档位于 http://api.votesmart.org/docs/Committee.html

getTypes() 返回所有委员会类型的列表。

示例

>>> for c in votesmart.committee.getTypes():
...     print c.committeeTypeId, c.name
H House
S Senate
J Joint

getCommitteesByTypeState(typeId=None, stateId=None) 返回委员会对象的列表,如果未指定 typeId,则返回该州的所有委员会;如果未指定 stateId,则返回国会委员会。

获取所有联合委员会的示例

>>> for c in votesmart.committee.getCommitteesByTypeState(typeId='J'):
...     print c
Joint Committee on Printing
Joint Committee on Taxation
Joint Committee on the Library
Joint Economic Committee

getCommittee(committeeId)CommitteeDetail 对象中获取委员会的详细信息。

获取众议院筹款委员会详细信息的示例

>>> committee = votesmart.committee.getCommittee(23)
>>> print committee
Ways and Means

getCommitteeMembers(committeeId) 获取代表给定委员会成员的 CommitteeMember 对象列表。

获取宪法、民权与公民自由小组委员会所有成员的示例

>>> for member in votesmart.committee.getCommitteeMembers(4015):
...     print member
Representative Trent Franks
Representative Mike Pence
Representative Steven Chabot
Representative John Conyers
Representative James Randy Forbes
Representative James Jordan
Representative Steve King
Representative Jerrold Nadler
Representative Mike Quigley
Representative Robert Scott

地区方法

官方API文档位于 http://api.votesmart.org/docs/District.html

getByOfficeState(officeId, stateId, districtName=None)getByZip(zip5, zip4=None) 返回匹配指定标准的地区对象列表。

获取北卡罗来纳州所有众议院地区的示例

>>> for district in votesmart.district.getByOfficeState(5, 'NC'):
...     print district
1
2
3
4
5
6
7
8
9
10
11
12
13

选举方法

官方API文档位于

getElection(electionId) 根据选举ID获取单个选举对象。

获取北卡罗来纳州2008年州长选举详细信息的示例

>>> election = votesmart.election.getElection(684)
>>> print election.name
North Carolina Gubernatorial 2008
>>> for stage in election.stages:
...     print stage.name, stage.electionDate
Primary 2008-05-06
General 2008-11-04

getElectionByYearState(year, stateId=None)getElectionByZip(zip5, zip4=None, year=None) 获取符合给定标准的所有选举对象。如果未指定 stateId,则默认为全国性选举。

获取2008年北卡罗来纳州所有选举详细信息的示例

>>> for election in votesmart.election.getElectionByYearState(2008, 'NC'):
...     print election
North Carolina Congressional 2008
North Carolina Gubernatorial 2008
North Carolina State Legislative 2008
North Carolina State Judicial 2008

getStageCandidates(electionId, stageId, party=None, districtId=None, stateId=None) 获取匹配给定标准的 StageCandidate 对象列表。

获取北卡罗来纳州2008年州长初选所有候选人的示例

for candidate in votesmart.election.getStageCandidates(684, ‘P’)

领导层方法

官方API文档位于 http://api.votesmart.org/docs/Leadership.html

getPositions(stateId=None, officeId=None) 根据给定的条件获取匹配的领导职位对象列表。

获取所有阿拉斯加领导职位的示例

>>> for pos in votesmart.leadership.getPositions('AK'):
...     print pos.officeName, pos.name
State House Speaker
State Senate President
State Senate Majority Leader
State House Majority Leader
State Senate Minority Leader
State House Minority Leader

本地方法

官方API文档位于 http://api.votesmart.org/docs/Local.html

getCounties(stateId)getCities(stateId) 返回县或城市的Locality对象列表。

获取阿拉斯加所有城市的示例

>>> for city in votesmart.local.getCities('AK'):
...     print city.name, city.localId
Anchorage 1
Fairbanks 2
Juneau 4322

getOfficials(localId) 获取给定地区的所有知名官员。

获取阿拉斯加安克雷奇所有官员的示例

>>> for official in votesmart.local.getOfficials(1)[0:1]:
...     print official
Mayor Mark Begich

度量方法

官方API文档位于 http://api.votesmart.org/docs/Measure.html

getMeasuresByYearState(year, stateId) 获取特定年份和州的度量对象列表。

获取2008年马里兰州所有投票措施的示例

>>> for measure in votesmart.measure.getMeasuresByYearState(2008, 'MD'):
...     print measure.measureId, measure.title
1260 Video Lottery
1261 Early Voting

getMeasure(measureId) 获取特定措施的详细信息对象。

获取马里兰州2008年早期投票措施的更多详细信息的示例

>>> measure = votesmart.measure.getMeasure(1260)
>>> print measure.source       # just print the url -- summary is long
http://www.elections.state.md.us/elections/2008/questions/index.html

npat方法

官方API文档位于 http://api.votesmart.org/docs/Npat.html

NPAT不转换为对象,getNpat方法特例,它返回一个代表相关NPAT的Python字典。

检查约翰·麦凯恩的NPAT的示例

>>> print votesmart.npat.getNpat(53270)['surveyMessage']
refused  to tell citizens where he/she stands on any of the issues addressed in the 2010 Political Courage Test, despite repeated requests from Vote Smart, national media, and prominent political leaders.

——e——– 职位方法 ——–

官方API文档位于 http://api.votesmart.org/docs/Office.html

getTypes() 获取表示PVS API跟踪的所有职位类型的OfficeType对象列表。

示例调用

>>> for type in votesmart.office.getTypes():
...     print type
P: Presidential and Cabinet
C: Congressional
J: Supreme Court
G: Governor and Cabinet
K: State Judicial
L: State Legislature
S: State Wide
H: Local Judicial
N: Local Legislative
M: Local Executive

getBranches() 获取表示PVS API跟踪的所有分支的OfficeBranch对象列表。

示例调用

>>> for branch in votesmart.office.getBranches():
...     print branch
E: Executive
L: Legislative
J: Judicial

getLevels() 获取表示PVS API跟踪的所有职位级别的OfficeLevel对象列表。

示例调用

>>> for level in votesmart.office.getLevels():
...     print level
F: Federal
S: State
L: Local

getOfficesByType(typeId)getOfficesByLevel(levelId)getOfficesByTypeLevel(typeId, levelId)getOfficesByBranchLevel(branchId, levelId) 根据提供的参数返回Office对象列表。

获取本地级别所有行政职务的示例

>>> for office in votesmart.office.getOfficesByBranchLevel('E', 'L'):
...     print office
Freeholder
Mayor
Public Advocate
Council
Comptroller
Village Manager

官员方法

官方API文档位于 http://api.votesmart.org/docs/Officials.html

  • getStatewide(stateId=None)

  • getByOfficeState(officeId, stateId=None)

  • getByLastname(lastName)

  • getByLevenstein(lastName)

  • getByElection(electionId)

  • getByDistrict(districtId)

  • getByZip(zip5, zip4=None)

所有官员方法返回包含一个或多个Candidate对象的列表。

获取加利福尼亚州所有参议员的示例

>>> for official in votesmart.officials.getByOfficeState(6, 'CA'):
...    print official
Senator Barbara Boxer
Senator Dianne Feinstein

评级方法

官方API文档位于 http://api.votesmart.org/docs/Rating.html

getCategories(stateId=None) 获取给定州的Category对象列表(如果没有提供州,则为全国)。

获取纽约州一些问题类别的示例

>>> for category in votesmart.rating.getCategories('NY')[0:5]:
...     print category
2: Abortion Issues
5: Animal Rights and Wildlife Issues
11: Business and Consumers
13: Civil Liberties and Civil Rights
17: Conservative

getSigList(categoryId, stateId=None) 获取表示与特定类别相关联的所有特殊利益集团的Sig对象列表。可选地,可以提供一个州以限制结果为在特定州运营的SIG。

获取一些关注环境问题的团体示例

>>> for sig in votesmart.rating.getSigList(30)[0:5]:
...     print sig
934: American Lands Alliance
1081: American Wilderness Coalition
1702: American Wind Energy Association
1107: California Park & Recreation Society
292: Citizens for Health - Food, Water & Ecological Health Rating

getSig(sigId) 获取特殊利益集团的所有详细信息。

获取Sierra Club所有详细信息的示例

>>> sig = votesmart.rating.getSig(657)
>>> print sig.address, sig.city, sig.state
408 C Street, Northeast Washington DC

getCandidateRating(candidateId, sigId) 获取特定特殊利益集团对候选人的评级对象。

检查Sierra Club如何对南希·佩洛西进行评级

>>> for rating in votesmart.rating.getCandidateRating(26732, 657):
...     print rating

州方法

官方API文档位于 http://api.votesmart.org/docs/State.html

getStateIDs() 返回所有州(及类似实体)的 State 对象。

打印从 getStateIds 返回的几个州示例。

>>> for state in votesmart.state.getStateIDs()[0:5]:
...     print state
NA National
AS American Samoa
FL Florida
MI Michigan
MO Missouri

getState(stateId) 返回一个包含给定州所有已知详情的 StateDetail 对象。

获取弗吉尼亚州几个详细信息的示例。

>>> va = votesmart.state.getState('VA')
>>> print va.population, va.motto
7,882,590 (2009) Sic Semper Tyrannis [Thus Always to Tyrants]

投票方法

官方 API 文档在 http://api.votesmart.org/docs/Votes.html

getCategories(year, stateId=None) 获取给定年份和可选的州(如果没有提供州,则为全国)的 Category 对象列表。

获取 2008 年一些全国性法案类别的示例。

>>> for category in votesmart.votes.getCategories(2008)[0:5]:
...     print category
2: Abortion Issues
4: Agriculture Issues
5: Animal Rights and Wildlife Issues
10: Budget, Spending and Taxes
11: Business and Consumers

getBill(billId) 返回一个提供特定法案详情的 BillDetail 对象。

获取 HR 7321 自动汽车行业融资法案详情的示例。

>>> bill = votesmart.votes.getBill(8528)
>>> print bill.officialTitle
HR 7321:  To authorize financial assistance to eligible automobile manufacturers, and for other purposes.
>>> for sponsor in bill.sponsors:
...     print sponsor
Barney  Frank
>>> for action in bill.actions:
...     print action
2008-12-10 - Introduced
2008-12-10 - Passage

getBillAction(actionId) 返回一个提供特定法案上所采取行动详情的 BillAction 对象。

获取 HR 5576 行动详情的示例。

>>> print votesmart.votes.getBillAction(8272)
HR 5576: Making appropriations for the Departments of Transportation, Treasury, and Housing and Urban Development, the Judiciary, District of Columbia, and independent agencies for the fiscal year ending September 30, 2007, and for other purposes.

getBillActionVotes(actionId)getBillActionVoteByOfficial(actionId, candidateId) 获取给定行动(和官员)的 Vote 对象列表。

获取南希·佩洛西对 HR 7321 通过投票的示例。

>>> print votesmart.votes.getBillActionVoteByOfficial(23069, 26732)
Pelosi, Nancy: Yea

有 8 个方法基于各种参数返回 Bill 对象

  • getByBillNumber(billNumber)

  • getBillsByCategoryYearState(categoryId, year, stateId=None)

  • getBillsByYearState(year, stateId=None)

  • getBillsByOfficialYearOffice(candidateId, year, officeId=None)

  • getBillsByCandidateCategoryOffice(candidateId, categoryId, officeId=None)

  • getBillsBySponsorYear(candidateId, year)

  • getBillsBySponsorCategory(candidateId, categoryId)

  • getBillsByStateRecent(stateId=None, amount=None)

获取 2008 年一些最近追踪的法案的示例。

>>> for bill in votesmart.votes.getBillsByYearState(2008)[-5:]:
...     print bill
HR 3997 Financial Asset Purchase Authority
HR 7321 Automotive Industry Financing
H Res 982 Contempt Charges
HR 5501 Funding to Combat AIDS, Malaria, and Tuberculosis
HR 415 Adding Parts of the Taunton River to the National Wild and Scenic Rivers System

getVetoes(candidateId) 返回特定执行官员的所有否决。

获取乔治·W·布什所有否决的示例。

>>> for veto in votesmart.votes.getVetoes(22369):
...     print veto
HR 6331 Medicare Bill
HR 6124 Second Farm, Nutrition, and Bioenergy Act of 2007 (Farm Bill)
HR 2419 Farm, Nutrition, and Bioenergy Act of 2007 (Farm Bill)
HR 1585
HR 3963 Children's Health Insurance Program Reauthorization Act of 2007 (CHIP)
HR 976 State Children's Health Insurance Program (CHIP) Reauthorization
S 5 Stem Cell Research Act of 2007
HR 1591 Emergency Supplemental Appropriations Bill of 2007 with Iraq Withdrawal Timeline

项目详情


下载文件

下载适合您平台的文件。如果您不确定选择哪个,请了解更多关于 安装包 的信息。

源分布

python-votesmart-0.3.3.tar.gz (12.4 kB 查看哈希值)

上传

支持者

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误记录 StatusPage StatusPage 状态页面