Skip to content

Commit

Permalink
state specific
Browse files Browse the repository at this point in the history
  • Loading branch information
alilland committed Jul 1, 2022
1 parent 48f9e97 commit c414457
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 82 deletions.
39 changes: 0 additions & 39 deletions tasks/download/fishing_report.rake

This file was deleted.

41 changes: 0 additions & 41 deletions tasks/import/fishing_report.rake

This file was deleted.

1 change: 1 addition & 0 deletions tasks/import/geofences.rake
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'

Expand Down
4 changes: 2 additions & 2 deletions tasks/run.rake
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
42 changes: 42 additions & 0 deletions tasks/states/california/download.rake
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
45 changes: 45 additions & 0 deletions tasks/states/california/import.rake
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

0 comments on commit c414457

Please sign in to comment.