101 lines
2.9 KiB
Go
101 lines
2.9 KiB
Go
|
|
//go:build llm_script
|
||
|
|
|
||
|
|
package main
|
||
|
|
|
||
|
|
import (
|
||
|
|
"database/sql"
|
||
|
|
"flag"
|
||
|
|
"fmt"
|
||
|
|
"io"
|
||
|
|
"net/http"
|
||
|
|
"os"
|
||
|
|
"time"
|
||
|
|
)
|
||
|
|
|
||
|
|
type bytedanceSubscriptionImportConfig struct {
|
||
|
|
PricingURL string
|
||
|
|
NoticeURL string
|
||
|
|
PricingFixture string
|
||
|
|
NoticeFixture string
|
||
|
|
DryRun bool
|
||
|
|
Timeout time.Duration
|
||
|
|
}
|
||
|
|
|
||
|
|
func main() {
|
||
|
|
loadSubscriptionImportEnv()
|
||
|
|
|
||
|
|
var pricingURL string
|
||
|
|
var noticeURL string
|
||
|
|
var pricingFixture string
|
||
|
|
var noticeFixture string
|
||
|
|
var dryRun bool
|
||
|
|
var timeoutSeconds int
|
||
|
|
|
||
|
|
flag.StringVar(&pricingURL, "pricing-url", defaultBytedanceCodingPlanURL, "火山方舟 Coding Plan 价格说明 URL")
|
||
|
|
flag.StringVar(¬iceURL, "notice-url", defaultBytedanceCodingPlanNotice, "火山方舟 Coding Plan 活动说明 URL")
|
||
|
|
flag.StringVar(&pricingFixture, "pricing-fixture", "", "火山方舟 Coding Plan 价格样例文件")
|
||
|
|
flag.StringVar(¬iceFixture, "notice-fixture", "", "火山方舟 Coding Plan 活动样例文件")
|
||
|
|
flag.BoolVar(&dryRun, "dry-run", false, "仅解析并打印摘要,不写入数据库")
|
||
|
|
flag.IntVar(&timeoutSeconds, "timeout", 20, "请求超时(秒)")
|
||
|
|
flag.Parse()
|
||
|
|
|
||
|
|
cfg := bytedanceSubscriptionImportConfig{
|
||
|
|
PricingURL: pricingURL,
|
||
|
|
NoticeURL: noticeURL,
|
||
|
|
PricingFixture: pricingFixture,
|
||
|
|
NoticeFixture: noticeFixture,
|
||
|
|
DryRun: dryRun,
|
||
|
|
Timeout: time.Duration(timeoutSeconds) * time.Second,
|
||
|
|
}
|
||
|
|
|
||
|
|
var db *sql.DB
|
||
|
|
var err error
|
||
|
|
if !cfg.DryRun {
|
||
|
|
db, err = subscriptionImportDB()
|
||
|
|
if err != nil {
|
||
|
|
fmt.Fprintf(os.Stderr, "open db: %v\n", err)
|
||
|
|
os.Exit(1)
|
||
|
|
}
|
||
|
|
defer db.Close()
|
||
|
|
}
|
||
|
|
|
||
|
|
if err := runBytedanceSubscriptionImport(cfg, db, os.Stdout); err != nil {
|
||
|
|
fmt.Fprintf(os.Stderr, "import_bytedance_subscription: %v\n", err)
|
||
|
|
os.Exit(1)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func runBytedanceSubscriptionImport(cfg bytedanceSubscriptionImportConfig, db *sql.DB, out io.Writer) error {
|
||
|
|
client := &http.Client{Timeout: cfg.Timeout}
|
||
|
|
pricingRaw, err := fetchSubscriptionPage(cfg.PricingURL, cfg.PricingFixture, client)
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
noticeRaw, err := fetchSubscriptionPage(cfg.NoticeURL, cfg.NoticeFixture, client)
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
|
||
|
|
records, err := parseBytedanceSubscriptionCatalog(pricingRaw, noticeRaw)
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
if cfg.DryRun {
|
||
|
|
_, err = fmt.Fprintf(out, "source=bytedance-subscription-import plans=%d provider=%s operator=%s dry_run=true\n", len(records), records[0].ProviderName, records[0].OperatorName)
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
if db == nil {
|
||
|
|
return fmt.Errorf("db is required when dry-run=false")
|
||
|
|
}
|
||
|
|
if err := upsertSubscriptionImportRecords(db, records); err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
|
||
|
|
var tableRows int
|
||
|
|
if err := db.QueryRow(`SELECT COUNT(*) FROM subscription_plan`).Scan(&tableRows); err != nil {
|
||
|
|
return fmt.Errorf("count subscription_plan: %w", err)
|
||
|
|
}
|
||
|
|
_, err = fmt.Fprintf(out, "source=bytedance-subscription-import plans=%d provider=%s operator=%s table_rows=%d dry_run=false\n", len(records), records[0].ProviderName, records[0].OperatorName, tableRows)
|
||
|
|
return err
|
||
|
|
}
|