glance/internal/feed/lobsters.go

82 lines
1.7 KiB
Go
Raw Normal View History

2024-05-12 13:20:34 +02:00
package feed
import (
"net/http"
"strings"
2024-05-12 13:20:34 +02:00
"time"
)
type lobstersPostResponseJson struct {
2024-06-02 18:51:07 +02:00
CreatedAt string `json:"created_at"`
Title string `json:"title"`
URL string `json:"url"`
Score int `json:"score"`
CommentCount int `json:"comment_count"`
CommentsURL string `json:"comments_url"`
Tags []string `json:"tags"`
2024-05-12 13:20:34 +02:00
}
2024-05-12 17:14:04 +02:00
type lobstersFeedResponseJson []lobstersPostResponseJson
2024-05-12 13:20:34 +02:00
2024-05-12 17:14:04 +02:00
func getLobstersPostsFromFeed(feedUrl string) (ForumPosts, error) {
request, err := http.NewRequest("GET", feedUrl, nil)
2024-05-12 13:20:34 +02:00
if err != nil {
2024-05-12 17:14:04 +02:00
return nil, err
2024-05-12 13:20:34 +02:00
}
2024-05-12 17:14:04 +02:00
feed, err := decodeJsonFromRequest[lobstersFeedResponseJson](defaultClient, request)
2024-05-12 13:20:34 +02:00
if err != nil {
return nil, err
}
2024-05-12 17:14:04 +02:00
posts := make(ForumPosts, 0, len(feed))
2024-05-12 13:20:34 +02:00
2024-05-12 17:14:04 +02:00
for i := range feed {
createdAt, _ := time.Parse(time.RFC3339, feed[i].CreatedAt)
2024-05-12 13:20:34 +02:00
posts = append(posts, ForumPost{
Title: feed[i].Title,
2024-05-12 17:14:04 +02:00
DiscussionUrl: feed[i].CommentsURL,
TargetUrl: feed[i].URL,
TargetUrlDomain: extractDomainFromUrl(feed[i].URL),
CommentCount: feed[i].CommentCount,
Score: feed[i].Score,
2024-05-12 13:20:34 +02:00
TimePosted: createdAt,
Tags: feed[i].Tags,
2024-05-12 13:20:34 +02:00
})
}
if len(posts) == 0 {
return nil, ErrNoContent
}
return posts, nil
}
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"
}
2024-05-12 17:14:04 +02:00
posts, err := getLobstersPostsFromFeed(feedUrl)
2024-05-12 13:20:34 +02:00
if err != nil {
return nil, err
}
2024-05-12 17:14:04 +02:00
return posts, nil
2024-05-12 13:20:34 +02:00
}