spider.go 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340
  1. package front
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "log"
  7. "sort"
  8. "spider"
  9. // "math/rand"
  10. mu "mfw/util"
  11. qu "qfw/util"
  12. mgdb "qfw/util/mongodb"
  13. mgu "qfw/util/mongodbutil"
  14. util "spiderutil"
  15. "strings"
  16. "time"
  17. u "util"
  18. "gopkg.in/mgo.v2/bson"
  19. )
  20. type Base struct {
  21. SpiderCode string
  22. SpiderCodeOld string
  23. SpiderName string
  24. SpiderChannel string
  25. SpiderDownDetailPage bool
  26. SpiderStartPage int
  27. SpiderMaxPage int
  28. SpiderRunRate int
  29. Spider2Collection string
  30. SpiderPageEncoding string
  31. SpiderStoreMode int //1,2
  32. SpiderStoreToMsgEvent int
  33. SpiderTargetChannelUrl string
  34. SpiderLastDownloadTime string
  35. SpiderIsHistoricalMend bool
  36. SpiderIsMustDownload bool
  37. }
  38. type Step1 struct {
  39. Address string
  40. ContentChooser string
  41. DateFormat string
  42. Expert string
  43. Types int
  44. }
  45. type Step2 struct {
  46. Listadd string
  47. Listadds string
  48. BlockChooser string
  49. AddressChooser string
  50. TitleChooser string
  51. DateChooser string
  52. DateFormat string
  53. Expert string
  54. Types int
  55. }
  56. type Step3 struct {
  57. ContentChooser string
  58. ElementChooser string
  59. T_title string
  60. T_href string
  61. T_date string
  62. Expert string
  63. Types int
  64. }
  65. type StepRe3 struct {
  66. Checked bool
  67. Expert string
  68. }
  69. //加载某个爬虫
  70. func (f *Front) LoadSpider(codeTaskIdReState string) error {
  71. tmpStr := strings.Split(codeTaskIdReState, "__")
  72. code := tmpStr[0]
  73. taskId := tmpStr[1]
  74. auth := qu.IntAll(f.GetSession("auth"))
  75. restate := -1
  76. if taskId == "restate=1" { //重采编辑
  77. restate = 1
  78. } else if taskId == "restate=2" {
  79. restate = 2
  80. } else if taskId == "restate=3" {
  81. restate = 3
  82. } else {
  83. if auth == role_dev && (f.GetSession(taskId) == nil || f.GetSession(taskId) == "") {
  84. xgTime := time.Unix(time.Now().Unix(), 0).Format("2006-01-02 15:04:05")
  85. f.SetSession(taskId, xgTime)
  86. }
  87. }
  88. copy := f.GetString("copy")
  89. if f.Method() == "GET" {
  90. code := util.Se.Decode4Hex(code)
  91. f.T["actiontext"] = "编辑"
  92. lua := *mgdb.FindOne("luaconfig", bson.M{"code": code})
  93. auth := qu.IntAll(f.GetSession("auth"))
  94. if lua["createuserid"].(string) == f.GetSession("userid").(string) || auth >= 1 {
  95. if len(lua) > 0 {
  96. luacopy := map[string]interface{}{}
  97. if copy != "" {
  98. luacopy = *mgdb.FindOne("luaconfig", bson.M{"code": copy})
  99. if len(luacopy) > 0 {
  100. lua["model"] = luacopy["model"]
  101. common_copy := luacopy["param_common"].([]interface{})
  102. common := lua["param_common"].([]interface{})
  103. common_copy[0] = common[0]
  104. common_copy[1] = common[1]
  105. common_copy[2] = common[2]
  106. common_copy[11] = common[11]
  107. lua["param_common"] = luacopy["param_common"]
  108. lua["param_time"] = luacopy["param_time"]
  109. lua["param_list"] = luacopy["param_list"]
  110. lua["param_content"] = luacopy["param_content"]
  111. lua["str_list"] = luacopy["str_list"]
  112. lua["str_time"] = luacopy["str_time"]
  113. lua["str_content"] = luacopy["str_content"]
  114. lua["Thref"] = luacopy["Thref"]
  115. lua["Tpublishtime"] = luacopy["Tpublishtime"]
  116. lua["Ttitle"] = luacopy["Ttitle"]
  117. lua["Tdate"] = luacopy["Tdate"]
  118. lua["type_content"] = luacopy["type_content"]
  119. lua["type_list"] = luacopy["type_list"]
  120. lua["type_time"] = luacopy["type_time"]
  121. }
  122. }
  123. if lua["listcheck"] != nil {
  124. listcheck := lua["listcheck"].(string)
  125. listcheck = strings.Replace(listcheck, "\\n", "\n", -1)
  126. listcheck = strings.Replace(listcheck, "\\", "", -1)
  127. lua["listcheck"] = listcheck
  128. }
  129. if lua["contentcheck"] != nil {
  130. contentcheck := lua["contentcheck"].(string)
  131. contentcheck = strings.Replace(contentcheck, "\\n", "\n", -1)
  132. contentcheck = strings.Replace(contentcheck, "\\", "", -1)
  133. lua["contentcheck"] = contentcheck
  134. }
  135. js, _ := json.MarshalIndent(lua["model"], "", " ")
  136. lua["js"] = string(js)
  137. f.T["lua"] = lua
  138. f.T["taskId"] = taskId
  139. f.T["restate"] = restate
  140. f.T["isflow"] = lua["isflow"]
  141. f.T["spidertype"] = lua["spidertype"]
  142. f.T["spidermovevent"] = lua["spidermovevent"]
  143. f.T["spiderhistorymaxpage"] = lua["spiderhistorymaxpage"]
  144. if lua["oldlua"] != nil {
  145. return f.Render("oldedit.html", &f.T)
  146. }
  147. return f.Render("spideredit.html", &f.T)
  148. }
  149. } else {
  150. f.Write("您没有编辑他人脚本的权限")
  151. }
  152. }
  153. return nil
  154. }
  155. //查看某个爬虫
  156. func (f *Front) ViewSpider(id string) error {
  157. auth := qu.IntAll(f.GetSession("auth"))
  158. if auth >= 1 {
  159. if f.Method() == "GET" {
  160. code := util.Se.Decode4Hex(id)
  161. f.T["actiontext"] = "编辑"
  162. lua := *mgdb.FindOne("luaconfig", bson.M{"code": code})
  163. if len(lua) > 0 {
  164. if lua["listcheck"] != nil {
  165. listcheck := lua["listcheck"].(string)
  166. listcheck = strings.Replace(listcheck, "\\n", "\n", -1)
  167. listcheck = strings.Replace(listcheck, "\\", "", -1)
  168. lua["listcheck"] = listcheck
  169. }
  170. if lua["contentcheck"] != nil {
  171. contentcheck := lua["contentcheck"].(string)
  172. contentcheck = strings.Replace(contentcheck, "\\n", "\n", -1)
  173. contentcheck = strings.Replace(contentcheck, "\\", "", -1)
  174. lua["contentcheck"] = contentcheck
  175. }
  176. js, _ := json.MarshalIndent(lua["model"], "", " ")
  177. lua["js"] = string(js)
  178. f.T["lua"] = lua
  179. f.T["isflow"] = lua["isflow"]
  180. f.T["spidertype"] = lua["spidertype"]
  181. f.T["spidermovevent"] = lua["spidermovevent"]
  182. f.T["spiderhistorymaxpage"] = lua["spiderhistorymaxpage"]
  183. if lua["oldlua"] != nil {
  184. return f.Render("oldedit.html", &f.T)
  185. }
  186. return f.Render("spiderview.html", &f.T)
  187. } else {
  188. f.Write("没有对应记录!")
  189. return nil
  190. }
  191. }
  192. return f.Redirect("/center")
  193. } else {
  194. f.Write("您没有查看他人脚本的权限")
  195. return nil
  196. }
  197. }
  198. func (f *Front) LoadModel(id string) error {
  199. if f.Method() == "GET" {
  200. lua := *mgdb.Find("luaconfig", bson.M{"code": id}, nil, bson.M{"model": 1}, true, -1, -1)
  201. if len(lua) > 0 {
  202. f.ServeJson(lua[0])
  203. }
  204. }
  205. return f.Redirect("/center")
  206. }
  207. func (f *Front) SaveStep() {
  208. userid, _ := f.GetSession("userid").(string)
  209. auth := qu.IntAll(f.GetSession("auth"))
  210. rep := map[string]interface{}{}
  211. if f.GetString("oldlua") != "" {
  212. id := f.GetString("code")
  213. one := *mgdb.FindOne("luaconfig", bson.M{"code": id})
  214. id = one["code"].(string)
  215. script := f.GetStringComm("script")
  216. if strings.Index(script, id) == -1 {
  217. rep["msg"] = "code/名称都不能更改"
  218. f.ServeJson(rep)
  219. return
  220. } else {
  221. upset := bson.M{"luacontent": script}
  222. upset["modifytime"] = time.Now().Unix()
  223. b := mgdb.Update("luaconfig", bson.M{"code": id}, bson.M{"$set": upset}, true, false)
  224. if b {
  225. rep["msg"] = "保存成功"
  226. rep["code"] = util.Se.Encode2Hex(id)
  227. f.ServeJson(rep)
  228. return
  229. }
  230. }
  231. } else {
  232. if f.Base.SpiderName != "" && f.Base.SpiderCode != "" {
  233. code := f.Base.SpiderCode
  234. one := *mgdb.FindOne("luaconfig", bson.M{"code": f.Base.SpiderCode})
  235. state := qu.IntAllDef(one["state"], 0)
  236. restate := qu.IntAll(one["restate"])
  237. comeintime := time.Now().Unix()
  238. if len(one) > 0 {
  239. comeintime = qu.Int64All(one["comeintime"])
  240. ouserid := one["createuserid"].(string)
  241. if ouserid != userid && auth == role_dev {
  242. f.Write("权限不够,不能修改他人脚本")
  243. return
  244. } else {
  245. code = one["code"].(string)
  246. f.Base.SpiderCode = one["code"].(string)
  247. f.Base.SpiderName = (one["param_common"].([]interface{}))[1].(string)
  248. }
  249. } else {
  250. if auth != role_admin {
  251. f.Write("不能新建爬虫,请联系管理员导入")
  252. return
  253. }
  254. }
  255. listcheck := f.GetString("listcheck")
  256. contentcheck := f.GetString("contentcheck")
  257. if auth == role_dev {
  258. //f.Base.SpiderStoreToMsgEvent = 4002
  259. }
  260. common := []interface{}{
  261. f.Base.SpiderCode,
  262. f.Base.SpiderName,
  263. f.Base.SpiderChannel,
  264. f.Base.SpiderDownDetailPage,
  265. f.Base.SpiderStartPage,
  266. f.Base.SpiderMaxPage,
  267. f.Base.SpiderRunRate,
  268. f.Base.Spider2Collection,
  269. f.Base.SpiderPageEncoding,
  270. f.Base.SpiderStoreMode,
  271. f.Base.SpiderStoreToMsgEvent,
  272. f.Base.SpiderTargetChannelUrl,
  273. f.Base.SpiderLastDownloadTime,
  274. f.Base.SpiderIsHistoricalMend,
  275. f.Base.SpiderIsMustDownload,
  276. }
  277. ptime := []interface{}{
  278. f.Step1.DateFormat,
  279. f.Step1.Address,
  280. f.Step1.ContentChooser,
  281. }
  282. list := []interface{}{
  283. f.Step2.Listadd,
  284. f.Step2.Listadds,
  285. f.Step2.BlockChooser,
  286. f.Step2.AddressChooser,
  287. f.Step2.TitleChooser,
  288. f.Step2.DateChooser,
  289. f.Step2.DateFormat,
  290. }
  291. content := []interface{}{
  292. f.Step3.ContentChooser,
  293. f.Step3.ElementChooser,
  294. }
  295. param := map[string]interface{}{}
  296. common[4] = 1
  297. param["param_common"] = common
  298. //向导模式
  299. param["param_time"] = ptime
  300. param["param_list"] = list
  301. param["param_content"] = content
  302. param["type_time"] = f.Step1.Types
  303. param["type_list"] = f.Step2.Types
  304. param["type_content"] = f.Step3.Types
  305. //专家模式
  306. param["str_time"] = f.Step1.Expert
  307. param["str_list"] = f.Step2.Expert
  308. param["str_content"] = f.Step3.Expert
  309. param["comeintime"] = comeintime
  310. listcheck = strings.Replace(listcheck, "\n", "\\\\n", -1)
  311. param["listcheck"] = strings.Replace(listcheck, "\"", "\\\\\"", -1)
  312. contentcheck = strings.Replace(contentcheck, "\n", "\\\\n", -1)
  313. param["contentcheck"] = strings.Replace(contentcheck, "\"", "\\\\\"", -1)
  314. //补充模型
  315. s_model := f.GetString("model")
  316. configModel := util.Config.Model[s_model]
  317. model := map[string]interface{}{}
  318. for k, _ := range configModel {
  319. model[k] = f.GetString(k)
  320. }
  321. model["model"] = s_model
  322. param["code"] = f.Base.SpiderCode
  323. param["model"] = model
  324. if len(one) > 0 {
  325. param["createuser"] = one["createuser"]
  326. param["createuserid"] = one["createuserid"]
  327. param["code"] = one["code"]
  328. //开发员关联任务修改爬虫状态
  329. state = qu.IntAll(one["state"])
  330. if auth == role_dev && state >= Sp_state_3 && restate != 1 { //开发员修改,已经审核通过(不包含已上架),状态重置为待完成(restate!=1判断,重采修改保存爬虫时不修改爬虫状态)
  331. param["state"] = 0
  332. } else {
  333. param["state"] = state
  334. }
  335. } else {
  336. param["createuser"] = f.GetSession("loginuser")
  337. param["createuserid"] = f.GetSession("userid")
  338. param["createuseremail"] = f.GetSession("email")
  339. param["next"] = f.GetSession("email")
  340. param["state"] = 0
  341. }
  342. if qu.ObjToString(one["modifyuser"]) == "" {
  343. param["modifyuser"] = param["createuser"]
  344. param["modifyuserid"] = param["createuserid"]
  345. }
  346. param["modifytime"] = time.Now().Unix()
  347. param["Ttitle"] = f.Step3.T_title
  348. param["Thref"] = f.Step3.T_href
  349. param["Tdate"] = f.Step3.T_date
  350. //其他信息
  351. param["isflow"] = f.OtherBase.IsFlow
  352. param["spidertype"] = f.OtherBase.SpiderType
  353. param["spidermovevent"] = f.OtherBase.SpiderMoveEvent
  354. param["spiderhistorymaxpage"] = f.OtherBase.SpiderHistoryMaxPage
  355. if f.OtherBase.SpiderType == "history" { //爬虫类型是history的放到7000节点,并记录历史节点
  356. param["event"] = 7000
  357. if event := qu.IntAll(one["event"]); event != 7000 {
  358. param["historyevent"] = event
  359. }
  360. }
  361. if f.OtherBase.SpiderMoveEvent == "7700" {
  362. param["historyevent"] = 7700
  363. }
  364. //三级页复制
  365. param["str_recontent"] = f.StepRe3.Expert
  366. param["iscopycontent"] = f.StepRe3.Checked
  367. issave := spider.SaveSpider(code, param) //保存脚本
  368. if issave {
  369. for k, v := range one {
  370. if k != "_id" && param[k] == nil {
  371. param[k] = v
  372. }
  373. }
  374. Wlog(f.Base.SpiderName, f.Base.SpiderCode, f.GetSession("username").(string), f.GetSession("userid").(string), "修改", param)
  375. rep["msg"] = "保存成功"
  376. } else {
  377. rep["msg"] = "保存失败"
  378. }
  379. rep["code"] = util.Se.Encode2Hex(code)
  380. f.ServeJson(rep)
  381. }
  382. }
  383. }
  384. //方法测试
  385. func (f *Front) RunStep() {
  386. imodal, _ := f.GetInteger("imodal")
  387. script, _ := f.GetBool("script")
  388. listcheck := f.GetString("listcheck")
  389. contentcheck := f.GetString("contentcheck")
  390. downloadnode := f.GetString("downloadnode") //下载节点
  391. common := []interface{}{
  392. f.Base.SpiderCode,
  393. f.Base.SpiderName,
  394. f.Base.SpiderChannel,
  395. f.Base.SpiderDownDetailPage,
  396. f.Base.SpiderStartPage,
  397. f.Base.SpiderMaxPage,
  398. f.Base.SpiderRunRate,
  399. f.Base.Spider2Collection,
  400. f.Base.SpiderPageEncoding,
  401. f.Base.SpiderStoreMode,
  402. f.Base.SpiderStoreToMsgEvent,
  403. f.Base.SpiderTargetChannelUrl,
  404. f.Base.SpiderLastDownloadTime,
  405. f.Base.SpiderIsHistoricalMend,
  406. f.Base.SpiderIsMustDownload,
  407. "",
  408. "",
  409. "",
  410. }
  411. if f.Method() == "POST" {
  412. switch f.GetString("step") {
  413. case "step1":
  414. ptime := []interface{}{
  415. f.Step1.DateFormat,
  416. f.Step1.Address,
  417. f.Step1.ContentChooser,
  418. }
  419. if script {
  420. _, scripts := spider.GetLastPublishTime(common, ptime, f.Step1.Expert, downloadnode, imodal, 1)
  421. f.ServeJson(scripts)
  422. return
  423. }
  424. rs, err := spider.GetLastPublishTime(common, ptime, f.Step1.Expert, downloadnode, imodal)
  425. if err == nil {
  426. f.ServeJson(rs)
  427. }
  428. case "step2":
  429. addrs := strings.Split(f.Step2.Listadds, "\n")
  430. if len(addrs) > 0 {
  431. for k, v := range addrs {
  432. addrs[k] = "'" + v + "'"
  433. }
  434. f.Step2.Listadds = strings.Join(addrs, ",")
  435. } else if len(f.Step2.Listadds) > 5 {
  436. f.Step2.Listadds = "'" + f.Step2.Listadds + "'"
  437. } else {
  438. f.Step2.Listadds = ""
  439. }
  440. list := []interface{}{
  441. f.Step2.Listadd,
  442. f.Step2.Listadds,
  443. f.Step2.BlockChooser,
  444. f.Step2.AddressChooser,
  445. f.Step2.TitleChooser,
  446. f.Step2.DateChooser,
  447. f.Step2.DateFormat,
  448. }
  449. listcheck = strings.Replace(listcheck, "\n", "\\n", -1)
  450. listcheck = strings.Replace(listcheck, "\"", "\\\"", -1)
  451. s_model := f.GetString("model")
  452. configModel := util.Config.Model[s_model]
  453. model := map[string]interface{}{}
  454. for k, _ := range configModel {
  455. model[k] = f.GetString(k)
  456. }
  457. if script {
  458. _, script := spider.GetPageList(common, list, model, listcheck, f.Step2.Expert, downloadnode, imodal, 1)
  459. f.ServeJson(script)
  460. return
  461. }
  462. rs, err := spider.GetPageList(common, list, model, listcheck, f.Step2.Expert, downloadnode, imodal)
  463. if err == nil {
  464. f.ServeJson(rs)
  465. } else if err.(error).Error() == "no" {
  466. f.ServeJson(rs[0])
  467. }
  468. case "step3":
  469. content := []interface{}{
  470. f.Step3.ContentChooser,
  471. f.Step3.ElementChooser,
  472. }
  473. contentcheck = strings.Replace(contentcheck, "\n", "\\n", -1)
  474. contentcheck = strings.Replace(contentcheck, "\"", "\\\"", -1)
  475. data := map[string]interface{}{}
  476. data["title"] = f.Step3.T_title
  477. data["href"] = f.Step3.T_href
  478. data["publishtime"] = f.Step3.T_date
  479. if script {
  480. _, script := spider.GetContentInfo(common, content, data, contentcheck, f.Step3.Expert, downloadnode, imodal, 1)
  481. f.ServeJson(script)
  482. return
  483. }
  484. rs, err := spider.GetContentInfo(common, content, data, contentcheck, f.Step3.Expert, downloadnode, imodal)
  485. if projectinfo, ok := rs["projectinfo"].(map[string]interface{}); ok && projectinfo != nil {
  486. if attachments, ok := projectinfo["attachments"].(map[string]interface{}); ok && attachments != nil {
  487. for _, tmp := range attachments {
  488. tmpMap := tmp.(map[string]interface{})
  489. if qu.ObjToString(tmpMap["filename"]) == "附件中含有乱码" {
  490. rs["msg"] = "附件中含有乱码"
  491. }
  492. }
  493. }
  494. }
  495. if err == nil {
  496. f.ServeJson(rs)
  497. } else {
  498. f.ServeJson(rs["no"])
  499. }
  500. }
  501. }
  502. }
  503. //爬虫测试数据json
  504. func (f *Front) GetJson() {
  505. list, _ := f.GetSession("listInfo").([]map[string]interface{})
  506. data, _ := f.GetSession("dataInfo").(map[string]interface{})
  507. descript := f.GetSession("task_descript")
  508. remark := f.GetSession("task_remark")
  509. reason := f.GetSession("reason")
  510. username := f.GetSession("username").(string)
  511. msg := f.GetSession(username + "_msg")
  512. if len(data) > 0 {
  513. data["contenthtml"] = ""
  514. }
  515. for k, v := range list {
  516. v["a_index"] = k + 1
  517. }
  518. f.T["list"] = list
  519. f.T["data"] = data
  520. f.T["descript"] = descript
  521. f.T["remark"] = remark
  522. f.T["reason"] = reason
  523. f.T["msg"] = msg
  524. f.DelSession("listInfo")
  525. f.DelSession("dataInfo")
  526. f.DelSession("task_descript")
  527. f.DelSession("task_remark")
  528. f.DelSession("reason")
  529. f.Render("jsonInfo.html", &f.T)
  530. }
  531. //整体测试
  532. func (f *Front) SpiderPass() {
  533. defer mu.Catch()
  534. list := []map[string]interface{}{}
  535. data := map[string]interface{}{}
  536. msg1 := ""
  537. code := f.GetString("code")
  538. downloadnode := f.GetString("node")
  539. //根据code查询待确认任务
  540. query := bson.M{
  541. "s_code": code,
  542. "i_state": 3,
  543. }
  544. task := *mgdb.FindOne("task", query)
  545. descript := "null"
  546. remark := "null"
  547. remarktmp := []string{}
  548. if len(task) > 0 {
  549. descript = task["s_descript"].(string)
  550. if mrecord, ok := task["a_mrecord"].([]interface{}); ok {
  551. for _, m := range mrecord {
  552. remarkInfo := m.(map[string]interface{})
  553. r := remarkInfo["s_mrecord_remark"].(string)
  554. if r != "" {
  555. remarktmp = append(remarktmp, r+";")
  556. }
  557. }
  558. }
  559. }
  560. if len(remarktmp) > 0 {
  561. remark = ""
  562. remark = strings.Join(remarktmp, "")
  563. }
  564. f.SetSession("task_remark", remark)
  565. f.SetSession("task_descript", descript)
  566. //基本信息、方法一、方法二、方法三、总请求次数、go方法一、go方法二、go方法三、列表页条数
  567. steps := []interface{}{false, false, false, false, 0, 0, 0, 0, 0}
  568. one := *mgdb.FindOne("luaconfig", bson.M{"code": code})
  569. reason, _ := one["reason"].(string)
  570. f.SetSession("reason", reason)
  571. if len(one) > 0 && one["oldlua"] == nil {
  572. common := one["param_common"].([]interface{})
  573. if len(common) < 13 {
  574. f.ServeJson(steps)
  575. return
  576. } else {
  577. steps[0] = true
  578. }
  579. } else {
  580. steps[0] = true
  581. }
  582. script, liststr, contentstr := "", "", ""
  583. if one["oldlua"] == nil {
  584. script, liststr, contentstr = spider.GetScript(code)
  585. } else {
  586. script = one["luacontent"].(string)
  587. }
  588. if liststr != "" && contentstr != "" {
  589. msg1 = u.SpiderPassCheckLua(liststr, contentstr, one)
  590. }
  591. s := spider.CreateSpider(downloadnode, script)
  592. s.SpiderMaxPage = 1
  593. s.Timeout = 60
  594. time, timeerr := s.GetLastPublishTime()
  595. if timeerr == nil && len(time) > 4 {
  596. steps[1] = true
  597. list, _ = s.DownListPageItem()
  598. if len(list) > 0 {
  599. f.SetSession("listInfo", list)
  600. listone := list[0]
  601. if len(qu.ObjToString(listone["href"])) < 7 ||
  602. (qu.ObjToString(listone["publishtime"]) != "0" && len(qu.ObjToString(listone["publishtime"])) < 5) ||
  603. len(qu.ObjToString(listone["title"])) < 3 {
  604. f.ServeJson(steps)
  605. return
  606. } else {
  607. steps[2] = true
  608. if s.DownDetail {
  609. param := map[string]string{}
  610. index := 0
  611. if len(list) > 0 {
  612. steps[8] = len(list)
  613. index = len(list) / 2
  614. for k, v := range list[index] {
  615. param[k] = qu.ObjToString(v)
  616. }
  617. data = map[string]interface{}{}
  618. s.DownloadDetailPage(param, data)
  619. if len(data) > 0 {
  620. f.SetSession("dataInfo", data)
  621. } else {
  622. f.SetSession("dataInfo", "")
  623. }
  624. if len(data) == 0 || data["detail"].(string) == "" {
  625. steps[3] = false
  626. } else {
  627. steps[3] = true
  628. }
  629. }
  630. } else {
  631. steps[3] = true
  632. }
  633. }
  634. } else {
  635. f.SetSession("listInfo", "")
  636. f.SetSession("dataInfo", "")
  637. }
  638. }
  639. //关闭laustate
  640. s.L.Close()
  641. steps[4] = s.Test_luareqcount
  642. steps[5] = s.Test_goreqtime
  643. steps[6] = s.Test_goreqlist
  644. steps[7] = s.Test_goreqcon
  645. //校验
  646. msg := u.SpiderPassCheckListAndDetail(list, data)
  647. if msg1 != "" {
  648. msg = msg1 + "," + msg
  649. }
  650. username := f.GetSession("username").(string)
  651. f.SetSession(username+"_msg", msg)
  652. f.ServeJson(steps)
  653. }
  654. func (f *Front) DownSpider(id string) {
  655. //auth := qu.IntAll(f.GetSession("auth"))
  656. //if auth > role_dev {
  657. one := *mgdb.FindOne("luaconfig", bson.M{"code": id})
  658. script := ""
  659. filename := id + ".lua"
  660. if len(one) > 0 {
  661. if one["oldlua"] != nil {
  662. if one["luacontent"] != nil {
  663. script = one["luacontent"].(string)
  664. }
  665. } else {
  666. user := *mgdb.FindOne("user", bson.M{"_id": bson.ObjectIdHex(one["createuserid"].(string))})
  667. name := one["createuser"]
  668. email := user["s_email"]
  669. upload := time.Now().Format("2006-01-02 15:04:05")
  670. script, _, _ = spider.GetScript(id, name, email, upload)
  671. }
  672. }
  673. f.ResponseWriter.Header().Del("Content-Type")
  674. f.ResponseWriter.Header().Add("Content-Type", "application/x-download")
  675. f.ResponseWriter.Header().Add("Content-Disposition", "attachment;filename=spider_"+filename)
  676. f.WriteBytes([]byte(script))
  677. // } else {
  678. // f.Write("您没有权限")
  679. // }
  680. }
  681. //更新爬虫状态
  682. func (f *Front) UpState() error {
  683. username := f.GetSession("username").(string)
  684. code := f.GetString("code")
  685. state, _ := f.GetInt("state")
  686. id := f.GetString("taskId")
  687. reason := f.GetString("reason")
  688. auth := qu.IntAll(f.GetSession("auth"))
  689. var codeArr = []string{code}
  690. var taskid []string
  691. //修改任务状态
  692. istotask := false
  693. res := map[string]interface{}{
  694. "istotask": istotask,
  695. "err": "没有权限",
  696. "code": util.Se.Encode2Hex(code),
  697. "taskid": taskid,
  698. }
  699. var xgTime int64
  700. if f.GetSession(id) == nil || f.GetSession(id) == "" {
  701. xgTime = time.Now().Unix()
  702. } else {
  703. xgTimeStr := qu.ObjToString(f.GetSession(id))
  704. xgTimeTmp, _ := time.ParseInLocation("2006-01-02 15:04:05", xgTimeStr, time.Local)
  705. xgTime = xgTimeTmp.Unix()
  706. }
  707. f.DelSession(id)
  708. if IsHasUpState(auth, int(state)) {
  709. b, err := UpStateAndUpSpider(code, "", reason, username, int(state)) //更新爬虫状态
  710. if b && state == Sp_state_1 { //提交审核
  711. //有对应任务跳转提交记录页
  712. taskid = checkTask(codeArr, 1)
  713. if len(taskid) > 0 {
  714. res["istotask"] = true
  715. res["taskid"] = taskid[0]
  716. }
  717. } else if b && state == Sp_state_2 { //打回
  718. taskid = checkTask(codeArr, 2)
  719. if len(taskid) > 0 {
  720. //UpTaskState([]string{taskid}, 2) //修改状态
  721. UpTaskState(taskid, 2, "", int64(0)) //修改任务状态
  722. SaveRemark(taskid, reason, username) //保存记录信息
  723. }
  724. } else if b && state == Sp_state_3 { //审核通过
  725. taskid = checkTask(codeArr, 3)
  726. if len(taskid) > 0 {
  727. //UpTaskState([]string{taskid}, 3)
  728. UpTaskState(taskid, 3, "", int64(0))
  729. SaveRemark(taskid, "", username)
  730. }
  731. } else if b && state == Sp_state_6 { //下架
  732. //下架成功删除download数据
  733. flag := delDownloadData(code)
  734. log.Println(code, "---下架删除download数据:", flag)
  735. } else if b && state == Sp_state_7 { //反馈
  736. taskid = checkTask(codeArr, 7)
  737. if len(taskid) > 0 {
  738. UpTaskState(taskid, 7, reason, xgTime)
  739. }
  740. }
  741. if err != nil {
  742. res["err"] = err.Error()
  743. f.ServeJson(res)
  744. } else {
  745. res["err"] = ""
  746. f.ServeJson(res)
  747. }
  748. } else {
  749. f.ServeJson(res)
  750. }
  751. return nil
  752. }
  753. //下架删除download数据
  754. func delDownloadData(code string) bool {
  755. return mgu.Del("download", "spider", "spider", `{"code":"`+code+`"}`)
  756. }
  757. //批量作废删除download数据
  758. func disableDelDownloadData(code []string) {
  759. for _, v := range code {
  760. flag := delDownloadData(v)
  761. log.Println(code, "---批量删除download数据:", flag)
  762. }
  763. }
  764. //爬虫核对
  765. func (f *Front) Checktime() {
  766. code := f.GetString("code")
  767. auth := qu.IntAll(f.GetSession("auth"))
  768. if auth != role_admin {
  769. f.ServeJson(false)
  770. } else {
  771. b := mgdb.Update("luaconfig", `{"code":"`+code+`"}`, `{"$set":{"l_checktime":`+fmt.Sprint(time.Now().Unix())+`}}`, true, false)
  772. f.ServeJson(b)
  773. }
  774. }
  775. //批量作废
  776. func (f *Front) Disables() error {
  777. auth := qu.IntAll(f.GetSession("auth"))
  778. names := strings.Split(f.GetString("names"), ",")
  779. ids := strings.Split(f.GetString("ids"), ",")
  780. codes := strings.Split(f.GetString("codes"), ",")
  781. disablereason := f.GetString("disablereason")
  782. res := ""
  783. if IsHasUpState(auth, Sp_state_4) {
  784. for k, id := range ids {
  785. b, err := UpStateAndUpSpider("", id, disablereason, "", Sp_state_4)
  786. if b { //作废成功
  787. //修改任务状态
  788. UpTaskState(codes, 4, "", int64(0))
  789. //删除download表数据
  790. //go disableDelDownloadData(codes)
  791. if err != nil {
  792. res = res + names[k] + ",ok" + qu.ObjToString(err.Error()) + ";"
  793. } else {
  794. res = res + names[k] + ",ok" + ";"
  795. }
  796. } else {
  797. res = res + names[k] + "," + qu.ObjToString(err.Error()) + ";"
  798. }
  799. }
  800. } else {
  801. res = "没有权限"
  802. }
  803. f.ServeJson(res)
  804. return nil
  805. }
  806. //批量上下架
  807. func (f *Front) BatchShelves() {
  808. codes := strings.Split(f.GetString("codes"), ",")
  809. state, _ := f.GetInteger("state")
  810. auth := qu.IntAll(f.GetSession("auth"))
  811. errCode := []string{}
  812. var err error
  813. b := false
  814. if IsHasUpState(auth, Sp_state_5) {
  815. if state == 5 { //批量上架
  816. for _, code := range codes {
  817. _, err = UpStateAndUpSpider(code, "", "", "", Sp_state_5)
  818. if err != nil {
  819. errCode = append(errCode, code)
  820. }
  821. }
  822. } else { //批量下架
  823. for _, code := range codes {
  824. b, err = UpStateAndUpSpider(code, "", "", "", Sp_state_6)
  825. if err != nil {
  826. errCode = append(errCode, code)
  827. }
  828. //下架删除download数据
  829. if b {
  830. flag := delDownloadData(code)
  831. log.Println(code, "---删除download数据:", flag)
  832. }
  833. }
  834. }
  835. } else {
  836. errCode = append(errCode, "没有权限")
  837. }
  838. f.ServeJson(errCode)
  839. }
  840. //更新爬虫状态,并判断是否更新节点爬虫
  841. func UpStateAndUpSpider(code, id, reason, username string, state int) (bool, error) {
  842. upresult := false
  843. var err error
  844. one := map[string]interface{}{}
  845. if code != "" {
  846. one = *mgdb.FindOne("luaconfig", bson.M{"code": code})
  847. } else {
  848. one = *mgdb.FindOne("luaconfig", bson.M{"_id": bson.ObjectIdHex(id)})
  849. code = one["code"].(string)
  850. }
  851. if len(one) > 0 {
  852. var event int
  853. if one["event"] != nil {
  854. event = qu.IntAll(one["event"])
  855. } else {
  856. for k, _ := range util.Config.Uploadevents { //?
  857. event = qu.IntAll(k)
  858. break
  859. }
  860. //r := rand.New(rand.NewSource(time.Now().UnixNano()))
  861. //event = util.Config.Uploadevents[r.Intn(len(util.Config.Uploadevents))]
  862. }
  863. //oldstate := qu.IntAll(one["state"])
  864. switch state {
  865. case Sp_state_4: //作废
  866. // if oldstate == Sp_state_5 {
  867. // upresult = false
  868. // err = errors.New("已上架不允许作废")
  869. // } else {
  870. // upresult = true
  871. // }
  872. upresult, err = spider.UpdateSpiderByCodeState(code, "6", event) //下架
  873. case Sp_state_5, Sp_state_6: //上下架
  874. upresult, err = spider.UpdateSpiderByCodeState(code, fmt.Sprint(state), event)
  875. //log.Println(upresult, err)
  876. default:
  877. upresult = true
  878. err = nil
  879. }
  880. if err != nil && strings.Contains(err.Error(), "timeout") {
  881. err = errors.New("连接节点" + fmt.Sprint(event) + "超时")
  882. upresult = true
  883. }
  884. if upresult && err == nil {
  885. upset := bson.M{"state": state} //修改状态
  886. if one["oldlua"] != nil { //老脚本上传
  887. upresult = mgdb.Update("luaconfig", bson.M{"code": code}, bson.M{"$set": upset}, true, false)
  888. } else {
  889. if state == Sp_state_1 { //提交审核
  890. upset["l_complete"] = time.Now().Unix()
  891. upset["report"] = ""
  892. } else if state == Sp_state_3 { //发布
  893. if one["event"] == nil {
  894. upset["event"] = event
  895. upset["modifytime"] = time.Now().Unix()
  896. }
  897. upset["l_uploadtime"] = time.Now().Unix()
  898. } else if state == Sp_state_2 { //打回原因
  899. upset["reason"] = reason
  900. } else if state == Sp_state_7 { //反馈问题
  901. upset["report"] = reason
  902. upset["state"] = 1 //反馈后爬虫改为待审核
  903. } else if state == Sp_state_5 { //上架,核对时间重置
  904. upset["l_checktime"] = 0
  905. } else if state == Sp_state_4 { //作废,作废原因
  906. upset["disablereason"] = reason
  907. upset["modifytime"] = time.Now().Unix()
  908. }
  909. upresult = mgdb.Update("luaconfig", map[string]interface{}{"code": code}, map[string]interface{}{"$set": upset}, false, false)
  910. if state == Sp_state_1 { //提交审核,验证是否提交成功
  911. for i := 1; i <= 5; i++ { //解决提交不上,重试5次
  912. lua := *mgdb.FindOne("luaconfig", map[string]interface{}{"code": code})
  913. tmpState := qu.IntAll(lua["state"])
  914. if state == tmpState {
  915. break
  916. } else {
  917. upresult = mgdb.Update("luaconfig", map[string]interface{}{"code": code}, map[string]interface{}{"$set": upset}, false, false)
  918. upresult = false
  919. }
  920. }
  921. }
  922. qu.Debug("提交日志:", code, upset, upresult)
  923. if upresult && (state == Sp_state_2 || state == Sp_state_3) { //打回、审核记录日志
  924. types := "打回"
  925. if state == Sp_state_3 {
  926. types = "审核"
  927. }
  928. obj := bson.M{
  929. "code": code,
  930. "auditor": username,
  931. "types": types,
  932. "comeintime": time.Now().Unix(),
  933. "reason": reason,
  934. "spideruser": one["createuser"],
  935. "modifytime": one["modifytime"],
  936. }
  937. mgdb.Save("lua_logs_auditor", obj)
  938. }
  939. }
  940. }
  941. }
  942. return upresult, err
  943. }
  944. //保存记录信息
  945. func SaveRemark(taskid []string, reason, username string) {
  946. timeNow := time.Now().Unix()
  947. if reason == "" {
  948. reason = "审核通过"
  949. }
  950. for _, id := range taskid {
  951. query := bson.M{
  952. "_id": bson.ObjectIdHex(string(id)),
  953. }
  954. task := *mgdb.FindOne("task", query)
  955. if task != nil {
  956. checkData := task["a_check"]
  957. var checkArr []map[string]interface{}
  958. newData := make(map[string]interface{})
  959. newData["s_check_checkUser"] = username
  960. newData["l_check_checkTime"] = timeNow
  961. newData["s_check_checkRemark"] = reason
  962. if checkData != nil {
  963. myArr := qu.ObjArrToMapArr(checkData.([]interface{}))
  964. if myArr != nil && len(myArr) > 0 {
  965. for _, v := range myArr {
  966. checkArr = append(checkArr, v)
  967. }
  968. }
  969. }
  970. checkArr = append(checkArr, newData)
  971. task["a_check"] = checkArr
  972. mgdb.Update("task", query, map[string]interface{}{
  973. "$set": task,
  974. }, false, false)
  975. }
  976. }
  977. }
  978. //修改任务状态
  979. func UpTaskState(code []string, num int, reason string, startTime int64) {
  980. query := bson.M{}
  981. update := bson.M{}
  982. for _, v := range code {
  983. if num == 1 || num == 2 || num == 3 || num == 7 {
  984. query = bson.M{
  985. "_id": bson.ObjectIdHex(v),
  986. }
  987. } else {
  988. query = bson.M{
  989. "s_code": v,
  990. }
  991. }
  992. if num == 1 { //提交审核
  993. update = bson.M{
  994. "$set": bson.M{
  995. "i_state": 3,
  996. },
  997. }
  998. } else if num == 2 { //打回 -->未通过
  999. update = bson.M{
  1000. "$set": bson.M{
  1001. "i_state": 5,
  1002. },
  1003. }
  1004. } else if num == 3 { //发布(审核通过) -->审核通过
  1005. update = bson.M{
  1006. "$set": bson.M{
  1007. "i_state": 4,
  1008. },
  1009. }
  1010. } else if num == 4 { //批量作废 -->关闭
  1011. update = bson.M{
  1012. "$set": bson.M{
  1013. "i_state": 6,
  1014. "l_complete": time.Now().Unix(),
  1015. },
  1016. }
  1017. } else if num == 7 { //反馈信息 -->待审核
  1018. newData := map[string]interface{}{
  1019. "l_mrecord_comeintime": startTime,
  1020. "l_mrecord_complete": time.Now().Unix(),
  1021. "s_mrecord_remark": reason,
  1022. }
  1023. mrecord := []interface{}{}
  1024. mrecord = append(mrecord, newData)
  1025. update = bson.M{
  1026. "$set": bson.M{
  1027. "i_state": 3,
  1028. "l_complete": time.Now().Unix(),
  1029. "a_mrecord": mrecord,
  1030. },
  1031. }
  1032. }
  1033. flag := mgdb.Update("task", query, update, false, true)
  1034. log.Println("codeOrId:", query, " 修改任务状态:", flag)
  1035. }
  1036. }
  1037. //更新节点
  1038. func (f *Front) ChangeEvent() {
  1039. auth := qu.IntAll(f.GetSession("auth"))
  1040. if auth != role_admin {
  1041. f.ServeJson("没有权限")
  1042. }
  1043. code := f.GetString("code")
  1044. event, _ := f.GetInt("event")
  1045. eventok := false
  1046. for k, _ := range util.Config.Uploadevents {
  1047. if event == qu.Int64All(k) {
  1048. eventok = true
  1049. break
  1050. }
  1051. }
  1052. if !eventok {
  1053. f.ServeJson("没有对应节点")
  1054. return
  1055. }
  1056. info := *mgdb.FindOne("luaconfig", `{"code":"`+code+`"}`)
  1057. if len(info) > 0 {
  1058. oldevent := qu.IntAll(info["event"])
  1059. if qu.IntAll(info["state"]) == Sp_state_5 {
  1060. //源节点下架
  1061. _, err := spider.UpdateSpiderByCodeState(code, fmt.Sprint(Sp_state_6), oldevent)
  1062. set := map[string]interface{}{
  1063. "$set": map[string]interface{}{
  1064. "event": qu.IntAll(event),
  1065. "state": Sp_state_6,
  1066. },
  1067. }
  1068. mgdb.Update("luaconfig", `{"code":"`+code+`"}`, set, true, false)
  1069. if err != nil && strings.Contains(err.Error(), "timeout") {
  1070. f.ServeJson("连接节点" + fmt.Sprint(oldevent) + "超时")
  1071. } else {
  1072. f.ServeJson(err.Error())
  1073. }
  1074. } else {
  1075. set := map[string]interface{}{
  1076. "$set": map[string]interface{}{
  1077. "event": qu.IntAll(event),
  1078. },
  1079. }
  1080. mgdb.Update("luaconfig", `{"code":"`+code+`"}`, set, true, false)
  1081. }
  1082. } else {
  1083. f.ServeJson("没有对应记录")
  1084. }
  1085. }
  1086. //验证用户是否有更改状态权限
  1087. func IsHasUpState(auth, state int) bool {
  1088. rep := false
  1089. switch auth {
  1090. case role_dev:
  1091. if state == Sp_state_1 || state == Sp_state_7 {
  1092. rep = true
  1093. }
  1094. case role_examine:
  1095. if state == Sp_state_2 || state == Sp_state_3 {
  1096. rep = true
  1097. }
  1098. case role_admin:
  1099. rep = true
  1100. default:
  1101. }
  1102. return rep
  1103. }
  1104. var list_fields = `{"_id":1,"code":1,"createuser":1,"modifyuser":1,"modifytime":1,"l_uploadtime":1,"l_checktime":1,"state":1,"param_common":1,"event":1,"urgency":1,"platform":1}`
  1105. //脚本管理,结合爬虫运行信息
  1106. func (f *Front) LuaList() {
  1107. auth := qu.IntAll(f.GetSession("auth"))
  1108. if auth != role_admin {
  1109. f.ServeJson("没有权限!")
  1110. return
  1111. }
  1112. if f.Method() == "POST" {
  1113. state, _ := f.GetInteger("state")
  1114. event, _ := f.GetInteger("event")
  1115. start, _ := f.GetInteger("start")
  1116. limit, _ := f.GetInteger("length")
  1117. draw, _ := f.GetInteger("draw")
  1118. searchStr := f.GetString("search[value]")
  1119. //search := strings.Replace(searchStr, " ", "", -1)
  1120. search := strings.TrimSpace(searchStr)
  1121. query := bson.M{}
  1122. q1 := bson.M{}
  1123. q1["$or"] = []interface{}{
  1124. bson.M{"code": bson.M{"$regex": search}},
  1125. bson.M{"createuser": bson.M{"$regex": search}},
  1126. bson.M{"param_common.1": bson.M{"$regex": search}},
  1127. }
  1128. q2 := bson.M{}
  1129. if state > -1 {
  1130. q2 = bson.M{"state": state}
  1131. } else {
  1132. q2["$or"] = []interface{}{
  1133. bson.M{"state": Sp_state_3},
  1134. bson.M{"state": Sp_state_5},
  1135. bson.M{"state": Sp_state_6},
  1136. }
  1137. }
  1138. q3 := bson.M{}
  1139. if event > -1 {
  1140. q3 = bson.M{"event": event}
  1141. }
  1142. if search != "" {
  1143. query["$and"] = []interface{}{q1, q2, q3}
  1144. } else {
  1145. query["$and"] = []interface{}{q2, q3}
  1146. }
  1147. sort := `{"%s":%d}`
  1148. orderIndex := f.GetString("order[0][column]")
  1149. orderName := f.GetString(fmt.Sprintf("columns[%s][data]", orderIndex))
  1150. orderType := 1
  1151. if f.GetString("order[0][dir]") != "asc" {
  1152. orderType = -1
  1153. }
  1154. sort = fmt.Sprintf(sort, orderName, orderType)
  1155. page := start / 10
  1156. luas := *mgdb.Find("luaconfig", query, sort, list_fields, false, start, limit)
  1157. count := mgdb.Count("luaconfig", query)
  1158. for k, v := range luas {
  1159. v["num"] = k + 1 + page*10
  1160. l_uploadtime := qu.Int64All(v["l_uploadtime"])
  1161. v["l_uploadtime"] = qu.FormatDateByInt64(&l_uploadtime, qu.Date_Full_Layout)
  1162. l_checktime := qu.Int64All(v["l_checktime"])
  1163. v["l_checktime"] = qu.FormatDateByInt64(&l_checktime, qu.Date_Full_Layout)
  1164. if l_checktime > 0 { //核对
  1165. v["is_check"] = true
  1166. } else { //未核对
  1167. v["is_check"] = false
  1168. }
  1169. if tmp, ok := spinfos.Load(v["code"]); ok {
  1170. info := tmp.(*spinfo)
  1171. v["modifytime"] = info.lastHeartbeat
  1172. v["yesterday"] = fmt.Sprint(info.yesterdayDowncount) + "/" + fmt.Sprint(info.yestoDayRequestNum)
  1173. v["terday"] = fmt.Sprint(info.todayDowncount) + "/" + fmt.Sprint(info.toDayRequestNum)
  1174. v["lastdowncount"] = info.lastDowncount
  1175. v["lstate"] = info.lstate
  1176. } else {
  1177. v["modifytime"] = ""
  1178. v["yesterday"] = ""
  1179. v["terday"] = ""
  1180. v["lastdowncount"] = 0
  1181. v["lstate"] = ""
  1182. }
  1183. }
  1184. f.ServeJson(map[string]interface{}{"draw": draw, "data": luas, "recordsFiltered": count, "recordsTotal": count})
  1185. } else {
  1186. events := []string{}
  1187. for k, _ := range util.Config.Uploadevents {
  1188. events = append(events, k)
  1189. }
  1190. sort.Strings(events)
  1191. f.T["events"] = events
  1192. f.Render("lualist.html", &f.T)
  1193. }
  1194. }
  1195. //爬虫信息
  1196. type spinfo struct {
  1197. code string
  1198. todayDowncount, toDayRequestNum int
  1199. yesterdayDowncount, yestoDayRequestNum int
  1200. totalDowncount, totalRequestNum int
  1201. errorNum, roundCount, runRate int
  1202. lastDowncount int
  1203. lastHeartbeat string
  1204. lstate string
  1205. }
  1206. //爬虫信息
  1207. func SpiderInfo(data string) {
  1208. data = util.Se.DecodeString(data)
  1209. infos := []map[string]interface{}{}
  1210. err := json.Unmarshal([]byte(data), &infos)
  1211. if err != nil {
  1212. return
  1213. }
  1214. for _, tmp := range infos {
  1215. lastHeartbeat := qu.Int64All(tmp["lastHeartbeat"])
  1216. info := &spinfo{
  1217. code: fmt.Sprint(tmp["code"]),
  1218. todayDowncount: qu.IntAll(tmp["todayDowncount"]),
  1219. toDayRequestNum: qu.IntAll(tmp["toDayRequestNum"]),
  1220. yesterdayDowncount: qu.IntAll(tmp["yesterdayDowncount"]),
  1221. yestoDayRequestNum: qu.IntAll(tmp["yestoDayRequestNum"]),
  1222. totalDowncount: qu.IntAll(tmp["totalDowncount"]),
  1223. totalRequestNum: qu.IntAll(tmp["totalRequestNum"]),
  1224. errorNum: qu.IntAll(tmp["errorNum"]),
  1225. roundCount: qu.IntAll(tmp["roundCount"]),
  1226. runRate: qu.IntAll(tmp["runRate"]),
  1227. lastHeartbeat: qu.FormatDateByInt64(&lastHeartbeat, qu.Date_Full_Layout),
  1228. lastDowncount: qu.IntAll(tmp["lastDowncount"]),
  1229. lstate: fmt.Sprint(tmp["lstate"]),
  1230. }
  1231. spinfos.Store(info.code, info)
  1232. //log.Println(info)
  1233. }
  1234. }
  1235. //接受维护任务信息
  1236. func SpiderModifyTask(data string) {
  1237. data = util.Se.DecodeString(data)
  1238. mtasks := []map[string]interface{}{}
  1239. err := json.Unmarshal([]byte(data), &mtasks)
  1240. if err != nil {
  1241. return
  1242. }
  1243. for k, tmp := range mtasks {
  1244. log.Println(k, tmp)
  1245. }
  1246. }
  1247. //查看是否有该任务
  1248. func checkTask(codes []string, num int) []string {
  1249. // var id string = ""
  1250. query := bson.M{}
  1251. var idArr []string
  1252. if len(codes) > 0 {
  1253. for _, v := range codes {
  1254. if num == 1 {
  1255. query = bson.M{
  1256. "s_code": v,
  1257. "i_state": bson.M{
  1258. "$in": []int{1, 2, 5},
  1259. },
  1260. }
  1261. } else if num == 2 { //打回时查询待审核的任务
  1262. query = bson.M{
  1263. "s_code": v,
  1264. "i_state": 3,
  1265. }
  1266. } else if num == 3 { //审核通过时查询待处理、处理中、待审核、未通过的任务
  1267. query = bson.M{
  1268. "s_code": v,
  1269. "i_state": bson.M{
  1270. "$in": []int{1, 2, 3, 5},
  1271. },
  1272. }
  1273. } else if num == 7 {
  1274. query = bson.M{
  1275. "s_code": v,
  1276. "i_state": bson.M{
  1277. "$in": []int{2, 5},
  1278. },
  1279. }
  1280. }
  1281. //task := *mgdb.FindOne("task", query)
  1282. task := *mgdb.Find("task", query, nil, nil, false, -1, -1)
  1283. if task != nil {
  1284. for _, v := range task {
  1285. //id = v["_id"].(bson.ObjectId).Hex()
  1286. idArr = append(idArr, v["_id"].(bson.ObjectId).Hex())
  1287. }
  1288. }
  1289. return idArr
  1290. }
  1291. }
  1292. return idArr
  1293. }