From d4fce2b9da0e5c83b2f5ce5e6a3fa5ab5e4f6327 Mon Sep 17 00:00:00 2001 From: DuGlaser Date: Sat, 2 Sep 2023 18:46:57 +0900 Subject: [PATCH] :recycle: Support when the title of the scoring table is h2. --- internal/repository/scraper/contest.go | 2 +- internal/repository/scraper/contest_test.go | 159 ++++++++++++++++++-- 2 files changed, 145 insertions(+), 16 deletions(-) diff --git a/internal/repository/scraper/contest.go b/internal/repository/scraper/contest.go index a909a9c..1e1d4c2 100644 --- a/internal/repository/scraper/contest.go +++ b/internal/repository/scraper/contest.go @@ -36,7 +36,7 @@ type Problem struct { func (cp *ContestPage) GetProblemIds() []Problem { ps := []Problem{} - cp.doc.Find("div#contest-statement h3").Each(func(i int, s *goquery.Selection) { + cp.doc.Find("div#contest-statement h3, div#contest-statement h2").Each(func(i int, s *goquery.Selection) { if s.Text() == "配点" { s.Next().Find("table tbody tr > td:first-child").Each(func(i int, s *goquery.Selection) { t := strings.ToLower(s.Text()) diff --git a/internal/repository/scraper/contest_test.go b/internal/repository/scraper/contest_test.go index ada40fa..5c23cd9 100644 --- a/internal/repository/scraper/contest_test.go +++ b/internal/repository/scraper/contest_test.go @@ -6,7 +6,7 @@ import ( "time" ) -var html = ` +var h3Example = `
@@ -123,33 +123,162 @@ var html = `
` +var h2Example = ` +
+
+
+ + + コンテスト時間: + + + + ~ + + + + + (100分) + + +
+
+ +
+ + +

配点

+
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
問題点数
A100
B200
C300
D400
E500
F500
G600
+
+
+ +

賞金

+
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
順位金額
1位
2位
3位
4位
5位
6位
7位
+
+
+
+
+
+
+` + func TestGetProblemIds(t *testing.T) { expect := []string{"a", "b", "c", "d", "e", "f", "g"} - - cp, err := NewContestPage(strings.NewReader(html)) - if err != nil { - t.Fatal("Failed to create ContestPage.") + var tests = []struct { + name string + inupt string + }{ + {"h3Example", h3Example}, + {"h2Example", h2Example}, } - ids := cp.GetProblemIds() + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + cp, err := NewContestPage(strings.NewReader(test.inupt)) + if err != nil { + t.Fatal("Failed to create ContestPage.") + } - if len(ids) != len(expect) { - t.Fatalf("ids has wrong value. got=%v.", ids) - } + ids := cp.GetProblemIds() + + if len(ids) != len(expect) { + t.Errorf("ids has wrong value. got=%v.", ids) + } - for i, _ := range ids { - id := ids[i].DisplayID + for i, _ := range ids { + id := ids[i].DisplayID - if id != expect[i] { - t.Errorf("ids has wrong value. got=%v, want=%v.", ids, expect) - } + if id != expect[i] { + t.Errorf("ids has wrong value. got=%v, want=%v.", ids, expect) + } + } + }) } + } func TestGetStartAt(t *testing.T) { expect, _ := time.Parse("20060102T1504", "20230812T2100") - cp, err := NewContestPage(strings.NewReader(html)) + cp, err := NewContestPage(strings.NewReader(h3Example)) if err != nil { t.Fatal(err) }