🐛 Trying to fix mastodon timeline fetching
This commit is contained in:
parent
7e5d75998d
commit
d28669f527
@ -35,7 +35,7 @@ func ReadFriendConfig() {
|
|||||||
func FetchFediversePost(cfg FediverseFriendConfig) ([]models.FediversePost, error) {
|
func FetchFediversePost(cfg FediverseFriendConfig) ([]models.FediversePost, error) {
|
||||||
switch cfg.Type {
|
switch cfg.Type {
|
||||||
case "mastodon":
|
case "mastodon":
|
||||||
data, err := mastodon.FetchTimeline(cfg.URL, cfg.BatchSize)
|
data, err := mastodon.FetchTimeline(cfg.URL, 50)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -2,13 +2,16 @@ package mastodon
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"git.solsynth.dev/hypernet/interactive/pkg/internal/models"
|
"git.solsynth.dev/hypernet/interactive/pkg/internal/models"
|
||||||
"git.solsynth.dev/hypernet/nexus/pkg/nex/cruda"
|
"git.solsynth.dev/hypernet/nexus/pkg/nex/cruda"
|
||||||
|
"github.com/gofiber/fiber/v2"
|
||||||
jsoniter "github.com/json-iterator/go"
|
jsoniter "github.com/json-iterator/go"
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
"github.com/samber/lo"
|
"github.com/samber/lo"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -56,6 +59,7 @@ func (v MastodonPost) ToFediversePost() models.FediversePost {
|
|||||||
|
|
||||||
func FetchTimeline(server string, limit int) ([]MastodonPost, error) {
|
func FetchTimeline(server string, limit int) ([]MastodonPost, error) {
|
||||||
url := fmt.Sprintf("%s/api/v1/timelines/public?limit=%d", server, limit)
|
url := fmt.Sprintf("%s/api/v1/timelines/public?limit=%d", server, limit)
|
||||||
|
log.Debug().Str("url", url).Msg("Fetching mastodon timeline...")
|
||||||
|
|
||||||
resp, err := http.Get(url)
|
resp, err := http.Get(url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -63,8 +67,19 @@ func FetchTimeline(server string, limit int) ([]MastodonPost, error) {
|
|||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
body, err := io.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to read response body: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if resp.StatusCode != fiber.StatusOK {
|
||||||
|
return nil, fmt.Errorf("unexpected status code: %d, response: %s", resp.StatusCode, body)
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Debug().Str("url", url).Bytes("response", body).Msg("Fetched mastodon timeline...")
|
||||||
|
|
||||||
var posts []MastodonPost
|
var posts []MastodonPost
|
||||||
if err := jsoniter.NewDecoder(resp.Body).Decode(&posts); err != nil {
|
if err := jsoniter.Unmarshal(body, &posts); err != nil {
|
||||||
return nil, fmt.Errorf("failed to parse timeline JSON: %v", err)
|
return nil, fmt.Errorf("failed to parse timeline JSON: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user