tstor/daemons/kemono/fetch.go

70 lines
1.4 KiB
Go
Raw Normal View History

2024-11-24 17:32:26 +00:00
package kemono
import (
"context"
"encoding/json"
"github.com/carlmjohnson/requests"
)
// FetchCreators fetch Creator list
func (k *kemono) 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 *kemono) FetchPosts(ctx context.Context, service, id string) (posts []Post, err error) {
const perUnit = 50
page := 0
for {
k.log.Printf("fetching post list page %d...", page)
var ps []json.RawMessage
err = requests.URL(k.Site).
Pathf("/api/v1/%s/user/%s", service, id).
ParamInt("o", page*perUnit).
Client(k.client).
CheckStatus(200).
ToJSON(&ps).
Fetch(ctx)
if err != nil {
return nil, err
}
if len(ps) == 0 {
break
}
for _, raw := range ps {
var p Post
err := json.Unmarshal(raw, &p)
if err != nil {
return posts, nil
}
}
}
return posts, nil
}