Last active
March 10, 2016 05:30
-
-
Save peryaudo/246311065cd65e006240 to your computer and use it in GitHub Desktop.
使い方: https://www2.adst.keio.ac.jp/rcs/topMenu の中身を投げ込む(POSTなのでChromeはCtrl+Sで保存できないので注意)
This file contains hidden or 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
package main | |
import ( | |
"fmt" | |
"github.com/PuerkitoBio/goquery" | |
"os" | |
"strconv" | |
) | |
func gradeToScore(grade string) float64 { | |
if grade == "A" { | |
return 4.0 | |
} else if grade == "B" { | |
return 3.0 | |
} else if grade == "C" { | |
return 2.0 | |
} else if grade == "D" { | |
return 0.0 | |
} else if grade == "★" { | |
return 0.0 | |
} else { | |
fmt.Println("Error: invalid grade", grade) | |
os.Exit(1) | |
return -1.0 | |
} | |
} | |
func ignoreGrade(grade string) bool { | |
return false | |
// この行をコメントアウトして外部向け成績表GPAに | |
// return grade == "D" || grade == "★" | |
} | |
func main() { | |
if len(os.Args) != 2 { | |
fmt.Println("Usage:", os.Args[0], "<foo.html>") | |
return | |
} | |
f, err := os.Open(os.Args[1]) | |
if err != nil { | |
fmt.Println(err) | |
return | |
} | |
defer f.Close() | |
doc, err := goquery.NewDocumentFromReader(f) | |
if err != nil { | |
fmt.Println(err) | |
return | |
} | |
var numer float64 | |
var denom float64 | |
doc.Find("tr").Each(func(_ int, s *goquery.Selection) { | |
tds := s.Find("td") | |
if tds.Length() != 8 { | |
return | |
} | |
var grade float64 | |
var degree float64 | |
var ignored bool = false | |
tds.Each(func(i int, s *goquery.Selection) { | |
t := s.Text() | |
if i == 2 { | |
grade = gradeToScore(t) | |
ignored = ignoreGrade(t) | |
} else if i == 3 { | |
degree, err = strconv.ParseFloat(t, 64) | |
if err != nil { | |
fmt.Println("Error: cannot parse float", t) | |
os.Exit(1) | |
} | |
} | |
}) | |
if !ignored { | |
numer += grade * degree | |
denom += degree | |
} | |
}) | |
gpa := numer / denom | |
fmt.Println("GPA:", gpa) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment