Browse Source

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.
Jaryl Chng 1 year ago
parent
commit
24ef8f6e2a
3 changed files with 28 additions and 3 deletions
  1. 13 0
      docs/configuration.md
  2. 6 2
      internal/feed/reddit.go
  3. 9 1
      internal/widget/reddit.go

+ 13 - 0
docs/configuration.md

@@ -515,6 +515,7 @@ Example:
 | limit | integer | no | 15 |
 | limit | integer | no | 15 |
 | collapse-after | integer | no | 5 |
 | collapse-after | integer | no | 5 |
 | comments-url-template | string | no | https://www.reddit.com/{POST-PATH} |
 | comments-url-template | string | no | https://www.reddit.com/{POST-PATH} |
+| request-url-template | string | no |  |
 
 
 ##### `subreddit`
 ##### `subreddit`
 The subreddit for which to fetch the posts from.
 The subreddit for which to fetch the posts from.
@@ -568,6 +569,18 @@ r/selfhosted/comments/bsp01i/welcome_to_rselfhosted_please_read_this_first/
 
 
 `{SUBREDDIT}` - the subreddit name
 `{SUBREDDIT}` - the subreddit name
 
 
+##### `request-url-template`
+A custom request url that will be used to fetch the data instead. This is useful when you're hosting Glance on a VPS and Reddit is blocking the requests, and you want to route it through an HTTP proxy.
+
+Placeholders:
+
+`{REQUEST-URL}` - will be templated and replaced with the expanded request URL (i.e. https://www.reddit.com/r/selfhosted/hot.json). Example:
+
+```
+https://proxy/{REQUEST-URL}
+https://your.proxy/?url={REQUEST-URL}
+```
+
 ### Weather
 ### Weather
 Display weather information for a specific location. The data is provided by https://open-meteo.com/.
 Display weather information for a specific location. The data is provided by https://open-meteo.com/.
 
 

+ 6 - 2
internal/feed/reddit.go

@@ -30,8 +30,12 @@ type subredditResponseJson struct {
 	} `json:"data"`
 	} `json:"data"`
 }
 }
 
 
-func FetchSubredditPosts(subreddit string, commentsUrlTemplate string) (ForumPosts, error) {
-	requestUrl := fmt.Sprintf("https://www.reddit.com/r/%s/hot.json", url.QueryEscape(subreddit))
+func FetchSubredditPosts(subreddit string, commentsUrlTemplate string, requestUrlTemplate string) (ForumPosts, error) {
+	subreddit = url.QueryEscape(subreddit)
+	requestUrl := fmt.Sprintf("https://www.reddit.com/r/%s/hot.json", subreddit)
+	if requestUrlTemplate != "" {
+		requestUrl = strings.ReplaceAll(requestUrlTemplate, "{REQUEST-URL}", requestUrl)
+	}
 	request, err := http.NewRequest("GET", requestUrl, nil)
 	request, err := http.NewRequest("GET", requestUrl, nil)
 
 
 	if err != nil {
 	if err != nil {

+ 9 - 1
internal/widget/reddit.go

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