@@ -2,13 +2,13 @@ package olivetv
22
33import (
44 "errors"
5+ "fmt"
56 "regexp"
67 "sort"
78 "time"
89
910 "github.com/imroc/req/v3"
1011 jsoniter "github.com/json-iterator/go"
11- "github.com/tidwall/gjson"
1212)
1313
1414func init () {
@@ -47,26 +47,41 @@ func (this *kuaishou) set(tv *TV) error {
4747 return err
4848 }
4949
50+ content := resp .String ()
51+
5052 re := regexp .MustCompile (`<script>window.__INITIAL_STATE__=([\s\S]*?);\(function` )
51- caps := re .FindStringSubmatch (resp . String () )
53+ caps := re .FindStringSubmatch (content )
5254 if len (caps ) < 2 {
53- return nil
55+ return errors . New ( "kuaishou: content=%s" + content )
5456 }
5557
5658 text := caps [1 ]
5759
58- tv .roomName = gjson .Get (text , "liveroom.liveStream.caption" ).String ()
59- tv .streamerName = gjson .Get (text , "liveroom.author.name" ).String ()
60+ // f, _ := os.Create("b.json")
61+ // f.WriteString(text)
62+ // f.Close()
63+
64+ var ag KuaishouAutoGenerated
65+ err = jsoniter .UnmarshalFromString (text , & ag )
66+ if err != nil {
67+ return fmt .Errorf ("kuaishou: text=%s\n err=%s" , text , err .Error ())
68+ }
69+
70+ if len (ag .Liveroom .PlayList ) == 0 {
71+ return errors .New ("kuaishou: empty PlayList" )
72+ }
73+
74+ data := ag .Liveroom .PlayList [0 ]
6075
61- tv .roomOn = gjson .Get (text , "liveroom.isLiving" ).Bool ()
62- // fmt.Println("tv.roomOn", tv.roomOn)
76+ tv .roomName = data .LiveStream .Caption
77+ tv .streamerName = data .Author .Name
78+
79+ tv .roomOn = data .IsLiving
6380 if ! tv .roomOn {
6481 return nil
6582 }
6683
67- var ag KuaishouAutoGenerated
68- jsoniter .UnmarshalFromString (text , & ag )
69- playUrls := ag .Liveroom .LiveStream .PlayUrls
84+ playUrls := data .LiveStream .PlayUrls
7085 if len (playUrls ) == 0 {
7186 return errors .New ("kuaishou: empty playUrls" )
7287 }
@@ -78,21 +93,27 @@ func (this *kuaishou) set(tv *TV) error {
7893 return cands [i ].Bitrate > cands [j ].Bitrate
7994 })
8095 tv .streamURL = cands [0 ].URL
81-
8296 return nil
8397}
8498
8599type KuaishouAutoGenerated struct {
86100 Liveroom struct {
87- LiveStream struct {
88- PlayUrls []struct {
89- AdaptationSet struct {
90- Representation []struct {
91- URL string `json:"url"`
92- Bitrate int `json:"bitrate"`
93- } `json:"representation"`
94- } `json:"adaptationSet"`
95- } `json:"playUrls"`
96- } `json:"liveStream"`
101+ PlayList []struct {
102+ LiveStream struct {
103+ Caption string `json:"caption"`
104+ PlayUrls []struct {
105+ AdaptationSet struct {
106+ Representation []struct {
107+ URL string `json:"url"`
108+ Bitrate int `json:"bitrate"`
109+ } `json:"representation"`
110+ } `json:"adaptationSet"`
111+ } `json:"playUrls"`
112+ } `json:"liveStream"`
113+ Author struct {
114+ Name string `json:"name"`
115+ } `json:"author"`
116+ IsLiving bool `json:"isLiving"`
117+ } `json:"playList"`
97118 } `json:"liveroom"`
98119}
0 commit comments