Skip to content

Instantly share code, notes, and snippets.

@Likk
Created September 30, 2012 11:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Likk/3806572 to your computer and use it in GitHub Desktop.
Save Likk/3806572 to your computer and use it in GitHub Desktop.
negatibot on wassr
#!/usr/bin/ruby -Ku
require 'uri';
require 'net/http';
require 'rexml/document';
class Negatibot
def initialize
@message = nil
@dt = Time.now;
@dict = Dictionary.new;
Net::HTTP.version_1_2
end
def put_wassr
wmess = 'source=negatibot&status=' + URI.escape(@message);
req = Net::HTTP::Post.new("/statuses/update.json?");
req.basic_auth "USER ID","PASSWORD"
Net::HTTP.start('api.wassr.jp',80) {|http|
#puts @message
response = http.request(req,wmess);
}
end
def date_line
@message = @dict.load_givenline('wday',@dt.wday)
end
def month_line
@message = @dict.load_givenline('month',@dt.mon)
end
def noon
@message = @dict.load_rndline('noon')
end
def the_tone
self.rndmsg
message_wk = @dict.load_rndline('tone')
message_wk.sub!(/__TIME__/,"#{@dt.hour}");
message_wk.sub!(/__MESSAGE__/,"#{@message}");
@message = message_wk
end
def rndmsg
@message = @dict.load_rndline('rnd')
end
def rebomb
msg = nil
now_epoc = @dt.to_i;
req = Net::HTTP::Get.new('/statuses/user_timeline.xml?id=kyubotter');
xml = Net::HTTP.start('api.wassr.jp',80) {|http|
http.request(req).body;
}
doc=nil;
doc=REXML::Document.new xml;
doc.elements.each('statuses/status') do |elm|
epoc = elm.elements['epoch'].text.to_i;
interval = now_epoc - epoc;
status = elm.elements['text'].text;
if /もう一回/ !~ status and-
/(?:の要求により|の代わりに)(.*)?を爆発させました/ =~ status then
msg = $1 + 'もう一回爆発しろ。' if interval < 30 * 60
end
end
@message = msg
end
def others
self.rebomb
if @message == nil
self.rndmsg-
end
end
def run
if @dt.min == 0 and @dt.hour == 12 #nn/nn 12:00
self.noon
elsif @dt.min == 0 and @dt.hour == 0 #nn/nn 00:00
if @dt.day == 1 #nn/01 00:00
self.month_line
else
self.date_line
end
elsif @dt.min == 0 #nn/nn nn:00
self.the_tone
else
self.others #nn/nn nn:30
end
self.put_wassr
end
end
class Dictionary
def initialize
@dirname = '/PATH/TO/dict'
@file_type = {
'rnd' => "#{@dirname}/rnd.txt",
'tone' => "#{@dirname}/tone.txt",
'noon' => "#{@dirname}/noon.txt",
'wday' => "#{@dirname}/wday.txt",
'month' => "#{@dirname}/month.txt",
};
end
def load_rndline (file)
res = nil
File::open(@file_type[file]){|fp|-
data = fp.readlines;
res = data[rand(data.size)].chomp;
}
return res
end
def load_givenline (file,line)
res = nil
File::open(@file_type[file]){|fp|-
res = fp.readlines[line];
}
return res
end
end
begin
ngb = Negatibot.new;
ngb.run
rescue
print "RuntimeError: ", $!, "\n";
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment