Files
top-wallpaper/reddit/fetcher.go
Ron Rise 14999e412a
All checks were successful
🚨 Test Code Base / 🧹 Lint (push) Successful in 58s
🚨 Test Code Base / 🔍 🐹 Go Tests (push) Successful in 1m10s
🏗️✨ Build Workflow / 🖥️ 🔨 Build (push) Successful in 4m24s
Simplify httpGet function in client_test.go for cleaner code
2026-01-21 22:35:39 -05:00

165 lines
3.1 KiB
Go

package reddit
import (
"context"
"io"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"time"
"github.com/labstack/echo/v4"
"github.com/siteworxpro/top-wallpaper/redis"
"github.com/siteworxpro/top-wallpaper/resize"
)
func Fetch(ctx context.Context) {
const defaultImageSize = 1600
const minImageSize = 100
rc, err := redis.FromContext(ctx)
if err != nil {
panic("Redis client not found in context: " + err.Error())
}
l := ctx.Value("logger").(echo.Logger)
var size int
if sizeS, ok := os.LookupEnv("MAX_SIZE"); ok {
size, err = strconv.Atoi(sizeS)
if err != nil {
size = defaultImageSize
}
} else {
size = defaultImageSize
}
if size < minImageSize {
size = defaultImageSize
}
for {
select {
case <-ctx.Done():
l.Info("Stopping Reddit fetcher...")
_ = rc.Close()
return
default:
doFetch(rc, l, false, size)
if os.Getenv("NSFW_MODE") == "true" {
doFetch(rc, l, true, size)
}
}
time.Sleep(10 * time.Minute)
}
}
func doFetch(rc *redis.Redis, l echo.Logger, nsfw bool, size int) {
val, err := rc.Get(rc.GetCacheKey(nsfw))
l.Info("Checking cache for Reddit image...", " nsfw=", nsfw)
if err != nil {
l.Error("Error fetching from Redis: ", err)
return
}
if val != "" {
l.Info("Reddit image is in cache... skipping...")
return
}
l.Info("Fetching latest image from Reddit...")
latestImageVal, err := GetLatestImage(func() (*http.Response, error) {
jsonUrl := wallpaperURL
if nsfw {
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.")
}