Mercurial > hg > Members > anatofuz > lectable
changeset 10:b6a2d89b06e7
add cmd_pdf
author | anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 31 Mar 2020 16:32:53 +0900 |
parents | 2348344480da |
children | e7d9f63d969c |
files | cmd_donwload.go cmd_pdf.go lectable.go syllabus/getSyllabus.go |
diffstat | 4 files changed, 83 insertions(+), 17 deletions(-) [+] |
line wrap: on
line diff
--- a/cmd_donwload.go Tue Mar 31 15:43:14 2020 +0900 +++ b/cmd_donwload.go Tue Mar 31 16:32:53 2020 +0900 @@ -28,8 +28,9 @@ arr3 := []string{"601495001", "600625001"} lwps, err := dh.LecIDStoDonwlodSyllabus(ctx, arr3, outStream) - - return dh.DumpLectureWithPathJSON(lwps) + lectures, err := dh.LectureWPathS2LectureStruct(ctx, lwps, outStream) + if err != nil { + return err + } + return dh.DumpLecureToJson(lectures) } - -
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/cmd_pdf.go Tue Mar 31 16:32:53 2020 +0900 @@ -0,0 +1,20 @@ +package lectable + +import ( + "context" + "io" +) + +type cmdPDF struct{} + +func (cp *cmdPDF) name() string { + return "pdf" +} + +func (cd *cmdPDF) description() string { + return "parse from pdf" +} + +func (cd *cmdPDF) run(ctx context.Context, argv []string, outStream, errStream io.Writer) error { + return nil +}
--- a/lectable.go Tue Mar 31 15:43:14 2020 +0900 +++ b/lectable.go Tue Mar 31 16:32:53 2020 +0900 @@ -15,6 +15,7 @@ var ( subCommands = []cmd{ &cmdDownload{}, + &cmdPDF{}, } dispatch = make(map[string]cmd, len(subCommands)) maxSubcommandName int
--- a/syllabus/getSyllabus.go Tue Mar 31 15:43:14 2020 +0900 +++ b/syllabus/getSyllabus.go Tue Mar 31 16:32:53 2020 +0900 @@ -18,6 +18,14 @@ "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 @@ -34,10 +42,10 @@ //Lecture ID is ex. 600625001 , Name is プログラミング1, Day is LecutreDay type Lecture struct { - ID string - Name string - Day LectureDay - Teacher string + ID string `json: "id"` + Name string `json: "name` + Day LectureDay `json: "day"` + Teacher string `json: "teacher"` } type LectureWPath struct { @@ -156,8 +164,28 @@ return outputPath, nil } +func (g *GetSyllabus) LectureWPathS2LectureStruct(ctx context.Context, lwps *[]LectureWPath, outStream io.Writer) (*[]Lecture, error) { + //var wg sync.WaitGroup + ch := make(chan Lecture, len(*lwps)) + for _, lwp := range *lwps { + //wg.Add(1) + go func(lwp LectureWPath) { + //defer wg.Done() + lec, _ := g.LectureWPath2LectureStruct(&lwp) + ch <- *lec + }(lwp) + } + //wg.Wait() + + var lecs []Lecture + for range *lwps { + lecs = append(lecs, <-ch) + } + return &lecs, nil +} + //LecIDwFilePath2LectureStruct is require LectureID (== Lecture.ID), filePath ( syllabus.html path) -func (g *GetSyllabus) LecIDwFilePath2LectureStruct(lwp *LectureWPath) (*Lecture, error) { +func (g *GetSyllabus) LectureWPath2LectureStruct(lwp *LectureWPath) (*Lecture, error) { file, err := os.Open(lwp.Path) if err != nil { @@ -223,25 +251,25 @@ func kanjiday2int(kanjiDay string) int { switch kanjiDay { case "月": - return 0 + return monday case "火": - return 1 + return tuesday case "水": - return 2 + return wednesday case "木": - return 3 + return thursday case "金": - return 4 + return friday } return -1 } -func (g *GetSyllabus)DumpLectureWithPathJSON(lwps *[]LectureWPath) error { - bytes, err := json.Marshal(lwps) +func (g *GetSyllabus) DumpLecureToJson(lectures *[]Lecture) error { + bytes, err := json.Marshal(lectures) if err != nil { return err } - fp := filepath.Join(g.outputdir, "dump.json") + fp := filepath.Join(g.outputdir, "dump_lectures.json") file, err := os.Create(fp) bw := bufio.NewWriter(file) _, err = bw.Write(bytes) @@ -253,3 +281,19 @@ return nil } +func (g *GetSyllabus) DumpLectureWithPathJSON(lwps *[]LectureWPath) error { + bytes, err := json.Marshal(lwps) + if err != nil { + return err + } + fp := filepath.Join(g.outputdir, "dump.json") + file, err := os.Create(fp) + bw := bufio.NewWriter(file) + _, err = bw.Write(bytes) + if err != nil { + return err + } + bw.Flush() + file.Close() + return nil +}