-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetcher.rb
74 lines (64 loc) · 1.56 KB
/
fetcher.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
require_relative 'dgisapi'
class MailSearch
@total
@logname
def initialize logname
@key = DGisAPI.get_api_key
@pager = 1
@logname = logname
File.open("logs/#{@logname}.json", 'a+') {|f| f.write('[')}
end
def search(query, reg_id, contact_type)
begin
params = {
page: @pager,
page_size: 10,
region_id: reg_id,
}
result = DGisAPI.search(params, ["items.contact_groups"], query, @key)
if result[:meta][:code] != 200
return
end
if result.has_key?(:result)
@total = result[:result][:total]
else
puts result
raise RuntimeError.new("API request failed")
end
if result[:result].nil? || result[:result][:items].nil? ||result[:result][:items].empty?
puts result
next
end
filter result[:result][:items], contact_type, reg_id
@pager += 1
end while @pager * 10 < @total
end
def filter raw_results, contact_type, reg_id
raw_results.map do |item|
if item[:contact_groups].any?
item[:contact_groups][0][:contacts].each do |contact|
if contact[:type] == contact_type.to_s
write_result(
region_id: reg_id,
name: item[:name],
contact_type => contact[:value]
)
end
end
end
end
end
def multi_search(query, regions, contact_type)
regions.each do |p|
puts "Search in region #{p}"
search query, p.keys[0].to_i, contact_type
@pager = 1
end
end
def write_result result
File.open("logs/#{@logname}.json", "a+") {|f| f.write(JSON.dump(result)+"\n")}
end
def close_log
File.open("logs/#{@logname}.json", "a+") { |f| f.write(']') }
end
end