diff --git a/db.go b/db.go index 3b88426..8fa45f9 100644 --- a/db.go +++ b/db.go @@ -26,7 +26,7 @@ import ( _ "github.com/mattn/go-sqlite3" ) -type insertFunc func(date, baseURL string) error +type insertFunc func(date, baseUrl string) error type queryFunc func(num int) ([]picture, error) type checkFunc func(date string) (bool, error) @@ -56,8 +56,8 @@ func newDbOperator(db *sql.DB, table string) (*dbOperator, error) { } return &dbOperator{ - insert: func(date, baseURL string) error { - _, err := insertCmd.Exec(date, baseURL) + insert: func(date, baseUrl string) error { + _, err := insertCmd.Exec(date, baseUrl) return err }, @@ -71,7 +71,7 @@ func newDbOperator(db *sql.DB, table string) (*dbOperator, error) { } for result.Next() { - err := result.Scan(&pic.Date, &pic.BaseURL) + err := result.Scan(&pic.Date, &pic.BaseUrl) if err != nil { return nil, err } diff --git a/handler.go b/handler.go index 6ecd45a..a733ee9 100644 --- a/handler.go +++ b/handler.go @@ -76,9 +76,9 @@ func (h *handler) updatePics() bool { return false } - for i := range pics { + for i := len(pics) - 1; i > -1; i-- { if ok, _ := h.db.check(pics[i].Date); ok { - h.db.insert(pics[i].Date, pics[i].BaseURL) + h.db.insert(pics[i].Date, pics[i].BaseUrl) } } return true @@ -97,7 +97,6 @@ func (h *handler) updateBuff() bool { } func (h *handler) updateTask(dur int, wg *sync.WaitGroup) { - wg.Add(1) first := true timer := time.NewTimer(0) for { @@ -138,7 +137,7 @@ func (h *handler) redirect(w http.ResponseWriter, r *http.Request) { } else { pic = h.pic[h.picNum-1] } - url.WriteString(pic.BaseURL) + url.WriteString(pic.BaseUrl) if h.withres { res, ok := args["res"] diff --git a/main.go b/main.go index 7bcd04e..481adf0 100644 --- a/main.go +++ b/main.go @@ -61,6 +61,7 @@ func init() { panic("CreateHandlerError") } + wg.Add(2) go bingHandler.updateTask(conf.UpdateTime, &wg) go apodHandler.updateTask(conf.UpdateTime, &wg) } diff --git a/origin.go b/origin.go index 2b52dd0..09d11e2 100644 --- a/origin.go +++ b/origin.go @@ -24,13 +24,14 @@ import ( "io/ioutil" "net/http" "regexp" + "strconv" "strings" "time" ) type picture struct { Date string `json:"enddate"` - BaseURL string `json:"urlbase"` + BaseUrl string `json:"urlbase"` } func httpGet(url string) ([]byte, bool) { @@ -48,9 +49,14 @@ func httpGet(url string) ([]byte, bool) { } func getBing(num int) ([]picture, bool) { - url := bing + "/HPImageArchive.aspx?format=js&n=2&mkt=zh-CN" + var url strings.Builder + url.Grow(192) + url.WriteString(bing) + url.WriteString("/HPImageArchive.aspx?format=js&n=") + url.WriteString(strconv.Itoa(num)) + url.WriteString("&mkt=zh-CN") - msg, ok := httpGet(url) + msg, ok := httpGet(url.String()) if !ok { return nil, false } @@ -72,7 +78,7 @@ func getAPOD(num int) ([]picture, bool) { date := time.Now() matcher := regexp.MustCompile(`image/.*\.jpg`) - for i := 0; i < 100; i++ { + for i := 0; i < num; i++ { date = date.AddDate(0, 0, -1) url.Reset() @@ -92,13 +98,13 @@ func getAPOD(num int) ([]picture, bool) { continue } - baseURL := matcher.FindAll(msg, -1) - if baseURL == nil { + baseUrl := matcher.FindAll(msg, -1) + if baseUrl == nil { continue } ret = append(ret, picture{ Date: date.Format("20060102"), - BaseURL: string(baseURL[1]), + BaseUrl: string(baseUrl[1]), }) } return ret, true