Created
July 31, 2018 22:31
-
-
Save igjit/e5ea26887bd23bdc2e4834793db7f30e to your computer and use it in GitHub Desktop.
プログラミング言語の基礎概念の演習システム http://www.fos.kuis.kyoto-u.ac.jp/~igarashi/CoPL/index.cgi に回答をpostするスクリプト
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'net/http' | |
require 'uri' | |
require 'nokogiri' | |
URL = 'http://www.fos.kuis.kyoto-u.ac.jp/~igarashi/CoPL/index.cgi' | |
DEFAULT_FORM_DATA = { | |
command: 'answer', | |
no: 0 | |
} | |
DEFAULT_GAME = 'Nat' | |
def post_sandbox(user, derivation, game = DEFAULT_GAME) | |
uri = URI.parse(URL) | |
req = Net::HTTP::Post.new(uri.request_uri) | |
req.add_field('Cookie', "loginas=#{user};") | |
form_data = DEFAULT_FORM_DATA.merge(game: game, derivation: derivation) | |
req.set_form_data(form_data) | |
Net::HTTP.new(uri.host).request(req) | |
end | |
def parse_result(html) | |
doc = Nokogiri::HTML.parse(html) | |
{ | |
title: doc.at_css('h1')&.inner_text&.strip, | |
message: doc.at_css('#main > pre')&.inner_text&.strip | |
} | |
end | |
def main | |
raise("usage: #{$0} user derivation_path [game]") if ARGV.size < 2 | |
user, derivation_path, game = ARGV | |
game ||= DEFAULT_GAME | |
derivation = File.open(derivation_path, &:read) | |
res = post_sandbox(user, derivation, game) | |
raise res.code unless res.is_a? Net::HTTPOK | |
puts parse_result(res.body).values.join("\n") | |
end | |
if __FILE__ == $0 | |
main | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage