tstor/plugins/kemono/kemonoapi/fetch.go
2025-03-22 08:49:14 +04:00

91 lines
2 KiB
Go

package kemonoapi
import (
"context"
"encoding/json"
"fmt"
"iter"
"log/slog"
"strconv"
)
// FetchCreators fetch Creator list
func (k *Client) FetchCreators() (creators []Creator, err error) {
// k.log.Print("fetching creator list...")
// url := fmt.Sprintf("https://%s/api/v1/creators", k.Site)
// resp, err := k.Downloader.Get(url)
// if err != nil {
// return nil, fmt.Errorf("fetch creator list error: %s", err)
// }
// reader, err := handleCompressedHTTPResponse(resp)
// if err != nil {
// return nil, err
// }
// data, err := ioutil.ReadAll(reader)
// if err != nil {
// return nil, fmt.Errorf("fetch creator list error: %s", err)
// }
// err = json.Unmarshal(data, &creators)
// if err != nil {
// return nil, fmt.Errorf("unmarshal creator list error: %s", err)
// }
return
}
// FetchPosts fetch post list
func (k *Client) FetchPosts(ctx context.Context, service, creator_id string) iter.Seq2[Post, error] {
const perUnit = 50
return func(yield func(Post, error) bool) {
page := 0
for {
k.log.Info("fetching post list", slog.Int("page", page))
if err := k.ratelimit.Wait(ctx); err != nil {
yield(Post{}, err)
return
}
posts, err := k.fetchPostsPage(ctx, service, creator_id, page*perUnit)
if err != nil {
yield(Post{}, err)
return
}
if len(posts) == 0 {
break
}
for _, post := range posts {
if !yield(post, nil) {
return
}
}
page++
}
}
}
func (k *Client) fetchPostsPage(ctx context.Context, service, creator_id string, offset int) ([]Post, error) {
resp, err := k.client.R().
SetContext(ctx).
SetQueryParam("o", strconv.Itoa(offset)).
SetPathParam("service", service).
SetPathParam("creator_id", creator_id).
Get("/{service}/user/{creator_id}")
if err != nil {
return nil, fmt.Errorf("fetch post list error: %s", err)
}
var posts []Post
err = json.Unmarshal(resp.Body(), &posts)
if err != nil {
return nil, fmt.Errorf("unmarshal post list error: %s", err)
}
return posts, nil
}