You've already forked top-wallpaper
more fixes
This commit is contained in:
3
go.mod
3
go.mod
@@ -4,13 +4,14 @@ go 1.24.0
|
||||
|
||||
require (
|
||||
github.com/labstack/echo/v4 v4.13.3
|
||||
github.com/labstack/gommon v0.4.2
|
||||
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646
|
||||
github.com/redis/go-redis/v9 v9.7.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/cespare/xxhash/v2 v2.2.0 // indirect
|
||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
|
||||
github.com/labstack/gommon v0.4.2 // indirect
|
||||
github.com/mattn/go-colorable v0.1.13 // indirect
|
||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||
github.com/valyala/bytebufferpool v1.0.0 // indirect
|
||||
|
||||
2
go.sum
2
go.sum
@@ -17,6 +17,8 @@ github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovk
|
||||
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
|
||||
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 h1:zYyBkD/k9seD2A7fsi6Oo2LfFZAehjjQMERAvZLEDnQ=
|
||||
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646/go.mod h1:jpp1/29i3P1S/RLdc7JQKbRpFeM1dOBd8T9ki5s+AY8=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/redis/go-redis/v9 v9.7.0 h1:HhLSs+B6O021gwzl+locl0zEDnyNkxMtf/Z3NNBMa9E=
|
||||
|
||||
26
main.go
26
main.go
@@ -8,6 +8,7 @@ import (
|
||||
"github.com/labstack/echo/v4/middleware"
|
||||
"github.com/labstack/gommon/log"
|
||||
"github.com/redis/go-redis/v9"
|
||||
"github.com/siteworxpro/top-wallpaper/resize"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
@@ -142,7 +143,11 @@ func main() {
|
||||
|
||||
if err != nil || latestImageVal == "" {
|
||||
c.Logger().Info("Fetching latest image")
|
||||
latestImageVal = getLatestImage()
|
||||
latestImageVal, err = getLatestImage()
|
||||
if err != nil {
|
||||
return c.String(http.StatusInternalServerError, "Error fetching latest image")
|
||||
}
|
||||
|
||||
if cc.redis != nil {
|
||||
cmd := cc.redis.Set(context.TODO(), "latestImage", latestImageVal, 600*time.Second)
|
||||
if cmd.Err() != nil {
|
||||
@@ -180,6 +185,12 @@ func main() {
|
||||
|
||||
imageData = string(imageDataBytes)
|
||||
|
||||
imageData, err := resize.Shrink(imageData, 1200, 70)
|
||||
|
||||
if err != nil {
|
||||
return c.String(http.StatusInternalServerError, "Error resizing image")
|
||||
}
|
||||
|
||||
go func(data string) {
|
||||
if cc.redis == nil {
|
||||
return
|
||||
@@ -210,21 +221,26 @@ func main() {
|
||||
e.Logger.Fatal(e.Start(":" + port))
|
||||
}
|
||||
|
||||
func getLatestImage() string {
|
||||
func getLatestImage() (string, error) {
|
||||
|
||||
response, err := http.Get("https://www.reddit.com/r/wallpaper/.json")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
return "", err
|
||||
}
|
||||
defer func(Body io.ReadCloser) {
|
||||
_ = Body.Close()
|
||||
}(response.Body)
|
||||
|
||||
if response.StatusCode != http.StatusOK {
|
||||
return "", fmt.Errorf("error fetching reddit data: %s", response.Status)
|
||||
}
|
||||
|
||||
jsonBytes, err := io.ReadAll(response.Body)
|
||||
|
||||
redditData := redditResponse{}
|
||||
err = json.Unmarshal(jsonBytes, &redditData)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
return "", err
|
||||
}
|
||||
|
||||
index := 1
|
||||
@@ -235,5 +251,5 @@ func getLatestImage() string {
|
||||
url = redditData.Data.Children[index].Data.UrlOverriddenByDest
|
||||
}
|
||||
|
||||
return url
|
||||
return url, nil
|
||||
}
|
||||
|
||||
25
resize/resize.go
Normal file
25
resize/resize.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package resize
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"github.com/nfnt/resize"
|
||||
i "image"
|
||||
"image/jpeg"
|
||||
_ "image/png"
|
||||
)
|
||||
|
||||
func Shrink(image string, maxSize uint, quality int) (string, error) {
|
||||
img, _, err := i.Decode(bytes.NewReader([]byte(image)))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
resizedImg := resize.Thumbnail(maxSize, maxSize, img, resize.Lanczos3)
|
||||
var buffer bytes.Buffer
|
||||
err = jpeg.Encode(&buffer, resizedImg, &jpeg.Options{Quality: quality})
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return buffer.String(), nil
|
||||
}
|
||||
Reference in New Issue
Block a user