You've already forked top-wallpaper
Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
e16bcc1780
|
|||
|
e489896af2
|
|||
|
24873fb0d7
|
|||
|
d321d96fef
|
@@ -7,7 +7,7 @@ name: 🚨 Test Code Base
|
|||||||
run-name: ${{ github.workflow }}-{{ github.ref_name }}-{{ github.run_number }}
|
run-name: ${{ github.workflow }}-{{ github.ref_name }}-{{ github.run_number }}
|
||||||
|
|
||||||
env:
|
env:
|
||||||
GO_VERSION: '1.25.0'
|
GO_VERSION: '1.25.6'
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
test-go:
|
test-go:
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
FROM siteworxpro/golang:1.25.0 AS build
|
FROM siteworxpro/golang:1.25.6 AS build
|
||||||
|
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
|
|||||||
2
go.mod
2
go.mod
@@ -1,6 +1,6 @@
|
|||||||
module github.com/siteworxpro/top-wallpaper
|
module github.com/siteworxpro/top-wallpaper
|
||||||
|
|
||||||
go 1.25.0
|
go 1.25.6
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/labstack/echo/v4 v4.13.4
|
github.com/labstack/echo/v4 v4.13.4
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package http
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
|
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
"github.com/siteworxpro/top-wallpaper/redis"
|
"github.com/siteworxpro/top-wallpaper/redis"
|
||||||
@@ -14,7 +15,10 @@ func Get(c echo.Context) error {
|
|||||||
return c.String(http.StatusInternalServerError, "Internal Server Error: "+err.Error())
|
return c.String(http.StatusInternalServerError, "Internal Server Error: "+err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
val, err := rc.Get(redis.CacheKey)
|
defer rc.Close()
|
||||||
|
|
||||||
|
nsfw := (c.QueryParam("nsfw") == "true" || c.QueryParam("nsfw") == "1") && os.Getenv("NSFW_MODE") == "true"
|
||||||
|
val, err := rc.Get(rc.GetCacheKey(nsfw))
|
||||||
|
|
||||||
if err != nil || val == "" {
|
if err != nil || val == "" {
|
||||||
return c.NoContent(http.StatusNoContent)
|
return c.NoContent(http.StatusNoContent)
|
||||||
|
|||||||
2
main.go
2
main.go
@@ -28,6 +28,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
e := echo.New()
|
e := echo.New()
|
||||||
|
defer e.Close()
|
||||||
|
|
||||||
e.AcquireContext().Set("redisClient", rc)
|
e.AcquireContext().Set("redisClient", rc)
|
||||||
e.Logger.SetOutput(os.Stdout)
|
e.Logger.SetOutput(os.Stdout)
|
||||||
|
|
||||||
|
|||||||
@@ -54,9 +54,14 @@ type redditResponse struct {
|
|||||||
} `json:"data"`
|
} `json:"data"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetLatestImage(httpGet func(url string) (*http.Response, error)) (string, error) {
|
const (
|
||||||
|
wallpaperURL = "https://www.reddit.com/r/wallpaper/.json"
|
||||||
|
nsfwWallpaperURL = "https://www.reddit.com/r/NSFW_Wallpapers/.json"
|
||||||
|
)
|
||||||
|
|
||||||
response, err := httpGet("https://www.reddit.com/r/wallpaper/.json")
|
func GetLatestImage(httpGet func() (*http.Response, error)) (string, error) {
|
||||||
|
|
||||||
|
response, err := httpGet()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package reddit
|
package reddit
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -50,11 +49,8 @@ func TestGetLatestImage(t *testing.T) {
|
|||||||
defer server.Close()
|
defer server.Close()
|
||||||
|
|
||||||
// Custom httpGet function for testing
|
// Custom httpGet function for testing
|
||||||
httpGet := func(url string) (*http.Response, error) {
|
httpGet := func() (*http.Response, error) {
|
||||||
if strings.Contains(url, "/r/wallpaper/.json") {
|
return http.Get(server.URL + "/r/wallpaper/.json")
|
||||||
return http.Get(server.URL + "/r/wallpaper/.json")
|
|
||||||
}
|
|
||||||
return nil, fmt.Errorf("unexpected URL: %s", url)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test the function
|
// Test the function
|
||||||
|
|||||||
@@ -50,99 +50,113 @@ func Fetch(ctx context.Context) {
|
|||||||
return
|
return
|
||||||
default:
|
default:
|
||||||
|
|
||||||
val, err := rc.Get(redis.CacheKey)
|
doFetch(rc, l, false, size)
|
||||||
if err != nil {
|
|
||||||
l.Error("Error fetching from Redis: ", err)
|
if os.Getenv("NSFW_MODE") == "true" {
|
||||||
break
|
doFetch(rc, l, true, size)
|
||||||
}
|
}
|
||||||
|
|
||||||
if val != "" {
|
|
||||||
l.Info("Reddit image fetched from cache...")
|
|
||||||
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
l.Info("Fetching latest image from Reddit...")
|
|
||||||
|
|
||||||
latestImageVal, err := GetLatestImage(func(u string) (*http.Response, error) {
|
|
||||||
|
|
||||||
request := &http.Request{
|
|
||||||
Method: http.MethodGet,
|
|
||||||
Header: http.Header{
|
|
||||||
"User-Agent": []string{"Mozilla/5.0 (compatible; RedditBot/1.0; +https://www.reddit.com/wiki/redditauth)"},
|
|
||||||
},
|
|
||||||
URL: &url.URL{
|
|
||||||
Scheme: "https",
|
|
||||||
Host: "www.reddit.com",
|
|
||||||
Path: strings.TrimPrefix(u, "https://www.reddit.com"),
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
httpClient := &http.Client{
|
|
||||||
Timeout: 10 * time.Second,
|
|
||||||
}
|
|
||||||
|
|
||||||
response, e := httpClient.Do(request)
|
|
||||||
|
|
||||||
if e != nil {
|
|
||||||
l.Error("Error fetching image from Reddit: ", e)
|
|
||||||
return nil, e
|
|
||||||
}
|
|
||||||
|
|
||||||
if response.StatusCode != http.StatusOK {
|
|
||||||
l.Error("Error fetching image from Reddit: ", response.Status)
|
|
||||||
return nil, &echo.HTTPError{
|
|
||||||
Code: response.StatusCode,
|
|
||||||
Message: "Error fetching image from Reddit",
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return response, nil
|
|
||||||
})
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
l.Error("Error getting latest image URL: ", err)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
response, err := http.Get(latestImageVal)
|
|
||||||
if err != nil {
|
|
||||||
l.Error("Error fetching image from Reddit: ", err)
|
|
||||||
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
if response.StatusCode != http.StatusOK {
|
|
||||||
l.Error("Error fetching image from Reddit: ", response.Status)
|
|
||||||
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
imageDataBytes, err := io.ReadAll(response.Body)
|
|
||||||
if err != nil {
|
|
||||||
l.Error("Error reading image data: ", err)
|
|
||||||
_ = response.Body.Close()
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
_ = response.Body.Close()
|
|
||||||
|
|
||||||
imageData := string(imageDataBytes)
|
|
||||||
resized, err := resize.Shrink(imageData, uint(size), 70)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
l.Error("Error resizing image: ", err)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
err = rc.Set(redis.CacheKey, resized, 10*time.Minute)
|
|
||||||
if err != nil {
|
|
||||||
l.Warn("could not cache image")
|
|
||||||
}
|
|
||||||
|
|
||||||
l.Info("Reddit image fetched and resized successfully, cached for 10 minutes.")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
time.Sleep(10 * time.Minute)
|
time.Sleep(10 * time.Minute)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func doFetch(rc *redis.Redis, l echo.Logger, nsfw bool, size int) {
|
||||||
|
val, err := rc.Get(rc.GetCacheKey(nsfw))
|
||||||
|
if err != nil {
|
||||||
|
l.Error("Error fetching from Redis: ", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if val != "" {
|
||||||
|
l.Info("Reddit image fetched from cache...")
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
l.Info("Fetching latest image from Reddit...")
|
||||||
|
|
||||||
|
latestImageVal, err := GetLatestImage(func() (*http.Response, error) {
|
||||||
|
|
||||||
|
jsonUrl := wallpaperURL
|
||||||
|
nsfwMode := os.Getenv("NSFW_MODE")
|
||||||
|
if nsfwMode == "true" {
|
||||||
|
jsonUrl = nsfwWallpaperURL
|
||||||
|
}
|
||||||
|
|
||||||
|
request := &http.Request{
|
||||||
|
Method: http.MethodGet,
|
||||||
|
Header: http.Header{
|
||||||
|
"User-Agent": []string{"Mozilla/5.0 (compatible; RedditBot/1.0; +https://www.reddit.com/wiki/redditauth)"},
|
||||||
|
},
|
||||||
|
URL: &url.URL{
|
||||||
|
Scheme: "https",
|
||||||
|
Host: "www.reddit.com",
|
||||||
|
Path: strings.TrimPrefix(jsonUrl, "https://www.reddit.com"),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
httpClient := &http.Client{
|
||||||
|
Timeout: 10 * time.Second,
|
||||||
|
}
|
||||||
|
|
||||||
|
response, e := httpClient.Do(request)
|
||||||
|
|
||||||
|
if e != nil {
|
||||||
|
l.Error("Error fetching image from Reddit: ", e)
|
||||||
|
return nil, e
|
||||||
|
}
|
||||||
|
|
||||||
|
if response.StatusCode != http.StatusOK {
|
||||||
|
l.Error("Error fetching image from Reddit: ", response.Status)
|
||||||
|
return nil, &echo.HTTPError{
|
||||||
|
Code: response.StatusCode,
|
||||||
|
Message: "Error fetching image from Reddit",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return response, nil
|
||||||
|
})
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
l.Error("Error getting latest image URL: ", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
response, err := http.Get(latestImageVal)
|
||||||
|
if err != nil {
|
||||||
|
l.Error("Error fetching image from Reddit: ", err)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if response.StatusCode != http.StatusOK {
|
||||||
|
l.Error("Error fetching image from Reddit: ", response.Status)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
imageDataBytes, err := io.ReadAll(response.Body)
|
||||||
|
if err != nil {
|
||||||
|
l.Error("Error reading image data: ", err)
|
||||||
|
_ = response.Body.Close()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
_ = response.Body.Close()
|
||||||
|
|
||||||
|
imageData := string(imageDataBytes)
|
||||||
|
resized, err := resize.Shrink(imageData, uint(size), 70)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
l.Error("Error resizing image: ", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = rc.Set(rc.GetCacheKey(nsfw), resized, 10*time.Minute)
|
||||||
|
if err != nil {
|
||||||
|
l.Warn("could not cache image")
|
||||||
|
}
|
||||||
|
|
||||||
|
l.Info("Reddit image fetched and resized successfully, cached for 10 minutes.")
|
||||||
|
}
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ type contextKey string
|
|||||||
const redisKey contextKey = "redisClient"
|
const redisKey contextKey = "redisClient"
|
||||||
|
|
||||||
const CacheKey = "top-wallpaper:latestImage"
|
const CacheKey = "top-wallpaper:latestImage"
|
||||||
|
const NsfwCacheKey = "top-wallpaper-nsfw:latestImage"
|
||||||
|
|
||||||
type Cache interface {
|
type Cache interface {
|
||||||
Get(key string) (string, error)
|
Get(key string) (string, error)
|
||||||
@@ -92,6 +93,14 @@ func (r *Redis) Set(key string, value string, expiration time.Duration) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (*Redis) GetCacheKey(nsfw bool) string {
|
||||||
|
if nsfw {
|
||||||
|
return NsfwCacheKey
|
||||||
|
}
|
||||||
|
|
||||||
|
return CacheKey
|
||||||
|
}
|
||||||
|
|
||||||
func (r *Redis) Close() error {
|
func (r *Redis) Close() error {
|
||||||
if r.client == nil {
|
if r.client == nil {
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
Reference in New Issue
Block a user