Files
t5de-importer/internal/hub/hub.go
T
dhkatzandClaude Opus 5.5 e8578e4a71
Pull request / title (pull_request) Successful in 1s
CI / build (pull_request) Successful in 31s
CI / image (pull_request) Skipped
feat: import IMVU products into a catalog
The importer from t5de-hub's docs/imports.md: says hello to the hub with its key pair, follows the console's config
(paused, rate, jobs at a time, IMVU accounts, whose sealed passwords it keeps), claims jobs, finds each product's
newest revision on IMVU's CDN, converts it (originals for placeholders, no _contents.json, ProductRating from IMVU's
rating), checks it by t5de-api's pack rules and uploads it to the catalog with an import token.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-08 06:04:48 -08:00

189 lines
5.7 KiB
Go

// Package hub talks to T5DE's hub as a catalog's importer: t5de-hub's docs/products.md ("For importers") and
// docs/imports.md. Every request carries `Authorization: Importer <catalog ID>:<key>`.
package hub
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strings"
)
// Client is one catalog's importer at a hub.
type Client struct {
url string
auth string
userAgent string
http *http.Client
}
// New makes a client for the hub at hubURL (its base, without /api/v1).
func New(hubURL, catalog, key, userAgent string, client *http.Client) *Client {
if client == nil {
client = http.DefaultClient
}
return &Client{
url: strings.TrimRight(hubURL, "/"),
auth: "Importer " + catalog + ":" + key,
userAgent: userAgent,
http: client,
}
}
// Error is the hub's refusal: its status and `{error, message}`.
type Error struct {
Status int
Code string
Message string
}
func (e *Error) Error() string {
if e.Message != "" {
return fmt.Sprintf("hub: %d %s: %s", e.Status, e.Code, e.Message)
}
return fmt.Sprintf("hub: %d %s", e.Status, e.Code)
}
// Settings are the importer's settings from the console.
type Settings struct {
Rate float64 `json:"rate"` // requests a second to IMVU, 0.2-10
Concurrency int `json:"concurrency"` // jobs at once, 1-8
}
// Account is an IMVU account the catalog's team added. Sealed is set until the importer says it has it.
type Account struct {
ID string `json:"id"`
IMVUID int64 `json:"imvuId"`
Username string `json:"username"`
Sealed *string `json:"sealed"`
}
// Config is what the importer runs with.
type Config struct {
Paused bool `json:"paused"`
Settings Settings `json:"settings"`
Fingerprint string `json:"fingerprint"`
Accounts []Account `json:"accounts"`
}
// Job is a claimed import job. Rating is IMVU's ("general", "adult"), or "" when the hub couldn't see it.
type Job struct {
ID string `json:"id"`
Kind string `json:"kind"` // "import" or "recheck"
IMVUProduct int64 `json:"imvuProduct"`
LastRevision int `json:"lastRevision"` // the newest IMVU revision imported already, or 0
Rating string `json:"rating"`
}
// Hello sends the importer's version and public key (base64 SPKI) and returns its config.
func (c *Client) Hello(ctx context.Context, version, publicKey string) (*Config, error) {
var cfg Config
err := c.do(ctx, http.MethodPost, "/api/v1/importer/hello", map[string]string{"version": version, "publicKey": publicKey}, &cfg)
return &cfg, err
}
// Config fetches the importer's config.
func (c *Client) Config(ctx context.Context) (*Config, error) {
var cfg Config
err := c.do(ctx, http.MethodGet, "/api/v1/importer/config", nil, &cfg)
return &cfg, err
}
// Claim claims up to limit (1-20) queued jobs.
func (c *Client) Claim(ctx context.Context, limit int) ([]Job, error) {
var r struct {
Jobs []Job `json:"jobs"`
}
err := c.do(ctx, http.MethodPost, "/api/v1/importer/jobs/claim", map[string]int{"limit": limit}, &r)
return r.Jobs, err
}
// Token gets a t5de-import token for a claimed job's IMVU revision, and the catalog's upload URL.
func (c *Client) Token(ctx context.Context, job string, imvuRevision int) (token, uploadURL string, err error) {
var r struct {
Token string `json:"token"`
URL string `json:"url"`
}
err = c.do(ctx, http.MethodPost, "/api/v1/importer/jobs/"+url.PathEscape(job)+"/token", map[string]int{"imvuRevision": imvuRevision}, &r)
return r.Token, r.URL, err
}
// Outcome is a job's outcome, for Report.
type Outcome struct {
Status string `json:"status"` // "done", "unchanged" or "failed"
IMVURevision int `json:"imvuRevision,omitempty"`
Reason string `json:"reason,omitempty"`
}
// Report reports a job's outcome. (An upload the catalog registered marks its job done already.)
func (c *Client) Report(ctx context.Context, job string, o Outcome) error {
if len(o.Reason) > 2000 {
o.Reason = o.Reason[:2000]
}
return c.do(ctx, http.MethodPost, "/api/v1/importer/jobs/"+url.PathEscape(job), o, nil)
}
// AccountReport is what the importer says about an account.
type AccountReport struct {
Received bool `json:"received,omitempty"`
State string `json:"state,omitempty"` // "ok", "sign_in_failed" or "needs_password"
AccessPass *bool `json:"accessPass,omitempty"`
Message string `json:"message,omitempty"`
}
// ReportAccount reports on an account: that the importer has its password, then how signing in went.
func (c *Client) ReportAccount(ctx context.Context, id string, r AccountReport) error {
return c.do(ctx, http.MethodPost, "/api/v1/importer/accounts/"+url.PathEscape(id), r, nil)
}
func (c *Client) do(ctx context.Context, method, path string, in, out any) error {
var body io.Reader
if in != nil {
b, err := json.Marshal(in)
if err != nil {
return err
}
body = bytes.NewReader(b)
}
req, err := http.NewRequestWithContext(ctx, method, c.url+path, body)
if err != nil {
return err
}
req.Header.Set("Authorization", c.auth)
req.Header.Set("User-Agent", c.userAgent)
if in != nil {
req.Header.Set("Content-Type", "application/json")
}
resp, err := c.http.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
data, err := io.ReadAll(io.LimitReader(resp.Body, 4<<20))
if err != nil {
return err
}
if resp.StatusCode/100 != 2 {
e := &Error{Status: resp.StatusCode}
var r struct {
Error string `json:"error"`
Message string `json:"message"`
}
if json.Unmarshal(data, &r) == nil {
e.Code, e.Message = r.Error, r.Message
}
return e
}
if out == nil {
return nil
}
if err := json.Unmarshal(data, out); err != nil {
return fmt.Errorf("hub: %s %s: %w", method, path, err)
}
return nil
}