-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathopencongress_xml.py
81 lines (66 loc) · 2.13 KB
/
opencongress_xml.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
import re
import json
import encode
import dump
from pprint import pprint
import legislators_current as leg
import cache
"""
Extract open congress data and records
"""
import getopt, sys
from lxml import etree
from StringIO import StringIO
def load():
legs= leg.load()
for x in sorted(legs['wp'].keys()):
idsobj= legs['wp'][x]['id']
name = legs['wp'][x]['name']['official_full']
congid = idsobj['govtrack']
wiki= idsobj['opencongwiki']
if (not wiki == "Error" ):
continue
xml = cache.cacheweb('http://api.opencongress.org/people?person_id=%d' % congid)
xml =xml.replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>","")
xmlio = StringIO(xml)
tree = etree.parse(xmlio)
name = unicode(tree.xpath("//unaccented-name/text()")[0])
name = name.replace(" ","_")
url = "http://www.opencongress.org/w/index.php?title=%s&printable=yes" % name
#url = "http://www.opencongress.org/wiki/%s" % name
try :
data = cache.cacheweb( url)
except Exception, e:
print "failed", name ,e
except KeyboardInterrupt:
print "bye"
exit()
# print data
# if 'govtrack' in idsobj:
# congid = idsobj['govtrack']
# xml = cache.cacheweb('http://api.opencongress.org/people?person_id=%d' % congid)
#some_file_like_object = BytesIO("<root>data</root>")
#print etree.tostring(tree)
def usage():
print "--help --verbose"
def main():
try:
opts, args = getopt.getopt(sys.argv[1:], "hv", ["help", "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)
verbose = False
convertInt=False
for o, a in opts:
if (o == "-v", "--verbose"):
verbose = True
elif o in ("-h", "--help"):
usage()
sys.exit()
else:
assert False, "unhandled option"
load()
if __name__ == "__main__":
main()