diff options
author | Nemael <100dragons@gmail.com> | 2021-02-12 16:43:53 +0100 |
---|---|---|
committer | Nemael <100dragons@gmail.com> | 2021-02-12 16:43:53 +0100 |
commit | 5c32c4a5c979be4784ccdbeb235aed73c920d05e (patch) | |
tree | 05c260c4c9feeff2300fe5efe03c2ea402db73cc /lib/tasks/import.rake | |
parent | 40effe28304cf183f6a1a05e15ac981b1dee4335 (diff) | |
parent | c1c920585581d189e9d98e1e20c39d689469c5b2 (diff) | |
download | incommon-map-5c32c4a5c979be4784ccdbeb235aed73c920d05e.tar.gz |
Merge branch 'import' of code.cepheide.org:incommon-map into importimport
Diffstat (limited to 'lib/tasks/import.rake')
-rw-r--r-- | lib/tasks/import.rake | 96 |
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 |