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]>
This commit is contained in:
1 parent
2b0d27831e
commit
e8578e4a71
26 files changed
+2345
-1
No files matched your search
@@ -0,0 +1,181 @@
|
||||
// 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
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package imvu_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.t5de.dev/t5de/t5de-importer/internal/imvu"
|
||||
"git.t5de.dev/t5de/t5de-importer/internal/imvutest"
|
||||
)
|
||||
|
||||
func TestLatest(t *testing.T) {
|
||||
cdn := imvutest.New(t)
|
||||
for _, rev := range []int{1, 2, 3, 5} { // 4 missing: a gap shorter than Misses is crossed
|
||||
cdn.Add(7, rev, imvutest.File{Name: "index.xml", Data: imvutest.Index(1)})
|
||||
}
|
||||
c := &imvu.CDN{URL: cdn.URL, Limiter: imvu.NewLimiter(1000)}
|
||||
ctx := context.Background()
|
||||
for _, tc := range []struct{ from, latest, probes int }{{0, 5, 8}, {1, 5, 8}, {4, 5, 5}, {6, 0, 3}} {
|
||||
latest, probes, err := c.Latest(ctx, 7, tc.from)
|
||||
if err != nil || latest != tc.latest || probes != tc.probes {
|
||||
t.Errorf("from %d: latest %d, %d probes, %v; want %d, %d", tc.from, latest, probes, err, tc.latest, tc.probes)
|
||||
}
|
||||
}
|
||||
if _, err := c.CFL(ctx, 7, 4); !errors.Is(err, imvu.ErrMissing) {
|
||||
t.Errorf("missing revision: %v", err)
|
||||
}
|
||||
if _, err := c.File(ctx, 7, 1, "../../8/1"); err == nil {
|
||||
t.Error("a file outside the revision: no error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLimiter(t *testing.T) {
|
||||
l := imvu.NewLimiter(50) // 20 ms apart
|
||||
start := time.Now()
|
||||
for range 6 {
|
||||
if err := l.Wait(context.Background()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if d := time.Since(start); d < 90*time.Millisecond {
|
||||
t.Errorf("6 requests at 50 a second took %s", d)
|
||||
}
|
||||
l.SetRate(0.001) // a minute apart
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Millisecond)
|
||||
defer cancel()
|
||||
l.Wait(ctx) // the next turn is ~20 ms away
|
||||
if err := l.Wait(ctx); err == nil {
|
||||
t.Error("waited a minute within 20 ms")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user