Skip to content

Commit

Permalink
Bing 的 API 有所改动,无法获取第二天的图片了,因此修改更新逻辑,减少耦合
Browse files Browse the repository at this point in the history
  • Loading branch information
CharlieYu4994 committed Jan 29, 2021
1 parent e4b84af commit e50296a
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 44 deletions.
24 changes: 17 additions & 7 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

type inserter func(date, hdURL, uhdURL string) error
type querier func(date string) (*picture, error)
type querier func(num int) ([]picture, error)
type validator func(date string) (bool, error)

func newInserter(db *sql.DB) (inserter, error) {
Expand All @@ -22,20 +22,30 @@ func newInserter(db *sql.DB) (inserter, error) {
}

func newQuerier(db *sql.DB) querier {
return func(date string) (*picture, error) {
res := db.QueryRow("SELECT Date,HDURL,UHDURL FROM Pictures WHERE Date = ?", date)
tmp := &picture{}
err := res.Scan(&tmp.DATE, &tmp.HDURL, &tmp.UHDURL)
return func(num int) ([]picture, error) {
res, err := db.Query(
"SELECT Date,HDURL,UHDURL FROM Pictures ORDER BY id DESC LIMIT ?", num)
if err != nil {
return nil, err
}
return tmp, err

ret := make([]picture, 0, num)
tmp := picture{}
for res.Next() {
err = res.Scan(&tmp.DATE, &tmp.HDURL, &tmp.UHDURL)
if err != nil {
return nil, err
}
ret = append(ret, tmp)
}
return ret, nil
}
}

func newValidator(db *sql.DB) validator {
return func(date string) (bool, error) {
res := db.QueryRow(`SELECT IFNULL((SELECT Date FROM Pictures WHERE Date=?), "NULL")`, date)
res := db.QueryRow(
`SELECT IFNULL((SELECT Date FROM Pictures WHERE Date=?), "NULL")`, date)
var tmp string
err := res.Scan(&tmp)
if err == nil {
Expand Down
31 changes: 2 additions & 29 deletions http.go
Original file line number Diff line number Diff line change
@@ -1,43 +1,16 @@
package main

import (
"errors"
"fmt"
"net/http"
"time"
)

func getPictureURL(q querier, res int) (string, error) {
pic, err := q(time.Now().Format("20060102"))
if err != nil {
return "", nil
}
switch res {
case 0:
return pic.HDURL, nil
case 1:
return pic.UHDURL, nil
default:
return "", errors.New("UnSupported Res")
}
}

func redirectToHD(w http.ResponseWriter, r *http.Request) {
URL, err := getPictureURL(dbquerier, 0)
if err != nil {
fmt.Fprint(w, err)
return
}
http.Redirect(w, r, URL, 301)
http.Redirect(w, r, picBuffer[0].HDURL, 302)
}

func redirectToUHD(w http.ResponseWriter, r *http.Request) {
URL, err := getPictureURL(dbquerier, 1)
if err != nil {
fmt.Fprint(w, err)
return
}
http.Redirect(w, r, URL, 301)
http.Redirect(w, r, picBuffer[0].UHDURL, 302)
}

func homePage(w http.ResponseWriter, r *http.Request) {
Expand Down
36 changes: 28 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,34 @@ var dbinserter inserter
var dbquerier querier
var dbvalidator validator
var conf config
var picBuffer [7]picture

func updatePic(i inserter, v validator) (err error) {
tmpPURL, tmpDATE, err := getPictureInfo(-1, 1, "zh-CN")
func updetePicBuffer(q querier, num int) error {
tmp, err := q(num)
if err != nil {
return
return err
}
for index := 0; index < num; index++ {
picBuffer[index] = tmp[index]
}
return nil
}

func updatePic(i inserter, v validator) error {
tmpPURL, tmpDATE, err := getPictureInfo(-1, 9, "zh-CN")
if err != nil {
return err
}

tmpPICs := rewriteURL(tmpPURL, tmpDATE)
for _, tmpPIC := range tmpPICs {
for index := len(tmpPICs) - 1; index >= 0; index-- {
tmpPIC := tmpPICs[index]
status, _ := v(tmpPIC.DATE)
if status {
i(tmpPIC.DATE, tmpPIC.HDURL, tmpPIC.UHDURL)
}
}
return
return nil
}

func getDuration(hour int) time.Duration {
Expand All @@ -40,10 +54,11 @@ func getDuration(hour int) time.Duration {
}

func timeToUpdatePic() {
timer := time.NewTimer(time.Second * 10)
timer := time.NewTimer(0)
for {
<-timer.C
updatePic(dbinserter, dbvalidator)
updetePicBuffer(dbquerier, 7)
timer.Reset(getDuration(conf.UpdateTime))
}
}
Expand All @@ -59,15 +74,20 @@ func init() {
}
dbquerier = newQuerier(db)
dbvalidator = newValidator(db)
readConf("./config.json", &conf)
fmt.Println(conf)
err = readConf("./config.json", &conf)
if err != nil {
panic("Open Config Error")
}
go timeToUpdatePic()
}

func main() {
http.HandleFunc("/", homePage)
http.HandleFunc("/HDRES/", redirectToHD)
http.HandleFunc("/UHDRES/", redirectToUHD)
time.Sleep(time.Second)
fmt.Println("Done")
fmt.Println(picBuffer)
if conf.EnableTLS {
http.ListenAndServeTLS("0.0.0.0:"+conf.Port,
conf.CertPath, conf.KeyPath, nil)
Expand Down
Binary file modified picture.db
Binary file not shown.

0 comments on commit e50296a

Please sign in to comment.