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
141
142
143
144
145
146
147
148
149
150
|
# File 'lib/trackchange/probe.rb', line 35
def probe(site)
url = site[:url]
fname = url.sub(%r{https?://}, '').tr_s('/?&', '...')
site_path = File.expand_path(fname, '~/.trackchange')
cmd = config.fetch + "> #{site_path}.new"
substitutions = {
url: url,
queryscript: File.expand_path('../query.coffee', __FILE__),
selector: site[:selector]
}
substitutions.each { |key, value| cmd = cmd.gsub("%#{key}%", value.to_s) }
logger.debug "% #{cmd}"
system cmd
unless File.exist?(site_path) logger.warn "new site #{url}"
FileUtils.mv("#{site_path}.new", site_path)
return
end
diff = "diff -u #{site_path} #{site_path}.new"
logger.debug "% #{diff}"
result = %x[ #{diff} ]
if result.empty? logger.info "same old #{url}"
FileUtils.rm_f("#{site_path}.new")
return
end
if site[:threshold]
diffgrepwc = "#{diff} | grep '^[-+]:' | wc -l"
logger.debug "% #{diffgrepwc}"
degree = %x[ #{diffgrepwc} ]
if degree.to_i < site[:threshold].to_i
logger.warn 'change below threshold, skipping notification'
skip_notification = true
end
end
logger.warn "changed #{url}"
if pat = config.archive_pattern
time = File.ctime(site_path).strftime(pat)
logger.info "archived #{site_path}.#{time}"
FileUtils.mv(site_path, "#{site_path}.#{time}")
end
FileUtils.mv("#{site_path}.new", site_path)
return if skip_notification
if slack_hook = config.slack_hook
message = slack_defaults.merge(config.slack.to_h)
message['text'] = "#{url}\n\n#{result}"
faraday.post slack_hook, payload: JSON.unparse(message)
end
if email = config.email logger.info "sending notification to #{email}"
begin
file = Tempfile.new('changetrack')
file.write "#{url}\n\n#{result}"
file.close
mail = "cat #{file.path} | mail -s 'Change detected on #{url}' #{email}"
logger.debug mail
system mail
ensure
file.unlink
end
end
if file = File.expand_path(config., ENV['HOME']) logger.info "update rss feed '#{file}'"
= RSS::Maker.make('1.0') do |maker|
maker.channel.id = %x[ whoami ].chomp
maker.channel.author = %x[ whoami ].chomp
maker.channel.updated = Time.now.to_s
maker.channel.title = "Trackchange"
maker.channel.link = "http://github.com/brnach14/trackchange"
maker.channel.about = "Feed of detected changes"
maker.channel.description = "Feed of detected changes"
maker.items.new_item do |item|
item.link = "#{url}##{Time.now.to_i}"
item.title = "Change detected on #{url}"
item.date = Time.now.to_s
item.description = "<pre>#{result}</pre>"
end
if File.exist?(file)
= RSS::Parser.parse(File.read(file))
.items.each_with_index do |in_item, index|
break if index > config.feed_size - 1
maker.items.new_item do |out_item|
out_item.link = in_item.link
out_item.title = in_item.title
out_item.date = in_item.date
out_item.description = in_item.description
end
end
end
end
if File.exist?(file) and !File.writable?(file)
raise "'#{file}' is not writable, skipping rss persistency"
else
File.open(file, 'w') { |f| f.puts() }
end
end
end
|