Skip to content

Instantly share code, notes, and snippets.

@athom
Forked from JulesWang/acm_bot.rb
Created July 7, 2011 07:05
Show Gist options
  • Save athom/1069021 to your computer and use it in GitHub Desktop.
Save athom/1069021 to your computer and use it in GitHub Desktop.
A acm bot do some boring work
# Ruby 1.9.2
# Home: https://gist.github.com/856219
# My CPP template: template_cpp.rb
# OJ Support:
# Online Judge of Zhejiang University
# Author: w.jq0722@gmail.com
require 'uri'
require 'net/http'
#require 'hpricot'
# Setting
$lang = "cpp"
$proxy_addr = nil #or 'your.proxy.host'
$proxy_port = 8080
$usrname = 'usrname'
$password = 'password'
$proxy = Net::HTTP::Proxy($proxy_addr, $proxy_port)
def parse_by_prefix(content, prefix)
match_str = content.partition(/#{prefix}[^\/]*<\//i)[1]
pos = match_str.index('>')
pos = pos + 1 if pos != nil
pos = prefix.length if pos == nil
#puts match_str if match_str.length < 20
return match_str[pos..-3]
end
def login()
zju_login = "http://acm.zju.edu.cn/onlinejudge/login.do"
url = URI.parse(zju_login)
req = Net::HTTP::Post.new(url.request_uri)
data = {'handle' => $usrname, 'password' => $password, 'rememberMe'=> 'Remember me'}
$proxy.start(url.host, 80) {|http|
req.set_form_data(data);
resp = http.request(req);
#puts 'Code = ' + resp.code
#puts 'Message = ' + resp.message
#resp.each {|key, val| puts key + ' = ' + val}
$cookie = resp.response['set-cookie'].split(/['; '', ']/)[4].split('=')[1]
case resp
when Net::HTTPSuccess, Net::HTTPRedirection
puts "Login Success!"
else
puts "Login Failed!"
exit
end
}
end
def parse()
#zju = "http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=#{$id}";
zju = "http://acm.zju.edu.cn/onlinejudge/showProblem.do";
pku = "http://poj.org/problem?id=#{$id}"
$oj = zju
str = $oj+";jsessionid="+$cookie;
html = $proxy.post_form(URI.parse(str),{"problemCode" => $id}).body
#html = open($oj).read
# title
hash = {zju => "\"bigProblemTitle\"", pku => "#{$id} -- "}
$title = parse_by_prefix(html, hash[$oj])
# author
$author = parse_by_prefix(html, "\"user_name\">")
#$author = "Jim"
# sample input
html = html.rpartition(/input/i)
if html[1] == ""
puts "Maybe this problem has no input"
else
html = html[2]
sample_input = html.partition(/>[^>]*output/i)[0]
sample_input += ">"
if(sample_input == "")
puts "Failed to get Sample Input"
else
lines = sample_input.split(/<[^>]*>/)
lines.each {|l| l.strip!}
sample_input = lines.join("\n")
end
sample_input.strip!
end
input_file = File.new("#{$id}_in.txt",'w')
input_file << sample_input
end
def submit()
map = {"c" => 1, "cpp" => 2, "fpc" => 3, "java" => 4} #...
zju_submit = "http://acm.zju.edu.cn/onlinejudge/submit.do"
zju_run = "http://acm.zju.edu.cn/onlinejudge/showRuns.do?contestId=1"
str = zju_submit+";jsessionid="+$cookie;
src = IO.read("#{$id}.#{$lang}")
html = $proxy.post_form(URI.parse(str),{"problemCode" => $id, "languageId" => map[$lang], "source" => src}).body
#<font color='red'>2567837</font>.
sub_id = html.partition(/'red'>[^<]*/)[1][6..-1]
puts sub_id
sleep 1
html = $proxy.get(URI.parse(zju_run))
=begin
<td class="runId">2567837</td>
<td class="runSubmitTime">2011-07-05 12:57:44</td>
<td class="runJudgeStatus">
<span class="judgeReplyAC">
Accepted
</span></td>
=end
puts html.partition(sub_id)[2].partition(/"judgeReply.*">[^<]*/)[1].sub(/".*">/,"").strip!
end
# begin
puts "Problem ID:"
while true do
line = gets
if line.to_i.to_s.length != 4 then
puts "Please Enter Problem ID of 4 digits."
else
$id = line.to_i
break
end
end
login
if !File.exists?("#{$id}.#{$lang}")
puts "Generating from template..."
parse();
load "template_#{$lang}.rb"
src_file = File.new("#{$id}.#{$lang}",'w')
src_file << $template
else
puts "#{$id}.#{$lang} already exists."
puts "Starting Submit..."
submit();
end
$template = <<EOF
/*
* #{($oj.split("/")[2])} Problem Set - #{$id} #{$title}
*
* Author: #{$author}
* Date: #{Time.now.strftime("%Y-%m-%d %A")}
*/
#include <iostream>
#include <cstdlib>
#include <cstdio>
using namespace std;
void solve();
int main(void)
{
#ifndef ONLINE_JUDGE
freopen("#{$id}_in.txt","r",stdin);
freopen("#{$id}_out.txt","w",stdout);
#endif
//Add Your Code Here
for( ; ; )
{
solve();
}
#ifndef ONLINE_JUDGE
fclose(stdin);
fclose(stdout);
#endif
return EXIT_SUCCESS;
}
void solve()
{
//Add Your Code Here
}
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment