Prechádzať zdrojové kódy

Add more refinement options for HN widget

Svilen Markov 1 rok pred
rodič
commit
82f51293df

+ 10 - 0
docs/configuration.md

@@ -508,6 +508,8 @@ Preview:
 | limit | integer | no | 15 |
 | limit | integer | no | 15 |
 | collapse-after | integer | no | 5 |
 | collapse-after | integer | no | 5 |
 | comments-url-template | string | no | https://news.ycombinator.com/item?id={POST-ID} |
 | comments-url-template | string | no | https://news.ycombinator.com/item?id={POST-ID} |
+| sort-by | string | no | top |
+| extra-sort-by | string | no | |
 
 
 ##### `comments-url-template`
 ##### `comments-url-template`
 Used to replace the default link for post comments. Useful if you want to use an alternative front-end. Example:
 Used to replace the default link for post comments. Useful if you want to use an alternative front-end. Example:
@@ -520,6 +522,14 @@ Placeholders:
 
 
 `{POST-ID}` - the ID of the post
 `{POST-ID}` - the ID of the post
 
 
+##### `sort-by`
+Used to specify the order in which the posts should get returned. Possible values are `top`, `new`, and `best`.
+
+##### `extra-sort-by`
+Can be used to specify an additional sort which will be applied on top of the already sorted posts. By default does not apply any extra sorting and the only available option is `engagement`.
+
+The `engagement` sort tries to place the posts with the most points and comments on top, also prioritizing recent over old posts.
+
 ### Reddit
 ### Reddit
 Display a list of posts from a specific subreddit.
 Display a list of posts from a specific subreddit.
 
 

+ 4 - 4
internal/feed/hacker-news.go

@@ -18,8 +18,8 @@ type hackerNewsPostResponseJson struct {
 	TimePosted   int64  `json:"time"`
 	TimePosted   int64  `json:"time"`
 }
 }
 
 
-func getHackerNewsTopPostIds() ([]int, error) {
-	request, _ := http.NewRequest("GET", "https://hacker-news.firebaseio.com/v0/topstories.json", nil)
+func getHackerNewsPostIds(sort string) ([]int, error) {
+	request, _ := http.NewRequest("GET", fmt.Sprintf("https://hacker-news.firebaseio.com/v0/%sstories.json", sort), nil)
 	response, err := decodeJsonFromRequest[[]int](defaultClient, request)
 	response, err := decodeJsonFromRequest[[]int](defaultClient, request)
 
 
 	if err != nil {
 	if err != nil {
@@ -83,8 +83,8 @@ func getHackerNewsPostsFromIds(postIds []int, commentsUrlTemplate string) (Forum
 	return posts, nil
 	return posts, nil
 }
 }
 
 
-func FetchHackerNewsTopPosts(limit int, commentsUrlTemplate string) (ForumPosts, error) {
-	postIds, err := getHackerNewsTopPostIds()
+func FetchHackerNewsPosts(sort string, limit int, commentsUrlTemplate string) (ForumPosts, error) {
+	postIds, err := getHackerNewsPostIds(sort)
 
 
 	if err != nil {
 	if err != nil {
 		return nil, err
 		return nil, err

+ 11 - 3
internal/widget/hacker-news.go

@@ -13,6 +13,8 @@ type HackerNews struct {
 	widgetBase          `yaml:",inline"`
 	widgetBase          `yaml:",inline"`
 	Posts               feed.ForumPosts `yaml:"-"`
 	Posts               feed.ForumPosts `yaml:"-"`
 	Limit               int             `yaml:"limit"`
 	Limit               int             `yaml:"limit"`
+	SortBy              string          `yaml:"sort-by"`
+	ExtraSortBy         string          `yaml:"extra-sort-by"`
 	CollapseAfter       int             `yaml:"collapse-after"`
 	CollapseAfter       int             `yaml:"collapse-after"`
 	CommentsUrlTemplate string          `yaml:"comments-url-template"`
 	CommentsUrlTemplate string          `yaml:"comments-url-template"`
 	ShowThumbnails      bool            `yaml:"-"`
 	ShowThumbnails      bool            `yaml:"-"`
@@ -29,18 +31,24 @@ func (widget *HackerNews) Initialize() error {
 		widget.CollapseAfter = 5
 		widget.CollapseAfter = 5
 	}
 	}
 
 
+	if widget.SortBy != "top" && widget.SortBy != "new" && widget.SortBy != "best" {
+		widget.SortBy = "top"
+	}
+
 	return nil
 	return nil
 }
 }
 
 
 func (widget *HackerNews) Update(ctx context.Context) {
 func (widget *HackerNews) Update(ctx context.Context) {
-	posts, err := feed.FetchHackerNewsTopPosts(40, widget.CommentsUrlTemplate)
+	posts, err := feed.FetchHackerNewsPosts(widget.SortBy, 40, widget.CommentsUrlTemplate)
 
 
 	if !widget.canContinueUpdateAfterHandlingErr(err) {
 	if !widget.canContinueUpdateAfterHandlingErr(err) {
 		return
 		return
 	}
 	}
 
 
-	posts.CalculateEngagement()
-	posts.SortByEngagement()
+	if widget.ExtraSortBy == "engagement" {
+		posts.CalculateEngagement()
+		posts.SortByEngagement()
+	}
 
 
 	if widget.Limit < len(posts) {
 	if widget.Limit < len(posts) {
 		posts = posts[:widget.Limit]
 		posts = posts[:widget.Limit]