main.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "os"
  6. "ouchn/config"
  7. "ouchn/student"
  8. "runtime"
  9. "strings"
  10. "sync"
  11. "time"
  12. "github.com/tebeka/selenium"
  13. )
  14. var (
  15. configFile = flag.String("config", "./common.yaml", "config file location")
  16. version = flag.Bool("version", false, "print the version")
  17. GitCommit = "library-import"
  18. Version = "library-import"
  19. )
  20. /*func handleCount(account []string) {
  21. fmt.Printf("账号:%v\n", account)
  22. service, wd, err := student.StartOneStudent(account[0], account[1], account[2])
  23. wd.Quit()
  24. service.Stop()
  25. if err != nil {
  26. fmt.Printf("%s %v \n", account[2], err)
  27. return
  28. }
  29. ioutil.WriteFile("record.txt", []byte(fmt.Sprintf("%s %s %s",account[0], account[1], account[2])), 0666)
  30. }*/
  31. func writeRecord(filename string, data []byte) {
  32. fi, err := os.OpenFile(filename, os.O_APPEND|os.O_CREATE, 0666)
  33. if err != nil {
  34. return
  35. }
  36. defer fi.Close()
  37. fi.Write(data)
  38. }
  39. func stest() {
  40. service, wd, err := student.StartOneStudent("2151101200269", "Ouchn@2021", "方")
  41. //service, wd, err := student.StartOneStudent("2251101200243", "Ouchn@2021", "肖")
  42. if wd != nil {
  43. wd.Quit()
  44. }
  45. if service != nil {
  46. service.Stop()
  47. }
  48. if err != nil {
  49. fmt.Printf("处理账号失败:%v \n", err)
  50. return
  51. }
  52. }
  53. func main() {
  54. os.Rename("error.txt", fmt.Sprintf("error_%s.txt", time.Now().Format("2006-01-02 15:04:05")))
  55. err := config.LoadConfig(*configFile)
  56. if err != nil {
  57. panic(err)
  58. }
  59. student.TokenDbOpen()
  60. student.TokenDbClear()
  61. defer func() {
  62. student.TokenDbClose()
  63. }()
  64. fmt.Printf("target:%d\n", config.Conf.Target)
  65. //stest()
  66. // 取账号
  67. for {
  68. accounts, _ := student.GetAcounts("account.txt")
  69. //accounts = accounts[0:1]
  70. //accounts[0][0] = "2051101250176"
  71. //accounts[0][1] = "Ouchn@2021"
  72. //accounts[0][2] = "将"
  73. fmt.Printf("accounts:%v\n", len(accounts))
  74. if len(accounts) == 0 {
  75. return
  76. }
  77. for _, v := range accounts {
  78. if len(v) != 3 {
  79. fmt.Printf("账号不完整\n")
  80. return
  81. }
  82. }
  83. count := 0
  84. var wg sync.WaitGroup
  85. serviceArray := []*selenium.Service{}
  86. wds := []selenium.WebDriver{}
  87. //accounts = [][]string{{"2151101451735", "Ouchn19951103", "罗建琴"}}
  88. for k, _ := range accounts {
  89. wg.Add(1)
  90. count++
  91. go func(account []string) {
  92. defer wg.Done()
  93. fmt.Printf("账号:%v\n", account)
  94. service, wd, err := student.StartOneStudent(account[0], account[1], account[2])
  95. if wd != nil {
  96. wd.Quit()
  97. wds = append(wds, wd)
  98. }
  99. if service != nil {
  100. //service.Stop()
  101. serviceArray = append(serviceArray, service)
  102. }
  103. if err != nil {
  104. fmt.Printf("处理账号%s失败:%v \n", account[2], err)
  105. if strings.Contains(err.Error(), "题") || strings.Contains(err.Error(), "panic") {
  106. writeRecord("error.txt", []byte(fmt.Sprintf("%s %s %s;%s\n", account[0], account[1], account[2], err.Error())))
  107. }
  108. return
  109. }
  110. writeRecord("record.txt", []byte(fmt.Sprintf("%s %s %s\n", account[0], account[1], account[2])))
  111. }(accounts[k])
  112. //fmt.Printf("count:%d,%d\n", count, config.Conf.Channel)
  113. //go handleCount(accounts[k])
  114. if count == config.Conf.Channel {
  115. wg.Wait()
  116. /*for _, v := range wds {
  117. v.Quit()
  118. }*/
  119. for _, v := range serviceArray {
  120. v.Stop()
  121. }
  122. serviceArray = []*selenium.Service{}
  123. wds = []selenium.WebDriver{}
  124. count = 0
  125. }
  126. }
  127. if count != 0 {
  128. wg.Wait()
  129. /*for _, v := range wds {
  130. v.Quit()
  131. }*/
  132. for _, v := range serviceArray {
  133. v.Stop()
  134. }
  135. serviceArray = []*selenium.Service{}
  136. wds = []selenium.WebDriver{}
  137. }
  138. fmt.Printf("等待执行下一批账号\n")
  139. runtime.GC()
  140. time.Sleep(5 * time.Second)
  141. }
  142. }