Skip to content

Commit

Permalink
bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
CharlieYu4994 committed Jul 30, 2021
1 parent deb5ca2 commit 4ebb516
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 15 deletions.
8 changes: 4 additions & 4 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
},

Expand All @@ -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
}
Expand Down
7 changes: 3 additions & 4 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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"]
Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ func init() {
panic("CreateHandlerError")
}

wg.Add(2)
go bingHandler.updateTask(conf.UpdateTime, &wg)
go apodHandler.updateTask(conf.UpdateTime, &wg)
}
Expand Down
20 changes: 13 additions & 7 deletions origin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
}
Expand All @@ -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()
Expand All @@ -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
Expand Down

0 comments on commit 4ebb516

Please sign in to comment.