Allow specifying sort order and tags through config

This commit is contained in:
Svilen Markov 2024-06-02 18:08:29 +01:00
parent 3aa1f273b6
commit 720549ec7e
2 changed files with 22 additions and 5 deletions

View File

@ -2,6 +2,7 @@ package feed
import ( import (
"net/http" "net/http"
"strings"
"time" "time"
) )
@ -54,7 +55,22 @@ func getLobstersPostsFromFeed(feedUrl string) (ForumPosts, error) {
return posts, nil return posts, nil
} }
func FetchLobstersTopPosts(feedUrl string) (ForumPosts, error) { func FetchLobstersPosts(sortBy string, tags []string) (ForumPosts, error) {
var feedUrl string
if sortBy == "hot" {
sortBy = "hottest"
} else if sortBy == "new" {
sortBy = "newest"
}
if len(tags) == 0 {
feedUrl = "https://lobste.rs/" + sortBy + ".json"
} else {
tags := strings.Join(tags, ",")
feedUrl = "https://lobste.rs/t/" + tags + ".json"
}
posts, err := getLobstersPostsFromFeed(feedUrl) posts, err := getLobstersPostsFromFeed(feedUrl)
if err != nil { if err != nil {

View File

@ -11,18 +11,19 @@ import (
type Lobsters struct { type Lobsters struct {
widgetBase `yaml:",inline"` widgetBase `yaml:",inline"`
FeedUrl string `yaml:"feed"`
Posts feed.ForumPosts `yaml:"-"` Posts feed.ForumPosts `yaml:"-"`
Limit int `yaml:"limit"` Limit int `yaml:"limit"`
CollapseAfter int `yaml:"collapse-after"` CollapseAfter int `yaml:"collapse-after"`
SortBy string `yaml:"sort-by"`
Tags []string `yaml:"tags"`
ShowThumbnails bool `yaml:"-"` ShowThumbnails bool `yaml:"-"`
} }
func (widget *Lobsters) Initialize() error { func (widget *Lobsters) Initialize() error {
widget.withTitle("Lobsters").withCacheDuration(30 * time.Minute) widget.withTitle("Lobsters").withCacheDuration(30 * time.Minute)
if widget.FeedUrl == "" { if widget.SortBy == "" || (widget.SortBy != "hot" && widget.SortBy != "new") {
widget.FeedUrl = "https://lobste.rs/hottest.json" widget.SortBy = "hot"
} }
if widget.Limit <= 0 { if widget.Limit <= 0 {
@ -37,7 +38,7 @@ func (widget *Lobsters) Initialize() error {
} }
func (widget *Lobsters) Update(ctx context.Context) { func (widget *Lobsters) Update(ctx context.Context) {
posts, err := feed.FetchLobstersTopPosts(widget.FeedUrl) posts, err := feed.FetchLobstersPosts(widget.SortBy, widget.Tags)
if !widget.canContinueUpdateAfterHandlingErr(err) { if !widget.canContinueUpdateAfterHandlingErr(err) {
return return