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,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"
|
||||
}
|
||||
Reference in new issue
Block a user