forked from meduza-corp/interstellar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsender.rb
108 lines (88 loc) · 2.81 KB
/
sender.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
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
require 'rest-client'
require 'json'
require 'date'
require 'csv'
require 'yaml'
CONFIG = YAML.load_file('./secrets/secrets.yml')
date = Date.today-2
file_date = date.strftime("%Y%m")
csv_file_name = "#{CONFIG["package_name"]}_#{file_date}.csv"
system "BOTO_PATH=./secrets/.boto gsutil/gsutil cp -r gs://#{CONFIG["app_repo"]}/reviews/#{csv_file_name} ."
class Slack
def self.notify(message)
RestClient.post CONFIG["slack_url"], {
payload:
{ text: message }.to_json
},
content_type: :json,
accept: :json
end
end
class Review
def self.collection
@collection ||= []
end
def self.send_reviews_from_date(date)
message = collection.select do |r|
r.submitted_at > date && (r.title || r.text)
end.sort_by do |r|
r.submitted_at
end.map do |r|
r.build_message
end.join("\n")
if message != ""
Slack.notify(message)
else
print "No new reviews\n"
end
end
attr_accessor :text, :title, :submitted_at, :original_subitted_at, :rate, :device, :url, :version, :edited
def initialize data = {}
@text = data[:text] ? data[:text].to_s.encode("utf-8") : nil
@title = data[:title] ? "*#{data[:title].to_s.encode("utf-8")}*\n" : nil
@submitted_at = DateTime.parse(data[:submitted_at].encode("utf-8"))
@original_subitted_at = DateTime.parse(data[:original_subitted_at].encode("utf-8"))
@rate = data[:rate].encode("utf-8").to_i
@device = data[:device] ? data[:device].to_s.encode("utf-8") : nil
@url = data[:url].to_s.encode("utf-8")
@version = data[:version].to_s.encode("utf-8")
@edited = data[:edited]
end
def notify_to_slack
if text || title
message = "*Rating: #{rate}* | version: #{version} | subdate: #{submitted_at}\n #{[title, text].join(" ")}\n <#{url}|Ответить в Google play>"
Slack.notify(message)
end
end
def build_message
date = if edited
"subdate: #{original_subitted_at.strftime("%d.%m.%Y at %I:%M%p")}, edited at: #{submitted_at.strftime("%d.%m.%Y at %I:%M%p")}"
else
"subdate: #{submitted_at.strftime("%d.%m.%Y at %I:%M%p")}"
end
stars = rate.times.map{"★"}.join + (5 - rate).times.map{"☆"}.join
[
"\n\n#{stars}",
"Version: #{version} | #{date}",
"#{[title, text].join(" ")}",
"<#{url}|Ответить в Google play>"
].join("\n")
end
end
CSV.foreach(csv_file_name, encoding: 'bom|utf-16le', headers: true) do |row|
# If there is no reply - push this review
if row[11].nil?
Review.collection << Review.new({
text: row[10],
title: row[9],
submitted_at: row[6],
edited: (row[4] != row[6]),
original_subitted_at: row[4],
rate: row[8],
device: row[3],
url: row[14],
version: row[1],
})
end
end
Review.send_reviews_from_date(date)