-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeyword_count.rb
49 lines (40 loc) · 1.08 KB
/
keyword_count.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
require_relative 'bundle/bundler/setup'
require 'aws-sdk'
require 'config_env'
require 'httparty'
require 'json'
ConfigEnv.path_to_config("#{__dir__}/config/config_env.rb")
sqs = Aws::SQS::Client.new()
q_url = sqs.get_queue_url(queue_name: 'Kiwi_messenger').queue_url
poller = Aws::SQS::QueuePoller.new(q_url)
# set hash default function
count = Hash.new{|h,k| h[k] = {'keyword'=>k,'count'=>0 }}
# get msg and count
begin
poller.poll(wait_time_seconds:nil, idle_timeout:2) do |msg|
word = msg.body.delete('"')
#set default
if count.has_key?(word)
count[word]
end
count[word]['count'] += 1
end
rescue AWS::SQS::Error::ServiceError
end
count = count.sort_by{|k,v| v['count']}.reverse.to_h
countarray = count.values
countarray.each_index do |i|
countarray[i]['rank']=i+1
end
# print countarray
h = {'results' => countarray.to_json}
print h
# post to api server
begin
HTTParty.post(
'https://kiwi-api.herokuapp.com/api/v1/popularity',
:header => {'Content-Type' => 'application/json'},
:body => {'results' => countarray.to_json})
rescue
puts "post error"
end