-
Notifications
You must be signed in to change notification settings - Fork 66
/
gao_reports.rb
267 lines (207 loc) · 8.81 KB
/
gao_reports.rb
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
require 'nokogiri'
require 'docsplit'
class GaoReports
# Fetches and indexes GAO reports. Can fetch entire years; by default, asks for the last 7 days.
# options:
# gao_id: specify a single report by its GAO ID.
# year: fetch a whole year's reports.
# limit: a number, limit the number of reports found.
# cache: fetch available docs from the disk.
def self.run(options = {})
if options[:gao_id]
gao_ids = [options[:gao_id]]
else
if options[:year]
beginning = Time.parse("#{options[:year]}-01-01").midnight
ending = Time.parse("#{options[:year]}-12-31").midnight
elsif options[:month]
beginning = Time.parse("#{options[:month]}-01").midnight
ending = Time.parse("#{options[:month]}-31").midnight
else
ending = Time.now.midnight
beginning = ending - 30.days
end
gao_ids = gao_ids_for beginning, ending, options
if options[:limit]
gao_ids = gao_ids.first options[:limit].to_i
end
end
if gao_ids.nil?
Report.warning self, "GAO seems down, couldn't list recent reports."
return
end
options[:batch_size] ||= 1000
count = 0
failures = []
warnings = []
batcher = [] # used to persist a batch indexing container
puts "Going to fetch #{gao_ids.size} GAO reports..." if options[:debug]
gao_ids.each do |gao_id|
url = "http://www.gao.gov/api/id/#{gao_id}"
cache = cache_path_for gao_id, "report.json"
unless details = Utils.download(url, options.merge(destination: cache, json: true))
failures << {gao_id: gao_id, url: url, message: "Couldn't download JSON of report"}
next
end
details = details.first # it's an array of one item for some reason
# details directly from JSON
categories = details['topics'] || []
categories += [details['bucket_term']] if details['bucket_term']
published_on = details['docdate']
posted_at = Time.parse details['actual_release_date']
report_number = details['rptno']
title = details['title']
gao_type = details['document_type']
description = strip_tags(details['description']) if details['description'].present?
# urls
landing_url = details['url']
text_url = details['text_url']
pdf_url = details['pdf_url']
# seen a mixup - http://gao.gov/api/id/586393
if pdf_url and (pdf_url =~ /\.txt$/)
text_url = pdf_url
pdf_url = nil
# seen it - http://gao.gov/api/id/586175
elsif pdf_url and (pdf_url !~ /\.pdf$/)
pdf_url = nil
end
unless landing_url or pdf_url
puts "[#{gao_id}] No landing URL or PDF, skipping..." if options[:debug]
next
end
# figure out whether we can get the full text
full_text = nil
# always download the PDF, just to have it
if pdf_url
cache = cache_path_for gao_id, "report.pdf"
unless Utils.download(pdf_url, options.merge(destination: cache))
warnings << {gao_id: gao_id, url: pdf_url, message: "Couldn't download PDF of report"}
end
end
# if GAO provides an "Accessible Text" version, use that
if text_url and (text_url =~ /\.txt$/)
cache = cache_path_for gao_id, "report.download.txt"
unless full_text = Utils.download(text_url, options.merge(destination: cache))
warnings << {gao_id: gao_id, url: text_url, message: "Couldn't download text version of report"}
end
# if the text is downloaded, needs to be treated as ISO-8859-1 and then converted to UTF-8
full_text.force_encoding("ISO-8859-1")
full_text = full_text.encode "UTF-8", :invalid => :replace, :undef => :replace
# "Accessible PDF" that we seem to be able to scrape the text out of - cdunwell 4/19/2016
elsif text_url and (text_url =~ /\.pdf$/)
txt_path = cache_path_for gao_id, "report.txt.pdf"
unless full_text = Utils.download(text_url, options.merge(destination: txt_path))
warnings << {gao_id: gao_id, url: text_url, message: "Couldn't download accessible pdf version of report"}
end
output = cache_path_for gao_id, "report.txt"
if File.exists?(txt_path)
# depending on Docsplit's behavior of just changing the extension
Docsplit.extract_text(txt_path, ocr: false, output: File.dirname(output))
full_text = File.read(output) if File.exists?(output)
end
# "Accessible PDF" that we seem to be able to scrape the text out of - cdunwell 4/19/2016
elsif text_url and (text_url =~ /\.pdf$/)
txt_path = cache_path_for gao_id, "report.txt.pdf"
unless full_text = Utils.download(text_url, options.merge(destination: txt_path))
warnings << {gao_id: gao_id, url: text_url, message: "Couldn't download accessible pdf version of report"}
end
output = cache_path_for gao_id, "report.txt"
if File.exists?(txt_path)
# depending on Docsplit's behavior of just changing the extension
Docsplit.extract_text(txt_path, ocr: false, output: File.dirname(output))
full_text = File.read(output) if File.exists?(output)
end
# otherwise, create a file in the same place using a rip of the PDF
elsif pdf_url
pdf_path = cache_path_for gao_id, "report.pdf"
output = cache_path_for gao_id, "report.txt"
if File.exists?(pdf_path)
# depending on Docsplit's behavior of just changing the extension
Docsplit.extract_text(pdf_path, ocr: false, output: File.dirname(output))
full_text = File.read(output) if File.exists?(output)
end
end
document_id = "GAO-#{gao_id}"
attributes = {
document_id: document_id,
document_type: 'gao_report',
document_type_name: "GAO Report",
posted_at: posted_at,
published_on: published_on,
title: title,
categories: categories,
url: (landing_url || pdf_url),
source_url: pdf_url,
gao_report: {
gao_id: gao_id,
report_number: report_number,
supplement_url: details['supplement_url'],
youtube_id: details['youtube_id'],
links: (details['additional_links'] if details['additional_links'].present?),
description: description
}
}
# save to Mongo
puts "[#{gao_id}] Saving report information..." if options[:debug]
document = Document.find_or_initialize_by document_id: document_id
document.attributes = attributes
# save to ElasticSearch if we got the text
if full_text
full_text = process_full_text full_text
unless citation_ids = Utils.citations_for(document, full_text, citation_cache(gao_id), options)
warnings << {message: "Failed to extract citations from #{document_id}"}
citation_ids = []
end
document['citation_ids'] = citation_ids # for Mongo
attributes['citation_ids'] = citation_ids # for ES
puts "[#{gao_id}] Indexing full text..." if options[:debug]
attributes['text'] = full_text
Utils.es_batch! 'documents', document_id, attributes, batcher, options
end
document.save!
puts "[#{gao_id}] Successfully saved report"
count += 1
end
Utils.es_flush! 'documents', batcher
if failures.any?
Report.failure self, "Failed to process #{failures.size} reports", {failures: failures}
end
if warnings.any?
Report.warning self, "Failed to process text for #{warnings.size} reports", {warnings: warnings}
end
Report.success self, "Created or updated #{count} GAO Reports"
end
# fetch search results for date range, return GAO IDs for every report in the list
def self.gao_ids_for(beginning, ending, options)
url = "http://gao.gov/browse/date/custom"
url << "?adv_begin_date=#{beginning.strftime("%m/%d/%Y")}"
url << "&adv_end_date=#{ending.strftime("%m/%d/%Y")}"
url << "&rows=15000" # 2011 had 835, 2010 had 794
cache = "data/gao/#{beginning.strftime("%Y%m%d")}-#{ending.strftime("%Y%m%d")}.html"
body = Utils.download(url, options.merge(destination: cache))
return nil unless body
Nokogiri::HTML(body).css("div.listing").map do |div|
div['data-content_id']
end
end
def self.cache_path_for(gao_id, filename)
"data/gao/#{gao_id}/#{filename}"
end
def self.citation_cache(gao_id)
"data/citations/gao_reports/#{gao_id}/citations.json"
end
# I should use sanitize or loofah for this
def self.strip_tags(text)
text = text.gsub(/<\/?(p|div)>/i, "\n\n").strip
text.gsub! /<[^>]+?>/, '' # don't yell at me
text.gsub! /\n{3,}\s*/, "\n\n"
text.strip
end
# collapse whitespace
def self.process_full_text(full_text)
full_text.gsub! '``', '"'
full_text.gsub! "''", '"'
full_text.gsub! /[\s\n]+/, " "
full_text
end
end