This repository has been archived by the owner on Feb 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser_diputados_popit
executable file
·241 lines (185 loc) · 6.12 KB
/
parser_diputados_popit
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#!/usr/bin/env ruby
# encoding: utf-8
require 'nokogiri'
require 'open-uri'
require 'json'
require 'optparse'
require 'i18n'
require 'popit'
@options = {}
optparse = OptionParser.new do |option_parser|
option_parser.banner = "Usage: parser_diputados.rb [@options]"
option_parser.on("-o", "--output FILENAME", "Save the output to a file") do |o|
@options[:output] = o
end
option_parser.on("-i n", "--instance=n", "Send every row this instance of the PopIt Api on popit.mysociety.org. Default legisladores-ar [INSTANCE]") do |instance|
@options[:instance_name] = instance
end
option_parser.on("-u n", "--user=n", "Api user [USERNAME]") do |user|
@options[:api_user] = user
end
option_parser.on("-p n", "--password=n", "Api password [PASSWORD]") do |pass|
@options[:api_pass] = pass
end
end
optparse.parse!
#Initialize API
@api = PopIt.new :instance_name => @options[:instance_name], :user => @options[:api_user], :password => @options[:api_pass]
URL_DIPUTADOS = "http://www.diputados.gov.ar/diputados/listadip.html"
def fetch_and_clean_html
cachefile = "html.cache"
if File.exist?(cachefile)
content = File.read(cachefile)
else
content = open(URL_DIPUTADOS)
.read
.gsub("<tbody>", "<tbody><tr>")
.gsub("</tr>", "</tr><tr>");
File.open(cachefile, 'w+') {|f| f.write(content) }
end
return content
end
def parse_email element
username = element
.css("td a")
.first['href']
.split("/")[2]
.strip
"#{username}@diputados.gob.ar"
end
def parse_url element
path = element.css("td a").first["href"]
"http://www.diputados.gov.ar#{path}"
end
def slug text
return I18n.transliterate(text).gsub(" ","-").downcase
end
def update_membership organization, person, role, start_date, end_date, district
membership = {}
membership[:organization_id] = organization["id"]
membership[:person_id] = person["id"]
membership[:role] = role
membership[:start_date] = start_date
membership[:end_date] = end_date
membership[:district] = district
# Check to see if content exists
exists = @api.search.memberships.get(
:q => 'organization_id: "'+membership[:organization_id]+'" &&
person_id: "'+membership[:person_id]+'" &&
start_date: "'+membership[:start_date].to_s+'" &&
end_date: "'+membership[:end_date].to_s+'"
')
#puts exists
if !exists.empty?
puts "Membership already exists for " + person["id"].to_s + " as " + role + " to " + organization["id" ].to_s
else
#Create content
puts "Adding membership for " + person["id"].to_s + " as " + role + " to " + organization["id" ].to_s
response = @api.memberships.post membership
end
rescue
p membership
raise
return response
end
def update_person content
# Check to see if content exists
exists = @api.search.persons.get(:q => 'slug: "'+content[:slug]+'"')
if !exists.empty?
puts "Already exists " + content[:slug]
response = exists.first
#puts "Updating " + content[:slug]
#response = @api.persons(exists.first["id"].to_s).put content
else
#Create content
puts "Creating " + content[:slug]
response = @api.persons.post content
end
rescue
p content
raise
return response
end
def update_organization content
# Check to see if content exists
exists = @api.search.organizations.get(:q => 'slug: "'+content[:slug]+'"')
if !exists.empty?
#Update content with new information
#puts "Updating " + content[:slug]
puts "Already exists " + content[:slug]
response = exists.first
#response = @api.organizations(exists.first["id"].to_s).put content
else
#Create content
puts "Creating " + content[:slug]
response = @api.organizations.post content
end
rescue
p content
raise
return response
end
doc = Nokogiri::HTML fetch_and_clean_html
data = {}
data[:bloques] = []
data[:diputados] = []
doc.css("#tablesorter tbody tr").reverse().each do |el|
next if el.css("td").empty?
diputado = {}
name = el
.css("td")[1]
.text
.split(",")
.map(&:strip)
diputado[:name] = name.last.strip.capitalize + " " + name.first.strip.capitalize
diputado[:slug] = slug diputado[:name];
diputado[:images] = {}
diputado[:images][:url] = el.css("td img")
.first['src']
.gsub("_medium", "")
diputado[:contact_details] = []
contact = {}
contact[:label] = "email"
contact[:type] = "email"
contact[:value] = parse_email el
diputado[:contact_details] << contact
diputado[:memberships] = []
bloque = {}
bloque[:name] = el.css("td")[5].text.strip.capitalize
bloque[:slug] = slug bloque[:name]
camara = {}
camara[:name] = "Honorable Cámara de Diputados de la Nación"
camara[:slug] = "hcdn"
start_date = Date.strptime(Date.parse(el.css("td")[3].text.strip).to_s,'%Y-%m-%d')
end_date = Date.strptime(Date.parse(el.css("td")[4].text.strip).to_s,'%Y-%m-%d')
district = el.css("td")[2].text.strip.capitalize
## Missing info for the new structure
# diputado[:links] = {}
# diputado[:links][:note] = "Link name"
# diputado[:links][:url] = "url"
# diputado[:commissions] = {}
# diputado[:birth_date] = ""
# diputado[:personal_info] = {}
# diputado[:electoral_info] = {}
# diputado[:represent] = {}
# diputado[:represent][:district] = el.css("td")[2].text
data[:diputados] << diputado
data[:bloques] << bloque
##Request to API
if @options[:instance_name]
diputado_response = update_person diputado
bloque_response = update_organization bloque
camara_response = update_organization camara
##Memberships
update_membership camara_response, diputado_response, "Diputado", start_date, end_date, district
update_membership bloque_response, diputado_response, "Integrante", start_date, end_date, district
end
end
data[:bloques].uniq!.sort!
if @options[:output]
File.open(@options[:output], "w") do |file|
file.puts data.to_json
end
else
$stdout.puts data.to_json
end