diff --git a/main.go b/main.go index 158cff5..7fbef9b 100644 --- a/main.go +++ b/main.go @@ -78,6 +78,19 @@ func run(command string, args []string, c chan string) { log.Panic(err) } c <- lp.PrettyString() + } else if len(args) > 0 && args[0] == "m" { + t := Now + if len(args) > 1 { + t, err = time.Parse("2006/01", args[1]) + if err != nil { + log.Panic("錯誤日期格式yyyy/mm") + } + } + lp, err := getMonthTotalLegalPerson(t) + if err != nil { + log.Panic(err) + } + c <- lp.PrettyString() } else { log.Panic("錯誤指令") } diff --git a/query.go b/query.go index 8da3f57..fca170b 100644 --- a/query.go +++ b/query.go @@ -89,6 +89,19 @@ func getDayTotalLegalPerson(date time.Time) (*twse.LegalPersonTotal, error) { return totalPerson, nil } +func getMonthTotalLegalPerson(date time.Time) (*twse.LegalPersonTotal, error) { + cacheKey := "month_total_legal_persons_" + if x, found := Cache.Get(cacheKey + date.Format("200601") + "01"); found { + return x.(*twse.LegalPersonTotal), nil + } + totalPerson, err := twse.MonthLegalPersonTotal(date) + if err != nil { + return &twse.LegalPersonTotal{}, err + } + Cache.Set(cacheKey+totalPerson.Date, totalPerson, cache.NoExpiration) + return totalPerson, nil +} + func getMonthLegalPersons(date time.Time) (*twse.LegalPersonStocks, error) { cacheKey := "month_legal_persons_" if x, found := Cache.Get(cacheKey + date.Format("200601") + "01"); found { diff --git a/twse/twse.go b/twse/twse.go index aefab8a..5643009 100644 --- a/twse/twse.go +++ b/twse/twse.go @@ -75,6 +75,23 @@ func DayLegalPersonTotal(date time.Time) (*LegalPersonTotal, error) { return legalPerson, nil } +func MonthLegalPersonTotal(date time.Time) (*LegalPersonTotal, error) { + var url string + if date.Format("20060102") == time.Now().In(Loc).Format("20060102") { + url = "https://www.twse.com.tw/fund/BFI82U?response=json&weekDate=&monthDate=&type=month" + } else { + url = fmt.Sprintf("https://www.twse.com.tw/fund/BFI82U?response=json&weekDate=&monthDate=%s&type=month&dayDate=", date.Format("20060102")) + } + response := &LegalPersonResponse{} + legalPerson := &LegalPersonTotal{} + err := request(url, response) + if err != nil { + return legalPerson, err + } + legalPerson = NewLegalPersonTotal(response.Date, response.Title, response.Data) + return legalPerson, nil +} + func MonthLegalPersons(date time.Time) (*LegalPersonStocks, error) { var url string if date.Format("20060102") == time.Now().In(Loc).Format("20060102") {