Mercurial > hg > Members > anatofuz > lectable
comparison lectable/getSyllabus.go @ 0:5191dd198bf4
add convertSyllabus method
author | anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Mon, 30 Mar 2020 20:40:42 +0900 |
parents | |
children | 1f47625c6948 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:5191dd198bf4 |
---|---|
1 package lectable | |
2 | |
3 import ( | |
4 "bufio" | |
5 "os" | |
6 "strings" | |
7 | |
8 "github.com/pkg/errors" | |
9 ) | |
10 | |
11 const ( | |
12 monday int = iota | |
13 tuesday | |
14 wednesday | |
15 thursday | |
16 friday | |
17 ) | |
18 | |
19 type lectureDay struct { | |
20 dayOfWeek int | |
21 period int | |
22 lastPeriod int | |
23 hasLast bool | |
24 } | |
25 | |
26 type lecture struct { | |
27 id string | |
28 name string | |
29 day *lectureDay | |
30 teacher string | |
31 } | |
32 | |
33 func getSyllabus(lectureID, filePath string) (*lecture, error) { | |
34 file, err := os.Open(filePath) | |
35 | |
36 if err != nil { | |
37 return nil, errors.Wrap(err, "failed open html file") | |
38 } | |
39 scanner := bufio.NewScanner(file) | |
40 | |
41 var lec lecture | |
42 lec.id = lectureID | |
43 | |
44 dayPeriodID := "ctl00_phContents_Detail_lbl_day_period\">" | |
45 lectureNameID := "ctl00_phContents_Detail_lbl_lbl_lct_name_double\">" | |
46 teacherNameID := "ctl00_phContents_Detail_lbl_syl_staff_name_double\">" | |
47 | |
48 endSpan := "</span>" | |
49 | |
50 for scanner.Scan() { | |
51 line := scanner.Text() | |
52 | |
53 // day Period | |
54 if i := strings.Index(line, dayPeriodID); i >= 0 { | |
55 if j := strings.Index(line, endSpan); j >= 0 { | |
56 i += len(dayPeriodID) | |
57 day := line[i:j] | |
58 if k := strings.Index(day, "~"); k >= 0 { | |
59 lec.day.hasLast = true | |
60 } else { | |
61 lec.day.hasLast = false | |
62 } | |
63 } | |
64 continue | |
65 } | |
66 | |
67 // lecture name | |
68 if i := strings.Index(line, lectureNameID); i >= 0 { | |
69 if j := strings.Index(line, endSpan); j >= 0 { | |
70 i += len(lectureNameID) | |
71 lec.name = line[i:j] | |
72 } | |
73 continue | |
74 } | |
75 | |
76 //teacher name | |
77 if i := strings.Index(line, teacherNameID); i >= 0 { | |
78 if j := strings.Index(line, endSpan); j >= 0 { | |
79 i += len(teacherNameID) | |
80 lec.teacher = line[i:j] | |
81 } | |
82 break | |
83 } | |
84 } | |
85 | |
86 file.Close() | |
87 return &lec, nil | |
88 } | |
89 | |
90 func kanjiday2int(kanjiDay string) (int, error) { | |
91 return 0, nil | |
92 } |