Files
2026-10-26 16:28:14 +08:00

120 lines
4.0 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 短剧数据回流 demo。
//
// 用法:
//
// go run ./example # 只看将发送的 JSON,不发请求
// STUDIO_API_KEY='ld_xxx' go run ./example -send # 真正上报
//
// 接入时照着这个文件抄:改 records 里的数据,把 main 里的发送逻辑搬进你们自己的任务里。
package main
import (
"flag"
"fmt"
"os"
datacenter "videoweb/pub/data_center"
)
// ============================================================
// 1. 要上报的数据。一条 = 一个视频在某一天的表现。
//
// 不用填的(SDK 自动补):
// - data_date 数据所属日期 → 当天 UTC 日期
// - company 所属公司 → 瑞升
// - role_id 角色 ID 数组 → []
//
// 必须填的(漏了会在本地就报错,不会白跑请求):
// - company_platform 所属产品/项目名
// - published_data 视频上线时间
// - script_name 剧本名称
// - drama_name 短剧名称
// - drama_no 集数
// - video_url 成片下载链接
// - platform_url 产品内播放页链接
//
// 其余都是选填,不填就不会出现在请求体里。
// ============================================================
var records = []datacenter.Record{
{
// ---- 必填 ----
CompanyPlatform: "黄果短剧", // 你们的产品/项目名
PublishedData: "01/09/2026",
ScriptName: "剧本A",
DramaName: "短剧A",
DramaNo: "第1集",
VideoURL: "https://cdn.example.com/a.mp4",
PlatformURL: "https://example.com/a",
// ---- 选填,建议填:方便中台把同一条视频的历史数据串起来 ----
VideoID: "12345",
ScriptID: "S-001",
PlatformVideoID: "pv-12345",
// ---- 数据 ----
Views: 10000, // 浏览量
Clicks: 8200, // 播放量
Likes: 300, // 点赞量
// ---- 选填,不用就整行删掉 ----
// LikeRate: "3.66%", // 点赞量 ÷ 播放量 × 100%
// CompletionRate: "43.00%", // 完播率,没单条数据可填剧本整体完播率
// PaidRate: "0.00%", // 免费短剧统一 0.00%
// CompletionRateScope: "video", // video | script | unknown
// Comments: datacenter.Int(14), // 填 0 也会正常发出,删掉则不发这个字段
// Shares: datacenter.Int(0),
},
// 想一次发多条就继续往这里加,SDK 会打包成一个请求发出去。
// {
// CompanyPlatform: "黄果短剧",
// ...
// },
}
func main() {
send := flag.Bool("send", false, "真正 POST。默认只打印 JSON")
flag.Parse()
// ------------------------------------------------------------
// 第 1 步:本地核对。打印补齐默认值之后最终会发出的 JSON。
// ------------------------------------------------------------
body, err := datacenter.JSON(records...)
if err != nil {
fatal(err)
}
fmt.Println(string(body))
if !*send {
fmt.Println("\n未发送。确认无误后执行:")
fmt.Println(" STUDIO_API_KEY='ld_...' go run ./example -send")
return
}
// ------------------------------------------------------------
// 第 2 步:异步上报。
//
// SendAsync 把数据丢进内存队列就立即返回,HTTP 请求在后台发,
// 不会拖慢你们的主流程(接口 handler、定时任务都适用)。
//
// 两条铁律:
// 1. 失败不会返回给你,用 OnError 接住写日志。
// 2. 进程退出前必须 Close(),否则队列里的数据会丢。
// ------------------------------------------------------------
client := datacenter.New(os.Getenv("STUDIO_API_KEY")) // 创建一次,全程复用
client.OnError = func(err error) {
// 漏填字段、网络失败、队列满被丢弃,都会走到这里。
fmt.Fprintln(os.Stderr, "上报失败:", err)
}
client.SendAsync(records...) // 立即返回,不阻塞
client.Close() // 等队列里的数据发完再退出
fmt.Println("\n上报完成")
}
func fatal(err error) {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}