Skip to content

Commit 3e5dec8

Browse files
committed
added infos to subparser format
1 parent 8958266 commit 3e5dec8

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

model/plan.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ type Plan struct {
77
Date string `json:"date" bson:"date"`
88
CreatedAt time.Time `json:"created_at" bson:"created_at"`
99
Absent []Absent `json:"absent" bson:"absent"`
10+
Infos []string `json:"infos" bson:"infos"`
1011
Substitutions []Substitution `json:"substitutions" bson:"substitutions"`
1112
}
1213

parsers/effnerDsb.go

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"github.com/EffnerApp/subparser/model"
66
"github.com/PuerkitoBio/goquery"
7+
"regexp"
78
"strings"
89
"time"
910
)
@@ -61,6 +62,12 @@ func parsePlan(content string) (*model.Plan, error) {
6162
return nil, err
6263
}
6364

65+
// load the infos
66+
infos, err := findInfos(document)
67+
if err != nil {
68+
return nil, err
69+
}
70+
6471
// load the substitutions
6572
substitutions, err := findSubstitutions(document)
6673

@@ -73,10 +80,50 @@ func parsePlan(content string) (*model.Plan, error) {
7380
Date: date,
7481
CreatedAt: createdAt,
7582
Absent: absent,
83+
Infos: infos,
7684
Substitutions: substitutions,
7785
}, nil
7886
}
7987

88+
func findInfos(document *goquery.Document) ([]string, error) {
89+
table := document.Find("table.F")
90+
91+
if table == nil {
92+
return []string{}, nil
93+
}
94+
95+
table = table.First()
96+
97+
if table == nil {
98+
return []string{}, nil
99+
}
100+
101+
infos := make([]string, 0)
102+
103+
space := regexp.MustCompile(`\s+`)
104+
105+
// very ugly but works...
106+
table.Find("th.F").Each(func(_ int, th *goquery.Selection) {
107+
text := th.Text()
108+
text = strings.TrimPrefix(text, "\n")
109+
110+
if text == "" {
111+
return
112+
}
113+
114+
text = strings.Replace(text, "\t", " ", -1)
115+
116+
// some strange character they for some reason contain. Never touch this.
117+
text = strings.Replace(text, " ", " ", -1)
118+
119+
text = strings.TrimSpace(text)
120+
text = space.ReplaceAllString(text, " ")
121+
122+
infos = append(infos, text)
123+
})
124+
return infos, nil
125+
}
126+
80127
func findSubstitutions(document *goquery.Document) ([]model.Substitution, error) {
81128
table := document.Find("table.k")
82129
if table != nil {
@@ -146,7 +193,7 @@ func findCreatedAt(document *goquery.Document) (time.Time, error) {
146193
text = text[11:strings.Index(text, ")")]
147194

148195
// parse the text into a date-time
149-
date, err := time.Parse("_2.01.2006 um 15:04 Uhr", text)
196+
date, err := time.Parse("_2.1.2006 um 15:04 Uhr", text)
150197

151198
if err != nil {
152199
return time.Now(), err

0 commit comments

Comments
 (0)