Files
top-wallpaper/reddit/fetcher.go
Ron Rise ff66877192
All checks were successful
🚨 Test Code Base / 🔍 🐹 Go Tests (push) Successful in 1m22s
🚨 Test Code Base / 🧹 Lint (push) Successful in 1m31s
🏗️✨ Build Workflow / 🖥️ 🔨 Build (push) Successful in 11m11s
update-deps (#1)
Reviewed-on: #1
Co-authored-by: Ron Rise <ron@siteworxpro.com>
Co-committed-by: Ron Rise <ron@siteworxpro.com>
2025-08-26 16:36:17 +00:00

149 lines
2.9 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:
val, err := rc.Get(redis.CacheKey)
if err != nil {
l.Error("Error fetching from Redis: ", err)
break
}
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)
}
}