mirror of
https://github.com/Xevion/glance.git
synced 2025-12-10 02:07:20 -06:00
Add more refinement options for HN widget
This commit is contained in:
@@ -18,8 +18,8 @@ type hackerNewsPostResponseJson struct {
|
||||
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)
|
||||
|
||||
if err != nil {
|
||||
@@ -83,8 +83,8 @@ func getHackerNewsPostsFromIds(postIds []int, commentsUrlTemplate string) (Forum
|
||||
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 {
|
||||
return nil, err
|
||||
|
||||
@@ -13,6 +13,8 @@ type HackerNews struct {
|
||||
widgetBase `yaml:",inline"`
|
||||
Posts feed.ForumPosts `yaml:"-"`
|
||||
Limit int `yaml:"limit"`
|
||||
SortBy string `yaml:"sort-by"`
|
||||
ExtraSortBy string `yaml:"extra-sort-by"`
|
||||
CollapseAfter int `yaml:"collapse-after"`
|
||||
CommentsUrlTemplate string `yaml:"comments-url-template"`
|
||||
ShowThumbnails bool `yaml:"-"`
|
||||
@@ -29,18 +31,24 @@ func (widget *HackerNews) Initialize() error {
|
||||
widget.CollapseAfter = 5
|
||||
}
|
||||
|
||||
if widget.SortBy != "top" && widget.SortBy != "new" && widget.SortBy != "best" {
|
||||
widget.SortBy = "top"
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
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) {
|
||||
return
|
||||
}
|
||||
|
||||
posts.CalculateEngagement()
|
||||
posts.SortByEngagement()
|
||||
if widget.ExtraSortBy == "engagement" {
|
||||
posts.CalculateEngagement()
|
||||
posts.SortByEngagement()
|
||||
}
|
||||
|
||||
if widget.Limit < len(posts) {
|
||||
posts = posts[:widget.Limit]
|
||||
|
||||
Reference in New Issue
Block a user