feat: import IMVU products into a catalog
Pull request / title (pull_request) Successful in 1s
CI / build (pull_request) Successful in 31s
CI / image (pull_request) Skipped

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:
dhkatzandClaude Opus 5.5 committed 2026-11-08 06:04:48 -08:00
1 parent 2b0d27831e
commit e8578e4a71
26 files changed
+2345 -1

No files matched your search

+167
View File
@@ -0,0 +1,167 @@
// Package convert turns an IMVU product revision, as IMVU's CDN serves it, into a T5 product's files (t5de-hub's
// docs/imports.md, "What's imported"): the CFL's entries, with the small placeholder textures replaced by the
// originals its _contents.json names (and files kept only on the CDN fetched), without _contents.json (packs don't
// use it), and with index.xml's ProductRating set. The parent stays as index.xml names it (__DATAIMPORT).
package convert
import (
"archive/zip"
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
"regexp"
"slices"
"strconv"
"git.t5de.dev/t5de/t5de-api/cfl"
"git.t5de.dev/t5de/t5de-api/pack"
)
// ContentsName is the file listing a revision's files on the CDN.
const ContentsName = "_contents.json"
// File is one of a product's files.
type File struct {
Name string
Data []byte
}
// content is one _contents.json entry. Originals (tag "original") and files kept only on the CDN have a url.
type content struct {
Name string `json:"name"`
URL string `json:"url"`
Tags []string `json:"tags"`
}
// Fetch downloads a file of the revision by its _contents.json url.
type Fetch func(ctx context.Context, url string) ([]byte, error)
// Files converts a revision's CFL entries. rating is IMVU's ("general" or "adult"); the files say AP if either
// IMVU or the files themselves do.
func Files(ctx context.Context, entries []cfl.Entry, rating string, fetch Fetch) ([]File, error) {
if rating != "general" && rating != "adult" {
return nil, fmt.Errorf("unknown rating %q", rating)
}
files := make([]File, 0, len(entries))
at := map[string]int{}
var contents []content
for _, e := range entries {
if e.Name == ContentsName {
if err := json.Unmarshal(e.Data, &contents); err != nil {
return nil, fmt.Errorf("%s: %w", ContentsName, err)
}
continue
}
if _, dup := at[e.Name]; dup {
return nil, fmt.Errorf("the CFL has %q twice", e.Name)
}
at[e.Name] = len(files)
files = append(files, File{Name: e.Name, Data: e.Data})
}
for _, c := range contents {
if c.URL == "" || c.Name == ContentsName {
continue
}
i, inCFL := at[c.Name]
if inCFL && !slices.Contains(c.Tags, "original") {
continue
}
data, err := fetch(ctx, c.URL)
if err != nil {
return nil, fmt.Errorf("%s: %w", c.Name, err)
}
if inCFL {
files[i].Data = data
} else {
at[c.Name] = len(files)
files = append(files, File{Name: c.Name, Data: data})
}
}
i, ok := at[pack.IndexName]
if !ok {
return nil, errors.New("the product has no index.xml")
}
index, err := SetRating(files[i].Data, rating)
if err != nil {
return nil, err
}
files[i].Data = index
return files, nil
}
var (
ratingElement = regexp.MustCompile(`<ProductRating>\s*([A-Za-z]*)\s*</ProductRating>`)
templateOpen = regexp.MustCompile(`<Template(\s[^>]*)?>`)
)
// SetRating sets index.xml's ProductRating: AP if rating is "adult" or it says AP already, else GA. IMVU's files
// usually have none, and the hub rates an imported revision the higher of IMVU's rating and the files'.
func SetRating(index []byte, rating string) ([]byte, error) {
want := "GA"
if m := ratingElement.FindSubmatch(index); rating == "adult" || (m != nil && string(m[1]) == "AP") {
want = "AP"
}
element := []byte("<ProductRating>" + want + "</ProductRating>")
if loc := ratingElement.FindIndex(index); loc != nil {
return slices.Concat(index[:loc[0]], element, index[loc[1]:]), nil
}
loc := templateOpen.FindIndex(index)
if loc == nil {
return nil, errors.New("index.xml has no <Template>")
}
return slices.Concat(index[:loc[1]], []byte("\n"), element, index[loc[1]:]), nil
}
// Chkn zips the files at the root, as Create Mode's .chkn: what the catalog's upload takes.
func Chkn(files []File) ([]byte, error) {
var b bytes.Buffer
z := zip.NewWriter(&b)
for _, f := range files {
w, err := z.CreateHeader(&zip.FileHeader{Name: f.Name, Method: zip.Deflate})
if err != nil {
return nil, err
}
if _, err := w.Write(f.Data); err != nil {
return nil, err
}
}
if err := z.Close(); err != nil {
return nil, err
}
return b.Bytes(), nil
}
// Check checks a .chkn by t5de-api's pack rules, as the catalog will (names, limits, index.xml and its rating), so
// a product that breaks them fails here with the reason. tmp is a folder for the check's files.
func Check(chkn []byte, imvuProduct int64, rating string, tmp string) error {
dir, err := os.MkdirTemp(tmp, "check-")
if err != nil {
return err
}
defer os.RemoveAll(dir)
m := pack.Manifest{Format: 1, ID: "imvu-" + strconv.FormatInt(imvuProduct, 10), Revision: 1, Name: "IMVU product", Rating: rating}
b, err := json.Marshal(m)
if err != nil {
return err
}
if err := os.WriteFile(filepath.Join(dir, pack.ManifestName), b, 0o600); err != nil {
return err
}
if err := os.WriteFile(filepath.Join(dir, pack.ChknName), chkn, 0o600); err != nil {
return err
}
_, err = pack.Load(dir)
return err
}
// Rating is the pack rating ("general", "adult") that index.xml's ProductRating means.
func Rating(index []byte) string {
if m := ratingElement.FindSubmatch(index); m != nil && string(m[1]) == "AP" {
return "adult"
}
return "general"
}
+125
View File
@@ -0,0 +1,125 @@
package convert
import (
"archive/zip"
"bytes"
"context"
"errors"
"io"
"strings"
"testing"
"git.t5de.dev/t5de/t5de-api/cfl"
)
func TestSetRating(t *testing.T) {
for _, c := range []struct{ in, rating, want string }{
{"<Template>\n<X/></Template>", "general", "<Template>\n<ProductRating>GA</ProductRating>\n<X/></Template>"},
{"<Template>\n<X/></Template>", "adult", "<Template>\n<ProductRating>AP</ProductRating>\n<X/></Template>"},
{`<?xml version="1.0"?><Template a="b"></Template>`, "general", `<?xml version="1.0"?><Template a="b">` + "\n<ProductRating>GA</ProductRating></Template>"},
{"<Template><ProductRating>AP</ProductRating></Template>", "general", "<Template><ProductRating>AP</ProductRating></Template>"},
{"<Template><ProductRating> GA </ProductRating></Template>", "adult", "<Template><ProductRating>AP</ProductRating></Template>"},
{"<Template><ProductRating>GA</ProductRating></Template>", "general", "<Template><ProductRating>GA</ProductRating></Template>"},
} {
got, err := SetRating([]byte(c.in), c.rating)
if err != nil || string(got) != c.want {
t.Errorf("%q, %s: got %q, %v; want %q", c.in, c.rating, got, err, c.want)
}
}
if _, err := SetRating([]byte("<NotATemplate/>"), "general"); err == nil {
t.Error("no <Template>: no error")
}
}
func TestFiles(t *testing.T) {
contents := `[{"name":"tex.png","url":"abc.png","tags":["original","thumbnail"]},` +
`{"name":"mat.xrf","url":"def.xrf"},{"name":"cdn-only.xrf","url":"ghi.xrf"},{"name":"small.png","tags":["original"]},{"name":"index.xml"}]`
entries := []cfl.Entry{
{Name: "index.xml", Data: []byte("<Template>\n</Template>")},
{Name: "tex.png", Data: []byte("placeholder")},
{Name: "mat.xrf", Data: []byte("material")},
{Name: "small.png", Data: []byte("small original")},
{Name: "_contents.json", Data: []byte(contents)},
}
cdn := map[string]string{"abc.png": "full-size original", "ghi.xrf": "only on the CDN"}
var asked []string
fetch := func(_ context.Context, url string) ([]byte, error) {
asked = append(asked, url)
if s, ok := cdn[url]; ok {
return []byte(s), nil
}
return nil, errors.New("missing")
}
files, err := Files(context.Background(), entries, "adult", fetch)
if err != nil {
t.Fatal(err)
}
got := map[string]string{}
var names []string
for _, f := range files {
got[f.Name] = string(f.Data)
names = append(names, f.Name)
}
want := map[string]string{
"index.xml": "<Template>\n<ProductRating>AP</ProductRating>\n</Template>",
"tex.png": "full-size original",
"mat.xrf": "material", // in the CFL already, not an original
"small.png": "small original",
"cdn-only.xrf": "only on the CDN",
}
if len(got) != len(want) {
t.Errorf("files %v", names)
}
for n, w := range want {
if got[n] != w {
t.Errorf("%s: got %q, want %q", n, got[n], w)
}
}
if strings.Join(asked, ",") != "abc.png,ghi.xrf" {
t.Errorf("fetched %v", asked)
}
if _, err := Files(context.Background(), entries[1:], "general", fetch); err == nil {
t.Error("no index.xml: no error")
}
cdn = nil
if _, err := Files(context.Background(), entries, "general", fetch); err == nil {
t.Error("an original missing: no error")
}
if _, err := Files(context.Background(), entries, "", fetch); err == nil {
t.Error("no rating: no error")
}
}
func TestChknAndCheck(t *testing.T) {
index, _ := SetRating([]byte("<Template>\n</Template>"), "general")
files := []File{{Name: "index.xml", Data: index}, {Name: "Skin%20%5B03%5D.xrf", Data: []byte{1, 2, 3}}}
chkn, err := Chkn(files)
if err != nil {
t.Fatal(err)
}
z, err := zip.NewReader(bytes.NewReader(chkn), int64(len(chkn)))
if err != nil {
t.Fatal(err)
}
for i, f := range z.File {
r, _ := f.Open()
data, _ := io.ReadAll(r)
if f.Name != files[i].Name || !bytes.Equal(data, files[i].Data) {
t.Errorf("zip entry %d: %q", i, f.Name)
}
}
if err := Check(chkn, 42, "general", t.TempDir()); err != nil {
t.Errorf("check: %v", err)
}
if err := Check(chkn, 42, "adult", t.TempDir()); err == nil {
t.Error("rating disagreeing with index.xml: no error")
}
bad, _ := Chkn(append(files, File{Name: "_contents.json", Data: []byte("[]")}))
if err := Check(bad, 42, "general", t.TempDir()); err == nil {
t.Error("_contents.json: no error")
}
if Rating(index) != "general" {
t.Error("Rating of GA")
}
}