forked from mudphone/HawaiiDataPipeline
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathclient.rb
306 lines (257 loc) · 8.84 KB
/
client.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#!/usr/bin/env ruby -w
require 'net/https'
require 'fileutils'
require 'json'
require 'set'
require 'yaml'
# require local libraries (from ./lib)
$:.unshift File.dirname(__FILE__)+"/lib"
require 'pipeline_dataset'
require 'catalog_item'
require 'data_io'
module HDPipeline
STATE_API_URL = "data.hawaii.gov"
STATE_CATALOG_ID = "kpyw-kpxf"
CITY_API_URL = "data.honolulu.gov"
CLIENT_ENV = "development"
APP_ROOT = File.expand_path(File.dirname(__FILE__))
WEEK_IN_MINUTES = 60 * 24 * 7
CACHE_MINUTES = WEEK_IN_MINUTES * 4
CACHE_ROOT = APP_ROOT + "/tmp/cache"
CONFIG_ROOT = APP_ROOT + "/config"
DATA_ROOT = APP_ROOT + "/data"
PER_FILE_LIMIT = 1000
class Client
include PipelineDataset
attr_reader :config
class << self
def slurp_config
raw_config = File.read "#{CONFIG_ROOT}/config.yml"
YAML.load(raw_config)[CLIENT_ENV]
end
end
def initialize(opts={})
@user_config = self.class.slurp_config || {}
@config = {}
@config[:gov] = opts[:gov] || :state
@config[:app_token] = opts[:app_token]
@config[:app_token] ||= @user_config[:socrata] ? @user_config[:socrata][:app_token] : nil
@config[:app_token] ||= "K6rLY8NBK0Hgm8QQybFmwIUQw"
puts "API requests will use app_token: #{@config[:app_token]}"
# Create required working directories:
FileUtils.mkdir_p CACHE_ROOT
FileUtils.mkdir_p DATA_ROOT
end
def set_dataset_type city_or_state
@config[:gov] = city_or_state
end
def dataset_type
@config[:gov]
end
def response_for! url
# Create our request
use_ssl = url.start_with? "https://"
uri = URI.parse(url)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = use_ssl
if use_ssl
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
end
request = Net::HTTP::Get.new(uri.request_uri)
request.add_field("X-App-Token", @config[:app_token])
response = http.request request
{ body: response.body,
code: response.code }
end
def cache_name_for url
url.gsub(/http[s]?:\/\//, "")
.gsub(/[;,\/&$?=]/, "_")
end
def response_for url
name = cache_name_for url
response = read_fragment(name)
return response if response
response = response_for! url
return nil if response[:code] != "200"
write_fragment(name, response[:body])
end
def read_fragment name
cache_file = "#{CACHE_ROOT}/#{name}.cache"
now = Time.now
if File.file?(cache_file)
if CACHE_MINUTES > 0
(current_age = (now - File.mtime(cache_file)).to_i / 60)
puts "Fragment for '#{name}' is #{current_age} minutes old."
return false if (current_age > CACHE_MINUTES)
end
return File.read(cache_file)
end
false
end
def write_fragment name, buf
cache_file = "#{CACHE_ROOT}/#{name}.cache"
cache_file += ".json" if name.end_with? ".json"
f = File.new(cache_file, "w+")
f.write(buf)
f.close
puts "Fragment written for '#{name}'"
buf
end
def clear_cache!
dataset_size = @dataset_catalog ? @dataset_catalog.size : 0
@dataset_catalog = {}
puts "Cache of #{dataset_size} catalog#{dataset_size == 1 ? '' : 's'} cleared."
end
def is_state_data?
@config[:gov] == :state
end
def api_url
is_state_data? ? STATE_API_URL : CITY_API_URL
end
# Paging supported, see docs here:
# http://dev.socrata.com/docs/queries
#
# keep_in_mem if you'd like to return all the data as an array.
# This means it will be kept in memory, so it can be returned.
def data_for index_or_id, opts={}
opts = {
offset: 0,
keep_in_mem: true,
from_cache: true,
soda_query: nil,
order_by: nil,
max_recs: nil
}.merge(opts)
offset = opts[:offset]
all_data = opts[:keep_in_mem] ? [] : nil
id = index_or_id
id = PipelineDataset.resource_id_at datasets, index_or_id if index_or_id.is_a? Integer
limit = PER_FILE_LIMIT
limit = opts[:max_recs] if opts[:max_recs] && opts[:max_recs] < limit
while true do
# Create URL and params:
url = "http://#{api_url}/resource/#{id}.json"
params = "?$limit=#{limit}&$offset=#{offset*limit}"
params += opts[:soda_query] unless opts[:soda_query].to_s.empty?
params += "&$order=#{opts[:order_by]}" unless opts[:order_by].to_s.empty?
url += URI.escape(params)
puts "url is: #{url}"
# Get response
r = opts[:from_cache] ? response_for(url) : response_for!(url)
return all_data if r.nil?
# Parse it
d = JSON.parse(r)
all_data += d if opts[:keep_in_mem]
# Stop pulling if no more data left:
break if d.size < limit
# Stop pulling if enough data pulled:
offset += 1
if opts[:max_recs] && offset * limit >= opts[:max_recs]
all_data = all_data.take opts[:max_recs]
break
end
end
all_data
end
alias_method :data_at, :data_for
# Retrieve all the data from an API end-point, but just throw it
# into cache files. It is not accumulated in memory.
def run_data_for index_or_id, opts={}
opts.merge!({ keep_in_mem: false }) # override!
id = index_or_id
id = PipelineDataset.resource_id_at datasets, index_or_id if index_or_id.is_a? Integer
data_for id, opts
end
alias_method :run_data_at, :run_data_for
def list_item_at index_or_id
d = PipelineDataset.catalog_item_at datasets, index_or_id
puts "No dataset for that index or ID." if d.nil?
d
end
alias_method :list_item_for, :list_item_at
def describe index_or_id
item = list_item_at index_or_id
pp item
nil
end
def catalog_at index_or_id
list_item_at index_or_id
end
def catalog_search search_str
PipelineDataset.select_catalog datasets, :any, search_str
end
alias_method :search_catalog, :catalog_search
def catalog_with_name search_str
PipelineDataset.select_catalog datasets, :name, search_str
end
# This section handles outputing data to file.
# Check the tmp/cache directory for all auto-generated output.
def export_csv dataset_or_index_or_id, file_name="export.csv", delimiter=nil
csv_opts = {}
csv_opts[:col_sep] = delimiter unless delimiter.nil?
path = DATA_ROOT + "/" + file_name
if dataset_or_index_or_id.is_a? Array
DataIO::csv_from_dataset dataset_or_index_or_id, path, csv_opts
end
end
def export_json dataset_or_index_or_id, file_name="export.json"
path = DATA_ROOT + "/" + file_name
if dataset_or_index_or_id.is_a? Array
DataIO::json_from_dataset dataset_or_index_or_id, path
end
end
# Getting the dataset catalog:
def datasets
return @dataset_catalog[dataset_type] unless @dataset_catalog.nil? || @dataset_catalog[dataset_type].nil?
if is_state_data?
# If state, use shiney, new catalog of datasets
d = data_for STATE_CATALOG_ID, soda_query: "&$where=type='datasets'"
raise "Unable to retrive any data" if d.nil? or d.empty?
items = d.map do |item|
PipelineDataset.format_catalog_item_hash(
CatalogItem.name(item),
CatalogItem.id(item),
metadata: item
)
end
else
# Use old page-scraping technique for other catalogs:
items = scraped_datasets
end
@dataset_catalog ||= {}
@dataset_catalog[dataset_type] = PipelineDataset.sort_catalog( items )
end
def scraped_datasets
puts "Generating scaped dataset catalog..."
links = Set.new
page = 1
while true do
puts "Looking for datasets on page #: #{page}"
url = "https://#{api_url}/browse/embed?limitTo=datasets&q=&view_type=table&page=#{page}"
puts "url is: #{url}"
response = response_for url
break if response.nil?
new_links = response.scan(/href="(?:http[s]?:\/\/.*?)?(\/[^\/]*?\/[^\/]*?)\/(.{4,4}-.{4,4})"/)
break if new_links.empty?
hashes = new_links.map do |link|
PipelineDataset.format_catalog_item_hash( link[0], link[1] )
end
links.merge Set.new(hashes)
puts "... #{links.size} unique datasets found... (still searching)"
page += 1
end
puts "Dataset catalog generation complete, found #{links.size} datasets."
PipelineDataset.sort_catalog( links )
end
def list_datasets search_str=nil
list = search_str.nil? ? datasets : catalog_search(search_str)
list ||= []
list.each do |d|
PipelineDataset.print_catalog_item d
end
nil
end
alias_method :list, :list_datasets
end
end
HDP=HDPipeline # Create module alias