view lectable/getSyllabus.go @ 1:1f47625c6948

...
author anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
date Mon, 30 Mar 2020 20:49:17 +0900
parents 5191dd198bf4
children
line wrap: on
line source

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
}