main.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package main
  2. import (
  3. "log"
  4. "strings"
  5. elastic "es"
  6. "mongodb"
  7. common "qfw/util"
  8. "github.com/tealeg/xlsx"
  9. )
  10. var (
  11. Bidding *mongodb.MongodbSim
  12. Es elastic.Es
  13. Es2 elastic.Es
  14. cfg = new(Config)
  15. sMap = map[string]string{}
  16. )
  17. func init() {
  18. common.ReadConfig(&cfg)
  19. Bidding = mongodb.NewMgoWithUser(cfg.Bidding.Address, cfg.Bidding.DbName, cfg.Bidding.UserName, cfg.Bidding.Password, cfg.Bidding.DbSize)
  20. Es = elastic.NewEs(cfg.Es.Version, cfg.Es.Address, cfg.Es.DbSize, cfg.Es.UserName, cfg.Es.Password)
  21. Es2 = elastic.NewEs(cfg.Es2.Version, cfg.Es2.Address, cfg.Es2.DbSize, cfg.Es2.UserName, cfg.Es2.Password)
  22. }
  23. func main() {
  24. // go FindBiddingBack()
  25. // sMaps()
  26. FindBidding()
  27. }
  28. func FindBidding() {
  29. session := Bidding.GetMgoConn()
  30. query := map[string]interface{}{}
  31. defer Bidding.DestoryMongoConn(session)
  32. count := 0
  33. iter := session.DB(cfg.Bidding.DbName).C("bidding").Find(&query).Sort("-_id").Iter()
  34. thisData := map[string]interface{}{}
  35. for {
  36. if !iter.Next(&thisData) {
  37. break
  38. }
  39. count++
  40. id := mongodb.BsonIdToSId(thisData["_id"])
  41. title := common.ObjToString(thisData["title"])
  42. projectName := common.ObjToString(thisData["projectname"])
  43. spidercode := common.ObjToString(thisData["spidercode"])
  44. if strings.Contains(title, "..") {
  45. if strings.Contains(projectName, "..") {
  46. title = strings.ReplaceAll(title, ".", "")
  47. } else {
  48. title = projectName
  49. }
  50. sMap[spidercode] = "1"
  51. ok := Bidding.UpdateById("bidding", id, map[string]interface{}{"$set": map[string]interface{}{"title": title}})
  52. if ok {
  53. updateData(id, title, spidercode)
  54. } else {
  55. log.Println("bidding修改失败!!", id)
  56. }
  57. }
  58. thisData = map[string]interface{}{}
  59. }
  60. }
  61. func FindBiddingBack() {
  62. session := Bidding.GetMgoConn()
  63. query := map[string]interface{}{}
  64. defer Bidding.DestoryMongoConn(session)
  65. count := 0
  66. iter := session.DB(cfg.Bidding.DbName).C("bidding_back").Find(&query).Iter()
  67. thisData := map[string]interface{}{}
  68. for {
  69. if !iter.Next(&thisData) {
  70. break
  71. }
  72. count++
  73. id := mongodb.BsonIdToSId(thisData["_id"])
  74. title := common.ObjToString(thisData["title"])
  75. projectName := common.ObjToString(thisData["projectname"])
  76. spidercode := common.ObjToString(thisData["spidercode"])
  77. if strings.Contains(title, "..") {
  78. if strings.Contains(projectName, "..") {
  79. thisData["title"] = strings.ReplaceAll(title, ".", "")
  80. } else {
  81. thisData["title"] = projectName
  82. }
  83. sMap[spidercode] = "1"
  84. ok := Bidding.UpdateById("bidding", id, map[string]interface{}{"$set": map[string]interface{}{"title": title}})
  85. if ok {
  86. updateData(id, title, spidercode)
  87. } else {
  88. log.Println("bidding修改失败!!", id)
  89. }
  90. }
  91. thisData = map[string]interface{}{}
  92. }
  93. }
  94. func updateData(id, title, spidercode string) {
  95. data := Es.GetById("bidding", "bidding", id)
  96. if data != nil && len(*data) > 0 {
  97. dataMap := (*data)[0]
  98. dataMap["title"] = title
  99. ok := Es.UpdateNewDoc("bidding", "bidding", dataMap)
  100. if ok {
  101. log.Println("es老集群修改成功", id, spidercode)
  102. } else {
  103. log.Println("es老集群修改失败 !!!", id, title)
  104. }
  105. oks := Es2.UpdateNewDoc("bidding", "bidding", dataMap)
  106. if oks {
  107. log.Println("es新集群修改成功", id, spidercode)
  108. } else {
  109. log.Println("es新集群修改失败 !!!", id, title)
  110. }
  111. }
  112. }
  113. func sMaps() {
  114. xf := xlsx.NewFile()
  115. sh, _ := xf.AddSheet("详细数据")
  116. for k, _ := range sMap {
  117. row := sh.AddRow()
  118. row.AddCell().SetString(k)
  119. }
  120. err := xf.Save("./爬虫代码统计.xlsx")
  121. if err != nil {
  122. log.Println("xls error")
  123. }
  124. }