@@ -17,7 +17,6 @@ import (
1717 "github.com/FloatTech/gg"
1818 "github.com/FloatTech/imgfactory"
1919 "github.com/FloatTech/rendercard"
20- "github.com/FloatTech/ttl"
2120 ctrl "github.com/FloatTech/zbpctrl"
2221 "github.com/FloatTech/zbputils/control"
2322 "github.com/FloatTech/zbputils/img/text"
@@ -32,8 +31,9 @@ const (
3231
3332var (
3433 mu sync.RWMutex
35- todayPic = ttl.NewCache [uint64 , []byte ](time .Hour * 12 )
36- en = control .Register ("movies" , & ctrl.Options [* zero.Ctx ]{
34+ todayPic = make ([][]byte , 2 )
35+ lasttime time.Time
36+ en = control .AutoRegister (& ctrl.Options [* zero.Ctx ]{
3737 DisableOnDefault : false ,
3838 Brief : "电影查询" ,
3939 Help : "- 今日电影\n " +
@@ -44,102 +44,49 @@ var (
4444
4545func init () {
4646 en .OnFullMatch ("今日电影" ).SetBlock (true ).Handle (func (ctx * zero.Ctx ) {
47- todayOnPic := todayPic .Get (0 )
48- if todayOnPic != nil {
49- ctx .SendChain (message .ImageBytes (todayOnPic ))
47+ if todayPic != nil && time .Since (lasttime ) < 12 * time .Hour {
48+ ctx .SendChain (message .ImageBytes (todayPic [0 ]))
5049 return
5150 }
52- data , err := web .RequestDataWith (web .NewDefaultClient (), apiURL + "movieOnInfoList" , "" , "GET" , ua , nil )
51+ lasttime = time .Now ()
52+ movieComingList , err := getMovieList ("今日电影" )
5353 if err != nil {
54- ctx .SendChain (message .Text ("[ERROR1] " , err ))
54+ ctx .SendChain (message .Text ("[ERROR]: " , err ))
5555 return
5656 }
57- var parsed movieOnList
58- err = json .Unmarshal (data , & parsed )
59- if err != nil {
60- ctx .SendChain (message .Text ("[EEROR2]:" , err ))
57+ if len (movieComingList ) == 0 {
58+ ctx .SendChain (message .Text ("没有今日电影" ))
6159 return
6260 }
63- if len (parsed .MovieList ) == 0 {
64- ctx .SendChain (message .Text ("今日无电影上映" ))
65- }
66- for i , info := range parsed .MovieList {
67- movieID := strconv .FormatInt (info .ID , 10 )
68- data , err := web .RequestDataWith (web .NewDefaultClient (), apiURL + "detailmovie?movieId=" + movieID , "" , "GET" , ua , nil )
69- if err != nil {
70- ctx .SendChain (message .Text ("[ERROR1]" , err ))
71- return
72- }
73- var movieInfo movieShow
74- err = json .Unmarshal (data , & movieInfo )
75- if err != nil {
76- ctx .SendChain (message .Text ("[EEROR2]:" , err ))
77- return
78- }
79- parsed .MovieList [i ] = movieInfo .MovieInfo
80- }
81- // 整理数据,进行排序
82- sort .Slice (parsed .MovieList , func (i , j int ) bool {
83- if parsed .MovieList [i ].Sc != parsed .MovieList [j ].Sc {
84- return parsed .MovieList [i ].Sc > parsed .MovieList [j ].Sc
85- }
86- return parsed .MovieList [i ].Watched > parsed .MovieList [j ].Watched
87- })
88- pic , err := drawOnListPic (parsed )
61+ pic , err := drawOnListPic (movieComingList )
8962 if err != nil {
90- ctx .SendChain (message .Text ("[EEROR3 ]:" , err ))
63+ ctx .SendChain (message .Text ("[ERROR ]:" , err ))
9164 return
9265 }
93- todayPic . Set ( 0 , pic )
66+ todayPic [ 0 ] = pic
9467 ctx .SendChain (message .ImageBytes (pic ))
9568 })
9669 en .OnFullMatch ("预售电影" ).SetBlock (true ).Handle (func (ctx * zero.Ctx ) {
97- todayOnPic := todayPic .Get (1 )
98- if todayOnPic != nil {
99- ctx .SendChain (message .ImageBytes (todayOnPic ))
70+ if todayPic [1 ] != nil && time .Since (lasttime ) < 12 * time .Hour {
71+ ctx .SendChain (message .ImageBytes (todayPic [1 ]))
10072 return
10173 }
102- data , err := web .RequestDataWith (web .NewDefaultClient (), apiURL + "comingList?token=" , "" , "GET" , ua , nil )
74+ lasttime = time .Now ()
75+ movieComingList , err := getMovieList ("预售电影" )
10376 if err != nil {
104- ctx .SendChain (message .Text ("[ERROR4] " , err ))
77+ ctx .SendChain (message .Text ("[ERROR]: " , err ))
10578 return
10679 }
107- var parsed comingList
108- err = json .Unmarshal (data , & parsed )
109- if err != nil {
110- ctx .SendChain (message .Text ("[EEROR5]:" , err ))
80+ if len (movieComingList ) == 0 {
81+ ctx .SendChain (message .Text ("没有预售电影" ))
11182 return
11283 }
113- if len (parsed .MovieList ) == 0 {
114- ctx .SendChain (message .Text ("没有预售信息" ))
115- }
116- for i , info := range parsed .MovieList {
117- movieID := strconv .FormatInt (info .ID , 10 )
118- data , err := web .RequestDataWith (web .NewDefaultClient (), apiURL + "detailmovie?movieId=" + movieID , "" , "GET" , ua , nil )
119- if err != nil {
120- ctx .SendChain (message .Text ("[ERROR1]" , err ))
121- return
122- }
123- var movieInfo movieShow
124- err = json .Unmarshal (data , & movieInfo )
125- if err != nil {
126- ctx .SendChain (message .Text ("[EEROR2]:" , err ))
127- return
128- }
129- movieInfo .MovieInfo .Wish = info .Wish
130- movieInfo .MovieInfo .ComingTitle = info .ComingTitle
131- parsed .MovieList [i ] = movieInfo .MovieInfo
132- }
133- // 整理数据,进行排序
134- sort .Slice (parsed .MovieList , func (i , j int ) bool {
135- return parsed .MovieList [i ].Wish > parsed .MovieList [j ].Wish
136- })
137- pic , err := drawComListPic (parsed )
84+ pic , err := drawComListPic (movieComingList )
13885 if err != nil {
139- ctx .SendChain (message .Text ("[EEROR6 ]:" , err ))
86+ ctx .SendChain (message .Text ("[ERROR ]:" , err ))
14087 return
14188 }
142- todayPic . Set ( 1 , pic )
89+ todayPic [ 1 ] = pic
14390 ctx .SendChain (message .ImageBytes (pic ))
14491 })
14592}
@@ -184,14 +131,71 @@ type cardInfo struct {
184131 Rank string
185132}
186133
187- func drawOnListPic (lits movieOnList ) (data []byte , err error ) {
188- rankinfo := make ([]* cardInfo , len (lits .MovieList ))
134+ func getMovieList (mode string ) (movieList []movieInfo , err error ) {
135+ var data []byte
136+ if mode == "今日电影" {
137+ data , err = web .RequestDataWith (web .NewDefaultClient (), apiURL + "movieOnInfoList" , "" , "GET" , ua , nil )
138+ if err != nil {
139+ return
140+ }
141+ var parsed movieOnList
142+ err = json .Unmarshal (data , & parsed )
143+ if err != nil {
144+ return
145+ }
146+ movieList = parsed .MovieList
147+ } else {
148+ data , err = web .RequestDataWith (web .NewDefaultClient (), apiURL + "comingList?token=" , "" , "GET" , ua , nil )
149+ if err != nil {
150+ return
151+ }
152+ var parsed comingList
153+ err = json .Unmarshal (data , & parsed )
154+ if err != nil {
155+ return
156+ }
157+ movieList = parsed .MovieList
158+ }
159+ if len (movieList ) == 0 {
160+ return
161+ }
162+ for i , info := range movieList {
163+ movieID := strconv .FormatInt (info .ID , 10 )
164+ data , err = web .RequestDataWith (web .NewDefaultClient (), apiURL + "detailmovie?movieId=" + movieID , "" , "GET" , ua , nil )
165+ if err != nil {
166+ return
167+ }
168+ var movieInfo movieShow
169+ err = json .Unmarshal (data , & movieInfo )
170+ if err != nil {
171+ return
172+ }
173+ if mode != "今日电影" {
174+ movieList [i ].ComingTitle = movieInfo .MovieInfo .ComingTitle
175+ }
176+ movieList [i ] = movieInfo .MovieInfo
177+ }
178+ // 整理数据,进行排序
179+ sort .Slice (movieList , func (i , j int ) bool {
180+ if movieList [i ].Sc != movieList [j ].Sc {
181+ return movieList [i ].Sc > movieList [j ].Sc
182+ }
183+ if mode == "今日电影" {
184+ return movieList [i ].Watched > movieList [j ].Watched
185+ } else {
186+ return movieList [i ].Wish > movieList [j ].Wish
187+ }
188+ })
189+ return movieList , nil
190+ }
191+ func drawOnListPic (lits []movieInfo ) (data []byte , err error ) {
192+ rankinfo := make ([]* cardInfo , len (lits ))
189193
190194 wg := & sync.WaitGroup {}
191- wg .Add (len (lits . MovieList ))
192- for i := 0 ; i < len (lits . MovieList ); i ++ {
195+ wg .Add (len (lits ))
196+ for i := 0 ; i < len (lits ); i ++ {
193197 go func (i int ) {
194- info := lits . MovieList [i ]
198+ info := lits [i ]
195199 defer wg .Done ()
196200 img , err := avatar (& info )
197201 if err != nil {
@@ -238,14 +242,14 @@ func drawOnListPic(lits movieOnList) (data []byte, err error) {
238242 return
239243}
240244
241- func drawComListPic (lits comingList ) (data []byte , err error ) {
242- rankinfo := make ([]* cardInfo , len (lits . MovieList ))
245+ func drawComListPic (lits [] movieInfo ) (data []byte , err error ) {
246+ rankinfo := make ([]* cardInfo , len (lits ))
243247
244248 wg := & sync.WaitGroup {}
245- wg .Add (len (lits . MovieList ))
246- for i := 0 ; i < len (lits . MovieList ); i ++ {
249+ wg .Add (len (lits ))
250+ for i := 0 ; i < len (lits ); i ++ {
247251 go func (i int ) {
248- info := lits . MovieList [i ]
252+ info := lits [i ]
249253 defer wg .Done ()
250254 img , err := avatar (& info )
251255 if err != nil {
@@ -400,6 +404,7 @@ func drawRankingCard(fontdata []byte, title string, rankinfo []*cardInfo) (img i
400404 return
401405}
402406
407+ // avatar 获取电影海报,图片大且多,存本地增加响应速度
403408func avatar (movieInfo * movieInfo ) (pic image.Image , err error ) {
404409 mu .Lock ()
405410 defer mu .Unlock ()
0 commit comments