forked from algolia/algoliasearch-zendesk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrawler.rb
59 lines (52 loc) · 1.3 KB
/
crawler.rb
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
require 'json'
require_relative './config.rb'
require_relative './algolia.rb'
require_relative './zendesk.rb'
require_relative './types.rb'
LOCALES = JSON.parse File.open('./locales.json', 'r').read
class Crawler
attr_accessor :data
def initialize
@data = {}
end
def locales
@locales ||= ZendeskAPI::CLIENT.hc_locales.to_a!.map do |l|
[l.id, LOCALES[l.id]]
end.to_h
end
def get type, obj
id = obj.is_a?(Integer) ? obj : obj.id
@data[type.plural] ||= {}
@data[type.plural][id] ||= type.new(self, obj)
end
alias_method :set, :get
def crawl type
count = ZendeskAPI::CLIENT.send(type.plural).count!
i = 1
ZendeskAPI::CLIENT.send(type.plural).all! do |obj|
set(type, obj)
puts "#{type.plural.capitalize}: #{i}/#{count}"
STDOUT.flush
i += 1
end
end
def crawl_and_index type
return unless CONFIG['types'].include? type.plural.to_s
count = ZendeskAPI::CLIENT.send(type.plural).count!
i = 1
last = []
ZendeskAPI::CLIENT.send(type.plural).all! do |obj|
last << type.new(self, obj)
if i % 100 == 0
type.index(last)
last = []
GC.start
end
puts "#{type.plural.capitalize}: #{i}/#{count}"
STDOUT.flush
i += 1
end
type.index last
type.move_temporary
end
end