<commit_before>package main
//package resolve

import (
	"io"
	"fmt"
	"strings"
	"io/ioutil"
	"net/http"
	"net/url"
	"database/sql"
	"github.com/PuerkitoBio/goquery"
	_ "github.com/mattn/go-sqlite3"
	"code.google.com/p/go.text/encoding/japanese"
	"code.google.com/p/go.text/transform"
)

func euc2utf(src io.Reader) io.Reader {
	return transform.NewReader(src, japanese.EUCJP.NewDecoder())
}

func utf2euc(str string) (string, error) {
	ret, err := ioutil.ReadAll(transform.NewReader(strings.NewReader(str), japanese.EUCJP.NewEncoder()))
	if err != nil {
		return "", err
	}
	return string(ret), err
}

func getList() []string {
	res, _ := http.PostForm(
		"http://webmizar.cs.shinshu-u.ac.jp/learn/infomath/",
		url.Values{},
	)
	defer res.Body.Close()
	doc, _ := goquery.NewDocumentFromReader(res.Body)
	url := []string{}
	doc.Find("table").Find("a").Each(func(_ int, s *goquery.Selection) {
		href, _ := s.Attr("href")
		i := strings.Index(href, "?exe_dir=")
		if i != -1 {
			query := href[i + 9:]
			url = append(url, query)
		}
	})
	return url
}

func resolve(exe_dir string, db *sql.DB, name, id, email string) {
	row := db.QueryRow("SELECT `tmp` FROM `process` WHERE `exe_dir` = ?", exe_dir)
	var tmp string
	row.Scan(&tmp)
	res, _ := http.PostForm("http://shirodanuki.cs.shinshu-u.ac.jp/cgi-bin/olts/sys/exercise.cgi",
	url.Values{
		"name": {name},
		"id": {id},
		"email": {email},
		"exe_dir": {exe_dir},
		"chapter": {""},
		"url": {"http://webmizar.cs.shinshu-u.ac.jp/learn/infomath/"},
		"tmp": {tmp},
	},)
	defer res.Body.Close()
	utf8 := euc2utf(res.Body)
	doc, _ := goquery.NewDocumentFromReader(utf8)
	html, _ := doc.Find("blockquote").Html()
	question := strings.TrimSpace(html)
	tmp, _ = doc.Find("input[name=tmp]").Attr("value")
	answers := doc.Find("input[name=answer]")
	println(doc.Find("body > u > i").Text())
	var answer string
	if answers.Length() == 1 {
		stmt, _ := db.Prepare("SELECT `answer` FROM `cai` WHERE `exe_dir` = ? AND `question` = ?")
		defer stmt.Close()
		row := stmt.QueryRow(exe_dir, question)
		row.Scan(&answer)
	} else {
		stmt, _ := db.Prepare("SELECT `answer` FROM `cai` WHERE `exe_dir` = ? AND `question` = ? AND `answer` in (?, ?, ?)")
		defer stmt.Close()
		a1, _ := answers.Attr("value")
		a2, _ := answers.Next().Attr("value")
		a3, _ := answers.Next().Next().Attr("value")
		row := stmt.QueryRow(exe_dir, question, a1, a2, a3)
		row.Scan(&answer)
	}
	answer, _ = utf2euc(answer)
	res, _ = http.PostForm("http://shirodanuki.cs.shinshu-u.ac.jp/cgi-bin/olts/sys/answer.cgi",
	url.Values{
		"answer": {answer},
		"subject": {""},
		"chapter": {""},
		"url": {"http://webmizar.cs.shinshu-u.ac.jp/learn/infomath/"},
		"tmp": {tmp},
	},)
	defer res.Body.Close()
	utf8 = euc2utf(res.Body)
	doc, _ = goquery.NewDocumentFromReader(utf8)
	tmp, _ = doc.Find("input[name=tmp]").Attr("value")
	db.Exec("REPLACE INTO `process` (`exe_dir`, `tmp`) VALUES (?, ?)", exe_dir, tmp)
	if strings.Contains(doc.Text(), "おめでとうございます") {
		println(exe_dir)
	} else {
		resolve(exe_dir, db, name, id, email)
	}
}

func main() {
	db, _ := sql.Open("sqlite3", "./cai.db")
	defer db.Close()
	var name, id, email string
	fmt.Printf("name > ")
	fmt.Scanln(&name)
	fmt.Printf("id > ")
	fmt.Scanln(&id)
	fmt.Printf("email > ")
	fmt.Scanln(&email)
	list := getList()
	for i := range(list) {
		fmt.Printf("[%d] %s\n", i, list[i])
	}
	var n int
	fmt.Printf("chapter > ")
	fmt.Scanf("%d", &n)
	resolve(list[n], db, name, id, email)
}<commit_msg>回答時間の指定を追加<commit_after>package main

//package resolve

import (
	"code.google.com/p/go.text/encoding/japanese"
	"code.google.com/p/go.text/transform"
	"database/sql"
	"fmt"
	"github.com/PuerkitoBio/goquery"
	_ "github.com/mattn/go-sqlite3"
	"io"
	"io/ioutil"
	"net/http"
	"net/url"
	"strings"
	"time"
)

