-
Notifications
You must be signed in to change notification settings - Fork 0
/
turtle.rb
82 lines (71 loc) · 1.63 KB
/
turtle.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
require 'cinch'
require 'open-uri'
require 'json'
OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE
TICK_TIME = 60
class PadHerder
def self.events_api
JSON.parse(open("https://www.padherder.com/api/events/").read)
end
def self.events(now, previous)
events_api.select do |event|
event_time = Time.iso8601(event["starts_at"])
event_time > previous && event_time < now
end
end
end
class AlarmPlugin
include Cinch::Plugin
timer TICK_TIME, method: :tick
listen_to :channel
def tick
if @recently_sent
@recently_sent = false
return
end
@channels ||= []
notices = []
now = Time.now
events = PadHerder.events(now, now - (TICK_TIME + 10))
events.each do |event|
notices << "#{event["country"] == 2 ? 'US' : 'JP'} group #{event["group_name"]}: #{event["title"]} is starting!"
end
if notices.length > 0
@recently_sent = true
end
@channels.each do |channel|
notices.each do |notice|
channel.send notice
end
end
end
def listen(m)
@channels ||= []
@channels << (m.channel)
end
end
bots =
[Cinch::Bot.new do
configure do |c|
c.nick = "chrono_turtle"
c.server = "irc.synirc.org"
c.channels = ["#pad"]
c.plugins.plugins = [AlarmPlugin]
end
end,
Cinch::Bot.new do
configure do |c|
c.nick = "chrono_turtle"
c.server = "irc.freenode.org"
c.channels = ["#redditpad", "#csuapad"]
c.plugins.plugins = [AlarmPlugin]
end
end]
workers = bots.map do |bot|
Thread.new do
bot.start
end
end
workers.each do |thread|
thread.join
end