spider.go 38 KB

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