view cmd_pdf.go @ 19:9014a505c54c

...
author anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
date Thu, 02 Apr 2020 04:13:34 +0900
parents bac5eb544d4d
children
line wrap: on
line source

package lectable

import (
	"bufio"
	"context"
	"encoding/json"
	"fmt"
	"io"
	"os"
	"path/filepath"
)

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 {
	outputDir := guessOutputDir()
	err := checkAndMkdirBuilddir(outputDir)
	if err != nil {
		return err
	}

	ppsr, err := convertStringFromPDF(argv)
	if err != nil {
		return err
	}
	var fpfs []*lectureFPDF
	for _, pps := range *ppsr {
		for _, pp := range pps {
			fpdf, err := str2lectureFPDF(pp)
			if err != nil {
				return err
			}
			fpfs = append(fpfs, fpdf...)
		}
	}

	var dlfps []DumpLectureFPDF

	lecmap := make(map[string][]*lectureFPDF, len(fpfs))
	for _, h := range fpfs {
		if fp, ok := lecmap[h.id]; ok {
			isIndividual := true
			for _, tmpFp := range fp {
				if tmpFp.place == "" {
					isIndividual = false
					break
				}

				if tmpFp.place == h.place {
					isIndividual = false
					break
				}
			}
			if !isIndividual {
				continue
			}

		}
		lecmap[h.id] = append(lecmap[h.id], h)
	}

	for _, lfpfs := range lecmap {
		for _, h := range lfpfs {
			dlfps = append(dlfps, DumpLectureFPDF{
				Id:       h.id,
				IsSelect: h.isSelect,
				Place:    h.place,
				Grades:   h.grades,
				Day:      h.day,
			})
		}
	}

	fmt.Println(dlfps)
	dumpJson(dlfps, outputDir)
	return nil
}

type DumpLectureFPDF struct {
	Id       string `json:id`
	IsSelect bool   `json:isSelect`
	Place    string `json:place`
	Day      string `json:day`
	Grades   []int  `json:grades`
}

func dumpJson(dlfp []DumpLectureFPDF, outputdir string) error {
	bytes, err := json.Marshal(dlfp)
	if err != nil {
		return err
	}
	fp := filepath.Join(outputdir, "dumpFromPDF.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
}