-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathphrase_search.py
81 lines (65 loc) · 2.7 KB
/
phrase_search.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# Modified from example:
# http://sunlightfoundation.com/blog/2012/02/13/introducing-python-sunlight/
# Copyright (c) 2012, BSD-3 clause, Sunlight Labs
from sunlight import capitolwords
from sunlight import congress
import sunlight
import urllib
from login import keys
sunlight.config.API_KEY = keys['sunlight']
phrase = 'jobs' # input_phrase
start_date = '2012-10-05' # input_date - 183
end_date = '2013-11-16' # input_date + 183
legislator_id = 'K000352' # input_bioguide_id
# return the count of how many times a certain legislator used
# this phrase in the congressional record during a period of time.
CAPITAL_WORDS_SEARCH_URL = 'http://capitolwords.org/api/1'
date_method = '/dates.json?'
phrase_method = '/phrases.json?'
params_dict = {}
params_dict['apikey'] = str(keys['sunlight'])
params_dict['phrase'] = "hello world"
params_dict['start_date'] = start_date
params_dict['end_date'] = end_date
def Build_URL(base_url, method, params):
URL = base_url + method + str(urllib.parse.urlencode(params))
return URL
# print(Build_URL(CAPITAL_WORDS_SEARCH_URL,
# date_method,
# params_dict
# ))
def count_phrase_for_legislator(phrase,
legislator_id,
start_date,
end_date):
for cw_record in capitolwords.phrases_by_entity(
"legislator", # We're getting all legislators
# bioguide=legislator_id,
phrase=phrase, # this word
start_date=start_date,
end_date=end_date,
sort="count", # sorted by how much they say
# sort="legislator", # sorted by who said it
)[:]:
legislator = congress.legislators(
bioguide_id=cw_record['legislator'],
# Look up this biogude (unique ID) for every fed. legislator
all_legislators="true" # search retired legislators
)
if len(legislator) >= 1: # If we were able to find the legislator
legislator = legislator[0] # (this is a search, so it's a list)
if cw_record['legislator'] == legislator_id:
# print("{0}. {1} said {2} {3} times".format(
# legislator['title'],
# legislator['last_name'],
# phrase,
# int(cw_record['count']))
# )
return (legislator['title'] + ' ' +
legislator['last_name'],
int(cw_record['count']))
# return (legislator['title'] +
# ' ' +legislator['last_name'],
# int(cw_record['count']))
return ('', 0)
print(count_phrase_for_legislator(phrase, legislator_id, start_date, end_date))