main.go 2.9 KB

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