Skip to content

Commit

Permalink
✨ Feat: Add month total legal person
Browse files Browse the repository at this point in the history
  • Loading branch information
henrywuC committed Jul 28, 2021
1 parent 86fab0c commit 3ca657b
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
13 changes: 13 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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("錯誤指令")
}
Expand Down
13 changes: 13 additions & 0 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
17 changes: 17 additions & 0 deletions twse/twse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down

0 comments on commit 3ca657b

Please sign in to comment.