front.go 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229
  1. // front
  2. package front
  3. import (
  4. "fmt"
  5. "github.com/go-xweb/httpsession"
  6. "github.com/go-xweb/xweb"
  7. "github.com/lauyoume/gopinyin"
  8. "github.com/tealeg/xlsx"
  9. "io/ioutil"
  10. "log"
  11. "mongodb"
  12. "os"
  13. qu "qfw/util"
  14. "regexp"
  15. "sort"
  16. "spider"
  17. util "spiderutil"
  18. "strconv"
  19. "strings"
  20. "sync"
  21. "time"
  22. u "util"
  23. )
  24. type Front struct {
  25. *xweb.Action
  26. login xweb.Mapper `xweb:"/"`
  27. logout xweb.Mapper `xweb:"/center/logout"` //退出
  28. loadIndex xweb.Mapper `xweb:"/center"` //控制中心
  29. spidernew xweb.Mapper `xweb:"/center/spider"` //爬虫新建
  30. reg xweb.Mapper `xweb:"/center/reg"` //爬虫注册
  31. assign xweb.Mapper `xweb:"/center/user/assign"` //分配爬虫
  32. auditExport xweb.Mapper `xweb:"/center/user/auditexport"` //导出审核日志
  33. loadSpider xweb.Mapper `xweb:"/center/spider/edit/(.*)"` //爬虫加载
  34. viewSpider xweb.Mapper `xweb:"/center/spider/view/(.*)"` //爬虫查看
  35. downSpider xweb.Mapper `xweb:"/center/spider/download/(.*)"` //爬虫下载
  36. upState xweb.Mapper `xweb:"/center/spider/upstate"` //爬虫状态更新
  37. assort xweb.Mapper `xweb:"/center/spider/assort"` //审核人员分类(无发布、需登录、无法处理、需删除)
  38. batchShelves xweb.Mapper `xweb:"/center/spider/batchShelves"` //批量上下架
  39. checktime xweb.Mapper `xweb:"/center/spider/checktime"` //爬虫核对
  40. disables xweb.Mapper `xweb:"/center/spider/disable"` //批量作废
  41. changeEvent xweb.Mapper `xweb:"/center/changeEvent"` //节点更新
  42. getJson xweb.Mapper `xweb:"/center/spider/json"` //
  43. //delRedis xweb.Mapper `xweb:"/center/spider/delRedis"` //清理Redis
  44. updateESP xweb.Mapper `xweb:"/center/spider/updateesp"` //修改爬虫的节点/状态/平台
  45. updatePendState xweb.Mapper `xweb:"/center/spider/updatePendState"` //更新爬虫挂起状态
  46. tagCode xweb.Mapper `xweb:"/center/spider/tagcode"` //标记爬虫
  47. spiderRemark xweb.Mapper `xweb:"/center/spider/spiderremark"` //标记爬虫
  48. spiderModel xweb.Mapper `xweb:"/center/model"` //获取补充模型
  49. runStep xweb.Mapper `xweb:"/center/run"` //方法测试
  50. spiderPass xweb.Mapper `xweb:"/center/spider/pass"` //整体测试
  51. runPinYin xweb.Mapper `xweb:"/center/runpy"` //获取拼音
  52. saveStep xweb.Mapper `xweb:"/center/save"` //爬虫保存
  53. saveJs xweb.Mapper `xweb:"/center/save/js"` //保存js
  54. loadModel xweb.Mapper `xweb:"/center/gmodel/(.*)"` //加载模型
  55. importdata xweb.Mapper `xweb:"/center/importdata"` //导入爬虫列表页面
  56. importLua xweb.Mapper `xweb:"/center/importlua"` //导入爬虫
  57. oldedit xweb.Mapper `xweb:"/center/oldedit"` //老文件编辑
  58. findName xweb.Mapper `xweb:"/center/findname"` //即时查询名称
  59. checkrepeat xweb.Mapper `xweb:"/center/spider/isrepeat"` //脚本代码判重
  60. heart xweb.Mapper `xweb:"/center/heart"` //心跳监控
  61. spiderCopy xweb.Mapper `xweb:"/center/spider/copy"` //补采复制爬虫
  62. spiderSplitCopy xweb.Mapper `xweb:"/center/spider/splitcopy"` //拆分复制爬虫
  63. spiderCloseErr xweb.Mapper `xweb:"/center/spider/closeerr"` //删除错误爬虫
  64. Base Base
  65. OtherBase OtherBase
  66. Step1 Step1
  67. Step2 Step2
  68. Step3 Step3
  69. StepRe3 StepRe3
  70. U U
  71. luaList xweb.Mapper `xweb:"/center/lualist.html"` //脚本管理
  72. user xweb.Mapper `xweb:"/center/user.html"` //用户管理
  73. delUser xweb.Mapper `xweb:"/center/user/del"` //删除用户
  74. updateUser xweb.Mapper `xweb:"/center/user/updateUser"` //修改用户信息
  75. checkUsenamer xweb.Mapper `xweb:"/center/user/checkUsenamer"` //校验用户名的唯一性
  76. checkEmail xweb.Mapper `xweb:"/center/user/checkEmail"` //校验邮箱的唯一性
  77. saveNewUser xweb.Mapper `xweb:"/center/user/saveNewUser"` //添加用户
  78. getCity xweb.Mapper `xweb:"/center/getCity"` //获取城市
  79. //补采信息
  80. supplementDayList xweb.Mapper `xweb:"/center/supplement/daylist"` //日补采列表
  81. supplementWeekList xweb.Mapper `xweb:"/center/supplement/weeklist"` //周补采列表
  82. //爬虫认领
  83. claimCode xweb.Mapper `xweb:"/center/claim/claimcode"` //爬虫认领
  84. returnCode xweb.Mapper `xweb:"/center/claim/returncode"` //爬虫归还
  85. }
  86. const Sp_state_0, Sp_state_1, Sp_state_2, Sp_state_3, Sp_state_4, Sp_state_5, Sp_state_6, Sp_state_7, Sp_state_8, Sp_state_9, Sp_state_10 = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 //0待完成,1待审核,2打回,3发布,4作废,5已上架,6已下架,7无发布,8需登录,9转python,10已删除
  87. var spinfos sync.Map = sync.Map{}
  88. var SessMap map[string]*httpsession.Session
  89. var AutoTpl map[string]interface{}
  90. // var Mails *util.Mail
  91. // var Reg = regexp.MustCompile(`(http|https)://([\w]+\.)+[\w]+`)
  92. var ProjectHrefReg = regexp.MustCompile("projecthref")
  93. var Transfercode map[string]interface{}
  94. // 爬虫列表
  95. func (f *Front) LoadIndex() {
  96. auth := qu.IntAll(f.GetSession("auth"))
  97. if f.Method() == "POST" {
  98. start, _ := f.GetInteger("start")
  99. limit, _ := f.GetInteger("length")
  100. draw, _ := f.GetInteger("draw")
  101. searchStr := f.GetString("search[value]")
  102. event, _ := f.GetInteger("taskEvent") //节点
  103. search := strings.TrimSpace(searchStr)
  104. state, _ := f.GetInteger("state")
  105. platform := f.GetString("platform")
  106. modifyuser := f.GetString("modifyuser")
  107. infoformat, _ := f.GetInteger("infoformat") //招标类型
  108. priority := f.GetString("priority") //优先级
  109. query := map[string]interface{}{}
  110. if event > -1 {
  111. query["event"] = event
  112. }
  113. if platform != "-1" {
  114. query["platform"] = platform
  115. }
  116. if modifyuser != "-1" {
  117. query["modifyuser"] = modifyuser
  118. }
  119. if priority != "-1" {
  120. priorityRange := strings.Split(priority, "-")
  121. gte := qu.IntAll(priorityRange[0])
  122. lt := qu.IntAll(priorityRange[1])
  123. q := map[string]interface{}{}
  124. if lt == 0 {
  125. q["$gte"] = gte
  126. } else {
  127. q = map[string]interface{}{
  128. "$gte": gte,
  129. "$lt": lt,
  130. }
  131. }
  132. query["priority"] = q
  133. }
  134. if infoformat > -1 {
  135. query["infoformat"] = infoformat
  136. }
  137. if search != "" {
  138. query["$or"] = []interface{}{
  139. map[string]interface{}{"code": map[string]interface{}{"$regex": search}},
  140. map[string]interface{}{"createuser": map[string]interface{}{"$regex": search}},
  141. map[string]interface{}{"param_common.1": map[string]interface{}{"$regex": search}},
  142. }
  143. }
  144. if auth == u.Role_Dev { //开发员
  145. if state > -1 {
  146. query["state"] = state
  147. }
  148. query["createuserid"] = f.GetSession("userid")
  149. query["claimtype"] = map[string]interface{}{ //爬虫开发只能搜索已认领和历史爬虫
  150. "$gte": CLAIMTYPECLAIMED,
  151. }
  152. } else {
  153. claimtype, _ := f.GetInteger("claimtype") //认领状态
  154. if claimtype > -1 {
  155. query["claimtype"] = claimtype
  156. }
  157. identity := qu.IntAll(f.GetSession("identity"))
  158. if identity == 0 { //外包管理员
  159. ids := []string{}
  160. if tmpIds := f.GetSession("ids"); tmpIds != nil {
  161. ids = qu.ObjArrToStringArr(tmpIds.([]interface{}))
  162. }
  163. query["createuserid"] = map[string]interface{}{
  164. "$in": ids,
  165. }
  166. }
  167. if state > -1 {
  168. query["state"] = state
  169. } else if auth == u.Role_Examine {
  170. query["state"] = Sp_state_1
  171. }
  172. }
  173. sort := `{"%s":%d}`
  174. orderIndex := f.GetString("order[0][column]")
  175. orderName := f.GetString(fmt.Sprintf("columns[%s][data]", orderIndex))
  176. orderType := 1
  177. if f.GetString("order[0][dir]") != "asc" {
  178. orderType = -1
  179. }
  180. if orderName == "param_common" {
  181. orderName = orderName + ".1"
  182. }
  183. sort = fmt.Sprintf(sort, orderName, orderType)
  184. page := start / 10
  185. //log.Println("sort", sort, orderName)
  186. qu.Debug("query:", query, "sort:", sort)
  187. //luas := *mgdb.Find("luaconfig", query, sort, list_fields, false, start, limit)
  188. //count := mgdb.Count("luaconfig", query)
  189. luas, _ := u.MgoEB.Find("luaconfig", query, sort, map[string]interface{}{"str_list": 0, "str_content": 0}, false, start, limit)
  190. count := u.MgoEB.Count("luaconfig", query)
  191. for k, v := range *luas {
  192. v["num"] = k + 1 + page*10
  193. v["encode"] = util.Se.Encode2Hex(fmt.Sprint(v["code"]))
  194. if v["event"] == nil { //节点
  195. v["event"] = 0
  196. }
  197. user, _ := u.MgoEB.FindOne("user", map[string]interface{}{"s_name": qu.ObjToString(v["createuser"])})
  198. //v["state"] = LuaStateMap[qu.IntAll(v["state"])]
  199. v["i_scope"] = (*user)["i_scope"]
  200. }
  201. f.ServeJson(map[string]interface{}{"draw": draw, "data": luas, "recordsFiltered": count, "recordsTotal": count})
  202. } else {
  203. events := []string{}
  204. for k, _ := range util.Config.Uploadevents {
  205. events = append(events, k)
  206. }
  207. identity := qu.IntAll(f.GetSession("identity"))
  208. ids := []string{}
  209. if identity == 0 && auth > u.Role_Dev { //外包管理员
  210. if tmpIds := f.GetSession("ids"); tmpIds != nil {
  211. ids = qu.ObjArrToStringArr(tmpIds.([]interface{}))
  212. }
  213. }
  214. f.T["identity"] = identity
  215. f.T["ids"] = ids
  216. f.T["modifyusers"] = u.GetModifyUsers()
  217. sort.Strings(events)
  218. f.T["events"] = events
  219. f.Render("index.html", &f.T)
  220. }
  221. }
  222. func (f *Front) Checkrepeat() {
  223. code := f.GetString("code")
  224. //one := *mgdb.FindOne("luaconfig", bson.M{"code": code})
  225. one, _ := u.MgoEB.FindOne("luaconfig", map[string]interface{}{"code": code})
  226. if len(*one) > 0 {
  227. f.ServeJson("y")
  228. } else {
  229. f.ServeJson("n")
  230. }
  231. }
  232. // 新建
  233. func (f *Front) Spidernew() error {
  234. auth := qu.IntAll(f.GetSession("auth"))
  235. if auth != u.Role_Admin {
  236. return nil
  237. }
  238. copy := f.GetString("copy")
  239. if copy != "" {
  240. //one := *mgdb.FindOne("luaconfig", bson.M{"code": copy})
  241. one, _ := u.MgoEB.FindOne("luaconfig", map[string]interface{}{"code": copy})
  242. delete((*one), "_id")
  243. delete((*one), "code")
  244. base := (*one)["param_common"].([]interface{})
  245. base[0] = ""
  246. base[1] = ""
  247. (*one)["param_common"] = base
  248. f.T["lua"] = one
  249. }
  250. f.T["isflow"] = 1 //新建爬虫时,初始化isflow的值
  251. f.T["actiontext"] = "新建"
  252. f.T["restate"] = 4 //此处设置restate=4无意义,只为了页面不报错
  253. f.T["areas"] = u.Area //
  254. f.T["citys"] = u.City //
  255. f.T["provinces"] = u.Province //
  256. return f.Render("spideredit.html", &f.T)
  257. }
  258. // 得到模型
  259. func (f *Front) SpiderModel() {
  260. f.ServeJson(util.Config.Model)
  261. }
  262. func (f *Front) RunPinYin() {
  263. word := f.GetString("word")
  264. str := gopinyin.Convert(word, true)
  265. f.Write(str)
  266. }
  267. type U struct {
  268. User string
  269. Name string
  270. Pwd string
  271. }
  272. func (f *Front) Reg() {
  273. }
  274. func (f *Front) ImportLua() {
  275. auth := qu.IntAll(f.GetSession("auth"))
  276. if auth != u.Role_Admin {
  277. f.ServeJson("没有权限")
  278. return
  279. }
  280. if f.Method() == "POST" {
  281. mf, _, err := f.GetFile("xlsx")
  282. errorinfo := map[string]interface{}{}
  283. if err == nil {
  284. binary, _ := ioutil.ReadAll(mf)
  285. xls, _ := xlsx.OpenBinary(binary)
  286. sheet := xls.Sheets[0]
  287. rows := sheet.Rows
  288. for k, v := range rows {
  289. if k != 0 {
  290. cells := v.Cells
  291. if cells[1].Value != "" {
  292. code := cells[1].Value
  293. code = u.SymbolReg.ReplaceAllString(code, "")
  294. query := map[string]interface{}{"code": cells[1].Value}
  295. rs, _ := u.MgoEB.FindOne("import", query)
  296. if len(*rs) > 0 {
  297. errorinfo[cells[1].Value] = "第" + strconv.Itoa(k) + "行爬虫代码重复,请修改"
  298. continue
  299. }
  300. o := make(map[string]interface{})
  301. o["name"] = u.SymbolReg.ReplaceAllString(cells[0].Value, "")
  302. o["code"] = u.SymbolReg.ReplaceAllString(code, "")
  303. o["channel"] = cells[2].Value
  304. spiderremark := cells[3].Value
  305. if spiderremark == "" {
  306. spiderremark = `采集“` + cells[2].Value + `”栏目(含子栏目)`
  307. }
  308. o["spiderremark"] = spiderremark
  309. //重复域名的网站不再新增爬虫
  310. href := cells[4].Value
  311. one, _ := u.MgoEB.FindOne("luaconfig", map[string]interface{}{"href": href})
  312. if len(*one) > 0 {
  313. errorinfo[cells[1].Value] = "第" + strconv.Itoa(k) + "行爬虫" + cells[1].Value + ",该栏目已存在,请确认"
  314. //continue
  315. }
  316. o["channeladdr"] = href
  317. o["timestamp"] = time.Now().Unix()
  318. o["modifyuser"] = cells[5].Value
  319. o["event"] = cells[6].Value
  320. o["incrementevent"] = cells[7].Value
  321. if cells[8].Value == "是" {
  322. o["isflow"] = 1
  323. } else {
  324. o["isflow"] = 0
  325. }
  326. o["priority"], _ = cells[9].Int()
  327. o["platform"] = cells[10].Value
  328. o["area"] = cells[11].Value
  329. o["city"] = cells[12].Value
  330. o["district"] = cells[13].Value
  331. weigh, _ := cells[14].Int()
  332. if weigh == -1 {
  333. weigh = 1
  334. }
  335. o["weight"] = weigh
  336. //存储表
  337. coll := cells[15].Value
  338. if coll == "" {
  339. coll = "bidding"
  340. }
  341. //爬虫类型
  342. infoformat, _ := cells[16].Int()
  343. if infoformat < 1 {
  344. errorinfo[cells[1].Value] = "第" + strconv.Itoa(k) + "行爬虫" + cells[1].Value + ",缺少爬虫类型信息"
  345. continue
  346. }
  347. o["infoformat"] = infoformat
  348. if strings.Contains(code, "bidding") {
  349. errorinfo[cells[1].Value] = "第" + strconv.Itoa(k) + "行爬虫" + cells[1].Value + ",存储表错误"
  350. continue
  351. }
  352. o["coll"] = coll
  353. //table := cells[6].Value
  354. //o["table"] = table
  355. //o["transfercode"] = qu.IntAll(Transfercode[table])
  356. ok := saveLua(o) //保存爬虫
  357. if ok == false {
  358. errorinfo[cells[1].Value] = "第" + strconv.Itoa(k) + "行找不到作者,已经过滤"
  359. } else {
  360. o["status"] = 1
  361. o["author"] = o["modifyuser"]
  362. o["importuser"] = f.GetSession("username")
  363. u.MgoEB.Save("import", o)
  364. }
  365. }
  366. }
  367. }
  368. f.ServeJson(errorinfo)
  369. } else {
  370. f.ServeJson(false)
  371. }
  372. }
  373. }
  374. func saveLua(o map[string]interface{}) bool {
  375. param := map[string]interface{}{}
  376. AutoTpl["Base.SpiderName"] = o["name"]
  377. AutoTpl["Base.SpiderCode"] = o["code"]
  378. AutoTpl["Base.SpiderChannel"] = o["channel"]
  379. AutoTpl["Base.SpiderTargetChannelUrl"] = o["channeladdr"]
  380. modifyuser := o["modifyuser"].(string)
  381. priority := qu.IntAll(o["priority"])
  382. param["priority"] = o["priority"] //优先级
  383. platform := qu.ObjToString(o["platform"])
  384. var userid, email string
  385. claimLog := map[string]interface{}{}
  386. if modifyuser != "" { //指定维护人
  387. one, _ := u.MgoEB.FindOne("user", map[string]interface{}{"s_name": modifyuser, "i_auth": 1, "i_delete": 0})
  388. if len(*one) == 0 {
  389. return false
  390. }
  391. email = qu.ObjToString((*one)["s_email"])
  392. userid = mongodb.BsonIdToSId((*one)["_id"])
  393. if platform != "python" {
  394. now := time.Now().Unix()
  395. recovertime := now + 365*86400
  396. param["claimtime"] = now //认领时间
  397. param["claimtype"] = CLAIMTYPECLAIMED
  398. param["recovertime"] = recovertime
  399. claimLog = map[string]interface{}{
  400. "site": o["name"],
  401. "code": o["code"],
  402. "channel": o["channel"],
  403. "modifyuser": modifyuser,
  404. "priority": priority,
  405. "stype": "认领",
  406. "comeintime": now,
  407. "claimtime": now,
  408. "recovertime": recovertime,
  409. "returntime": int64(0),
  410. "important": false,
  411. "returnreason": "",
  412. "claimrecovertype": 0,
  413. }
  414. } else {
  415. param["recovertime"] = int64(0) //回收时间
  416. param["claimtime"] = int64(0) //认领时间
  417. param["claimtype"] = CLAIMTYPEHISTORY //爬虫认领状态
  418. }
  419. } else { //未指定人
  420. param["recovertime"] = int64(0) //回收时间
  421. param["claimtime"] = int64(0) //认领时间
  422. param["claimtype"] = CLAIMTYPEUNCLAIMED //爬虫认领状态(未认领)
  423. }
  424. common := []interface{}{
  425. AutoTpl["Base.SpiderCode"],
  426. AutoTpl["Base.SpiderName"],
  427. AutoTpl["Base.SpiderChannel"],
  428. AutoTpl["Base.SpiderDownDetailPage"],
  429. AutoTpl["Base.SpiderStartPage"],
  430. AutoTpl["Base.SpiderMaxPage"],
  431. AutoTpl["Base.SpiderRunRate"],
  432. //AutoTpl["Base.Spider2Collection"],
  433. //"bidding", //爬虫导入新建默认为bidding
  434. qu.ObjToString(o["coll"]),
  435. AutoTpl["Base.SpiderPageEncoding"],
  436. AutoTpl["Base.SpiderStoreMode"],
  437. AutoTpl["Base.SpiderStoreToMsgEvent"],
  438. AutoTpl["Base.SpiderTargetChannelUrl"],
  439. AutoTpl["Base.SpiderLastDownloadTime"],
  440. AutoTpl["Base.SpiderIsHistoricalMend"],
  441. AutoTpl["Base.SpiderIsMustDownload"],
  442. }
  443. ptime := []interface{}{
  444. AutoTpl["Step1.DateFormat"],
  445. AutoTpl["Step1.Address"],
  446. AutoTpl["Step1.ContentChooser"],
  447. }
  448. list := []interface{}{
  449. AutoTpl["Step2.Listadd"],
  450. AutoTpl["Step2.Listadds"],
  451. AutoTpl["Step2.BlockChooser"],
  452. AutoTpl["Step2.AddressChooser"],
  453. AutoTpl["Step2.TitleChooser"],
  454. AutoTpl["Step2.DateChooser"],
  455. AutoTpl["Step2.DateFormat"],
  456. }
  457. content := []interface{}{
  458. AutoTpl["Step3.ContentChooser"],
  459. AutoTpl["Step3.ElementChooser"],
  460. }
  461. param["param_common"] = common
  462. //向导模式
  463. param["param_time"] = ptime
  464. param["param_list"] = list
  465. param["param_content"] = content
  466. param["type_time"] = 0
  467. param["type_list"] = 0
  468. param["type_content"] = 0
  469. //专家模式
  470. param["str_time"] = ""
  471. param["str_list"] = ""
  472. param["str_content"] = ""
  473. param["comeintime"] = time.Now().Unix()
  474. param["code"] = o["code"]
  475. param["site"] = o["name"]
  476. param["href"] = o["channeladdr"]
  477. param["channel"] = o["channel"]
  478. param["createuser"] = modifyuser
  479. param["createuserid"] = userid
  480. param["createuseremail"] = email
  481. param["modifyuser"] = modifyuser
  482. param["modifyuserid"] = userid
  483. param["modifytime"] = time.Now().Unix()
  484. param["state"] = 0 //未完成
  485. if qu.IntAll(o["event"]) > 0 {
  486. param["event"] = qu.IntAll(o["event"])
  487. }
  488. s_model := "bid"
  489. configModel := util.Config.Model[s_model]
  490. model := map[string]interface{}{}
  491. for k, _ := range configModel {
  492. model[k] = qu.ObjToString(o[k])
  493. }
  494. param["model"] = model
  495. param["next"] = email
  496. param["urgency"] = o["urgency"]
  497. param["isflow"] = o["isflow"]
  498. param["spidertype"] = "history"
  499. param["spiderremark"] = o["spiderremark"]
  500. incrementevent := qu.ObjToString(o["incrementevent"])
  501. if movevent, ok := util.Config.Uploadevents[incrementevent].(string); ok && movevent != "" {
  502. param["spidermovevent"] = movevent
  503. }
  504. //} else {
  505. // param["spidermovevent"] = "7700"
  506. //}
  507. param["incrementevent"] = qu.IntAll(o["incrementevent"])
  508. param["platform"] = platform
  509. param["weight"] = o["weight"]
  510. param["infoformat"] = o["infoformat"]
  511. infoformat := qu.IntAll(o["infoformat"])
  512. infotype := "招标"
  513. if infoformat == 2 {
  514. infotype = "拟建/审批"
  515. } else if infoformat == 3 {
  516. infotype = "产权"
  517. } else if infoformat == 4 {
  518. infotype = "舆情"
  519. }
  520. //默认字段
  521. param["spidercompete"] = true //2021-11-20后爬虫加此字段(表示新爬虫,剑鱼网站不展示原文)
  522. param["spiderhistorymaxpage"] = 1 //历史最大页
  523. param["pendstate"] = 0 //
  524. param["grade"] = 0 //爬虫难易度(主要用于python爬虫使用)
  525. param["spiderimportant"] = false //是否为重点网站爬虫
  526. param["urgency"] = 0
  527. //qu.Debug("param---", param)
  528. ok := spider.SaveSpider(o["code"].(string), param)
  529. if ok { //保存成功,校验新导入的爬虫对应站点是否存在,否则加站点记录
  530. site, _ := u.MgoEB.FindOneByField("site", map[string]interface{}{"site": o["name"]}, map[string]interface{}{"important": 1})
  531. if len(*site) == 0 {
  532. qu.Debug("补充站点信息:", o["name"])
  533. domain := u.DomainReg.FindString(qu.ObjToString(AutoTpl["Base.SpiderTargetChannelUrl"]))
  534. if domain != "" {
  535. domain = u.ReplaceReg.ReplaceAllString(domain, "")
  536. }
  537. siteInfo := map[string]interface{}{
  538. "site": o["name"],
  539. "domain": domain, //
  540. "another_name": "",
  541. "area": qu.ObjToString(model["area"]),
  542. "city": qu.ObjToString(model["city"]),
  543. "district": qu.ObjToString(model["district"]),
  544. "site_type": "",
  545. "second_type": "",
  546. "industry": "",
  547. "p_site": "",
  548. "s_site": "",
  549. "remarktime": time.Now().Unix(),
  550. "event": incrementevent,
  551. "platform": o["platform"],
  552. "spider_status": "0/1",
  553. "updatetime": time.Now().Unix(),
  554. "delete": false,
  555. "comeintime": time.Now().Unix(),
  556. "important": 0,
  557. "site_status": 1,
  558. "lasttime": int64(0),
  559. "site_datanum": 0,
  560. "period": float32(0),
  561. "infotype": infotype,
  562. "sponsor": "",
  563. "isneedregister": 0,
  564. "isregistered": 0,
  565. "special_type": "",
  566. "account": "",
  567. "password": "",
  568. "f_area": "",
  569. "f_city": "",
  570. "f_district": "",
  571. "site_subtype": "",
  572. "site_toptype": "",
  573. "type_plate": "",
  574. }
  575. u.MgoEB.Save("site", siteInfo)
  576. } else if qu.IntAll((*site)["important"]) == 1 { //重点网站
  577. u.MgoEB.Update("luaconfig", map[string]interface{}{"code": o["code"]}, map[string]interface{}{"$set": map[string]interface{}{"spiderimportant": true}}, false, false)
  578. u.MgoEB.Update("site_code_baseinfo", map[string]interface{}{"spidercode": o["code"]}, map[string]interface{}{"$set": map[string]interface{}{
  579. "site": o["name"],
  580. "channel": o["channel"],
  581. "spidercode": o["code"],
  582. "platform": o["platform"],
  583. "modifyuser": "",
  584. "state": 0,
  585. }}, true, false)
  586. }
  587. //生成认领日志
  588. if len(claimLog) > 0 {
  589. u.MgoEB.Save("lua_logs_claim", claimLog)
  590. }
  591. }
  592. return ok
  593. }
  594. func (f *Front) Importdata() {
  595. auth := qu.IntAll(f.GetSession("auth"))
  596. if auth == u.Role_Admin {
  597. if f.Method() == "GET" {
  598. f.Render("import.html")
  599. } else {
  600. query := map[string]interface{}{}
  601. searchStr := f.GetString("search[value]")
  602. search := strings.TrimSpace(searchStr)
  603. if search != "" {
  604. query["$or"] = []interface{}{
  605. map[string]interface{}{"code": map[string]interface{}{"$regex": search}},
  606. map[string]interface{}{"name": map[string]interface{}{"$regex": search}},
  607. }
  608. }
  609. start, _ := f.GetInteger("start")
  610. limit, _ := f.GetInteger("length")
  611. draw, _ := f.GetInteger("draw")
  612. data, _ := u.MgoEB.Find("import", query, `{"timestamp": -1}`, nil, false, start, limit)
  613. count := u.MgoEB.Count("import", query)
  614. f.ServeJson(map[string]interface{}{
  615. "draw": draw,
  616. "data": data,
  617. "recordsFiltered": count,
  618. "recordsTotal": count,
  619. })
  620. }
  621. } else {
  622. f.Write("您没有导入脚本的权限")
  623. }
  624. }
  625. func Wlog(name, code, man, manid, types string, content map[string]interface{}) {
  626. obj := map[string]interface{}{
  627. "name": name,
  628. "code": code,
  629. "man": man,
  630. "manid": manid,
  631. "types": types,
  632. "time": time.Now().Unix(),
  633. "content": content,
  634. }
  635. u.MgoEB.Save("lua_logs", obj)
  636. }
  637. func (f *Front) Oldedit() {
  638. if f.Method() == "GET" {
  639. f.Render("oldedit.html")
  640. } else {
  641. }
  642. }
  643. func (f *Front) FindName() {
  644. words := f.GetString("words")
  645. if words == "" {
  646. f.ServeJson(map[string]interface{}{"error": "null"})
  647. }
  648. //query := bson.M{"$or": []interface{}{
  649. // bson.M{"param_common.0": bson.M{"$regex": words}},
  650. // bson.M{"param_common.1": bson.M{"$regex": words}},
  651. // bson.M{"createuser": bson.M{"$regex": words}},
  652. //}, "oldlua": bson.M{"$exists": false}}
  653. query := map[string]interface{}{
  654. "$or": []interface{}{
  655. map[string]interface{}{"param_common.0": map[string]interface{}{"$regex": words}},
  656. map[string]interface{}{"param_common.1": map[string]interface{}{"$regex": words}},
  657. map[string]interface{}{"createuser": map[string]interface{}{"$regex": words}},
  658. },
  659. "oldlua": map[string]interface{}{
  660. "$exists": false,
  661. },
  662. }
  663. //rs := *mgdb.Find("luaconfig", query, bson.M{"modifytime": -1}, bson.M{"param_common": 1}, false, -1, -1)
  664. rs, _ := u.MgoEB.Find("luaconfig", query, map[string]interface{}{"modifytime": -1}, map[string]interface{}{"param_common": 1}, false, -1, -1)
  665. if len(*rs) > 0 {
  666. f.ServeJson(map[string]interface{}{"data": (*rs)})
  667. } else {
  668. f.ServeJson(map[string]interface{}{"error": "data"})
  669. }
  670. }
  671. // 分配爬虫
  672. func (f *Front) Assign() {
  673. auth := qu.IntAll(f.GetSession("auth"))
  674. if auth != u.Role_Admin {
  675. f.Write("n")
  676. return
  677. }
  678. codes := f.GetString("codes")
  679. userid := f.GetString("userid") //分配给谁
  680. codesarr := strings.Split(codes, ",") //被分配的爬虫
  681. user, _ := u.MgoEB.FindById("user", userid, nil)
  682. if user != nil && len(*user) > 0 {
  683. name := qu.ObjToString((*user)["s_name"])
  684. query := map[string]interface{}{
  685. "code": map[string]interface{}{
  686. "$in": codesarr,
  687. },
  688. }
  689. luas, _ := u.MgoEB.Find("luaconfig", query, nil, ClaimQueryFields, false, -1, -1)
  690. update := [][]map[string]interface{}{}
  691. save := []map[string]interface{}{}
  692. for _, l := range *luas {
  693. set := map[string]interface{}{}
  694. now := time.Now().Unix()
  695. claimtype := qu.IntAll(l["claimtype"])
  696. priority := qu.IntAll(l["priority"])
  697. spiderimportant, _ := l["spiderimportant"].(bool)
  698. if claimtype <= CLAIMTYPECLAIMED {
  699. recovertime := CreateRecovertime(spiderimportant, priority)
  700. set = map[string]interface{}{
  701. "claimtype": CLAIMTYPECLAIMED,
  702. "claimtime": now,
  703. "recovertime": recovertime,
  704. "createuseremail": (*user)["s_email"],
  705. "createuser": name,
  706. "createuserid": userid,
  707. "modifyuser": name,
  708. "modifyuserid": userid,
  709. "platform": (*user)["s_platform"],
  710. }
  711. if claimtype == CLAIMTYPECLAIMED {
  712. //回收日志
  713. recovelog := map[string]interface{}{
  714. "site": l["site"],
  715. "code": l["code"],
  716. "channel": l["channel"],
  717. "modifyuser": l["modifyuser"],
  718. "priority": priority,
  719. "stype": "回收",
  720. "comeintime": now,
  721. "claimtime": l["claimtime"],
  722. "recovertime": l["recovertime"],
  723. "returntime": int64(0),
  724. "important": spiderimportant,
  725. "returnreason": "",
  726. "claimrecovertype": 0,
  727. }
  728. save = append(save, recovelog)
  729. }
  730. //认领日志
  731. claimlog := map[string]interface{}{
  732. "site": l["site"],
  733. "code": l["code"],
  734. "channel": l["channel"],
  735. "modifyuser": name,
  736. "priority": priority,
  737. "stype": "认领",
  738. "comeintime": now,
  739. "claimtime": now,
  740. "recovertime": recovertime,
  741. "returntime": int64(0),
  742. "important": spiderimportant,
  743. "returnreason": "",
  744. "claimrecovertype": 0,
  745. }
  746. save = append(save, claimlog)
  747. } else { //历史爬虫
  748. set = map[string]interface{}{
  749. "createuserid": userid,
  750. "createuser": name,
  751. "createuseremail": (*user)["s_email"],
  752. "modifyuser": name,
  753. "modifyuserid": userid,
  754. "platform": (*user)["s_platform"],
  755. }
  756. }
  757. //更新爬虫
  758. up := []map[string]interface{}{
  759. {"code": l["code"]},
  760. {"$set": set},
  761. }
  762. update = append(update, up)
  763. }
  764. qu.Debug(query)
  765. //更新爬虫信息
  766. b := u.MgoEB.UpdateBulk("luaconfig", update...)
  767. //保存认领日志
  768. if len(save) > 0 {
  769. u.MgoEB.SaveBulk("lua_logs_claim", save...)
  770. }
  771. if b {
  772. go editModify(codesarr, userid, name) //修改爬虫对应任务的维护人
  773. f.Write("y")
  774. } else {
  775. f.Write("n")
  776. }
  777. } else {
  778. f.Write("null")
  779. }
  780. }
  781. // 审核日志导出
  782. func (f *Front) AuditExport() {
  783. auth := qu.IntAll(f.GetSession("auth"))
  784. if auth > u.Role_Admin {
  785. exportnames := f.GetString("exportnames")
  786. exporttype := f.GetString("exporttype")
  787. starttime := f.GetString("starttime")
  788. endtime := f.GetString("endtime")
  789. qu.Debug(exportnames, exporttype, starttime, endtime)
  790. file, err := xlsx.OpenFile("爬虫审核记录.xlsx")
  791. if err != nil {
  792. f.Write("文件打开失败!")
  793. return
  794. }
  795. sheet := file.Sheets[0]
  796. exportLogs(sheet, exportnames, exporttype, starttime, endtime) //导出数据
  797. fname := fmt.Sprintf("爬虫审核打回记录%d.xlsx", time.Now().Unix())
  798. err = file.Save(fname)
  799. if err != nil {
  800. qu.Debug("Save Excel" + fname + "Error")
  801. f.ServeJson("导出失败")
  802. return
  803. }
  804. f.ResponseWriter.Header().Add("Content-Disposition", "attachment;filename=爬虫审核打回记录.xlsx")
  805. f.ServeFile(fname)
  806. go func(path string) { //删除
  807. time.Sleep(time.Second * 30)
  808. os.Remove(path)
  809. }(fname)
  810. } else {
  811. f.Write("您没有权限")
  812. }
  813. }
  814. func exportLogs(sheet *xlsx.Sheet, exportnames, exporttype, starttime, endtime string) {
  815. st, _ := time.ParseInLocation(qu.Date_Short_Layout, starttime, time.Local)
  816. et, _ := time.ParseInLocation(qu.Date_Short_Layout, endtime, time.Local)
  817. query := map[string]interface{}{
  818. "comeintime": map[string]interface{}{
  819. "$gte": st.Unix(),
  820. "$lt": et.Unix(),
  821. },
  822. }
  823. if exporttype == "repulse" { //打回
  824. query["types"] = "打回"
  825. } else if exporttype == "audit" { //审核
  826. query["types"] = "审核"
  827. }
  828. users := map[string]bool{}
  829. for _, user := range strings.Split(exportnames, ",") {
  830. users[user] = true
  831. }
  832. qu.Debug("audit log query:", query)
  833. sess := u.MgoEB.GetMgoConn()
  834. defer u.MgoEB.DestoryMongoConn(sess)
  835. lock := &sync.Mutex{}
  836. wg := &sync.WaitGroup{}
  837. ch := make(chan bool, 10)
  838. n := 0
  839. it := sess.DB(u.MgoEB.DbName).C("lua_logs_auditor").Find(&query).Iter()
  840. count, _ := sess.DB(u.MgoEB.DbName).C("lua_logs_auditor").Find(&query).Count()
  841. qu.Debug("count:", count)
  842. for tmp := map[string]interface{}{}; it.Next(tmp); n++ {
  843. ch <- true
  844. wg.Add(1)
  845. go func(tmp map[string]interface{}) {
  846. defer func() {
  847. <-ch
  848. wg.Done()
  849. }()
  850. id := mongodb.BsonIdToSId(tmp["_id"])
  851. auditor := qu.ObjToString(tmp["auditor"])
  852. code := qu.ObjToString(tmp["code"])
  853. comeintime := qu.Int64All(tmp["comeintime"])
  854. comeintime_z := qu.FormatDateByInt64(&comeintime, qu.Date_Full_Layout)
  855. event := qu.IntAll(tmp["event"])
  856. modifytime := qu.Int64All(tmp["modifytime"])
  857. modifytime_z := qu.FormatDateByInt64(&modifytime, qu.Date_Full_Layout)
  858. reason := qu.ObjToString(tmp["reason"])
  859. types := qu.ObjToString(tmp["types"])
  860. spideruser := qu.ObjToString(tmp["spideruser"])
  861. if !users[spideruser] {
  862. return
  863. }
  864. lock.Lock()
  865. row := sheet.AddRow()
  866. lock.Unlock()
  867. row.AddCell().SetValue(id)
  868. row.AddCell().SetValue(auditor)
  869. row.AddCell().SetValue(code)
  870. row.AddCell().SetValue(comeintime)
  871. row.AddCell().SetValue(comeintime_z)
  872. row.AddCell().SetValue(event)
  873. row.AddCell().SetValue(modifytime)
  874. row.AddCell().SetValue(modifytime_z)
  875. row.AddCell().SetValue(reason)
  876. row.AddCell().SetValue(spideruser)
  877. row.AddCell().SetValue(types)
  878. }(tmp)
  879. tmp = make(map[string]interface{})
  880. }
  881. }
  882. // 修改维护人
  883. func editModify(codesarr []string, userid, name string) {
  884. qu.Debug("修改爬虫对应任务的维护人:", name, codesarr)
  885. //修改modifyid和modify
  886. query := map[string]interface{}{
  887. "s_code": map[string]interface{}{
  888. "$in": codesarr,
  889. },
  890. "i_state": map[string]interface{}{
  891. "$nin": []int{4, 6},
  892. },
  893. }
  894. set := map[string]interface{}{
  895. "$set": map[string]interface{}{
  896. "s_modify": name,
  897. "s_modifyid": userid,
  898. },
  899. }
  900. b := u.MgoEB.Update("task", query, set, false, true)
  901. qu.Debug("任务重新分配成功", b)
  902. }
  903. func (f *Front) UpdatePendState() {
  904. success := false
  905. pendstate, _ := f.GetInteger("pendstate")
  906. code := f.GetString("code")
  907. stype := f.GetString("stype")
  908. qu.Debug(code, stype, pendstate)
  909. //success = u.MgoE.Update("luaconfig", map[string]interface{}{"code": code}, map[string]interface{}{"$set": map[string]interface{}{"pendstate": pendstate}}, false, false)
  910. success = u.MgoEB.Update("luaconfig", map[string]interface{}{"code": code}, map[string]interface{}{"$set": map[string]interface{}{"pendstate": pendstate, "pendtime": time.Now().Unix()}}, false, false)
  911. if success {
  912. if stype == "code" { //爬虫挂起时,关闭所有任务
  913. u.MgoEB.Update("task", map[string]interface{}{"s_code": code, "i_state": map[string]interface{}{"$ne": 6}}, map[string]interface{}{"$set": map[string]interface{}{"i_state": 6, "l_pendtime": time.Now().Unix()}}, false, true)
  914. } else if stype == "task" { //任务挂起任务时任务关闭
  915. id := f.GetString("id")
  916. u.MgoEB.UpdateById("task", id, map[string]interface{}{"$set": map[string]interface{}{"i_pendstate": pendstate, "i_state": 6, "l_pendtime": time.Now().Unix()}})
  917. }
  918. }
  919. f.ServeJson(map[string]interface{}{"success": success})
  920. }
  921. func (f *Front) UpdateESP() {
  922. val := f.GetString("val")
  923. w := f.GetString("w")
  924. id := f.GetString("id")
  925. code := f.GetString("c")
  926. query := map[string]interface{}{
  927. "_id": mongodb.StringTOBsonId(id),
  928. }
  929. set := map[string]interface{}{}
  930. update := map[string]interface{}{
  931. "$set": set,
  932. }
  933. //one, _ := u.MgoE.FindById("luaconfig", id, nil)
  934. one, _ := u.MgoEB.FindById("luaconfig", id, nil)
  935. if len(*one) == 0 {
  936. f.Write("n")
  937. return
  938. }
  939. if w == "infoformat" {
  940. infoformat, _ := f.GetInteger("val")
  941. set["infoformat"] = infoformat
  942. } else if w == "urgency" { //修改紧急度
  943. urgency, _ := f.GetInteger("val")
  944. set["urgency"] = urgency
  945. } else if w == "state" { //无效爬虫改为待完成
  946. tmpEvent := qu.IntAll((*one)["event"])
  947. if one != nil && len(*one) > 0 {
  948. if (*one)["incrementevent"] == nil && tmpEvent != 7000 { //除7000节点外没有incrementevent的要重新设置
  949. set["incrementevent"] = tmpEvent
  950. }
  951. }
  952. set["state"] = 0
  953. set["event"] = 7000
  954. set["spidertype"] = "history"
  955. go ModifyLogs_UpdateCodeState(code)
  956. //更新、新建任务
  957. //task, _ := u.MgoEB.FindOne("task", map[string]interface{}{"s_code": code, "i_state": map[string]interface{}{"$in": []int{0, 1, 2, 3, 5}}})
  958. //text := "指定追加描述:------------------------------\n无发布转待完成\n"
  959. //if len(*task) > 0 { //已有任务,更新
  960. // u.MgoEB.UpdateById("task", (*task)["_id"],
  961. // map[string]interface{}{
  962. // "$set": map[string]interface{}{
  963. // "i_state": 2,
  964. // "s_descript": qu.ObjToString((*task)["s_descript"]) + text,
  965. // "l_updatetime": time.Now().Unix(),
  966. // },
  967. // })
  968. //} else {
  969. // save := map[string]interface{}{
  970. // "s_channel": (*one)["channel"],
  971. // "i_event": (*one)["event"],
  972. // "i_frequencyerrtimes": 0,
  973. // "i_state": 2,
  974. // "s_descript": text,
  975. // "l_complete": time.Now().Unix() + int64(24*3600),
  976. // "s_urgency": "4",
  977. // "i_pendstate": 0,
  978. // "s_modifyid": (*one)["modifyuserid"],
  979. // "s_source": "人工",
  980. // "i_times": 0,
  981. // "l_comeintime": time.Now().Unix(),
  982. // "i_num": 0,
  983. // "s_modify": (*one)["modifyuser"],
  984. // "s_code": code,
  985. // "s_site": (*one)["site"],
  986. // "s_type": "0",
  987. // }
  988. // u.MgoEB.Save("task", save)
  989. //}
  990. } else if w == "platform" {
  991. set["platform"] = val
  992. set["comeintime"] = time.Now().Unix()
  993. if val != "golua平台" && val != "chrome" {
  994. b := u.MgoS.Update("spider_heart", map[string]interface{}{"code": code}, map[string]interface{}{"$set": map[string]interface{}{"del": true}}, false, true)
  995. qu.Debug("Del Heart:", b)
  996. b, err := spider.UpdateSpiderByCodeState(code, "6", qu.IntAll((*one)["event"])) //下架
  997. if b && err == nil {
  998. //历史节点下架为了避免线上运行爬虫待完成时改为7000采集历史,但是又转到其他平台,导致原线上运行节点爬虫并未下线,心跳异常
  999. if incrementevent := (*one)["incrementevent"]; incrementevent != nil {
  1000. b, err = spider.UpdateSpiderByCodeState(code, "6", qu.IntAll(incrementevent))
  1001. }
  1002. //关闭任务
  1003. u.MgoEB.Update("task", map[string]interface{}{"s_code": code, "i_state": map[string]interface{}{"$in": []int{0, 1, 2, 3, 5}}}, map[string]interface{}{"$set": map[string]interface{}{"i_state": 6}}, false, true)
  1004. if b && err == nil {
  1005. set["state"] = 0 //更新状态
  1006. set["luauser"] = map[string]interface{}{
  1007. "createuser": (*one)["createuser"],
  1008. "createuserid": (*one)["createuserid"],
  1009. "createuseremail": (*one)["createuseremail"],
  1010. "modifyuser": (*one)["modifyuser"],
  1011. "modifyuserid": (*one)["modifyuserid"],
  1012. }
  1013. } else {
  1014. qu.Debug("历史节点下架失败")
  1015. f.Write("n")
  1016. return
  1017. }
  1018. } else {
  1019. qu.Debug("增量节点下架失败")
  1020. f.Write("n")
  1021. return
  1022. }
  1023. //qu.Debug("下架:", upresult, code)
  1024. //b, err := UpStateAndUpSpider(code, "", "", "", Sp_state_6) //线上爬虫下架
  1025. //qu.Debug("爬虫下架成功:", b)
  1026. //if !b || err != nil {
  1027. // f.Write("n")
  1028. // return
  1029. //}
  1030. }
  1031. } else { //修改节点
  1032. event, _ := strconv.Atoi(val)
  1033. set["event"] = event
  1034. set["incrementevent"] = event
  1035. //state := f.GetString("s")
  1036. //if state == "5" { //已上架状态改为下架
  1037. code := f.GetString("c")
  1038. set["state"] = 6
  1039. b, err := UpStateAndUpSpider(code, "", "", "", Sp_state_6) //线上爬虫下架
  1040. qu.Debug("爬虫下架成功:", b)
  1041. if !b || err != nil {
  1042. f.Write("n")
  1043. return
  1044. }
  1045. //}
  1046. }
  1047. //if mgdb.Update("luaconfig", query, update, false, false) {
  1048. if u.MgoEB.Update("luaconfig", query, update, false, false) {
  1049. log.Println("Id:", id, " Update", w, val, "Success")
  1050. f.Write("y")
  1051. return
  1052. } else {
  1053. log.Println("Id:", id, " Update", w, val, "Failed")
  1054. f.Write("n")
  1055. return
  1056. }
  1057. f.Write("n")
  1058. }
  1059. func (f *Front) GetCity() {
  1060. area := f.GetString("area")
  1061. cityArr := []string{}
  1062. cityArr = u.Province[area]
  1063. f.ServeJson(cityArr)
  1064. }
  1065. func (f *Front) SpiderCopy() {
  1066. defer qu.Catch()
  1067. code := f.GetString("code")
  1068. code = strings.TrimSpace(code)
  1069. success := false
  1070. text := ""
  1071. encode := ""
  1072. if code != "" {
  1073. //lua := *mgdb.FindOne("luaconfig", map[string]interface{}{"code": code})
  1074. lua, _ := u.MgoEB.FindOne("luaconfig", map[string]interface{}{"code": code})
  1075. if len(*lua) > 0 {
  1076. codeOld := qu.ObjToString((*lua)["code"])
  1077. codeNew := codeOld + u.Bu
  1078. //luaTmp := *mgdb.FindOne("luaconfig", map[string]interface{}{"code": codeNew})
  1079. luaTmp, _ := u.MgoEB.FindOne("luaconfig", map[string]interface{}{"code": codeNew})
  1080. if len(*luaTmp) > 0 {
  1081. f.ServeJson(map[string]interface{}{"success": false, "text": "已存在补漏爬虫"})
  1082. return
  1083. }
  1084. //u.MgoE.Update("luaconfig", map[string]interface{}{"code": codeOld}, map[string]interface{}{"$set": map[string]interface{}{"relatecode": codeNew}}, false, false)
  1085. u.MgoEB.Update("luaconfig", map[string]interface{}{"code": codeOld}, map[string]interface{}{"$set": map[string]interface{}{"relatecode": codeNew}}, false, false)
  1086. (*lua)["code"] = codeNew
  1087. (*lua)["relatecode"] = codeOld
  1088. if param_common := (*lua)["param_common"].([]interface{}); len(param_common) < 13 {
  1089. f.ServeJson(map[string]interface{}{"success": false, "text": "脚本异常,请联系管理员"})
  1090. return
  1091. } else {
  1092. param_common = param_common[:13]
  1093. param_common[0] = codeNew //code
  1094. param_common = append(param_common, true) //历史补漏
  1095. param_common = append(param_common, false) //强制下载
  1096. (*lua)["param_common"] = param_common
  1097. }
  1098. str_list := qu.ObjToString((*lua)["str_list"])
  1099. (*lua)["str_list"] = strings.ReplaceAll(str_list, codeOld, codeNew)
  1100. (*lua)["comeintime"] = time.Now().Unix()
  1101. (*lua)["modifytime"] = time.Now().Unix()
  1102. (*lua)["spidertype"] = "increment"
  1103. (*lua)["event"] = 7000
  1104. (*lua)["state"] = 6
  1105. id := u.MgoEB.Save("luaconfig", (*lua))
  1106. if id != "" {
  1107. success = true
  1108. encode = util.Se.Encode2Hex(codeNew)
  1109. } else {
  1110. text = "新爬虫保存失败"
  1111. }
  1112. } else {
  1113. text = "未查询到复制原"
  1114. }
  1115. } else {
  1116. text = "脚本不能为空"
  1117. }
  1118. f.ServeJson(map[string]interface{}{"success": success, "text": text, "encode": encode})
  1119. }
  1120. func (f *Front) SpiderSplitCopy() {
  1121. defer qu.Catch()
  1122. code := f.GetString("code")
  1123. code = strings.TrimSpace(code)
  1124. num, _ := f.GetInteger("num")
  1125. text := ""
  1126. lua, _ := u.MgoEB.FindOne("luaconfig", map[string]interface{}{"code": code})
  1127. if len(*lua) > 0 {
  1128. param_common := (*lua)["param_common"].([]interface{})
  1129. str_list := qu.ObjToString((*lua)["str_list"])
  1130. channelOld := qu.ObjToString((*lua)["channel"])
  1131. copyNum := qu.IntAll((*lua)["copynum"]) //当前已经拆分了多少个爬虫
  1132. delete(*lua, "relatecode") //删除关联爬虫字段
  1133. for i := 1; i <= num; i++ {
  1134. copyNum++
  1135. var codeSuffix string
  1136. if copyNum < 10 {
  1137. codeSuffix = fmt.Sprintf("0%s", fmt.Sprint(copyNum))
  1138. } else {
  1139. codeSuffix = fmt.Sprint(copyNum)
  1140. }
  1141. (*lua)["code"] = code + "_" + codeSuffix
  1142. (*lua)["channel"] = channelOld + "_" + codeSuffix
  1143. (*lua)["str_list"] = strings.ReplaceAll(str_list, code, code+"_"+codeSuffix)
  1144. (*lua)["comeintime"] = time.Now().Unix()
  1145. (*lua)["modifytime"] = time.Now().Unix()
  1146. (*lua)["state"] = 0
  1147. param_common[0] = code + "_" + codeSuffix
  1148. param_common[2] = channelOld + "_" + codeSuffix
  1149. (*lua)["param_common"] = param_common
  1150. u.MgoEB.Save("luaconfig", (*lua))
  1151. }
  1152. u.MgoEB.Update("luaconfig", map[string]interface{}{"code": code}, map[string]interface{}{"$set": map[string]interface{}{"copynum": copyNum}}, false, false)
  1153. } else {
  1154. text = "未查询到复制原"
  1155. }
  1156. f.ServeJson(map[string]interface{}{"text": text})
  1157. }
  1158. /*// 清理Redis
  1159. func (f *Front) DelRedis() {
  1160. hrefs := f.GetString("href")
  1161. hrefsarr := strings.Split(hrefs, ",")
  1162. auth := qu.IntAll(f.GetSession("auth"))
  1163. err := []string{}
  1164. if auth == u.Role_Admin { //权限控制
  1165. if len(hrefsarr) > 0 {
  1166. for k1, h := range hrefsarr {
  1167. href := Reg.FindString(h)
  1168. if href != "" {
  1169. href = "url_repeat_" + href + "*"
  1170. res := redis.GetKeysByPattern("title_repeat_judgement", href)
  1171. if res != nil {
  1172. for _, v := range res {
  1173. hf := string(v.([]uint8))
  1174. b := redis.Del("title_repeat_judgement", hf)
  1175. if !b {
  1176. err = append(err, "第"+strconv.Itoa(k1+1)+"个")
  1177. }
  1178. }
  1179. }
  1180. } else {
  1181. err = append(err, "第"+strconv.Itoa(k1+1)+"个")
  1182. }
  1183. }
  1184. }
  1185. } else {
  1186. err = append(err, "没有权限")
  1187. }
  1188. f.ServeJson(err)
  1189. }*/