Mercurial > hg > Members > anatofuz > lectable
changeset 2:dccd0dd6cfbc
move package
author | anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 31 Mar 2020 09:49:20 +0900 |
parents | 1f47625c6948 |
children | e4088b031eba |
files | lectable/getSyllabus.go syllabus/getSyllabus.go |
diffstat | 2 files changed, 157 insertions(+), 91 deletions(-) [+] |
line wrap: on
line diff
--- a/lectable/getSyllabus.go Mon Mar 30 20:49:17 2020 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,91 +0,0 @@ -package lectable - -import ( - "bufio" - "os" - "strings" - - "github.com/pkg/errors" -) - -const ( - monday int = iota - tuesday - wednesday - thursday - friday -) - -type lectureDay struct { - dayOfWeek int - period int - lastPeriod int - hasLast bool -} - -type lecture struct { - id string - name string - day *lectureDay - teacher string -} - -var dayPeriodID = "ctl00_phContents_Detail_lbl_day_period\">" -var lectureNameID = "ctl00_phContents_Detail_lbl_lbl_lct_name_double\">" -var teacherNameID = "ctl00_phContents_Detail_lbl_syl_staff_name_double\">" -var endSpan = "</span>" - -func syllabus2LectureStruct(lectureID, filePath string) (*lecture, error) { - file, err := os.Open(filePath) - - if err != nil { - return nil, errors.Wrap(err, "failed open html file") - } - scanner := bufio.NewScanner(file) - - var lec lecture - lec.id = lectureID - - for scanner.Scan() { - line := scanner.Text() - - // day Period - if i := strings.Index(line, dayPeriodID); i >= 0 { - if j := strings.Index(line, endSpan); j >= 0 { - i += len(dayPeriodID) - day := line[i:j] - if k := strings.Index(day, "~"); k >= 0 { - lec.day.hasLast = true - } else { - lec.day.hasLast = false - } - } - continue - } - - // lecture name - if i := strings.Index(line, lectureNameID); i >= 0 { - if j := strings.Index(line, endSpan); j >= 0 { - i += len(lectureNameID) - lec.name = line[i:j] - } - continue - } - - //teacher name - if i := strings.Index(line, teacherNameID); i >= 0 { - if j := strings.Index(line, endSpan); j >= 0 { - i += len(teacherNameID) - lec.teacher = line[i:j] - } - break - } - } - - file.Close() - return &lec, nil -} - -func kanjiday2int(kanjiDay string) (int, error) { - return 0, nil -}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/syllabus/getSyllabus.go Tue Mar 31 09:49:20 2020 +0900 @@ -0,0 +1,157 @@ +package syllabus + +import ( + "bufio" + "io" + "net/http" + "os" + "strconv" + "strings" + "time" + + "github.com/pkg/errors" +) + +const ( + monday int = iota + tuesday + wednesday + thursday + friday +) + +//GetSyllabus is use main function struct. members using download html operation +type GetSyllabus struct { + year int + term string + outputdir string +} + +// LectureDay include day of week (0~4, error -> 5), period, lastpriod (1~6) +type LectureDay struct { + DayOfWeek int + Period int + LastPeriod int +} + +//Lecture ID is ex. 600625001 , Name is プログラミング1, Day is LecutreDay +type Lecture struct { + ID string + Name string + Day *lectureDay + Teacher string +} + +//CreateGetSyllabus is constructor and initialize from now time +func CreateGetSyllabus() *GetSyllabus { + var gs GetSyllabus + tm := time.Now() + gs.year = tm.Year() + if tm.Month() < 7 { + gs.term = "previous" + } else { + gs.term = "latter" + } + var b strings.Builder + b.WriteString(strconv.Itoa(gs.year)) + b.WriteString("/") + b.WriteString(gs.term) + gs.outputdir = b.String() + return &gs +} + +var dayPeriodID = "ctl00_phContents_Detail_lbl_day_period\">" +var lectureNameID = "ctl00_phContents_Detail_lbl_lbl_lct_name_double\">" +var teacherNameID = "ctl00_phContents_Detail_lbl_syl_staff_name_double\">" +var endSpan = "</span>" +var dayOfWeeklen = len("月") + +//LecIDtoDownloadSyllabus is download from lecture ID +func (g *GetSyllabus) LecIDtoDownloadSyllabus(lectureID string) error { + var strBuilder strings.Builder + strBuilder.WriteString(g.outputdir) + strBuilder.WriteString("/") + strBuilder.WriteString(lectureID) + strBuilder.WriteString(".html") + + file, err := os.Create(strBuilder.String()) + defer file.Close() + + if err != nil { + return errors.Wrap("failed create html...") + } + + strBuilder.Reset() + + res, err := http.Get("http://google.com") + defer res.Body.Close() + + if err != nil { + return errors.Wrap("failed download html") + } + + _, err := io.Copy(file, res.Body) + if err != nil { + return errors.Wrap("failed download html") + } + + return nil +} + +//LecIDwFilePath2LectureStruct is require LectureID (== Lecture.ID), filePath ( syllabus.html path) +func (g *GetSyllabus) LecIDwFilePath2LectureStruct(lectureID, filePath string) (*lecture, error) { + file, err := os.Open(filePath) + + if err != nil { + return nil, errors.Wrap(err, "failed open html file") + } + scanner := bufio.NewScanner(file) + + var lec lecture + lec.id = lectureID + + for scanner.Scan() { + line := scanner.Text() + + // day Period + if i := strings.Index(line, dayPeriodID); i >= 0 { + if j := strings.Index(line, endSpan); j >= 0 { + i += len(dayPeriodID) + day := line[i:j] + lec.day.dayOfWeek, err = kanjiday2int(day[0:dayOfWeeklen]) + if err != nil { + return nil, errors.Wrap(err, "failed convert day") + } + lec.day.period, err = strconv.Atoi(day[dayOfWeeklen : dayOfWeeklen+1]) + + // dayの長さで〜があるかどうかが判定する + } + continue + } + + // lecture name + if i := strings.Index(line, lectureNameID); i >= 0 { + if j := strings.Index(line, endSpan); j >= 0 { + i += len(lectureNameID) + lec.name = line[i:j] + } + continue + } + + //teacher name + if i := strings.Index(line, teacherNameID); i >= 0 { + if j := strings.Index(line, endSpan); j >= 0 { + i += len(teacherNameID) + lec.teacher = line[i:j] + } + break + } + } + + file.Close() + return &lec, nil +} + +func kanjiday2int(kanjiDay string) (int, error) { + return 0, nil +}