view download.go @ 0:7f5ee6f5e801 default tip

...
author anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
date Mon, 20 Apr 2020 13:51:39 +0900
parents
children
line wrap: on
line source

package syllabusdiff

import (
	"fmt"
	"io"
	"net/http"
	"net/url"
	"os"
	"path"
	"path/filepath"
	"strconv"
	"strings"
	"time"

	"github.com/pkg/errors"
)

type season struct {
	year         int
	term         string
	outputDir    string
	preOutputDir string
}

func createSeaspn() *season {
	nowSeason := new(season)
	tm := time.Now()
	year := tm.Year()
	var term string
	if tm.Month() < 7 {
		term = "previous"
	} else {
		term = "latter"
	}
	nowSeason.term = term
	nowSeason.year = year
	yearString := strconv.Itoa(year)
	nowSeason.outputDir = filepath.Join(yearString, term)
	nowSeason.preOutputDir = filepath.Join("prev", yearString, term)
	return nowSeason
}

type lect struct {
	name string
	id   string
	url  string
}

var endpoint = "https://tiglon.jim.u-ryukyu.ac.jp"
var lectureNameID = "ctl00_phContents_Detail_lbl_lbl_lct_name_double\">" // 講義名があるID
var endSpan = "</span>"                                                  // 後ろの方

// idいれるとダウンロードしてくれる
func (ss *season) downloadSyllabusFromID(lectureID string) (string, error) {
	var strBuilder strings.Builder
	strBuilder.WriteString(lectureID)
	strBuilder.WriteString(".html")

	outputPath := filepath.Join(ss.outputDir, strBuilder.String())

	if _, err := os.Stat(outputPath); err == nil {
		fmt.Printf("already download %s.html\n", lectureID)
		return outputPath, nil
	}

	file, err := os.Create(outputPath)
	defer file.Close()

	if err != nil {
		return "", errors.Wrap(err, "failed create html...")
	}

	strBuilder.Reset()

	u, err := url.Parse(endpoint)
	if err != nil {
		return "", err
	}

	u.Path = path.Join(u.Path, "portal", "Public", "Syllabus", "SyllabusSearchStart.aspx")
	q := u.Query()
	q.Set("lct_year", strconv.Itoa(ss.year))
	q.Set("lct_cd", lectureID)
	q.Set("je_cd", "1")
	u.RawQuery = q.Encode()

	fmt.Println(u.String())
	res, err := http.Get(u.String())
	defer res.Body.Close()

	if err != nil {
		return "", errors.Wrap(err, "failed download html")
	}

	_, err = io.Copy(file, res.Body)
	if err != nil {
		return "", errors.Wrap(err, "failed download html")
	}

	return outputPath, nil
}

func downloadWithDiff(url string) error {
	return nil
}

func creatLect(id, lectURL string) (*lect, error) {
	lec := &lect{id: id, url: lectURL}
	return lec, nil
}