2024-05-12 13:20:34 +02:00
|
|
|
package feed
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type lobstersPostResponseJson struct {
|
|
|
|
ShortID string `json:"short_id"`
|
|
|
|
ShortIDURL string `json:"short_id_url"`
|
|
|
|
CreatedAt string `json:"created_at"`
|
|
|
|
Title string `json:"title"`
|
|
|
|
URL string `json:"url"`
|
|
|
|
Score int `json:"score"`
|
|
|
|
Flags int `json:"flags"`
|
|
|
|
CommentCount int `json:"comment_count"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
DescriptionPlain string `json:"description_plain"`
|
|
|
|
CommentsURL string `json:"comments_url"`
|
|
|
|
SubmitterUser string `json:"submitter_user"`
|
|
|
|
UserIsAuthor bool `json:"user_is_author"`
|
|
|
|
Tags []string `json:"tags"`
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
tags := strings.Join(feed[i].Tags, ",")
|
2024-05-12 13:20:34 +02:00
|
|
|
|
|
|
|
if tags != "" {
|
|
|
|
tags = " [" + tags + "]"
|
|
|
|
}
|
|
|
|
|
2024-05-12 17:14:04 +02:00
|
|
|
createdAt, _ := time.Parse(time.RFC3339, feed[i].CreatedAt)
|
2024-05-12 13:20:34 +02:00
|
|
|
|
|
|
|
posts = append(posts, ForumPost{
|
2024-05-12 17:14:04 +02:00
|
|
|
Title: feed[i].Title + tags,
|
|
|
|
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,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(posts) == 0 {
|
|
|
|
return nil, ErrNoContent
|
|
|
|
}
|
|
|
|
|
|
|
|
return posts, nil
|
|
|
|
}
|
|
|
|
|
2024-05-12 17:14:04 +02:00
|
|
|
func FetchLobstersTopPosts(feedUrl string) (ForumPosts, error) {
|
|
|
|
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
|
|
|
}
|