front.go 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221
  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. platform := (*user)["s_platform"]
  684. name := qu.ObjToString((*user)["s_name"])
  685. query := map[string]interface{}{
  686. "code": map[string]interface{}{
  687. "$in": codesarr,
  688. },
  689. }
  690. luas, _ := u.MgoEB.Find("luaconfig", query, nil, ClaimQueryFields, false, -1, -1)
  691. update := [][]map[string]interface{}{}
  692. save := []map[string]interface{}{}
  693. for _, l := range *luas {
  694. set := map[string]interface{}{}
  695. now := time.Now().Unix()
  696. claimtype := qu.IntAll(l["claimtype"])
  697. priority := qu.IntAll(l["priority"])
  698. spiderimportant, _ := l["spiderimportant"].(bool)
  699. if claimtype <= CLAIMTYPECLAIMED { //非历史爬虫分配
  700. recovertime := CreateRecovertime(spiderimportant, priority)
  701. set = map[string]interface{}{
  702. "claimtype": CLAIMTYPECLAIMED,
  703. "claimtime": now,
  704. "recovertime": recovertime,
  705. "createuseremail": (*user)["s_email"],
  706. "createuser": name,
  707. "createuserid": userid,
  708. "modifyuser": name,
  709. "modifyuserid": userid,
  710. "platform": platform,
  711. }
  712. if claimtype == CLAIMTYPECLAIMED { //已认领的爬虫增加回收日志
  713. //回收日志
  714. recovelog := map[string]interface{}{
  715. "site": l["site"],
  716. "code": l["code"],
  717. "channel": l["channel"],
  718. "modifyuser": l["modifyuser"],
  719. "priority": priority,
  720. "stype": "回收",
  721. "comeintime": now,
  722. "claimtime": l["claimtime"],
  723. "recovertime": l["recovertime"],
  724. "returntime": int64(0),
  725. "important": spiderimportant,
  726. "returnreason": "",
  727. "claimrecovertype": 0,
  728. }
  729. save = append(save, recovelog)
  730. }
  731. //被重新分配的爬虫视为重新认领(注意:lua爬虫分配到除chrome外的平台,变为历史爬虫)
  732. if platform == "golua平台" { //爬虫分配到非lua平台,不视为认领
  733. //认领日志
  734. claimlog := map[string]interface{}{
  735. "site": l["site"],
  736. "code": l["code"],
  737. "channel": l["channel"],
  738. "modifyuser": name,
  739. "priority": priority,
  740. "stype": "认领",
  741. "comeintime": now,
  742. "claimtime": now,
  743. "recovertime": recovertime,
  744. "returntime": int64(0),
  745. "important": spiderimportant,
  746. "returnreason": "",
  747. "claimrecovertype": 0,
  748. }
  749. save = append(save, claimlog)
  750. } else if platform != "golua平台" && platform != "chrome" { //分配到其他平台改为历史爬虫
  751. set["claimtype"] = CLAIMTYPEHISTORY
  752. set["claimtime"] = 0
  753. set["recovertime"] = 0
  754. }
  755. } else { //历史爬虫
  756. set = map[string]interface{}{
  757. "createuserid": userid,
  758. "createuser": name,
  759. "createuseremail": (*user)["s_email"],
  760. "modifyuser": name,
  761. "modifyuserid": userid,
  762. "platform": (*user)["s_platform"],
  763. }
  764. }
  765. //更新爬虫
  766. up := []map[string]interface{}{
  767. {"code": l["code"]},
  768. {"$set": set},
  769. }
  770. update = append(update, up)
  771. }
  772. qu.Debug(query)
  773. //更新爬虫信息
  774. b := u.MgoEB.UpdateBulk("luaconfig", update...)
  775. //保存认领日志
  776. if len(save) > 0 {
  777. u.MgoEB.SaveBulk("lua_logs_claim", save...)
  778. }
  779. if b {
  780. go editModify(codesarr, userid, name) //修改爬虫对应任务的维护人
  781. f.Write("y")
  782. } else {
  783. f.Write("n")
  784. }
  785. } else {
  786. f.Write("null")
  787. }
  788. }
  789. // 审核日志导出
  790. func (f *Front) AuditExport() {
  791. auth := qu.IntAll(f.GetSession("auth"))
  792. if auth > u.Role_Admin {
  793. exportnames := f.GetString("exportnames")
  794. exporttype := f.GetString("exporttype")
  795. starttime := f.GetString("starttime")
  796. endtime := f.GetString("endtime")
  797. qu.Debug(exportnames, exporttype, starttime, endtime)
  798. file, err := xlsx.OpenFile("爬虫审核记录.xlsx")
  799. if err != nil {
  800. f.Write("文件打开失败!")
  801. return
  802. }
  803. sheet := file.Sheets[0]
  804. exportLogs(sheet, exportnames, exporttype, starttime, endtime) //导出数据
  805. fname := fmt.Sprintf("爬虫审核打回记录%d.xlsx", time.Now().Unix())
  806. err = file.Save(fname)
  807. if err != nil {
  808. qu.Debug("Save Excel" + fname + "Error")
  809. f.ServeJson("导出失败")
  810. return
  811. }
  812. f.ResponseWriter.Header().Add("Content-Disposition", "attachment;filename=爬虫审核打回记录.xlsx")
  813. f.ServeFile(fname)
  814. go func(path string) { //删除
  815. time.Sleep(time.Second * 30)
  816. os.Remove(path)
  817. }(fname)
  818. } else {
  819. f.Write("您没有权限")
  820. }
  821. }
  822. func exportLogs(sheet *xlsx.Sheet, exportnames, exporttype, starttime, endtime string) {
  823. st, _ := time.ParseInLocation(qu.Date_Short_Layout, starttime, time.Local)
  824. et, _ := time.ParseInLocation(qu.Date_Short_Layout, endtime, time.Local)
  825. query := map[string]interface{}{
  826. "comeintime": map[string]interface{}{
  827. "$gte": st.Unix(),
  828. "$lt": et.Unix(),
  829. },
  830. }
  831. if exporttype == "repulse" { //打回
  832. query["types"] = "打回"
  833. } else if exporttype == "audit" { //审核
  834. query["types"] = "审核"
  835. }
  836. users := map[string]bool{}
  837. for _, user := range strings.Split(exportnames, ",") {
  838. users[user] = true
  839. }
  840. qu.Debug("audit log query:", query)
  841. sess := u.MgoEB.GetMgoConn()
  842. defer u.MgoEB.DestoryMongoConn(sess)
  843. lock := &sync.Mutex{}
  844. wg := &sync.WaitGroup{}
  845. ch := make(chan bool, 10)
  846. n := 0
  847. it := sess.DB(u.MgoEB.DbName).C("lua_logs_auditor").Find(&query).Iter()
  848. count, _ := sess.DB(u.MgoEB.DbName).C("lua_logs_auditor").Find(&query).Count()
  849. qu.Debug("count:", count)
  850. for tmp := map[string]interface{}{}; it.Next(tmp); n++ {
  851. ch <- true
  852. wg.Add(1)
  853. go func(tmp map[string]interface{}) {
  854. defer func() {
  855. <-ch
  856. wg.Done()
  857. }()
  858. id := mongodb.BsonIdToSId(tmp["_id"])
  859. auditor := qu.ObjToString(tmp["auditor"])
  860. code := qu.ObjToString(tmp["code"])
  861. comeintime := qu.Int64All(tmp["comeintime"])
  862. comeintime_z := qu.FormatDateByInt64(&comeintime, qu.Date_Full_Layout)
  863. event := qu.IntAll(tmp["event"])
  864. modifytime := qu.Int64All(tmp["modifytime"])
  865. modifytime_z := qu.FormatDateByInt64(&modifytime, qu.Date_Full_Layout)
  866. reason := qu.ObjToString(tmp["reason"])
  867. types := qu.ObjToString(tmp["types"])
  868. spideruser := qu.ObjToString(tmp["spideruser"])
  869. if !users[spideruser] {
  870. return
  871. }
  872. lock.Lock()
  873. row := sheet.AddRow()
  874. lock.Unlock()
  875. row.AddCell().SetValue(id)
  876. row.AddCell().SetValue(auditor)
  877. row.AddCell().SetValue(code)
  878. row.AddCell().SetValue(comeintime)
  879. row.AddCell().SetValue(comeintime_z)
  880. row.AddCell().SetValue(event)
  881. row.AddCell().SetValue(modifytime)
  882. row.AddCell().SetValue(modifytime_z)
  883. row.AddCell().SetValue(reason)
  884. row.AddCell().SetValue(spideruser)
  885. row.AddCell().SetValue(types)
  886. }(tmp)
  887. tmp = make(map[string]interface{})
  888. }
  889. }
  890. // 修改维护人
  891. func editModify(codesarr []string, userid, name string) {
  892. qu.Debug("修改爬虫对应任务的维护人:", name, codesarr)
  893. //修改modifyid和modify
  894. query := map[string]interface{}{
  895. "s_code": map[string]interface{}{
  896. "$in": codesarr,
  897. },
  898. "i_state": map[string]interface{}{
  899. "$nin": []int{4, 6},
  900. },
  901. }
  902. set := map[string]interface{}{
  903. "$set": map[string]interface{}{
  904. "s_modify": name,
  905. "s_modifyid": userid,
  906. },
  907. }
  908. b := u.MgoEB.Update("task", query, set, false, true)
  909. qu.Debug("任务重新分配成功", b)
  910. }
  911. func (f *Front) UpdatePendState() {
  912. success := false
  913. pendstate, _ := f.GetInteger("pendstate")
  914. code := f.GetString("code")
  915. stype := f.GetString("stype")
  916. qu.Debug(code, stype, pendstate)
  917. //success = u.MgoE.Update("luaconfig", map[string]interface{}{"code": code}, map[string]interface{}{"$set": map[string]interface{}{"pendstate": pendstate}}, false, false)
  918. 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)
  919. if success {
  920. if stype == "code" { //爬虫挂起时,关闭所有任务
  921. 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)
  922. } else if stype == "task" { //任务挂起任务时任务关闭
  923. id := f.GetString("id")
  924. u.MgoEB.UpdateById("task", id, map[string]interface{}{"$set": map[string]interface{}{"i_pendstate": pendstate, "i_state": 6, "l_pendtime": time.Now().Unix()}})
  925. }
  926. }
  927. f.ServeJson(map[string]interface{}{"success": success})
  928. }
  929. func (f *Front) UpdateESP() {
  930. val := f.GetString("val")
  931. w := f.GetString("w")
  932. id := f.GetString("id")
  933. code := f.GetString("c")
  934. query := map[string]interface{}{
  935. "_id": mongodb.StringTOBsonId(id),
  936. }
  937. set := map[string]interface{}{}
  938. update := map[string]interface{}{
  939. "$set": set,
  940. }
  941. //one, _ := u.MgoE.FindById("luaconfig", id, nil)
  942. one, _ := u.MgoEB.FindById("luaconfig", id, nil)
  943. if len(*one) == 0 {
  944. f.Write("n")
  945. return
  946. }
  947. if w == "infoformat" {
  948. infoformat, _ := f.GetInteger("val")
  949. set["infoformat"] = infoformat
  950. } else if w == "urgency" { //修改紧急度
  951. urgency, _ := f.GetInteger("val")
  952. set["urgency"] = urgency
  953. } else if w == "state" { //无效爬虫改为待完成
  954. tmpEvent := qu.IntAll((*one)["event"])
  955. if one != nil && len(*one) > 0 {
  956. if (*one)["incrementevent"] == nil && tmpEvent != 7000 { //除7000节点外没有incrementevent的要重新设置
  957. set["incrementevent"] = tmpEvent
  958. }
  959. }
  960. set["state"] = 0
  961. set["event"] = 7000
  962. set["spidertype"] = "history"
  963. go ModifyLogs_UpdateCodeState(code)
  964. } else if w == "platform" {
  965. //常规字段更新
  966. set["platform"] = val
  967. set["comeintime"] = time.Now().Unix()
  968. if val != "golua平台" && val != "chrome" {
  969. b := u.MgoS.Update("spider_heart", map[string]interface{}{"code": code}, map[string]interface{}{"$set": map[string]interface{}{"del": true}}, false, true)
  970. qu.Debug("Del Heart:", b)
  971. b, err := spider.UpdateSpiderByCodeState(code, "6", qu.IntAll((*one)["event"])) //下架
  972. if b && err == nil {
  973. //历史节点下架为了避免线上运行爬虫待完成时改为7000采集历史,但是又转到其他平台,导致原线上运行节点爬虫并未下线,心跳异常
  974. if incrementevent := (*one)["incrementevent"]; incrementevent != nil {
  975. b, err = spider.UpdateSpiderByCodeState(code, "6", qu.IntAll(incrementevent))
  976. }
  977. //关闭任务
  978. 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)
  979. if b && err == nil {
  980. set["state"] = 0 //更新状态
  981. set["luauser"] = map[string]interface{}{
  982. "createuser": (*one)["createuser"],
  983. "createuserid": (*one)["createuserid"],
  984. "createuseremail": (*one)["createuseremail"],
  985. "modifyuser": (*one)["modifyuser"],
  986. "modifyuserid": (*one)["modifyuserid"],
  987. }
  988. } else {
  989. qu.Debug("历史节点下架失败")
  990. f.Write("n")
  991. return
  992. }
  993. } else {
  994. qu.Debug("增量节点下架失败")
  995. f.Write("n")
  996. return
  997. }
  998. //lua转python,爬虫认领状态更新
  999. UpdateLuaClaimtype(code)
  1000. //换平台爬虫回收,保存日志
  1001. recovelog := map[string]interface{}{
  1002. "site": (*one)["site"],
  1003. "code": (*one)["code"],
  1004. "channel": (*one)["channel"],
  1005. "modifyuser": (*one)["modifyuser"],
  1006. "priority": (*one)["priority"],
  1007. "stype": "回收",
  1008. "comeintime": time.Now().Unix(),
  1009. "claimtime": (*one)["claimtime"],
  1010. "recovertime": (*one)["recovertime"],
  1011. "returntime": int64(0),
  1012. "important": (*one)["spiderimportant"],
  1013. "returnreason": "转平台",
  1014. "claimrecovertype": 0,
  1015. }
  1016. u.MgoEB.Save("lua_logs_claim", recovelog)
  1017. }
  1018. } else if w == "priority" { //调整优先级
  1019. priority := qu.IntAll(val)
  1020. if priority < 0 {
  1021. priority = 0
  1022. } else if priority > 1000 {
  1023. priority = 1000
  1024. }
  1025. set["priority"] = priority
  1026. } else { //修改节点
  1027. event, _ := strconv.Atoi(val)
  1028. set["event"] = event
  1029. set["incrementevent"] = event
  1030. set["state"] = 6
  1031. b, err := UpStateAndUpSpider(code, "", "", "", Sp_state_6) //线上爬虫下架
  1032. qu.Debug("爬虫下架成功:", b)
  1033. if !b || err != nil {
  1034. f.Write("n")
  1035. return
  1036. }
  1037. }
  1038. if u.MgoEB.Update("luaconfig", query, update, false, false) {
  1039. log.Println("Id:", id, " Update", w, val, "Success")
  1040. f.Write("y")
  1041. return
  1042. } else {
  1043. log.Println("Id:", id, " Update", w, val, "Failed")
  1044. f.Write("n")
  1045. return
  1046. }
  1047. f.Write("n")
  1048. }
  1049. func (f *Front) GetCity() {
  1050. area := f.GetString("area")
  1051. cityArr := []string{}
  1052. cityArr = u.Province[area]
  1053. f.ServeJson(cityArr)
  1054. }
  1055. func (f *Front) SpiderCopy() {
  1056. defer qu.Catch()
  1057. code := f.GetString("code")
  1058. code = strings.TrimSpace(code)
  1059. success := false
  1060. text := ""
  1061. encode := ""
  1062. if code != "" {
  1063. //lua := *mgdb.FindOne("luaconfig", map[string]interface{}{"code": code})
  1064. lua, _ := u.MgoEB.FindOne("luaconfig", map[string]interface{}{"code": code})
  1065. if len(*lua) > 0 {
  1066. codeOld := qu.ObjToString((*lua)["code"])
  1067. codeNew := codeOld + u.Bu
  1068. //luaTmp := *mgdb.FindOne("luaconfig", map[string]interface{}{"code": codeNew})
  1069. luaTmp, _ := u.MgoEB.FindOne("luaconfig", map[string]interface{}{"code": codeNew})
  1070. if len(*luaTmp) > 0 {
  1071. f.ServeJson(map[string]interface{}{"success": false, "text": "已存在补漏爬虫"})
  1072. return
  1073. }
  1074. //u.MgoE.Update("luaconfig", map[string]interface{}{"code": codeOld}, map[string]interface{}{"$set": map[string]interface{}{"relatecode": codeNew}}, false, false)
  1075. u.MgoEB.Update("luaconfig", map[string]interface{}{"code": codeOld}, map[string]interface{}{"$set": map[string]interface{}{"relatecode": codeNew}}, false, false)
  1076. (*lua)["code"] = codeNew
  1077. (*lua)["relatecode"] = codeOld
  1078. if param_common := (*lua)["param_common"].([]interface{}); len(param_common) < 13 {
  1079. f.ServeJson(map[string]interface{}{"success": false, "text": "脚本异常,请联系管理员"})
  1080. return
  1081. } else {
  1082. param_common = param_common[:13]
  1083. param_common[0] = codeNew //code
  1084. param_common = append(param_common, true) //历史补漏
  1085. param_common = append(param_common, false) //强制下载
  1086. (*lua)["param_common"] = param_common
  1087. }
  1088. str_list := qu.ObjToString((*lua)["str_list"])
  1089. (*lua)["str_list"] = strings.ReplaceAll(str_list, codeOld, codeNew)
  1090. (*lua)["comeintime"] = time.Now().Unix()
  1091. (*lua)["modifytime"] = time.Now().Unix()
  1092. (*lua)["spidertype"] = "increment"
  1093. (*lua)["event"] = 7000
  1094. (*lua)["state"] = 6
  1095. id := u.MgoEB.Save("luaconfig", (*lua))
  1096. if id != "" {
  1097. success = true
  1098. encode = util.Se.Encode2Hex(codeNew)
  1099. } else {
  1100. text = "新爬虫保存失败"
  1101. }
  1102. } else {
  1103. text = "未查询到复制原"
  1104. }
  1105. } else {
  1106. text = "脚本不能为空"
  1107. }
  1108. f.ServeJson(map[string]interface{}{"success": success, "text": text, "encode": encode})
  1109. }
  1110. func (f *Front) SpiderSplitCopy() {
  1111. defer qu.Catch()
  1112. code := f.GetString("code")
  1113. code = strings.TrimSpace(code)
  1114. num, _ := f.GetInteger("num")
  1115. text := ""
  1116. lua, _ := u.MgoEB.FindOne("luaconfig", map[string]interface{}{"code": code})
  1117. if len(*lua) > 0 {
  1118. param_common := (*lua)["param_common"].([]interface{})
  1119. str_list := qu.ObjToString((*lua)["str_list"])
  1120. channelOld := qu.ObjToString((*lua)["channel"])
  1121. copyNum := qu.IntAll((*lua)["copynum"]) //当前已经拆分了多少个爬虫
  1122. delete(*lua, "relatecode") //删除关联爬虫字段
  1123. for i := 1; i <= num; i++ {
  1124. copyNum++
  1125. var codeSuffix string
  1126. if copyNum < 10 {
  1127. codeSuffix = fmt.Sprintf("0%s", fmt.Sprint(copyNum))
  1128. } else {
  1129. codeSuffix = fmt.Sprint(copyNum)
  1130. }
  1131. (*lua)["code"] = code + "_" + codeSuffix
  1132. (*lua)["channel"] = channelOld + "_" + codeSuffix
  1133. (*lua)["str_list"] = strings.ReplaceAll(str_list, code, code+"_"+codeSuffix)
  1134. (*lua)["comeintime"] = time.Now().Unix()
  1135. (*lua)["modifytime"] = time.Now().Unix()
  1136. (*lua)["state"] = 0
  1137. param_common[0] = code + "_" + codeSuffix
  1138. param_common[2] = channelOld + "_" + codeSuffix
  1139. (*lua)["param_common"] = param_common
  1140. u.MgoEB.Save("luaconfig", (*lua))
  1141. }
  1142. u.MgoEB.Update("luaconfig", map[string]interface{}{"code": code}, map[string]interface{}{"$set": map[string]interface{}{"copynum": copyNum}}, false, false)
  1143. } else {
  1144. text = "未查询到复制原"
  1145. }
  1146. f.ServeJson(map[string]interface{}{"text": text})
  1147. }
  1148. /*// 清理Redis
  1149. func (f *Front) DelRedis() {
  1150. hrefs := f.GetString("href")
  1151. hrefsarr := strings.Split(hrefs, ",")
  1152. auth := qu.IntAll(f.GetSession("auth"))
  1153. err := []string{}
  1154. if auth == u.Role_Admin { //权限控制
  1155. if len(hrefsarr) > 0 {
  1156. for k1, h := range hrefsarr {
  1157. href := Reg.FindString(h)
  1158. if href != "" {
  1159. href = "url_repeat_" + href + "*"
  1160. res := redis.GetKeysByPattern("title_repeat_judgement", href)
  1161. if res != nil {
  1162. for _, v := range res {
  1163. hf := string(v.([]uint8))
  1164. b := redis.Del("title_repeat_judgement", hf)
  1165. if !b {
  1166. err = append(err, "第"+strconv.Itoa(k1+1)+"个")
  1167. }
  1168. }
  1169. }
  1170. } else {
  1171. err = append(err, "第"+strconv.Itoa(k1+1)+"个")
  1172. }
  1173. }
  1174. }
  1175. } else {
  1176. err = append(err, "没有权限")
  1177. }
  1178. f.ServeJson(err)
  1179. }*/