script.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. package script
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "github.com/yuin/gopher-lua/parse"
  7. qu "jygit.jydev.jianyu360.cn/data_processing/common_utils"
  8. "net/url"
  9. "os"
  10. "path/filepath"
  11. "spider_creator/backend"
  12. "strconv"
  13. "strings"
  14. "time"
  15. "github.com/chromedp/cdproto/browser"
  16. "github.com/chromedp/cdproto/network"
  17. "github.com/chromedp/cdproto/page"
  18. "github.com/chromedp/chromedp"
  19. "github.com/yuin/gopher-lua"
  20. be "spider_creator/backend"
  21. )
  22. const (
  23. selector_type_id = 0
  24. selector_type_query = 1
  25. selector_type_search = 2
  26. selector_type_jspath = 3
  27. selector_type_query_all = 4
  28. execute_return_type_string = 0
  29. execute_return_type_list = 1
  30. execute_return_type_table = 2
  31. qlm_list_lua = "/script/qlm_list.lua"
  32. qlm_detail_lua = "/script/qlm_detail.lua"
  33. )
  34. var (
  35. DataCache = make(chan map[string]interface{}, 2000)
  36. Datas []map[string]interface{}
  37. )
  38. type GLVm struct {
  39. AttachesDir string
  40. Dnf backend.EventNotifyFace
  41. Headless bool
  42. ShowImage bool
  43. ProxyServer bool
  44. ProxyAddr string
  45. B *GLBrowser
  46. ScriptRunning bool //控制一次只能执行一个脚本
  47. DataSaveOver chan bool
  48. }
  49. type GLBrowser struct {
  50. Ctx context.Context
  51. CancelFn context.CancelFunc
  52. }
  53. func NewGLVM(attachesDir string, dnf be.EventNotifyFace) *GLVm {
  54. return &GLVm{
  55. AttachesDir: attachesDir,
  56. Dnf: dnf,
  57. DataSaveOver: make(chan bool, 1),
  58. }
  59. }
  60. // LoadScript 加载脚本
  61. func (glvm *GLVm) LoadScript(page string) string {
  62. var path string
  63. if page == "list" {
  64. path = glvm.AttachesDir + qlm_list_lua
  65. } else if page == "detail" {
  66. path = glvm.AttachesDir + qlm_detail_lua
  67. }
  68. bs, err := os.ReadFile(path)
  69. if err != nil {
  70. qu.Debug(path, "脚本加载失败...")
  71. }
  72. return string(bs)
  73. }
  74. // RunScript 执行lua代码
  75. func (glvm *GLVm) RunScript(script, recordId string) error {
  76. defer Catch()
  77. var s *lua.LState = lua.NewState()
  78. defer s.Close()
  79. //方法绑定
  80. glvm.ResetBrowser() //先创建浏览器对象
  81. glvm.BindLuaState(s) //绑定虚拟机函数
  82. glvm.B.BindLuaState(s, recordId)
  83. defer func() {
  84. if b := glvm.B; b != nil {
  85. b.CancelFn()
  86. b.Ctx = nil
  87. b.CancelFn = nil
  88. }
  89. }()
  90. reader := strings.NewReader(script)
  91. chunk, err := parse.Parse(reader, "code")
  92. if err != nil {
  93. return err
  94. }
  95. proto, err := lua.Compile(chunk, script)
  96. if err != nil {
  97. return err
  98. }
  99. lfunc := s.NewFunctionFromProto(proto)
  100. s.Push(lfunc)
  101. s.Call(0, 0)
  102. return nil
  103. }
  104. // ResetBrowser 重置浏览器
  105. func (glvm *GLVm) ResetBrowser() {
  106. if glvm.B != nil && glvm.B.CancelFn != nil {
  107. glvm.B.CancelFn()
  108. glvm.B.Ctx = nil
  109. glvm.B.CancelFn = nil
  110. }
  111. _, _, _, _, ctx, incCancelFn := backend.NewBrowser(glvm.Headless, glvm.ShowImage, glvm.ProxyServer, "http://")
  112. b := &GLBrowser{
  113. Ctx: ctx,
  114. CancelFn: incCancelFn,
  115. }
  116. if glvm.B == nil {
  117. glvm.B = b
  118. } else {
  119. glvm.B.Ctx, glvm.B.CancelFn = b.Ctx, b.CancelFn
  120. }
  121. }
  122. // BindLuaState 绑定虚拟机函数
  123. func (glvm *GLVm) BindLuaState(state *lua.LState) {
  124. state.SetGlobal("browser_reset", state.NewFunction(func(l *lua.LState) int {
  125. glvm.ResetBrowser()
  126. return 0
  127. }))
  128. //
  129. state.SetGlobal("browser_save", state.NewFunction(func(l *lua.LState) int {
  130. //spiderCode := l.ToString(-5)
  131. //siteName := l.ToString(-4)
  132. //siteChannelName := l.ToString(-3)
  133. //siteChannelUrl := l.ToString(-2)
  134. /*table := l.ToTable(-1)
  135. data := TableToMap(table)*/
  136. //vm.S.Save(spiderCode, siteName, siteChannelName, siteChannelUrl, data)
  137. return 0
  138. }))
  139. }
  140. func (glvm *GLVm) CloseTabs() {
  141. if b := glvm.B; b != nil {
  142. b.CancelFn()
  143. b.Ctx = nil
  144. b.CancelFn = nil
  145. }
  146. }
  147. // findTab 根据标题、url找tab
  148. func (b *GLBrowser) findTabContext(tabTitle, tabUrl string, timeoutInt64 int64) (ctx context.Context, err error) {
  149. if b.Ctx != nil {
  150. if timeoutInt64 == 0 {
  151. timeoutInt64 = 5000
  152. }
  153. timeout := time.Duration(timeoutInt64) * time.Millisecond
  154. if tabTitle == "" && tabUrl == "" {
  155. ctx, _ = context.WithTimeout(b.Ctx, timeout)
  156. return ctx, nil
  157. } else {
  158. ts, err := chromedp.Targets(b.Ctx)
  159. if err != nil {
  160. return nil, err
  161. }
  162. for _, t := range ts {
  163. if (tabTitle != "" && strings.Contains(t.Title, tabTitle)) || (tabUrl != "" && strings.Contains(t.URL, tabUrl)) {
  164. // log.Printf("find tab param<title,url>: %s %s found %s %s", tabTitle, tabUrl,
  165. // t.Title, t.URL)
  166. newCtx, _ := chromedp.NewContext(b.Ctx, chromedp.WithTargetID(t.TargetID))
  167. ctx, _ = context.WithTimeout(newCtx, timeout)
  168. return ctx, nil
  169. }
  170. }
  171. }
  172. return nil, errors.New("can't find tab")
  173. }
  174. return nil, errors.New("context is error")
  175. }
  176. // CloseTabs 关闭页面
  177. func (b *GLBrowser) CloseTabs(tabTitle, tabUrl string, timeoutInt64 int64) (err error) {
  178. if timeoutInt64 == 0 {
  179. timeoutInt64 = 5
  180. }
  181. timeout := time.Duration(timeoutInt64) * time.Millisecond
  182. ts, err := chromedp.Targets(b.Ctx)
  183. if err != nil {
  184. return err
  185. }
  186. for _, t := range ts {
  187. if (tabTitle != "" && strings.Contains(t.Title, tabTitle)) || (tabUrl != "" && strings.Contains(t.URL, tabUrl)) {
  188. newCtx, _ := chromedp.NewContext(b.Ctx, chromedp.WithTargetID(t.TargetID))
  189. ctx, _ := context.WithTimeout(newCtx, timeout)
  190. chromedp.Run(
  191. ctx,
  192. page.Close(),
  193. )
  194. }
  195. }
  196. return nil
  197. }
  198. // Navigate 导航到指定网址
  199. func (b *GLBrowser) Navigate(tabTitle string, tabUrl string, isNewTab bool, targetUrl string, timeout int64) (err error) {
  200. ctx, err := b.findTabContext(tabTitle, tabUrl, timeout)
  201. if err != nil {
  202. return err
  203. }
  204. //新标签页
  205. if isNewTab {
  206. ctx, _ = chromedp.NewContext(ctx)
  207. }
  208. //
  209. return chromedp.Run(ctx,
  210. chromedp.Navigate(targetUrl))
  211. }
  212. // Navigate 导航到指定网址,并保存请求资源,如图片等
  213. func (b *GLBrowser) NavigateAndSaveRes(tabTitle string, tabUrl string, timeout int64, isNewTab bool, targetUrl string, saveFileTypeList, save2dir string) (err error) {
  214. ctx, err := b.findTabContext(tabTitle, tabUrl, timeout)
  215. if err != nil {
  216. return err
  217. }
  218. //新标签页
  219. if isNewTab {
  220. ctx, _ = chromedp.NewContext(ctx)
  221. }
  222. //
  223. saveFileType := strings.Split(saveFileTypeList, " ")
  224. isNeedRes := func(fileType string) bool {
  225. for _, v := range saveFileType {
  226. if strings.Contains(fileType, v) {
  227. return true
  228. }
  229. }
  230. return false
  231. }
  232. fnURL2FileName := func(requestURL string) string {
  233. u, err := url.Parse(requestURL)
  234. if err != nil {
  235. return ""
  236. }
  237. _, filename := filepath.Split(u.Path)
  238. return filename
  239. }
  240. var cache = map[network.RequestID]string{}
  241. chromedp.ListenTarget(ctx, func(v interface{}) {
  242. switch ev := v.(type) {
  243. case *network.EventRequestWillBeSent: //准备下载
  244. cache[ev.RequestID] = ev.Request.URL
  245. case *network.EventResponseReceived: //检查回应头的contenttype
  246. contentType, _ := ev.Response.Headers["Content-Type"].(string)
  247. fmt.Println(contentType)
  248. if !isNeedRes(contentType) {
  249. delete(cache, ev.RequestID)
  250. }
  251. case *network.EventLoadingFinished: //下载完成
  252. if uri, ok := cache[ev.RequestID]; ok {
  253. filename := fnURL2FileName(uri)
  254. fmt.Println("save2file", filename)
  255. if filename != "" {
  256. filePath := filepath.Join(save2dir, filename)
  257. var buf []byte
  258. if err := chromedp.Run(ctx, chromedp.ActionFunc(func(ctx context.Context) error {
  259. var err error
  260. buf, err = network.GetResponseBody(ev.RequestID).Do(ctx)
  261. return err
  262. })); err == nil {
  263. os.WriteFile(filePath, buf, 0777)
  264. } else {
  265. fmt.Println(err.Error())
  266. }
  267. }
  268. }
  269. }
  270. })
  271. //
  272. err = chromedp.Run(ctx,
  273. chromedp.Navigate(targetUrl))
  274. //下载存储
  275. return err
  276. }
  277. // ExecuteJS 执行脚本
  278. func (b *GLBrowser) ExecuteJS(tabTitle, tabUrl, script string, ret interface{}, timeout int64) (err error) {
  279. ctx, err := b.findTabContext(tabTitle, tabUrl, timeout)
  280. if err != nil {
  281. return err
  282. }
  283. return chromedp.Run(ctx,
  284. chromedp.Evaluate(script, ret))
  285. }
  286. // Click 点击
  287. func (b *GLBrowser) Click(tabTitle, tabUrl, selector string, selectorType int, timeout int64) (err error) {
  288. ctx, err := b.findTabContext(tabTitle, tabUrl, timeout)
  289. if err != nil {
  290. return err
  291. }
  292. var act chromedp.QueryAction
  293. switch selectorType {
  294. case selector_type_id:
  295. act = chromedp.Click(selector, chromedp.ByID)
  296. case selector_type_query:
  297. act = chromedp.Click(selector, chromedp.ByQuery)
  298. case selector_type_search:
  299. act = chromedp.Click(selector, chromedp.BySearch)
  300. case selector_type_jspath:
  301. act = chromedp.Click(selector, chromedp.ByJSPath)
  302. default:
  303. act = chromedp.Click(selector, chromedp.ByQueryAll)
  304. }
  305. err = chromedp.Run(ctx,
  306. act)
  307. return err
  308. }
  309. // KeySend 键盘输入
  310. func (b *GLBrowser) KeySend(tabTitle, tabUrl, selector, sendStr string, selectorType int, timeout int64) (err error) {
  311. ctx, err := b.findTabContext(tabTitle, tabUrl, timeout)
  312. if err != nil {
  313. return err
  314. }
  315. var act chromedp.QueryAction
  316. switch selectorType {
  317. case selector_type_id:
  318. act = chromedp.SendKeys(selector, sendStr, chromedp.ByID)
  319. case selector_type_query:
  320. act = chromedp.SendKeys(selector, sendStr, chromedp.ByQuery)
  321. case selector_type_search:
  322. act = chromedp.SendKeys(selector, sendStr, chromedp.BySearch)
  323. case selector_type_jspath:
  324. act = chromedp.SendKeys(selector, sendStr, chromedp.ByJSPath)
  325. default:
  326. act = chromedp.SendKeys(selector, sendStr, chromedp.ByQueryAll)
  327. }
  328. return chromedp.Run(ctx,
  329. act)
  330. }
  331. // WaitVisible 等待元素可见
  332. func (b *GLBrowser) WaitVisible(tabTitle, tabUrl, selector string, selectorType int, timeout int64) error {
  333. ctx, err := b.findTabContext(tabTitle, tabUrl, timeout)
  334. if err != nil {
  335. return err
  336. }
  337. var act chromedp.QueryAction
  338. switch selectorType {
  339. case selector_type_id:
  340. act = chromedp.WaitVisible(selector, chromedp.ByID)
  341. case selector_type_query:
  342. act = chromedp.WaitVisible(selector, chromedp.ByQuery)
  343. case selector_type_search:
  344. act = chromedp.WaitVisible(selector, chromedp.BySearch)
  345. case selector_type_jspath:
  346. act = chromedp.WaitVisible(selector, chromedp.ByJSPath)
  347. default:
  348. act = chromedp.WaitVisible(selector, chromedp.ByQueryAll)
  349. }
  350. return chromedp.Run(ctx,
  351. act)
  352. }
  353. // 重置浏览器
  354. func (b *GLBrowser) Reset() {
  355. }
  356. // DownloadFile 只有在非headless模式下有效,与click方法其实是一致的
  357. func (b *GLBrowser) DownloadFile(tabTitle, tabUrl string, timeout int64, selector string, selectorType int, save2dir string) error {
  358. ctx, err := b.findTabContext(tabTitle, tabUrl, timeout)
  359. if err != nil {
  360. return err
  361. }
  362. var act chromedp.QueryAction
  363. switch selectorType {
  364. case selector_type_id:
  365. act = chromedp.Click(selector, chromedp.ByID)
  366. case selector_type_query:
  367. act = chromedp.Click(selector, chromedp.ByQuery)
  368. case selector_type_search:
  369. act = chromedp.Click(selector, chromedp.BySearch)
  370. case selector_type_jspath:
  371. act = chromedp.Click(selector, chromedp.ByJSPath)
  372. default:
  373. act = chromedp.Click(selector, chromedp.ByQueryAll)
  374. }
  375. return chromedp.Run(ctx,
  376. browser.SetDownloadBehavior(browser.SetDownloadBehaviorBehaviorAllowAndName).WithDownloadPath(save2dir).WithEventsEnabled(true),
  377. act)
  378. }
  379. // BindLuaState
  380. func (b *GLBrowser) BindLuaState(s *lua.LState, recordId string) {
  381. //执行暂停
  382. s.SetGlobal("browser_sleep", s.NewFunction(func(l *lua.LState) int {
  383. fmt.Println("---browser_sleep---")
  384. timeout := l.ToInt64(-1)
  385. if timeout == 0 {
  386. timeout = 5
  387. }
  388. time.Sleep(time.Duration(timeout) * time.Millisecond)
  389. return 0
  390. }))
  391. //关闭tabl页
  392. s.SetGlobal("browser_closetabs", s.NewFunction(func(l *lua.LState) int {
  393. fmt.Println("---browser_closetabs---")
  394. timeout := l.ToInt64(-3)
  395. tabTitle := l.ToString(-2)
  396. tabUrl := l.ToString(-1)
  397. if timeout == 0 {
  398. timeout = 5
  399. }
  400. b.CloseTabs(tabTitle, tabUrl, timeout)
  401. return 0
  402. }))
  403. //注册打开地址
  404. s.SetGlobal("browser_navagite", s.NewFunction(func(l *lua.LState) int {
  405. fmt.Println("---browser_navagite---")
  406. tabTitle := l.ToString(-5) //指定标签页title
  407. tabUrl := l.ToString(-4) //指定标签页url
  408. isNewTab := l.ToBool(-3) //是否打开新的标签页
  409. timeout := l.ToInt64(-2) //网页打开的超时时间
  410. targetUrl := l.ToString(-1) //打开网页的链接
  411. if err := b.Navigate(tabTitle, tabUrl, isNewTab, targetUrl, timeout); err != nil {
  412. l.Push(lua.LString(err.Error()))
  413. } else {
  414. l.Push(lua.LString("ok"))
  415. }
  416. return 1
  417. }))
  418. //执行浏览器端js
  419. s.SetGlobal("browser_executejs", s.NewFunction(func(l *lua.LState) int {
  420. fmt.Println("---browser_executejs---")
  421. tabTitle := l.ToString(-5)
  422. tabUrl := l.ToString(-4)
  423. timeout := l.ToInt64(-3)
  424. returnType := l.ToInt(-2) //返回数据类型
  425. script := l.ToString(-1) //执行的js
  426. switch returnType {
  427. case execute_return_type_string: //返回string
  428. var ret string
  429. if err := b.ExecuteJS(tabTitle, tabUrl, script, &ret, timeout); err == nil {
  430. l.Push(lua.LString("ok"))
  431. l.Push(lua.LString(ret))
  432. } else {
  433. l.Push(lua.LString("err"))
  434. l.Push(lua.LString(err.Error()))
  435. }
  436. case execute_return_type_list: //返回list
  437. var ret = make([]interface{}, 0, 0)
  438. var tmp = make(map[string]interface{})
  439. if err := b.ExecuteJS(tabTitle, tabUrl, script, &ret, timeout); err == nil {
  440. for i, v := range ret {
  441. tmp[strconv.Itoa(i)] = v
  442. }
  443. l.Push(lua.LString("ok"))
  444. l.Push(MapToTable(tmp))
  445. } else {
  446. l.Push(lua.LString("err"))
  447. l.Push(lua.LString(err.Error()))
  448. }
  449. case execute_return_type_table: //返回table
  450. var ret = make(map[string]interface{})
  451. if err := b.ExecuteJS(tabTitle, tabUrl, script, &ret, timeout); err == nil {
  452. l.Push(lua.LString("ok"))
  453. l.Push(MapToTable(ret))
  454. } else {
  455. l.Push(lua.LString("err"))
  456. l.Push(lua.LString(err.Error()))
  457. }
  458. }
  459. return 2
  460. }))
  461. //按键
  462. s.SetGlobal("browser_keysend", s.NewFunction(func(l *lua.LState) int {
  463. fmt.Println("---browser_keysend---")
  464. tabTitle := l.ToString(-6)
  465. tabUrl := l.ToString(-5)
  466. timeout := l.ToInt64(-4)
  467. words := l.ToString(-3)
  468. selectorType := l.ToInt(-2)
  469. selector := l.ToString(-1)
  470. fmt.Println(selector, words, selectorType, timeout)
  471. err := b.KeySend(tabTitle, tabUrl, selector, words, selectorType, timeout)
  472. if err != nil {
  473. l.Push(lua.LString(err.Error()))
  474. } else {
  475. l.Push(lua.LString("ok"))
  476. }
  477. return 1
  478. }))
  479. //点击
  480. s.SetGlobal("browser_click", s.NewFunction(func(l *lua.LState) int {
  481. fmt.Println("---browser_click---")
  482. tabTitle := l.ToString(-5)
  483. tabUrl := l.ToString(-4)
  484. timeout := l.ToInt64(-3)
  485. selectorType := l.ToInt(-2)
  486. selector := l.ToString(-1)
  487. err := b.Click(tabTitle, tabUrl, selector, selectorType, timeout)
  488. if err != nil {
  489. l.Push(lua.LString(err.Error()))
  490. } else {
  491. l.Push(lua.LString("ok"))
  492. }
  493. return 1
  494. }))
  495. //等待元素加载
  496. s.SetGlobal("browser_waitvisible", s.NewFunction(func(l *lua.LState) int {
  497. fmt.Println("---browser_waitvisible---")
  498. tabTitle := l.ToString(-5)
  499. tabUrl := l.ToString(-4)
  500. timeout := l.ToInt64(-3)
  501. selectorType := l.ToInt(-2) //选择器类型
  502. selector := l.ToString(-1) //选择器
  503. err := b.WaitVisible(tabTitle, tabUrl, selector, selectorType, timeout)
  504. if err != nil {
  505. l.Push(lua.LString(err.Error()))
  506. } else {
  507. l.Push(lua.LString("ok"))
  508. }
  509. return 1
  510. }))
  511. //下载附件
  512. s.SetGlobal("browser_downloadfile", s.NewFunction(func(l *lua.LState) int {
  513. tabTitle := l.ToString(-6)
  514. tabUrl := l.ToString(-5)
  515. timeout := l.ToInt64(-4)
  516. selectorType := l.ToInt(-3)
  517. selector := l.ToString(-2)
  518. save2dir := l.ToString(-1)
  519. err := b.DownloadFile(tabTitle, tabUrl, timeout, selector, selectorType, save2dir)
  520. if err != nil {
  521. l.Push(lua.LString(err.Error()))
  522. } else {
  523. l.Push(lua.LString("ok"))
  524. }
  525. return 1
  526. }))
  527. //注册打开地址
  528. s.SetGlobal("browser_navagite_download_res", s.NewFunction(func(l *lua.LState) int {
  529. tabTitle := l.ToString(-7)
  530. tabUrl := l.ToString(-6)
  531. timeout := l.ToInt64(-5)
  532. isNewTab := l.ToBool(-4)
  533. targetUrl := l.ToString(-3)
  534. saveFileTypeList := l.ToString(-2)
  535. savedir := l.ToString(-1)
  536. if err := b.NavigateAndSaveRes(tabTitle, tabUrl, timeout, isNewTab, targetUrl, saveFileTypeList, savedir); err != nil {
  537. l.Push(lua.LString(err.Error()))
  538. } else {
  539. l.Push(lua.LString("ok"))
  540. }
  541. return 1
  542. }))
  543. //发布时间格式化
  544. s.SetGlobal("browser_publishtime", s.NewFunction(func(l *lua.LState) int {
  545. text := l.ToString(-1)
  546. publishtime := getPublitime(text)
  547. l.Push(lua.LString(publishtime))
  548. return 1
  549. }))
  550. //保存数据
  551. s.SetGlobal("browser_savedata", s.NewFunction(func(l *lua.LState) int {
  552. fmt.Println("---browser_upsertdata---")
  553. page := l.ToString(-2)
  554. data := l.ToTable(-1)
  555. result := TableToMap(data)
  556. if page == "list" {
  557. result["recordid"] = recordId
  558. }
  559. DataCache <- result
  560. return 1
  561. }))
  562. //获取数据
  563. s.SetGlobal("browser_getdata", s.NewFunction(func(l *lua.LState) int {
  564. fmt.Println("---browser_getdata---")
  565. num := l.ToInt(-1) //获取多少条数据
  566. count := len(Datas)
  567. if count == 0 {
  568. l.Push(lua.LString("err"))
  569. l.Push(lua.LString("当前可下载量为0"))
  570. } else {
  571. if count < num {
  572. num = count
  573. }
  574. data := Datas[:num]
  575. Datas = Datas[num:]
  576. tMap := MapToTable(map[string]interface{}{"data": data})
  577. l.Push(lua.LString("ok"))
  578. l.Push(tMap.RawGetString("data"))
  579. }
  580. return 2
  581. }))
  582. }