Added a request-url-template option for Reddit requests

Reddit blocks known datacenter IPs from accessing their endpoints unless you request them to unblock it. Even so it has a low chance of success.

The new option will allow users to specify a request URL to handle Reddit requests.
This commit is contained in:
Jaryl Chng
2024-04-30 09:21:33 +08:00
parent eb85a7656f
commit 24ef8f6e2a
3 changed files with 28 additions and 3 deletions

View File

@@ -4,6 +4,7 @@ import (
"context"
"errors"
"html/template"
"strings"
"time"
"github.com/glanceapp/glance/internal/assets"
@@ -19,6 +20,7 @@ type Reddit struct {
CommentsUrlTemplate string `yaml:"comments-url-template"`
Limit int `yaml:"limit"`
CollapseAfter int `yaml:"collapse-after"`
RequestUrlTemplate string `yaml:"request-url-template"`
}
func (widget *Reddit) Initialize() error {
@@ -34,13 +36,19 @@ func (widget *Reddit) Initialize() error {
widget.CollapseAfter = 5
}
if widget.RequestUrlTemplate != "" {
if !strings.Contains(widget.RequestUrlTemplate, "{REQUEST-URL}") {
return errors.New("no `{REQUEST-URL}` placeholder specified")
}
}
widget.withTitle("/r/" + widget.Subreddit).withCacheDuration(30 * time.Minute)
return nil
}
func (widget *Reddit) Update(ctx context.Context) {
posts, err := feed.FetchSubredditPosts(widget.Subreddit, widget.CommentsUrlTemplate)
posts, err := feed.FetchSubredditPosts(widget.Subreddit, widget.CommentsUrlTemplate, widget.RequestUrlTemplate)
if !widget.canContinueUpdateAfterHandlingErr(err) {
return