main_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // main_test
  2. package main
  3. import (
  4. "log"
  5. "luaweb/spider"
  6. "luaweb/util"
  7. mgdb "qfw/util/mongodb"
  8. "testing"
  9. "time"
  10. "github.com/lauyoume/gopinyin"
  11. )
  12. //模板测试
  13. func Test_Tmp(t *testing.T) {
  14. proficient := ""
  15. //通用变量配置
  16. common := []interface{}{
  17. "upload_test", "中央采购网-上传测试", "测试脚本栏目", true, 1, 10, 30, "bidding", "utf8", 1, 4002,
  18. "http://www.ccgp.gov.cn/zycg/zycgdt/index.htm",
  19. }
  20. //最新时间配置
  21. ptime := []interface{}{
  22. "yyyyMMddHHmm",
  23. "http://www.ccgp.gov.cn/zycg/zycgdt/index.htm",
  24. "ul li em:eq(0)",
  25. }
  26. // proficient = `function getLastPublishTime()
  27. // local content = download("",{})
  28. // return lastpushtime
  29. // end`
  30. log.Println(spider.GetLastPublishTime(common, ptime, proficient, 0))
  31. //列表页配置
  32. list := []interface{}{
  33. "http://www.ccgp.gov.cn/zycg/zycgdt/index#pageno#.htm",
  34. "'http://www.ccgp.gov.cn/zycg/zycgdt/index.htm','http://www.ccgp.gov.cn/zycg/zycgdt/index_1.htm'",
  35. "ul#main_list_lt_list>li",
  36. "a:eq(1):attr(href)",
  37. "a:eq(1):attr(title)",
  38. "em:eq(0)",
  39. "yyyyMMddHHmm",
  40. }
  41. model := map[string]interface{}{}
  42. model["type"] = "other"
  43. model["area"] = "HA"
  44. model["city"] = "郑州"
  45. model["publishdept"] = "郑州市财政厅"
  46. // proficient = `function downloadAndParseListPage(pageno)
  47. // end`
  48. log.Println(spider.GetPageList(common, list, model, proficient, 0))
  49. content := []interface{}{
  50. "div.TRS_Editor",
  51. "div.TRS_Editor",
  52. }
  53. data := map[string]interface{}{}
  54. data["title"] = "苏州市召开市级政府采购信用融资工作座谈会"
  55. data["href"] = "http://www.zfcg.suzhou.gov.cn/html/content/20160729165741501.shtml"
  56. data["publishtime"] = "2016-08-17 12:12:12"
  57. // proficient = `function downloadDetailPage(data)
  58. // end`
  59. log.Println(spider.GetContentInfo(common, content, data, proficient, 0))
  60. }
  61. //保存更新爬虫
  62. func Test_saveSpider(t *testing.T) {
  63. mgdb.InitMongodbPool(2, "192.168.3.18:27080", "luaweb")
  64. param := map[string]interface{}{}
  65. //通用变量配置
  66. common := []interface{}{
  67. "upload_test", "中央采购网-上传测试", "测试脚本栏目", true, 1, 10, 30, "bidding", "utf8", 1, 4002,
  68. "http://www.ccgp.gov.cn/zycg/zycgdt/index.htm",
  69. }
  70. //最新时间配置
  71. ptime := []interface{}{
  72. "yyyyMMddHHmm",
  73. "http://www.ccgp.gov.cn/zycg/zycgdt/",
  74. "ul li em:eq(0)",
  75. }
  76. param["type_time"] = 0 //0向导模式 1专家模式
  77. //列表页配置
  78. list := []interface{}{
  79. "http://www.ccgp.gov.cn/zycg/zycgdt/index#pageno#.htm",
  80. "'http://www.ccgp.gov.cn/zycg/zycgdt/index.htm','http://www.ccgp.gov.cn/zycg/zycgdt/index_1.htm'",
  81. "ul#main_list_lt_list li",
  82. "a:eq(1):attr(href)",
  83. "a:eq(1):attr(title)",
  84. "em:eq(0)",
  85. "yyyyMMddHHmm",
  86. }
  87. param["type_list"] = 0 //0向导模式 1专家模式
  88. //三级页配置
  89. content := []interface{}{
  90. "div.TRS_Editor",
  91. "div.TRS_Editor",
  92. }
  93. param["type_content"] = 0 //0向导模式 1专家模式
  94. param["param_common"] = common
  95. //向导模式
  96. param["param_time"] = ptime
  97. param["param_list"] = list
  98. param["param_content"] = content
  99. //专家模式
  100. param["str_time"] = `function getLastPublishTime()
  101. local content = download("href",{})
  102. return lastpushtime
  103. end`
  104. param["str_list"] = `function downloadAndParseListPage(pageno)
  105. end`
  106. param["str_content"] = `function downloadDetailPage(data)
  107. end`
  108. param["comeintime"] = time.Now().Unix()
  109. param["model"] = map[string]interface{}{
  110. "type": "tender",
  111. "area": "HA",
  112. "city": "郑州",
  113. "publishdept": "郑州市财政厅",
  114. } //补充数据模型
  115. param["createuser"] = "zjk" //姓名
  116. param["upload"] = false //是否上传
  117. spider.SaveSpider("upload_test", param)
  118. }
  119. //生成lua文件测试
  120. func Test_createFile(t *testing.T) {
  121. mgdb.InitMongodbPool(2, "192.168.3.18:27080", "luaweb")
  122. scritp := spider.GetScript("upload_test")
  123. if scritp != "" {
  124. _, err := spider.CreateFile("upload_test", scritp)
  125. log.Println(err)
  126. }
  127. }
  128. //上传脚本
  129. func Test_uploadFile(t *testing.T) {
  130. util.InitMsgClient(util.Config.Msgserveraddr, util.Config.Msgname)
  131. mgdb.InitMongodbPool(2, "192.168.3.18:27080", "luaweb")
  132. ret, err := spider.UpLoadScript("upload_test", 7001)
  133. if err != nil {
  134. log.Println("err", err)
  135. } else {
  136. log.Println("ret", ret)
  137. }
  138. }
  139. //获取拼音首子母
  140. func Test_getFirstName(t *testing.T) {
  141. str := gopinyin.Convert("HA_河南省政府采购网_招标公告", true)
  142. log.Println(str)
  143. }
  144. func TestCheck(t *testing.T) {
  145. steps := make([]bool, 3)
  146. s := spider.CreateSpider(spider.GetScript("zgwlzbw_zbgg"))
  147. s.SpiderMaxPage = 1
  148. time, timeerr := s.GetLastPublishTime()
  149. if timeerr != nil || len(time) < 5 {
  150. steps[0] = false
  151. return
  152. } else {
  153. list, listerr := s.DownListPageItem()
  154. if listerr != nil || len(list) == 0 {
  155. steps[0] = false
  156. } else {
  157. steps[0] = true
  158. listone := list[0]
  159. if len(listone["href"].(string)) < 7 || len(listone["publishtime"].(string)) < 5 || len(listone["title"].(string)) < 10 {
  160. steps[1] = false
  161. } else {
  162. steps[1] = true
  163. param := map[string]string{}
  164. param["title"] = list[0]["title"].(string)
  165. param["href"] = list[0]["href"].(string)
  166. param["publishtime"] = list[0]["publishtime"].(string)
  167. data := map[string]interface{}{}
  168. s.DownloadDetailPage(param, data)
  169. if len(data) == 0 || len(data["detail"].(string)) < 50 {
  170. steps[2] = false
  171. } else {
  172. steps[2] = true
  173. }
  174. }
  175. }
  176. }
  177. }