-
Notifications
You must be signed in to change notification settings - Fork 2
/
collecta_notifyio.rb
92 lines (80 loc) · 2.27 KB
/
collecta_notifyio.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
#!/usr/bin/env ruby
require 'rubygems'
require 'collecta'
require 'httpclient'
require 'jcode'
$KCODE = 'UTF8'
begin
require 'system_timer'
MyTimer = SystemTimer
rescue
require 'timeout'
MyTimer = Timeout
end
class String
def strip_tags
gsub(/<.+?>/,'').gsub(/&/,'&').gsub(/"/,'"').gsub(/</,'<').gsub(/>/,'>').
gsub(/&ellip;/,'...').gsub(/'/, "'")
end
def condense
gsub("\n",'').gsub("\r",' ').gsub("\t",' ').gsub(/\s+/,' ')
end
# 140 - "...".length
def truncate(limit = 137)
self.match(%r{^(.{0,#{limit}})})[1]
end
def auto_link
gsub /((https?:\/\/|www\.)([-\w\.]+)+(:\d+)?(\/([\w\/_\.]*(\?\S+)?)?)?)/, %Q{<a href="\\1">\\1</a>}
end
end
#Jabber::debug = true
@debug = true
#@notify = ["windows", "vista"]
@settings = YAML.load(File.read("config.yml"))
@client = Collecta::Client.new(@settings["collecta.apikey"])
@notifyio_url = "http://api.notify.io/v1/notify/#{@settings['notifyio.userhash']}?api_key=#{@settings['notifyio.apikey']}"
@client.anonymous_connect
puts "Connected: #{@client.jid.to_s}" if @debug
while @query = ARGV.pop
# Let's connect to the Pubsub service
if @client.subscribe(@query, nil) and @debug
msg = "Subscribed: '#{@query}'"
puts msg
end
end
@client.add_message_callback do |msg|
begin
next unless payload = Collecta::Payload.new(msg)
next if payload.type == :unknown
title = "[#{payload.category}]: #{payload.title.strip_tags.condense.truncate}..."
puts title
text = "#{payload.body.strip_tags.condense.auto_link}"
query = { 'title' => title,
'text' => text,
'icon' => '/usr/share/pixmaps/collecta.png' }
if payload.links
link = Array(payload.links)[0]
link = link['href'] if link.kind_of?(Hash)
link = link[1] if link.kind_of?(Array) and link[0] == 'href'
query['link'] = link
end
MyTimer.timeout(5) do
res = HTTPClient.post(@notifyio_url, query)
status = res.status.to_i
raise "invalid message: #{title}" if (status < 200 or status >= 300)
end
rescue Exception => e
puts "[E] #{e.to_s}"
next
end
end
# register a handler for SIGINTs
trap(:INT) do
exit
end
at_exit do
@client.unsubscribe
puts "UnSubscribed." if @debug
@client.close
end
Thread.stop