-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfec_electronic_contrib.py
150 lines (132 loc) · 3.55 KB
/
fec_electronic_contrib.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
u"""
Produce a wikitable based on the fec data
the output files are written next to the input files
"""
import legislators_current as leg
import datetime
import getopt
import sys
def dord(day):
u"""
day ordinal, inflect the day
"""
day = int(day)
if 4 <= day <= 20 or 24 <= day <= 30:
suffix = "th"
else:
suffix = ["st", "nd", "rd"][day % 10 - 1]
return "%d%s" % (day, suffix)
def format_date(date):
u"""
format the date
"""
date = datetime.datetime.strptime(date, "%Y%m%d")
record_day = dord(date.strftime("%d"))
record_month = date.strftime("%B")
record_year = date.strftime("%Y")
return "%s %s %s" % (record_month, record_day, record_year)
def wikify_fields(fecids, fields, table, wiki):
u"""
Wikify the fields, and write them to the table as well
"""
date = fields[19]
amount = float(fields[20])
fromfec = fields[1]
#fec = fields[24]
fec2 = fields[26]
campaign = fields[22]
committee = fields[6]
#name = ' '.join([fields[28], fields[29], fields[27]])
#state = fields[33]
if (fec2 in fecids):
tablestr = (
"|-\n|[[%s]]\n|"
" [[%s]]\n| $%s\n| %s | %s | '''%s'''" % (
fromfec,
fecids[fec2]['id']["wikipedia"],
amount,
format_date(date),
campaign,
committee
)
)
table.write(tablestr.encode("UTF-8"))
wikistr = (
" * [[%s]] %s donated $%s on %s "
"to the %s via '''%s'''." % (
fecids[fec2]['id']["wikipedia"],
fromfec,
amount,
date,
campaign,
committee
)
)
wiki.write(wikistr.encode("UTF-8"))
def process(filename, verbose):
u"""
Process the file
"""
if verbose:
print("reading", filename)
#
fileobj = open(filename, "rb")
wiki = open(filename + '.wiki', "wb")
table = open(filename + '.wikitable', "wb")
legs = leg.load()
# index
fecids = {}
for fec_id in sorted(legs['wp'].keys()):
if 'fec' in legs['wp'][fec_id]['id']:
for field in legs['wp'][fec_id]['id']['fec']:
fecids[field] = legs['wp'][fec_id]
for line in fileobj.read().split("\n"):
if line[0:4] == "SB23":
# print "---"
fields = line.split("")
wikify_fields(fecids, fields, table, wiki)
wiki.close()
table.close()
def usage():
u"""
print usage
"""
print "--help"
print "-f,--file read this file"
print "-v --verbose"
def main():
u"""
main routine
"""
try:
(opts, args) = getopt.getopt(
sys.argv[1:],
"hf:v",
["help",
"file=",
"verbose"]
)
except getopt.GetoptError as err:
# print help information and exit:
print str(err) # will print something like "option -a not recognized"
usage()
sys.exit(2)
filename = None
verbose = False
print (opts, args)
for opt, value in opts:
if opt in ("-v", "--verbose"):
verbose = True
elif opt in ("-h", "--help"):
usage()
sys.exit()
elif opt in ("-f", "--file"):
filename = value
else:
assert False, "unhandled option"
if filename is not None:
process(filename, verbose)
else:
print "Filename is none"
if __name__ == "__main__":
main()