Skip to content

Instantly share code, notes, and snippets.

@yhara
Created August 21, 2008 09:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yhara/6528 to your computer and use it in GitHub Desktop.
Save yhara/6528 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'meow'
require 'mechanize'
require 'yaml'
require 'optparse'
require 'kconv'
class Kosoan
DURATION = 10 # min
def initialize(config)
raise ArgumentError, "bad config: no 'mail'" unless config["mail"]
raise ArgumentError, "bad config: no 'password'" unless config["password"]
@config = config
@agent = WWW::Mechanize.new
@known_enqs = {}
end
def login
login_page = @agent.get("http://find.2ch.net/moritapo/login.php?ENQ=Y")
login_form = login_page.forms.first
login_form["EMAIL"] = @config["mail"]
login_form["PW"] = @config["password"]
top_page = login_form.submit
# TODO: check whether login was successful
self
end
def start_watch
growl = Meow.new('Meow Test')
loop do
if not (enqs = new_enquetes).empty?
puts "new enquete found!"
info = "新着アンケートができたようです\n" + enqs.map{|href, title|
"* #{title.toutf8}\n"
}.join
puts info
growl.notify('こっそりアンケート', info) do
system "open http://find.2ch.net/enq/answer_index.php?ONLYQ=t"
end
end
sleep DURATION * 60
end
end
def new_enquetes
get_enquetes.select{|href, title|
if not @known_enqs[href]
@known_enqs[href] = title
true
end
}
end
def get_enquetes
puts "checking new enquete..."
enquetes_page = @agent.get("http://find.2ch.net/enq/answer_index.php?ONLYQ=t")
if enquetes_page.body.toutf8 =~ /答えてないアンケートは無いようです。/
[]
else
(enquetes_page.root / "p.toc a").map{|a| [a["href"], a.inner_text]}
end
end
end
if $0==__FILE__
config_path = "~/.kosoan"
OptionParser.new{|opt|
opt.on("-c", "--config=PATH", "set path to config file. default=~/.kosoan"){|path|
config_path = path
}
}.parse(ARGV)
config = YAML.load_file(File.expand_path config_path)
Kosoan.new(config).login.start_watch
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment