-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
90 additions
and
82 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
# frozen_string_literal: true | ||
|
||
namespace :import do | ||
## | ||
task :geofences do |tn| | ||
log :debug, tn, 'start' | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
# frozen_string_literal: true | ||
|
||
task run: [ | ||
'download:fishing_report', | ||
'import:fishing_report', | ||
'states:california:download', | ||
'states:california:import', | ||
'notifications:interests' | ||
] do | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# frozen_string_literal: true | ||
|
||
namespace :states do | ||
## | ||
namespace :california do | ||
## | ||
task :download do |tn| | ||
log :debug, tn, 'started' | ||
|
||
## use mechanize gem to browse the webpage and click the export link we need | ||
agent = Mechanize.new | ||
|
||
url = 'https://nrm.dfg.ca.gov/FishPlants/' | ||
|
||
log :debug, tn, "reading HTML from #{url}" | ||
|
||
page = agent.get(url) | ||
|
||
log :debug, tn, 'loaded page' | ||
|
||
form = page.forms.first | ||
|
||
button = form.buttons.select { |btn| btn.type == 'submit' }.first | ||
|
||
log :debug, tn, 'clicking Export button' | ||
response = agent.submit(form, button) | ||
|
||
form.add_button_to_query(button) | ||
|
||
log :debug, tn, 'saving to file' | ||
|
||
csv_file = File.join(File.dirname(__FILE__), '../../../tmp/ca_fishing.csv') | ||
|
||
File.open(csv_file, 'wb') { |f| f << response.body } | ||
|
||
log :info, tn, 'done' | ||
rescue StandardError => e | ||
log :fatal, tn, e.message | ||
log :fatal, tn, e.backtrace | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# frozen_string_literal: true | ||
|
||
namespace :states do | ||
## | ||
namespace :california do | ||
## | ||
task :import do |tn| | ||
log :debug, tn, 'start' | ||
|
||
csv_file = File.join(File.dirname(__FILE__), '../../../tmp/ca_fishing.csv') | ||
|
||
i = 0 | ||
File.foreach(csv_file) do |line| | ||
i += 1 | ||
next if i == 1 | ||
|
||
# p line: line | ||
csv_data = CSV.parse(line, col_sep: ',', headers: false) | ||
row = csv_data[0] | ||
hash = { date: row[0], county: row[1], name: row[2], specie: row[3], coordinates: [row[4], row[5]], unit_id: row[6] } | ||
hash[:date] = DateTime.strptime(hash[:date], '%m/%d/%Y %H:%M:%S %p') | ||
|
||
location = FR::Location.where(unit_id: hash[:unit_id], date: hash[:date]).first | ||
location = FR::Location.new if location.nil? | ||
|
||
location.state = 'CA' | ||
location.date = hash[:date] | ||
location.county = hash[:county] | ||
location.name = hash[:name] | ||
location.specie = hash[:specie] | ||
location.coordinates = [hash[:coordinates][0], hash[:coordinates][1]] | ||
location.unit_id = hash[:unit_id] | ||
location.save! | ||
rescue StandardError => e | ||
log :warn, tn, e.message | ||
log :warn, tn, e.backtrace | ||
end | ||
|
||
log :info, tn, 'done' | ||
rescue StandardError => e | ||
log :fatal, tn, e.message | ||
log :fatal, tn, e.backtrace | ||
end | ||
end | ||
end |