@@ -38,26 +38,28 @@ func FetchSubscription(url string) error {
3838
3939 // 先用 encoding/json 解析以保持键的顺序
4040 var rawConfig struct {
41- APISites json.RawMessage `json:"api_site"`
41+ APISites json.RawMessage `json:"api_site"`
42+ Lives json.RawMessage `json:"lives"`
4243 }
43-
44+
4445 if err := json .Unmarshal (decodedData , & rawConfig ); err != nil {
4546 return fmt .Errorf ("failed to unmarshal config JSON: %v" , err )
4647 }
47-
48+
4849 // 构建最终的 config
4950 var config models.Config
5051 config .APISites = make (map [string ]models.APISite )
5152 config .SiteList = make ([]models.APISite , 0 )
52-
53+ config .Lives = make ([]models.LiveSource , 0 )
54+
5355 // 使用 json.Decoder 按顺序解析 api_site 对象
5456 dec := json .NewDecoder (bytes .NewReader (rawConfig .APISites ))
55-
57+
5658 // 读取开始的 {
5759 if _ , err := dec .Token (); err != nil {
5860 return fmt .Errorf ("failed to parse api_site: %v" , err )
5961 }
60-
62+
6163 // 按顺序读取每个键值对
6264 for dec .More () {
6365 // 读取键
@@ -66,23 +68,54 @@ func FetchSubscription(url string) error {
6668 return fmt .Errorf ("failed to read key: %v" , err )
6769 }
6870 key := token .(string )
69-
71+
7072 // 读取值
7173 var site models.APISite
7274 if err := dec .Decode (& site ); err != nil {
7375 return fmt .Errorf ("failed to decode site: %v" , err )
7476 }
75-
77+
7678 site .Key = key
7779 config .APISites [key ] = site
7880 config .SiteList = append (config .SiteList , site )
7981 }
80-
82+
8183 if len (config .APISites ) == 0 {
8284 return fmt .Errorf ("no API sites found in subscription" )
8385 }
8486
87+ // 解析 lives 字段(如果存在)
88+ if len (rawConfig .Lives ) > 0 {
89+ liveDec := json .NewDecoder (bytes .NewReader (rawConfig .Lives ))
90+
91+ // 读取开始的 {
92+ if _ , err := liveDec .Token (); err != nil {
93+ return fmt .Errorf ("failed to parse lives: %v" , err )
94+ }
95+
96+ // 按顺序读取每个键值对
97+ for liveDec .More () {
98+ // 读取键
99+ token , err := liveDec .Token ()
100+ if err != nil {
101+ return fmt .Errorf ("failed to read live key: %v" , err )
102+ }
103+ key := token .(string )
104+
105+ // 读取值
106+ var live models.LiveSource
107+ if err := liveDec .Decode (& live ); err != nil {
108+ return fmt .Errorf ("failed to decode live source: %v" , err )
109+ }
110+
111+ live .Key = key
112+ config .Lives = append (config .Lives , live )
113+ }
114+
115+ log .Printf ("Loaded %d live sources" , len (config .Lives ))
116+ }
117+
85118 GlobalConfig = config
86- log .Printf ("Subscription config loaded successfully. API sites: %d" , len (config .APISites ))
119+ log .Printf ("Subscription config loaded successfully. API sites: %d, Live sources: %d " , len (config .APISites ), len ( config . Lives ))
87120 return nil
88121}
0 commit comments