This repository has been archived by the owner on Oct 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
88 lines (80 loc) · 2.59 KB
/
Rakefile
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
require "place.rb"
require "logger"
desc "download records from the site"
task :download_records do
ENV["overwrite"] == ("true" || "True" || 1) ? overwrite = true : overwrite = false
if ENV["start"] && ENV["end"]
(ENV['start'].to_i..ENV['end'].to_i).each do |record_number|
place = Place.new(record_number, overwrite)
if place.valid_record?
p "Processing: #{record_number}"
f = File.new("raw/#{record_number}.html", "w")
f.puts place.raw
f.close
else
p "Skipping: #{record_number}"
f = File.new("raw/#{record_number}.skip", "w")
f.puts place.raw
f.close
end
end
else
p "Example Usage: rake download_records start=1 end=100"
end
end
desc "List all attributes that could match fields"
task :list_attributes do
fields = []
(ENV['start'].to_i..ENV['end'].to_i).each do |record_number|
p "Processing record #{record_number}"
place = Place.new(record_number, false)
fields += place.possible_fields
end
p fields.uniq.sort
end
namespace :json do
desc "Write out a single record from the cahce to a file"
task :single_from_cache do
if File.exists?("raw/#{ENV['id']}.html")
place = Place.new(ENV['id'], false)
#Write out a single record
data = File.new("place_#{ENV['id']}.json", "w+")
data.write(place.to_json)
data.close
elsif File.exists?("raw/#{ENV['id']}.skip")
p "Record not found"
else
p "record not downloaded"
end
end
desc "Write out all records from the cache to a file using start and end ID's"
task :all_from_cache do
if ENV["start"] && ENV["end"]
all = {}
(ENV['start'].to_i..ENV['end'].to_i).each do |record_number|
p "Processing #{record_number}"
if File.exists?("raw/#{record_number}.html")
place = Place.new(record_number, false)
all[record_number] = place.formatted_fields
elsif File.exists?("raw/#{record_number}.skip")
p "Record not found"
else
p "record not downloaded"
end
end
data = File.new("all.json", "w+")
data.write(all.to_json)
data.close
else
p "Example Usage: rake json:all_from_cache start=1 end=100"
end
end
desc "Tidy the JSON output so it is nicely formatted"
task :tidy do
if ENV["input"] && ENV["output"]
system "python -c \"import sys,json; print json.dumps(json.loads(open(sys.argv[1], 'rb').read()), indent=2)\" #{ENV['input']} > #{ENV['output']}"
else
p "Example Usage: rake json:tidy input=all.json output=all_formatted.json"
end
end
end