func euc2utf(src io.Reader) io.Reader {
	return transform.NewReader(src, japanese.EUCJP.NewDecoder())
}

func utf2euc(str string) (string, error) {
	ret, err := ioutil.ReadAll(transform.NewReader(strings.NewReader(str), japanese.EUCJP.NewEncoder()))
	if err != nil {
		return "", err
	}
	return string(ret), err
}

func getList() []string {
	res, _ := http.PostForm(
		"http://webmizar.cs.shinshu-u.ac.jp/learn/infomath/",
		url.Values{},
	)
	defer res.Body.Close()
	doc, _ := goquery.NewDocumentFromReader(res.Body)
	url := []string{}
	doc.Find("table").Find("a").Each(func(_ int, s *goquery.Selection) {
		href, _ := s.Attr("href")
		i := strings.Index(href, "?exe_dir=")
		if i != -1 {
			query := href[i+9:]
			url = append(url, query)
		}
	})
	return url
}

func resolve(exe_dir string, db *sql.DB, name, id, email string, t int) {
	time.Sleep(time.Duration(t) * time.Millisecond * 100)
	row := db.QueryRow("SELECT `tmp` FROM `process` WHERE `exe_dir` = ?", exe_dir)
	var tmp string
	row.Scan(&tmp)
	res, _ := http.PostForm("http://shirodanuki.cs.shinshu-u.ac.jp/cgi-bin/olts/sys/exercise.cgi",
		url.Values{
			"name":    {name},
			"id":      {id},
			"email":   {email},
			"exe_dir": {exe_dir},
			"chapter": {""},
			"url":     {"http://webmizar.cs.shinshu-u.ac.jp/learn/infomath/"},
			"tmp":     {tmp},
		})
	defer res.Body.Close()
	utf8 := euc2utf(res.Body)
	doc, _ := goquery.NewDocumentFromReader(utf8)
	html, _ := doc.Find("blockquote").Html()
	question := strings.TrimSpace(html)
	tmp, _ = doc.Find("input[name=tmp]").Attr("value")
	answers := doc.Find("input[name=answer]")
	println(doc.Find("body > u > i").Text())
	var answer string
	if answers.Length() == 1 {
		stmt, _ := db.Prepare("SELECT `answer` FROM `cai` WHERE `exe_dir` = ? AND `question` = ?")
		defer stmt.Close()
		row := stmt.QueryRow(exe_dir, question)
		row.Scan(&answer)
	} else {
		stmt, _ := db.Prepare("SELECT `answer` FROM `cai` WHERE `exe_dir` = ? AND `question` = ? AND `answer` in (?, ?, ?)")
		defer stmt.Close()
		a1, _ := answers.Attr("value")
		a2, _ := answers.Next().Attr("value")
		a3, _ := answers.Next().Next().Attr("value")
		row := stmt.QueryRow(exe_dir, question, a1, a2, a3)
		row.Scan(&answer)
	}
	answer, _ = utf2euc(answer)
	res, _ = http.PostForm("http://shirodanuki.cs.shinshu-u.ac.jp/cgi-bin/olts/sys/answer.cgi",
		url.Values{
			"answer":  {answer},
			"subject": {""},
			"chapter": {""},
			"url":     {"http://webmizar.cs.shinshu-u.ac.jp/learn/infomath/"},
			"tmp":     {tmp},
		})
	defer res.Body.Close()
	utf8 = euc2utf(res.Body)
	doc, _ = goquery.NewDocumentFromReader(utf8)
	tmp, _ = doc.Find("input[name=tmp]").Attr("value")
	db.Exec("REPLACE INTO `process` (`exe_dir`, `tmp`) VALUES (?, ?)", exe_dir, tmp)
	if strings.Contains(doc.Text(), "おめでとうございます") {
		println(exe_dir)
	} else {
		resolve(exe_dir, db, name, id, email, t)
	}
}

func main() {
	db, _ := sql.Open("sqlite3", "./cai.db")
	defer db.Close()
	var name, id, email string
	fmt.Printf("name > ")
	fmt.Scanln(&name)
	fmt.Printf("id > ")
	fmt.Scanln(&id)
	fmt.Printf("email > ")
	fmt.Scanln(&email)
	list := getList()
	for i := range list {
		fmt.Printf("[%d] %s\n", i, list[i])
	}
	var n, t int
	fmt.Printf("chapter > ")
	fmt.Scanf("%d", &n)
	fmt.Printf("time[sec] > ")
	fmt.Scanf("%d", &t)
	resolve(list[n], db, name, id, email, t)
}
{'fraction_non_alphanumeric': 0.21521309624611912, 'fraction_numerical': 0.006068303697431555, 'mean_word_length': 3.987332864180155, 'pattern_counts': {'":': 24, '<': 3, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1812050', 'n_tokens_mistral': 3148, 'n_tokens_neox': 2972, 'n_words': 518}