spider.go 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474
  1. package front
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "log"
  7. "mongodb"
  8. "sort"
  9. "spider"
  10. mu "mfw/util"
  11. qu "qfw/util"
  12. util "spiderutil"
  13. "strings"
  14. "time"
  15. u "util"
  16. )
  17. type Base struct {
  18. SpiderCode string
  19. SpiderCodeOld string
  20. SpiderName string
  21. SpiderChannel string
  22. SpiderDownDetailPage bool
  23. SpiderStartPage int
  24. SpiderMaxPage int
  25. SpiderRunRate int
  26. Spider2Collection string
  27. SpiderPageEncoding string
  28. SpiderStoreMode int //1,2
  29. SpiderStoreToMsgEvent int
  30. SpiderTargetChannelUrl string
  31. SpiderLastDownloadTime string
  32. SpiderIsHistoricalMend bool
  33. SpiderIsMustDownload bool
  34. }
  35. type Step1 struct {
  36. Address string
  37. ContentChooser string
  38. DateFormat string
  39. Expert string
  40. Types int
  41. }
  42. type Step2 struct {
  43. Listadd string
  44. Listadds string
  45. BlockChooser string
  46. AddressChooser string
  47. TitleChooser string
  48. DateChooser string
  49. DateFormat string
  50. Expert string
  51. Types int
  52. }
  53. type Step3 struct {
  54. ContentChooser string
  55. ElementChooser string
  56. T_title string
  57. T_href string
  58. T_date string
  59. Expert string
  60. Types int
  61. }
  62. type StepRe3 struct {
  63. Checked bool
  64. Expert string
  65. }
  66. //加载某个爬虫
  67. func (f *Front) LoadSpider(codeTaskIdReState string) error {
  68. tmpStr := strings.Split(codeTaskIdReState, "__")
  69. code := tmpStr[0]
  70. taskId := tmpStr[1]
  71. auth := qu.IntAll(f.GetSession("auth"))
  72. restate := -1
  73. if taskId == "restate=1" { //重采编辑
  74. restate = 1
  75. } else if taskId == "restate=2" {
  76. restate = 2
  77. } else if taskId == "restate=3" {
  78. restate = 3
  79. } else {
  80. if auth == role_dev && (f.GetSession(taskId) == nil || f.GetSession(taskId) == "") {
  81. xgTime := time.Unix(time.Now().Unix(), 0).Format("2006-01-02 15:04:05")
  82. f.SetSession(taskId, xgTime)
  83. }
  84. }
  85. copy := f.GetString("copy")
  86. if f.Method() == "GET" {
  87. code := util.Se.Decode4Hex(code)
  88. f.T["actiontext"] = "编辑"
  89. lua, _ := u.MgoE.FindOne("luaconfig", map[string]interface{}{"code": code})
  90. auth := qu.IntAll(f.GetSession("auth"))
  91. if qu.ObjToString((*lua)["createuserid"]) == f.GetSession("userid").(string) || auth >= 1 {
  92. if len(*lua) > 0 {
  93. if copy != "" {
  94. luacopy, _ := u.MgoE.FindOne("luaconfig", map[string]interface{}{"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, _ := u.MgoE.FindOne("luaconfig", map[string]interface{}{"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, _ := u.MgoE.Find("luaconfig", map[string]interface{}{"code": id}, nil, map[string]interface{}{"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, _ := u.MgoE.FindOne("luaconfig", map[string]interface{}{"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 := map[string]interface{}{"luacontent": script}
  218. upset["modifytime"] = time.Now().Unix()
  219. b := u.MgoE.Update("luaconfig", map[string]interface{}{"code": id}, map[string]interface{}{"$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, _ := u.MgoE.FindOne("luaconfig", map[string]interface{}{"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. //三级页复制
  361. param["str_recontent"] = f.StepRe3.Expert
  362. param["iscopycontent"] = f.StepRe3.Checked
  363. //
  364. param["listisfilter"] = ListFilterReg.MatchString(f.Step2.Expert) //列表页校验是否含“--关键词过滤”
  365. matchLua, msg := LuaTextCheck(f.Step2.Expert, f.Step3.Expert, f.Step2.Types)
  366. if !matchLua {
  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. } else {
  380. rep["msg"] = "保存失败," + msg
  381. }
  382. rep["code"] = util.Se.Encode2Hex(code)
  383. f.ServeJson(rep)
  384. }
  385. }
  386. }
  387. //检查列表页和三级页代码中是否含lua原生方法
  388. func LuaTextCheck(list, detail string, type_list int) (b bool, msg string) {
  389. defer qu.Catch()
  390. if LuaReg.MatchString(list) || LuaReg.MatchString(detail) {
  391. b = true
  392. msg = "代码中含有lua原生方法;"
  393. }
  394. //if type_list != 0 && !strings.Contains(list, "sendListNum") { //列表页专家模式且不含sendListNum
  395. // b = true
  396. // msg = "代码中缺少sendListNum方法;" + msg
  397. //}
  398. return
  399. }
  400. func (f *Front) Assort() {
  401. state, _ := f.GetInteger("state")
  402. code := f.GetString("code")
  403. sql := map[string]interface{}{
  404. "$set": map[string]interface{}{
  405. "state": state,
  406. "modifytime": time.Now().Unix(),
  407. },
  408. }
  409. queryT := map[string]interface{}{
  410. "code": code,
  411. }
  412. //下架爬虫
  413. lua, _ := u.MgoE.FindOne("luaconfig", queryT)
  414. upresult, err := spider.UpdateSpiderByCodeState(code, "6", qu.IntAll((*lua)["event"]))
  415. qu.Debug("下架爬虫:", code, upresult, err)
  416. if upresult && err == nil {
  417. //更新爬虫
  418. u.MgoE.Update("luaconfig", queryT, sql, false, false)
  419. //关闭任务
  420. query := map[string]interface{}{
  421. "s_code": code,
  422. }
  423. sql = map[string]interface{}{
  424. "$set": map[string]interface{}{
  425. "i_state": 6,
  426. },
  427. }
  428. u.MgoE.Update("task", query, sql, false, true)
  429. //删除心跳
  430. DelSpiderHeart(code)
  431. }
  432. f.ServeJson(map[string]interface{}{"upresult": upresult})
  433. }
  434. //方法测试
  435. func (f *Front) RunStep() {
  436. imodal, _ := f.GetInteger("imodal")
  437. script, _ := f.GetBool("script")
  438. listcheck := f.GetString("listcheck")
  439. contentcheck := f.GetString("contentcheck")
  440. downloadnode := f.GetString("downloadnode") //下载节点
  441. common := []interface{}{
  442. f.Base.SpiderCode,
  443. f.Base.SpiderName,
  444. f.Base.SpiderChannel,
  445. f.Base.SpiderDownDetailPage,
  446. f.Base.SpiderStartPage,
  447. f.Base.SpiderMaxPage,
  448. f.Base.SpiderRunRate,
  449. f.Base.Spider2Collection,
  450. f.Base.SpiderPageEncoding,
  451. f.Base.SpiderStoreMode,
  452. f.Base.SpiderStoreToMsgEvent,
  453. f.Base.SpiderTargetChannelUrl,
  454. f.Base.SpiderLastDownloadTime,
  455. f.Base.SpiderIsHistoricalMend,
  456. f.Base.SpiderIsMustDownload,
  457. "",
  458. "",
  459. "",
  460. }
  461. if f.Method() == "POST" {
  462. switch f.GetString("step") {
  463. case "step1":
  464. ptime := []interface{}{
  465. f.Step1.DateFormat,
  466. f.Step1.Address,
  467. f.Step1.ContentChooser,
  468. }
  469. if script {
  470. _, scripts := spider.GetLastPublishTime(common, ptime, f.Step1.Expert, downloadnode, imodal, 1)
  471. f.ServeJson(scripts)
  472. return
  473. }
  474. rs, err := spider.GetLastPublishTime(common, ptime, f.Step1.Expert, downloadnode, imodal)
  475. if err == nil {
  476. f.ServeJson(rs)
  477. }
  478. case "step2":
  479. addrs := strings.Split(f.Step2.Listadds, "\n")
  480. if len(addrs) > 0 {
  481. for k, v := range addrs {
  482. addrs[k] = "'" + v + "'"
  483. }
  484. f.Step2.Listadds = strings.Join(addrs, ",")
  485. } else if len(f.Step2.Listadds) > 5 {
  486. f.Step2.Listadds = "'" + f.Step2.Listadds + "'"
  487. } else {
  488. f.Step2.Listadds = ""
  489. }
  490. list := []interface{}{
  491. f.Step2.Listadd,
  492. f.Step2.Listadds,
  493. f.Step2.BlockChooser,
  494. f.Step2.AddressChooser,
  495. f.Step2.TitleChooser,
  496. f.Step2.DateChooser,
  497. f.Step2.DateFormat,
  498. }
  499. listcheck = strings.Replace(listcheck, "\n", "\\n", -1)
  500. listcheck = strings.Replace(listcheck, "\"", "\\\"", -1)
  501. s_model := f.GetString("model")
  502. configModel := util.Config.Model[s_model]
  503. model := map[string]interface{}{}
  504. for k, _ := range configModel {
  505. model[k] = f.GetString(k)
  506. }
  507. if script {
  508. _, script := spider.GetPageList(common, list, model, listcheck, f.Step2.Expert, downloadnode, imodal, 1)
  509. f.ServeJson(script)
  510. return
  511. }
  512. rs, err := spider.GetPageList(common, list, model, listcheck, f.Step2.Expert, downloadnode, imodal)
  513. if err == nil {
  514. f.ServeJson(rs)
  515. } else if err.(error).Error() == "no" {
  516. f.ServeJson(rs[0])
  517. }
  518. case "step3":
  519. content := []interface{}{
  520. f.Step3.ContentChooser,
  521. f.Step3.ElementChooser,
  522. }
  523. contentcheck = strings.Replace(contentcheck, "\n", "\\n", -1)
  524. contentcheck = strings.Replace(contentcheck, "\"", "\\\"", -1)
  525. data := map[string]interface{}{}
  526. data["title"] = f.Step3.T_title
  527. data["href"] = f.Step3.T_href
  528. data["publishtime"] = f.Step3.T_date
  529. if script {
  530. _, script := spider.GetContentInfo(common, content, data, contentcheck, f.Step3.Expert, downloadnode, imodal, 1)
  531. f.ServeJson(script)
  532. return
  533. }
  534. rs, err := spider.GetContentInfo(common, content, data, contentcheck, f.Step3.Expert, downloadnode, imodal)
  535. if projectinfo, ok := rs["projectinfo"].(map[string]interface{}); ok && projectinfo != nil {
  536. if attachments, ok := projectinfo["attachments"].(map[string]interface{}); ok && attachments != nil {
  537. for _, tmp := range attachments {
  538. tmpMap := tmp.(map[string]interface{})
  539. if qu.ObjToString(tmpMap["filename"]) == "附件中含有乱码" {
  540. rs["msg"] = "附件中含有乱码"
  541. }
  542. }
  543. }
  544. }
  545. if err == nil {
  546. f.ServeJson(rs)
  547. } else {
  548. f.ServeJson(rs["no"])
  549. }
  550. }
  551. }
  552. }
  553. //爬虫测试数据json
  554. func (f *Front) GetJson() {
  555. list, _ := f.GetSession("listInfo").([]map[string]interface{})
  556. data, _ := f.GetSession("dataInfo").(map[string]interface{})
  557. descript := f.GetSession("task_descript")
  558. remark := f.GetSession("task_remark")
  559. rateremark := f.GetSession("task_rateremark")
  560. reason := f.GetSession("reason")
  561. username := f.GetSession("username").(string)
  562. msg := f.GetSession(username + "_msg")
  563. if len(data) > 0 {
  564. data["contenthtml"] = ""
  565. }
  566. for k, v := range list {
  567. v["a_index"] = k + 1
  568. }
  569. f.T["list"] = list
  570. f.T["data"] = data
  571. f.T["descript"] = descript
  572. f.T["remark"] = remark
  573. f.T["rateremark"] = rateremark
  574. f.T["reason"] = reason
  575. f.T["msg"] = msg
  576. f.DelSession("listInfo")
  577. f.DelSession("dataInfo")
  578. f.DelSession("task_descript")
  579. f.DelSession("task_remark")
  580. f.DelSession("task_rateremark")
  581. f.DelSession("reason")
  582. f.Render("jsonInfo.html", &f.T)
  583. }
  584. //整体测试
  585. func (f *Front) SpiderPass() {
  586. defer mu.Catch()
  587. list := []map[string]interface{}{}
  588. data := map[string]interface{}{}
  589. msg1 := ""
  590. code := f.GetString("code")
  591. downloadnode := f.GetString("node")
  592. //根据code查询待确认任务
  593. query := map[string]interface{}{
  594. "s_code": code,
  595. "i_state": 3,
  596. }
  597. task, _ := u.MgoE.FindOne("task", query)
  598. descript := "null"
  599. remark := "null"
  600. remarktmp := []string{}
  601. rateremarktmp := []string{}
  602. if len(*task) > 0 {
  603. descript = (*task)["s_descript"].(string)
  604. if mrecord, ok := (*task)["a_mrecord"].([]interface{}); ok {
  605. for _, m := range mrecord {
  606. remarkInfo := m.(map[string]interface{})
  607. if remark := qu.ObjToString(remarkInfo["s_mrecord_remark"]); remark != "" {
  608. remarktmp = append(remarktmp, remark+";")
  609. }
  610. if rateremark := qu.ObjToString(remarkInfo["s_mrecord_rateremark"]); rateremark != "" {
  611. rateremarktmp = append(rateremarktmp, rateremark+";")
  612. }
  613. }
  614. }
  615. }
  616. if len(remarktmp) > 0 {
  617. remark = ""
  618. remark = strings.Join(remarktmp, "")
  619. }
  620. f.SetSession("task_remark", remark)
  621. f.SetSession("task_rateremark", rateremarktmp)
  622. f.SetSession("task_descript", descript)
  623. //基本信息、方法一、方法二、方法三、总请求次数、go方法一、go方法二、go方法三、列表页条数
  624. steps := []interface{}{false, false, false, false, 0, 0, 0, 0, 0}
  625. one, _ := u.MgoE.FindOne("luaconfig", map[string]interface{}{"code": code})
  626. reason, _ := (*one)["reason"].(string)
  627. f.SetSession("reason", reason)
  628. if len(*one) > 0 && (*one)["oldlua"] == nil {
  629. common := (*one)["param_common"].([]interface{})
  630. if len(common) < 13 {
  631. f.ServeJson(steps)
  632. return
  633. } else {
  634. steps[0] = true
  635. }
  636. } else {
  637. steps[0] = true
  638. }
  639. script, liststr, contentstr := "", "", ""
  640. if (*one)["oldlua"] == nil {
  641. script, liststr, contentstr = spider.GetScript(code)
  642. } else {
  643. script = (*one)["luacontent"].(string)
  644. }
  645. if liststr != "" && contentstr != "" {
  646. msg1 = u.SpiderPassCheckLua(liststr, contentstr, (*one))
  647. }
  648. s := spider.CreateSpider(downloadnode, script)
  649. s.SpiderMaxPage = 1
  650. s.Timeout = 60
  651. time, timeerr := s.GetLastPublishTime()
  652. if timeerr == nil && len(time) > 4 {
  653. steps[1] = true
  654. list, _ = s.DownListPageItem()
  655. if len(list) > 0 {
  656. f.SetSession("listInfo", list)
  657. listone := list[0]
  658. if len(qu.ObjToString(listone["href"])) < 7 ||
  659. (qu.ObjToString(listone["publishtime"]) != "0" && len(qu.ObjToString(listone["publishtime"])) < 5) ||
  660. len(qu.ObjToString(listone["title"])) < 3 {
  661. f.ServeJson(steps)
  662. return
  663. } else {
  664. steps[2] = true
  665. if s.DownDetail {
  666. param := map[string]string{}
  667. index := 0
  668. if len(list) > 0 {
  669. steps[8] = len(list)
  670. index = len(list) / 2
  671. for k, v := range list[index] {
  672. param[k] = qu.ObjToString(v)
  673. }
  674. data = map[string]interface{}{}
  675. s.DownloadDetailPage(param, data)
  676. if len(data) > 0 {
  677. f.SetSession("dataInfo", data)
  678. } else {
  679. f.SetSession("dataInfo", "")
  680. }
  681. if len(data) == 0 || data["detail"].(string) == "" {
  682. steps[3] = false
  683. } else {
  684. steps[3] = true
  685. }
  686. }
  687. } else {
  688. steps[3] = true
  689. }
  690. }
  691. } else {
  692. f.SetSession("listInfo", "")
  693. f.SetSession("dataInfo", "")
  694. }
  695. }
  696. //关闭laustate
  697. s.L.Close()
  698. steps[4] = s.Test_luareqcount
  699. steps[5] = s.Test_goreqtime
  700. steps[6] = s.Test_goreqlist
  701. steps[7] = s.Test_goreqcon
  702. //校验
  703. msg := u.SpiderPassCheckListAndDetail(list, data)
  704. if msg1 != "" {
  705. msg = msg1 + ";" + msg
  706. }
  707. username := f.GetSession("username").(string)
  708. f.SetSession(username+"_msg", msg)
  709. f.ServeJson(steps)
  710. }
  711. func (f *Front) DownSpider(id string) {
  712. //auth := qu.IntAll(f.GetSession("auth"))
  713. //if auth > role_dev {
  714. one, _ := u.MgoE.FindOne("luaconfig", map[string]interface{}{"code": id})
  715. createuserid := qu.ObjToString((*one)["createuserid"])
  716. script := ""
  717. filename := id + ".lua"
  718. if len(*one) > 0 {
  719. if (*one)["oldlua"] != nil {
  720. if (*one)["luacontent"] != nil {
  721. script = (*one)["luacontent"].(string)
  722. }
  723. } else {
  724. user, _ := u.MgoE.FindById("user", createuserid, nil)
  725. name := (*one)["createuser"]
  726. email := (*user)["s_email"]
  727. upload := time.Now().Format("2006-01-02 15:04:05")
  728. script, _, _ = spider.GetScript(id, name, email, upload)
  729. }
  730. }
  731. f.ResponseWriter.Header().Del("Content-Type")
  732. f.ResponseWriter.Header().Add("Content-Type", "application/x-download")
  733. f.ResponseWriter.Header().Add("Content-Disposition", "attachment;filename=spider_"+filename)
  734. f.WriteBytes([]byte(script))
  735. // } else {
  736. // f.Write("您没有权限")
  737. // }
  738. }
  739. //更新爬虫状态
  740. func (f *Front) UpState() error {
  741. username := f.GetSession("username").(string)
  742. code := f.GetString("code")
  743. state, _ := f.GetInt("state")
  744. id := f.GetString("taskId")
  745. reason := f.GetString("reason")
  746. auth := qu.IntAll(f.GetSession("auth"))
  747. var codeArr = []string{code}
  748. var taskid []string
  749. //修改任务状态
  750. istotask := false
  751. res := map[string]interface{}{
  752. "istotask": istotask,
  753. "err": "没有权限",
  754. "code": util.Se.Encode2Hex(code),
  755. "taskid": taskid,
  756. }
  757. var xgTime int64
  758. if f.GetSession(id) == nil || f.GetSession(id) == "" {
  759. xgTime = time.Now().Unix()
  760. } else {
  761. xgTimeStr := qu.ObjToString(f.GetSession(id))
  762. xgTimeTmp, _ := time.ParseInLocation("2006-01-02 15:04:05", xgTimeStr, time.Local)
  763. xgTime = xgTimeTmp.Unix()
  764. }
  765. f.DelSession(id)
  766. if IsHasUpState(auth, int(state)) {
  767. b, err := UpStateAndUpSpider(code, "", reason, username, int(state)) //更新爬虫状态
  768. if b && state == Sp_state_1 { //提交审核
  769. //有对应任务跳转提交记录页
  770. taskid = checkTask(codeArr, 1)
  771. if len(taskid) > 0 {
  772. res["istotask"] = true
  773. res["taskid"] = taskid[0]
  774. }
  775. } else if b && state == Sp_state_2 { //打回
  776. taskid = checkTask(codeArr, 2)
  777. if len(taskid) > 0 {
  778. //UpTaskState([]string{taskid}, 2) //修改状态
  779. UpTaskState(taskid, 2, "", int64(0)) //修改任务状态
  780. SaveRemark(taskid, reason, username) //保存记录信息
  781. }
  782. } else if b && state == Sp_state_3 { //审核通过
  783. taskid = checkTask(codeArr, 3)
  784. if len(taskid) > 0 {
  785. //UpTaskState([]string{taskid}, 3)
  786. UpTaskState(taskid, 3, "", int64(0))
  787. SaveRemark(taskid, "", username)
  788. }
  789. } else if b && state == Sp_state_6 { //下架
  790. //下架成功删除心跳数据
  791. flag := DelSpiderHeart(code)
  792. log.Println(code, "---下架删除download数据:", flag)
  793. } else if b && state == Sp_state_7 { //反馈
  794. taskid = checkTask(codeArr, 7)
  795. if len(taskid) > 0 {
  796. UpTaskState(taskid, 7, reason, xgTime)
  797. }
  798. }
  799. if err != nil {
  800. res["err"] = err.Error()
  801. f.ServeJson(res)
  802. } else {
  803. res["err"] = ""
  804. f.ServeJson(res)
  805. }
  806. } else {
  807. f.ServeJson(res)
  808. }
  809. return nil
  810. }
  811. //下架删除心跳
  812. func DelSpiderHeart(code string) bool {
  813. return u.MgoS.Update("spider_heart", map[string]interface{}{"code": code}, map[string]interface{}{"$set": map[string]interface{}{"del": true}}, false, true)
  814. }
  815. //下架删除download数据
  816. //func delDownloadData(code string) bool {
  817. // return mgu.Del("download", "spider", "spider", `{"code":"`+code+`"}`)
  818. //}
  819. //批量作废删除download数据
  820. //func disableDelDownloadData(code []string) {
  821. // for _, v := range code {
  822. // flag := delDownloadData(v)
  823. // log.Println(code, "---批量删除download数据:", flag)
  824. // }
  825. //}
  826. //爬虫核对
  827. func (f *Front) Checktime() {
  828. code := f.GetString("code")
  829. auth := qu.IntAll(f.GetSession("auth"))
  830. if auth != role_admin {
  831. f.ServeJson(false)
  832. } else {
  833. b := u.MgoE.Update("luaconfig", map[string]interface{}{"code": code}, map[string]interface{}{"$set": map[string]interface{}{
  834. "l_checktime": time.Now().Unix(),
  835. }}, true, false)
  836. f.ServeJson(b)
  837. }
  838. }
  839. //批量作废
  840. func (f *Front) Disables() error {
  841. auth := qu.IntAll(f.GetSession("auth"))
  842. names := strings.Split(f.GetString("names"), ",")
  843. ids := strings.Split(f.GetString("ids"), ",")
  844. codes := strings.Split(f.GetString("codes"), ",")
  845. disablereason := f.GetString("disablereason")
  846. res := ""
  847. if IsHasUpState(auth, Sp_state_4) {
  848. for k, id := range ids {
  849. b, err := UpStateAndUpSpider("", id, disablereason, "", Sp_state_4)
  850. if b { //作废成功
  851. //修改任务状态
  852. UpTaskState(codes, 4, "", int64(0))
  853. //删除download表数据
  854. //go disableDelDownloadData(codes)
  855. if err != nil {
  856. res = res + names[k] + ",ok" + qu.ObjToString(err.Error()) + ";"
  857. } else {
  858. res = res + names[k] + ",ok" + ";"
  859. }
  860. } else {
  861. res = res + names[k] + "," + qu.ObjToString(err.Error()) + ";"
  862. }
  863. }
  864. } else {
  865. res = "没有权限"
  866. }
  867. f.ServeJson(res)
  868. return nil
  869. }
  870. //批量上下架
  871. func (f *Front) BatchShelves() {
  872. codes := strings.Split(f.GetString("codes"), ",")
  873. state, _ := f.GetInteger("state")
  874. auth := qu.IntAll(f.GetSession("auth"))
  875. errCode := []string{}
  876. var err error
  877. b := false
  878. if IsHasUpState(auth, Sp_state_5) {
  879. if state == 5 { //批量上架
  880. for _, code := range codes {
  881. _, err = UpStateAndUpSpider(code, "", "", "", Sp_state_5)
  882. if err != nil {
  883. errCode = append(errCode, code)
  884. }
  885. }
  886. } else { //批量下架
  887. for _, code := range codes {
  888. b, err = UpStateAndUpSpider(code, "", "", "", Sp_state_6)
  889. if !b || err != nil {
  890. errCode = append(errCode, code)
  891. }
  892. //下架删除download数据
  893. //if b {
  894. // flag := delDownloadData(code)
  895. // log.Println(code, "---删除download数据:", flag)
  896. //}
  897. }
  898. }
  899. } else {
  900. errCode = append(errCode, "没有权限")
  901. }
  902. f.ServeJson(errCode)
  903. }
  904. //更新爬虫状态,并判断是否更新节点爬虫
  905. func UpStateAndUpSpider(code, id, reason, username string, state int) (bool, error) {
  906. upresult := false
  907. var err error
  908. one := &map[string]interface{}{}
  909. if code != "" {
  910. one, _ = u.MgoE.FindOne("luaconfig", map[string]interface{}{"code": code})
  911. } else {
  912. one, _ = u.MgoE.FindById("luaconfig", id, nil)
  913. code = qu.ObjToString((*one)["code"])
  914. }
  915. if len(*one) > 0 {
  916. var event int
  917. if (*one)["event"] != nil {
  918. event = qu.IntAll((*one)["event"])
  919. } else {
  920. for k, _ := range util.Config.Uploadevents { //?
  921. event = qu.IntAll(k)
  922. break
  923. }
  924. //r := rand.New(rand.NewSource(time.Now().UnixNano()))
  925. //event = util.Config.Uploadevents[r.Intn(len(util.Config.Uploadevents))]
  926. }
  927. //oldstate := qu.IntAll(one["state"])
  928. switch state {
  929. case Sp_state_4, Sp_state_6: //作废、下架
  930. // if oldstate == Sp_state_5 {
  931. // upresult = false
  932. // err = errors.New("已上架不允许作废")
  933. // } else {
  934. // upresult = true
  935. // }
  936. upresult, err = spider.UpdateSpiderByCodeState(code, fmt.Sprint(state), event) //下架
  937. qu.Debug("下架:", upresult, code)
  938. case Sp_state_5: //上架(爬虫端在更新上架的时候为了更新内存中字段,采用先下架在上架)
  939. upresult, err = spider.UpdateSpiderByCodeState(code, "6", event)
  940. qu.Debug("下架:", upresult, code)
  941. if upresult && err == nil {
  942. upresult, err = spider.UpdateSpiderByCodeState(code, fmt.Sprint(state), event)
  943. qu.Debug("上架:", upresult, code)
  944. }
  945. default:
  946. upresult = true
  947. err = nil
  948. }
  949. if err != nil && strings.Contains(err.Error(), "timeout") {
  950. err = errors.New("连接节点" + fmt.Sprint(event) + "超时")
  951. upresult = true
  952. }
  953. if upresult && err == nil {
  954. upset := map[string]interface{}{"state": state} //修改状态
  955. if (*one)["oldlua"] != nil { //老脚本上传
  956. upresult = u.MgoE.Update("luaconfig", map[string]interface{}{"code": code}, map[string]interface{}{"$set": upset}, true, false)
  957. } else {
  958. if state == Sp_state_1 { //提交审核
  959. upset["l_complete"] = time.Now().Unix()
  960. upset["report"] = ""
  961. } else if state == Sp_state_3 { //审核通过
  962. if (*one)["event"] == nil {
  963. upset["event"] = event
  964. upset["modifytime"] = time.Now().Unix()
  965. }
  966. upset["frequencyerrtimes"] = 0 //爬虫审核通过,重置采集频率异常次数
  967. upset["l_uploadtime"] = time.Now().Unix()
  968. } else if state == Sp_state_2 { //打回原因
  969. upset["reason"] = reason
  970. } else if state == Sp_state_7 { //反馈问题
  971. upset["report"] = reason
  972. upset["state"] = 1 //反馈后爬虫改为待审核
  973. } else if state == Sp_state_5 { //上架,核对时间重置
  974. upset["l_checktime"] = 0
  975. } else if state == Sp_state_4 { //作废,作废原因
  976. upset["disablereason"] = reason
  977. upset["modifytime"] = time.Now().Unix()
  978. }
  979. upresult = u.MgoE.Update("luaconfig", map[string]interface{}{"code": code}, map[string]interface{}{"$set": upset}, false, false)
  980. qu.Debug("提交日志:", code, upset, upresult)
  981. if upresult && (state == Sp_state_2 || state == Sp_state_3) { //打回、审核记录日志
  982. types := "打回"
  983. if state == Sp_state_3 {
  984. types = "审核"
  985. }
  986. obj := map[string]interface{}{
  987. "code": code,
  988. "auditor": username,
  989. "types": types,
  990. "comeintime": time.Now().Unix(),
  991. "reason": reason,
  992. "spideruser": (*one)["createuser"],
  993. "modifytime": (*one)["modifytime"],
  994. }
  995. u.MgoE.Save("lua_logs_auditor", obj)
  996. }
  997. }
  998. }
  999. }
  1000. return upresult, err
  1001. }
  1002. //保存记录信息
  1003. func SaveRemark(taskid []string, reason, username string) {
  1004. timeNow := time.Now().Unix()
  1005. if reason == "" {
  1006. reason = "审核通过"
  1007. }
  1008. for _, id := range taskid {
  1009. task, _ := u.MgoE.FindById("task", id, nil)
  1010. if task != nil && len(*task) > 0 {
  1011. checkData := (*task)["a_check"]
  1012. var checkArr []map[string]interface{}
  1013. newData := make(map[string]interface{})
  1014. newData["s_check_checkUser"] = username
  1015. newData["l_check_checkTime"] = timeNow
  1016. newData["s_check_checkRemark"] = reason
  1017. if checkData != nil {
  1018. myArr := qu.ObjArrToMapArr(checkData.([]interface{}))
  1019. if myArr != nil && len(myArr) > 0 {
  1020. for _, v := range myArr {
  1021. checkArr = append(checkArr, v)
  1022. }
  1023. }
  1024. }
  1025. checkArr = append(checkArr, newData)
  1026. (*task)["a_check"] = checkArr
  1027. u.MgoE.UpdateById("task", id, map[string]interface{}{"$set": &task})
  1028. }
  1029. }
  1030. }
  1031. //修改任务状态
  1032. func UpTaskState(code []string, num int, reason string, startTime int64) {
  1033. query := map[string]interface{}{}
  1034. update := map[string]interface{}{}
  1035. for _, v := range code {
  1036. if num == 1 || num == 2 || num == 3 || num == 7 { //id
  1037. query = map[string]interface{}{
  1038. "_id": mongodb.StringTOBsonId(v),
  1039. }
  1040. } else {
  1041. query = map[string]interface{}{ //code
  1042. "s_code": v,
  1043. }
  1044. }
  1045. if num == 1 { //提交审核
  1046. update = map[string]interface{}{
  1047. "$set": map[string]interface{}{
  1048. "i_state": 3,
  1049. },
  1050. }
  1051. } else if num == 2 { //打回 -->未通过
  1052. update = map[string]interface{}{
  1053. "$set": map[string]interface{}{
  1054. "i_state": 5,
  1055. },
  1056. }
  1057. } else if num == 3 { //发布(审核通过) -->审核通过
  1058. update = map[string]interface{}{
  1059. "$set": map[string]interface{}{
  1060. "i_state": 4,
  1061. },
  1062. }
  1063. } else if num == 4 { //批量作废 -->关闭
  1064. update = map[string]interface{}{
  1065. "$set": map[string]interface{}{
  1066. "i_state": 6,
  1067. "l_complete": time.Now().Unix(),
  1068. },
  1069. }
  1070. } else if num == 7 { //反馈信息 -->待审核
  1071. newData := map[string]interface{}{
  1072. "l_mrecord_comeintime": startTime,
  1073. "l_mrecord_complete": time.Now().Unix(),
  1074. "s_mrecord_remark": reason,
  1075. }
  1076. mrecord := []interface{}{}
  1077. mrecord = append(mrecord, newData)
  1078. update = map[string]interface{}{
  1079. "$set": map[string]interface{}{
  1080. "i_state": 3,
  1081. "l_complete": time.Now().Unix(),
  1082. "a_mrecord": mrecord,
  1083. },
  1084. }
  1085. }
  1086. flag := u.MgoE.Update("task", query, update, false, true)
  1087. log.Println("codeOrId:", query, " 修改任务状态:", flag)
  1088. }
  1089. }
  1090. //更新节点
  1091. func (f *Front) ChangeEvent() {
  1092. auth := qu.IntAll(f.GetSession("auth"))
  1093. if auth != role_admin {
  1094. f.ServeJson("没有权限")
  1095. }
  1096. code := f.GetString("code")
  1097. event, _ := f.GetInt("event")
  1098. eventok := false
  1099. for k, _ := range util.Config.Uploadevents {
  1100. if event == qu.Int64All(k) {
  1101. eventok = true
  1102. break
  1103. }
  1104. }
  1105. if !eventok {
  1106. f.ServeJson("没有对应节点")
  1107. return
  1108. }
  1109. info, _ := u.MgoE.FindOne("luaconfig", map[string]interface{}{"code": code})
  1110. if len(*info) > 0 {
  1111. oldevent := qu.IntAll((*info)["event"])
  1112. if qu.IntAll((*info)["state"]) == Sp_state_5 {
  1113. //源节点下架
  1114. _, err := spider.UpdateSpiderByCodeState(code, fmt.Sprint(Sp_state_6), oldevent)
  1115. set := map[string]interface{}{
  1116. "$set": map[string]interface{}{
  1117. "event": qu.IntAll(event),
  1118. "state": Sp_state_6,
  1119. },
  1120. }
  1121. u.MgoE.Update("luaconfig", map[string]interface{}{"code": code}, set, true, false)
  1122. if err != nil && strings.Contains(err.Error(), "timeout") {
  1123. f.ServeJson("连接节点" + fmt.Sprint(oldevent) + "超时")
  1124. } else {
  1125. f.ServeJson(err.Error())
  1126. }
  1127. } else {
  1128. set := map[string]interface{}{
  1129. "$set": map[string]interface{}{
  1130. "event": qu.IntAll(event),
  1131. },
  1132. }
  1133. u.MgoE.Update("luaconfig", map[string]interface{}{"code": code}, set, true, false)
  1134. }
  1135. } else {
  1136. f.ServeJson("没有对应记录")
  1137. }
  1138. }
  1139. //验证用户是否有更改状态权限
  1140. func IsHasUpState(auth, state int) bool {
  1141. rep := false
  1142. switch auth {
  1143. case role_dev:
  1144. if state == Sp_state_1 || state == Sp_state_7 {
  1145. rep = true
  1146. }
  1147. case role_examine:
  1148. if state == Sp_state_2 || state == Sp_state_3 {
  1149. rep = true
  1150. }
  1151. case role_admin:
  1152. rep = true
  1153. default:
  1154. }
  1155. return rep
  1156. }
  1157. 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,"pendstate":1}`
  1158. //脚本管理,结合爬虫运行信息
  1159. func (f *Front) LuaList() {
  1160. auth := qu.IntAll(f.GetSession("auth"))
  1161. if auth != role_admin {
  1162. f.ServeJson("没有权限!")
  1163. return
  1164. }
  1165. if f.Method() == "POST" {
  1166. state, _ := f.GetInteger("state")
  1167. event, _ := f.GetInteger("event")
  1168. start, _ := f.GetInteger("start")
  1169. limit, _ := f.GetInteger("length")
  1170. draw, _ := f.GetInteger("draw")
  1171. searchStr := f.GetString("search[value]")
  1172. //search := strings.Replace(searchStr, " ", "", -1)
  1173. search := strings.TrimSpace(searchStr)
  1174. platform := f.GetString("platform")
  1175. query := map[string]interface{}{}
  1176. q1 := map[string]interface{}{}
  1177. q1["$or"] = []interface{}{
  1178. map[string]interface{}{"code": map[string]interface{}{"$regex": search}},
  1179. map[string]interface{}{"createuser": map[string]interface{}{"$regex": search}},
  1180. map[string]interface{}{"param_common.1": map[string]interface{}{"$regex": search}},
  1181. }
  1182. q2 := map[string]interface{}{}
  1183. if state > -1 {
  1184. q2 = map[string]interface{}{"state": state}
  1185. } else {
  1186. q2["$or"] = []interface{}{
  1187. map[string]interface{}{"state": Sp_state_3},
  1188. map[string]interface{}{"state": Sp_state_5},
  1189. map[string]interface{}{"state": Sp_state_6},
  1190. }
  1191. }
  1192. q3 := map[string]interface{}{}
  1193. if event > -1 {
  1194. q3 = map[string]interface{}{"event": event}
  1195. }
  1196. q4 := map[string]interface{}{}
  1197. if platform != "-1" {
  1198. q4 = map[string]interface{}{"platform": platform}
  1199. }
  1200. if search != "" {
  1201. query["$and"] = []interface{}{q1, q2, q3, q4}
  1202. } else {
  1203. query["$and"] = []interface{}{q2, q3, q4}
  1204. }
  1205. sort := `{"%s":%d}`
  1206. orderIndex := f.GetString("order[0][column]")
  1207. orderName := f.GetString(fmt.Sprintf("columns[%s][data]", orderIndex))
  1208. orderType := 1
  1209. if f.GetString("order[0][dir]") != "asc" {
  1210. orderType = -1
  1211. }
  1212. sort = fmt.Sprintf(sort, orderName, orderType)
  1213. page := start / 10
  1214. qu.Debug("query:", query)
  1215. luas, _ := u.MgoE.Find("luaconfig", query, sort, list_fields, false, start, limit)
  1216. count := u.MgoE.Count("luaconfig", query)
  1217. for k, v := range *luas {
  1218. v["num"] = k + 1 + page*10
  1219. l_uploadtime := qu.Int64All(v["l_uploadtime"])
  1220. v["l_uploadtime"] = qu.FormatDateByInt64(&l_uploadtime, qu.Date_Full_Layout)
  1221. l_checktime := qu.Int64All(v["l_checktime"])
  1222. v["l_checktime"] = qu.FormatDateByInt64(&l_checktime, qu.Date_Full_Layout)
  1223. if l_checktime > 0 { //核对
  1224. v["is_check"] = true
  1225. } else { //未核对
  1226. v["is_check"] = false
  1227. }
  1228. if tmp, ok := spinfos.Load(v["code"]); ok {
  1229. info := tmp.(*spinfo)
  1230. v["modifytime"] = info.lastHeartbeat
  1231. v["yesterday"] = fmt.Sprint(info.yesterdayDowncount) + "/" + fmt.Sprint(info.yestoDayRequestNum)
  1232. v["terday"] = fmt.Sprint(info.todayDowncount) + "/" + fmt.Sprint(info.toDayRequestNum)
  1233. v["lastdowncount"] = info.lastDowncount
  1234. v["lstate"] = info.lstate
  1235. } else {
  1236. v["modifytime"] = ""
  1237. v["yesterday"] = ""
  1238. v["terday"] = ""
  1239. v["lastdowncount"] = 0
  1240. v["lstate"] = ""
  1241. }
  1242. }
  1243. f.ServeJson(map[string]interface{}{"draw": draw, "data": luas, "recordsFiltered": count, "recordsTotal": count})
  1244. } else {
  1245. events := []string{}
  1246. for k, _ := range util.Config.Uploadevents {
  1247. events = append(events, k)
  1248. }
  1249. sort.Strings(events)
  1250. f.T["events"] = events
  1251. f.Render("lualist.html", &f.T)
  1252. }
  1253. }
  1254. //心跳监控
  1255. func (f *Front) Heart() {
  1256. if f.Method() == "POST" {
  1257. event, _ := f.GetInteger("event")
  1258. start, _ := f.GetInteger("start")
  1259. limit, _ := f.GetInteger("length")
  1260. draw, _ := f.GetInteger("draw")
  1261. searchStr := f.GetString("search[value]")
  1262. search := strings.TrimSpace(searchStr) //只能搜索code
  1263. //qu.Debug("search:", search, "start:", start, "limit:", limit, "draw:", draw)
  1264. query := map[string]interface{}{
  1265. "del": false,
  1266. }
  1267. if event > -1 {
  1268. query["event"] = event
  1269. }
  1270. if search != "" {
  1271. query["code"] = search
  1272. }
  1273. sort := `{"%s":%d}`
  1274. orderIndex := f.GetString("order[0][column]")
  1275. orderName := f.GetString(fmt.Sprintf("columns[%s][data]", orderIndex))
  1276. orderType := 1
  1277. if f.GetString("order[0][dir]") != "asc" {
  1278. orderType = -1
  1279. }
  1280. sort = fmt.Sprintf(sort, orderName, orderType)
  1281. qu.Debug("query:", query, "sort:", sort)
  1282. list, _ := u.MgoS.Find("spider_heart", query, sort, nil, false, start, limit)
  1283. count := u.MgoS.Count("spider_heart", query)
  1284. for _, l := range *list {
  1285. code := qu.ObjToString(l["code"])
  1286. d, _ := u.MgoE.FindOneByField("luaconfig", map[string]interface{}{"code": code}, map[string]interface{}{"state": 1, "param_common": 1, "str_list": 1, "type_list": 1})
  1287. l["state"] = (*d)["state"]
  1288. l["param_common"] = (*d)["param_common"]
  1289. if lt := qu.Int64All(l["list"]); lt != 0 {
  1290. l["list"] = qu.FormatDateByInt64(&lt, qu.Date_Full_Layout)
  1291. } else {
  1292. l["list"] = 0
  1293. }
  1294. if dt := qu.Int64All(l["detail"]); dt != 0 {
  1295. l["detail"] = qu.FormatDateByInt64(&dt, qu.Date_Full_Layout)
  1296. } else {
  1297. l["detail"] = 0
  1298. }
  1299. if det := qu.Int64All(l["detailexecute"]); det != 0 {
  1300. l["detailexecute"] = qu.FormatDateByInt64(&det, qu.Date_Full_Layout)
  1301. } else {
  1302. l["detailexecute"] = 0
  1303. }
  1304. if ft := qu.Int64All(l["findlist"]); ft != 0 {
  1305. l["findlist"] = qu.FormatDateByInt64(&ft, qu.Date_Full_Layout)
  1306. } else {
  1307. l["findlist"] = 0
  1308. }
  1309. ut := qu.Int64All(l["updatetime"])
  1310. l["updatetime"] = qu.FormatDateByInt64(&ut, qu.Date_Full_Layout)
  1311. //l["isfindlist"] = "否"
  1312. //typeList := qu.IntAll((*d)["type_list"])
  1313. //strList := qu.ObjToString((*d)["str_list"])
  1314. //if typeList == 1 && strings.Contains(strList, "findListHtml") {
  1315. // l["isfindlist"] = "是"
  1316. //}
  1317. }
  1318. f.ServeJson(map[string]interface{}{"draw": draw, "data": list, "recordsFiltered": count, "recordsTotal": count})
  1319. } else {
  1320. events := []string{}
  1321. for k, _ := range util.Config.Uploadevents {
  1322. events = append(events, k)
  1323. }
  1324. sort.Strings(events)
  1325. f.T["events"] = events
  1326. f.Render("heart.html", &f.T)
  1327. }
  1328. }
  1329. //爬虫信息
  1330. type spinfo struct {
  1331. code string
  1332. todayDowncount, toDayRequestNum int
  1333. yesterdayDowncount, yestoDayRequestNum int
  1334. totalDowncount, totalRequestNum int
  1335. errorNum, roundCount, runRate int
  1336. lastDowncount int
  1337. lastHeartbeat string
  1338. lstate string
  1339. }
  1340. //爬虫信息
  1341. func SpiderInfo(data string) {
  1342. data = util.Se.DecodeString(data)
  1343. infos := []map[string]interface{}{}
  1344. err := json.Unmarshal([]byte(data), &infos)
  1345. if err != nil {
  1346. return
  1347. }
  1348. for _, tmp := range infos {
  1349. lastHeartbeat := qu.Int64All(tmp["lastHeartbeat"])
  1350. info := &spinfo{
  1351. code: fmt.Sprint(tmp["code"]),
  1352. todayDowncount: qu.IntAll(tmp["todayDowncount"]),
  1353. toDayRequestNum: qu.IntAll(tmp["toDayRequestNum"]),
  1354. yesterdayDowncount: qu.IntAll(tmp["yesterdayDowncount"]),
  1355. yestoDayRequestNum: qu.IntAll(tmp["yestoDayRequestNum"]),
  1356. totalDowncount: qu.IntAll(tmp["totalDowncount"]),
  1357. totalRequestNum: qu.IntAll(tmp["totalRequestNum"]),
  1358. errorNum: qu.IntAll(tmp["errorNum"]),
  1359. roundCount: qu.IntAll(tmp["roundCount"]),
  1360. runRate: qu.IntAll(tmp["runRate"]),
  1361. lastHeartbeat: qu.FormatDateByInt64(&lastHeartbeat, qu.Date_Full_Layout),
  1362. lastDowncount: qu.IntAll(tmp["lastDowncount"]),
  1363. lstate: fmt.Sprint(tmp["lstate"]),
  1364. }
  1365. spinfos.Store(info.code, info)
  1366. //log.Println(info)
  1367. }
  1368. }
  1369. //接受维护任务信息
  1370. func SpiderModifyTask(data string) {
  1371. data = util.Se.DecodeString(data)
  1372. mtasks := []map[string]interface{}{}
  1373. err := json.Unmarshal([]byte(data), &mtasks)
  1374. if err != nil {
  1375. return
  1376. }
  1377. for k, tmp := range mtasks {
  1378. log.Println(k, tmp)
  1379. }
  1380. }
  1381. //查看是否有该任务
  1382. func checkTask(codes []string, num int) []string {
  1383. // var id string = ""
  1384. query := map[string]interface{}{}
  1385. var idArr []string
  1386. if len(codes) > 0 {
  1387. for _, v := range codes {
  1388. if num == 1 {
  1389. query = map[string]interface{}{
  1390. "s_code": v,
  1391. "i_state": map[string]interface{}{
  1392. "$in": []int{1, 2, 5},
  1393. },
  1394. }
  1395. } else if num == 2 { //打回时查询待审核的任务
  1396. query = map[string]interface{}{
  1397. "s_code": v,
  1398. "i_state": 3,
  1399. }
  1400. } else if num == 3 { //审核通过时查询待处理、处理中、待审核、未通过的任务
  1401. query = map[string]interface{}{
  1402. "s_code": v,
  1403. "i_state": map[string]interface{}{
  1404. "$in": []int{1, 2, 3, 5},
  1405. },
  1406. }
  1407. } else if num == 7 {
  1408. query = map[string]interface{}{
  1409. "s_code": v,
  1410. "i_state": map[string]interface{}{
  1411. "$in": []int{2, 5},
  1412. },
  1413. }
  1414. }
  1415. task, _ := u.MgoE.Find("task", query, nil, nil, false, -1, -1)
  1416. if task != nil {
  1417. for _, t := range *task {
  1418. idArr = append(idArr, mongodb.BsonIdToSId(t["_id"]))
  1419. }
  1420. }
  1421. return idArr
  1422. }
  1423. }
  1424. return idArr
  1425. }