aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorhellekin <hellekin@cepheide.org>2021-02-11 18:00:01 +0100
committerhellekin <hellekin@cepheide.org>2021-02-11 18:00:01 +0100
commitc1c920585581d189e9d98e1e20c39d689469c5b2 (patch)
tree9f90dca9a7a4331052dcd0e3cbe7a255eef66aa0 /lib
parentcc8e259c14b7d5298c6fed6b53c9a41fcbf880de (diff)
downloadincommon-map-c1c920585581d189e9d98e1e20c39d689469c5b2.tar.gz
WTF
Diffstat (limited to 'lib')
-rw-r--r--lib/tasks/import.rake96
1 files changed, 96 insertions, 0 deletions
diff --git a/lib/tasks/import.rake b/lib/tasks/import.rake
new file mode 100644
index 0000000..6fedbe2
--- /dev/null
+++ b/lib/tasks/import.rake
@@ -0,0 +1,96 @@
+namespace :import do
+ desc "Check doc/import for resources to import against existing database"
+ task check: :environment do
+ begin
+ features = JSON.parse(IO.read(File.join(Rails.root, 'doc/import/Locations.geojson')), { symbolize_names: false })['features']
+ rescue Exception => e
+ p e
+ $stderr.puts "Missing Locations.geojson. Did you run the parser.rb first?"
+ exit(1)
+ end
+
+ # Make a first pass to see if we have existing records
+
+ existing_records = {}
+ i = 0 # temp
+ known_records = new_records = 0
+
+
+ features.each do |feature|
+ i+= 1
+ name = feature['name']
+ phone_number = feature['phone_number']
+ email = feature['email']
+ website = feature['website']
+
+ unless name.blank?
+ r0 = Resource.where("feature #>> '{properties,name}' = :name", name: name)
+ $stderr.puts "No existing resource with name '%s'" % name if r0.blank?
+ end
+
+ unless phone_number.blank?
+ r1 = Resource.where("feature #>> '{properties,phone_number}' = :num", num: PhonyRails.normalize_number(phone_number, default_country_code: 'BE'))
+ $stderr.puts "No existing resource with phone '%s'" % phone_number if r1.blank?
+ end
+
+ unless email.blank?
+ r2 = Resource.where("feature #>> '{properties,email}' = :email", email: email)
+ $stderr.puts "No existing resource with email = '%s'" % email if r2.blank?
+ end
+
+ unless website.blank?
+ r3 = Resource.where("feature #>> '{properties,website}' = :url", url: website)
+ $stderr.puts "No existing resource with website = '%s" % website if r3.blank?
+ end
+
+ unless r0.blank? && r1.blank? && r2.blank? && r3.blank?
+ $stderr.print '!'
+ known_records += 1
+=begin
+ $stderr.puts "Some potential resources found..."
+ p r0
+ p r1
+ p r2
+ p r3
+=end
+ else
+ new_records += 1
+ $stderr.print "."
+ end
+
+ # Normalize fields
+
+ # stop at 3
+ #i >= 3 && exit(0)
+ end
+ $stderr.puts "Task complete (known: #{known_records}, new: #{new_records}, total: #{features.count})"
+ end
+
+ desc "Import new resources"
+ task import: :environment do
+ dewey = Agent.find_by(name: 'Dewey')
+ concertes = Agent.find_by(name: 'ConcertES')
+
+ $stderr.puts "Resources: #{Resource.count}"
+
+ begin
+ features = JSON.parse(IO.read(File.join(Rails.root, 'doc/import/Locations.geojson')), { symbolize_names: false })
+ rescue Exception => e
+ p e
+ $stderr.puts "Missing Locations.geojson. Did you run the parser.rb first?"
+ exit(1)
+ end
+
+ $stderr.puts "Features: #{features['features'].count}"
+
+ features['features'].each do |feat|
+ ag = feat['properties']['source'] == 'ConcertES' ? concertes : dewey
+ $stderr.puts "Creating Resource ( #{feat['properties']['name']} ) for #{ag.name}"
+ r = Resource.new(feature: feat, agent_id: ag.id)
+ p r
+ p r.errors unless r.valid?
+ exit unless r.save
+ $stderr.puts "Created Resource #{r.uuid}"
+ end
+ end
+end