Files
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

182 lines
5.0 KiB
Go

// Package imvu fetches products' files from IMVU's CDN, which serves them to anyone: a revision's CFL at
// productdata/<product>/<revision>, and the files its _contents.json names at productdata/<product>/<revision>/<url>
// (t5de-hub's docs/imports.md, "Spike"). Every request waits its turn at a shared rate.
package imvu
import (
"context"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"strconv"
"strings"
"sync"
"time"
)
// CDNURL is IMVU's product files.
const CDNURL = "https://userimages-akm.imvu.com/productdata"
// Size limits on downloads: a CFL (compressed) and one file. Products are far smaller (the spike's largest was
// 0.29 MiB); the pack limits are 64 MiB a file, 256 MiB in all.
const (
MaxCFL = 256 << 20
MaxFile = 64 << 20
)
// Misses is how many missing revisions in a row end a probe: revisions are numbered from 1, without gaps so far.
const Misses = 3
// CDN is IMVU's CDN.
type CDN struct {
URL string // CDNURL, or a test server's
UserAgent string
HTTP *http.Client
Limiter *Limiter
}
// ErrMissing is a file the CDN doesn't have.
var ErrMissing = errors.New("not on IMVU's CDN")
// Exists says whether a product revision is on the CDN.
func (c *CDN) Exists(ctx context.Context, product int64, revision int) (bool, error) {
resp, err := c.request(ctx, http.MethodHead, c.revisionURL(product, revision))
if err != nil {
return false, err
}
resp.Body.Close()
switch {
case resp.StatusCode == http.StatusOK:
return true, nil
case resp.StatusCode == http.StatusNotFound || resp.StatusCode == http.StatusForbidden:
return false, nil
default:
return false, fmt.Errorf("IMVU's CDN answered %d for product %d revision %d", resp.StatusCode, product, revision)
}
}
// Latest probes revisions from `from` up until Misses in a row are missing, and returns the newest found (0: none).
// Probes returns how many requests that took.
func (c *CDN) Latest(ctx context.Context, product int64, from int) (latest, probes int, err error) {
if from < 1 {
from = 1
}
for n, miss := from, 0; miss < Misses; n++ {
probes++
ok, err := c.Exists(ctx, product, n)
if err != nil {
return 0, probes, err
}
if ok {
latest, miss = n, 0
} else {
miss++
}
}
return latest, probes, nil
}
// CFL downloads a product revision's CFL.
func (c *CDN) CFL(ctx context.Context, product int64, revision int) ([]byte, error) {
return c.get(ctx, c.revisionURL(product, revision), MaxCFL)
}
// File downloads a file of a product revision by the url its _contents.json gives (a name relative to the
// revision's folder).
func (c *CDN) File(ctx context.Context, product int64, revision int, name string) ([]byte, error) {
if name == "" || strings.ContainsAny(name, "/\\?#") || name == "." || name == ".." {
return nil, fmt.Errorf("_contents.json names a file %q outside the revision", name)
}
return c.get(ctx, c.revisionURL(product, revision)+"/"+url.PathEscape(name), MaxFile)
}
func (c *CDN) revisionURL(product int64, revision int) string {
return strings.TrimRight(c.URL, "/") + "/" + strconv.FormatInt(product, 10) + "/" + strconv.Itoa(revision)
}
func (c *CDN) get(ctx context.Context, u string, limit int64) ([]byte, error) {
resp, err := c.request(ctx, http.MethodGet, u)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusNotFound || resp.StatusCode == http.StatusForbidden {
return nil, fmt.Errorf("%s: %w", u, ErrMissing)
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("IMVU's CDN answered %d for %s", resp.StatusCode, u)
}
data, err := io.ReadAll(io.LimitReader(resp.Body, limit+1))
if err != nil {
return nil, fmt.Errorf("%s: %w", u, err)
}
if int64(len(data)) > limit {
return nil, fmt.Errorf("%s is over %d bytes", u, limit)
}
return data, nil
}
func (c *CDN) request(ctx context.Context, method, u string) (*http.Response, error) {
if err := c.Limiter.Wait(ctx); err != nil {
return nil, err
}
req, err := http.NewRequestWithContext(ctx, method, u, nil)
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", c.UserAgent)
client := c.HTTP
if client == nil {
client = http.DefaultClient
}
return client.Do(req)
}
// Limiter spaces requests out to a rate (a second), shared by every job; the rate can change while it runs.
type Limiter struct {
mu sync.Mutex
gap time.Duration
next time.Time
}
// NewLimiter makes a limiter for rate requests a second.
func NewLimiter(rate float64) *Limiter {
l := &Limiter{}
l.SetRate(rate)
return l
}
// SetRate changes the rate, in requests a second (at least one a minute).
func (l *Limiter) SetRate(rate float64) {
if rate < 1.0/60 {
rate = 1.0 / 60
}
l.mu.Lock()
l.gap = time.Duration(float64(time.Second) / rate)
l.mu.Unlock()
}
// Wait waits for the next request's turn.
func (l *Limiter) Wait(ctx context.Context) error {
l.mu.Lock()
now := time.Now()
at := l.next
if at.Before(now) {
at = now
}
l.next = at.Add(l.gap)
l.mu.Unlock()
if d := time.Until(at); d > 0 {
t := time.NewTimer(d)
defer t.Stop()
select {
case <-t.C:
case <-ctx.Done():
return ctx.Err()
}
}
return nil
}