This repository has been archived by the owner on Jun 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRakefile
140 lines (109 loc) · 3.29 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
require 'dotenv/tasks'
require 'algoliasearch'
require 'csv'
require 'yaml'
require 'nationbuilder'
task :init_config do
@config = YAML.load_file('config.yml')
end
task :init_algolia => :dotenv do
Algolia.init :application_id => ENV['ALGOLIA_APP_ID'],
:api_key => ENV['ALGOLIA_ADMIN_KEY']
@algolia = Algolia::Index.new ENV['ALGOLIA_INDEX']
end
task :init_nationbuilder => :dotenv do
@nationbuilder = NationBuilder::Client.new(ENV['NATIONBUILDER_NATION'], ENV['NATIONBUILDER_API_TOKEN'])
end
# Imports people from NationBuilder to an Algolia index
task :import_people => [:init_config, :init_algolia, :init_nationbuilder] do |t, args|
puts 'Import all people from NationBuilder'
response = @nationbuilder.call(:people, :index, limit: 100)
paginated = NationBuilder::Paginator.new(@nationbuilder, response)
# Batch import, 100 at a time
page = 1
imported = 0
loop do
data = paginated.body['results']
persons = []
data.each do |line|
full_name = [line['first_name'], line['last_name']].reject(&:empty?).join(' ')
puts "Importing person ##{line['id']} (#{full_name})"
person = @nationbuilder.call(:people, :show, id: line['id'])
next if person.nil?
person = person['person']
# Skip banned
unless person['banned_at'].nil?
puts '> skip (banned)'
next
end
person.select! do |key, value|
@config['allowed_keys'].include? key
end
# Emails as array
person['emails'] = []
(1..5).each do |i|
person['emails'] << person["email#{i}"]
person.delete("email#{i}")
end
person['emails'] = person['emails'].compact.uniq
# Geoloc
unless person['primary_address'].nil? or person['primary_address']['lat'].nil? or person['primary_address']['lng'].nil?
person['_geoloc'] = {
lat: person['primary_address']['lat'],
lng: person['primary_address']['lng']
}
end
# Tags
person['_tags'] = person['tags']
person.delete('tags')
# Set id
person['objectID'] = person['id']
person.delete('id')
persons << person
end
@algolia.save_objects(persons)
imported += persons.length
puts "#{imported} people imported..."
if paginated.next?
sleep 10 # seconds
page += 1
paginated = paginated.next
else
puts 'Everybody has been imported!'
break
end
end
end
# Imports a CSV file to an Algolia index
# Usage: `rake import_csv[file.csv]`
task :import_csv, [:file] => [:init_config, :init_algolia] do |t, args|
puts 'Initial import'
puts "File: #{args.file}"
# Read the file
data = CSV.read(args.file)
puts "#{data.length} lines read"
keys = data.slice!(0)
# Batch import, 500 at a time
while data.length > 0
if data.length > 500
batch = data.slice! 0, 500
else
batch = data
end
batch.map! do |line|
hash = Hash[keys.zip(line)]
hash.select! do |key, value|
@config['allowed_keys'].include? key
end
# Tags
person['_tags'] = person['tag_list']
person.delete('tag_list')
# Set id
hash['objectID'] = hash['nationbuilder_id']
hash.delete('nationbuilder_id')
hash
end
res = @algolia.save_objects(batch)
puts res.inspect
end
end