main.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 onlyVisit() {
  38. if config.Conf.VisitCount == 0 {
  39. fmt.Printf("请设置总点击次数\n")
  40. return
  41. }
  42. fmt.Printf("增加浏览量\n")
  43. // 取账号
  44. for {
  45. accounts, _ := student.GetAcounts("account.txt")
  46. fmt.Printf("accounts:%v\n", len(accounts))
  47. if len(accounts) == 0 {
  48. return
  49. }
  50. for _, v := range accounts {
  51. if len(v) != 3 {
  52. fmt.Printf("账号不完整\n")
  53. return
  54. }
  55. }
  56. count := 0
  57. var wg sync.WaitGroup
  58. serviceArray := []*selenium.Service{}
  59. wds := []selenium.WebDriver{}
  60. //accounts = [][]string{{"2151101451735", "Ouchn19951103", "罗建琴"}}
  61. for k, _ := range accounts {
  62. wg.Add(1)
  63. count++
  64. go func(account []string) {
  65. defer wg.Done()
  66. fmt.Printf("账号:%v\n", account)
  67. service, wd, err := student.StudentVisit(account[0], account[1], account[2])
  68. if wd != nil {
  69. wd.Quit()
  70. wds = append(wds, wd)
  71. }
  72. if service != nil {
  73. //service.Stop()
  74. serviceArray = append(serviceArray, service)
  75. }
  76. if err != nil {
  77. fmt.Printf("处理账号%s失败:%v \n", account[2], err)
  78. return
  79. }
  80. writeRecord("record.txt", []byte(fmt.Sprintf("%s %s %s\n", account[0], account[1], account[2])))
  81. }(accounts[k])
  82. //fmt.Printf("count:%d,%d\n", count, config.Conf.Channel)
  83. //go handleCount(accounts[k])
  84. if count == config.Conf.Channel {
  85. wg.Wait()
  86. /*for _, v := range wds {
  87. v.Quit()
  88. }*/
  89. for _, v := range serviceArray {
  90. v.Stop()
  91. }
  92. serviceArray = []*selenium.Service{}
  93. wds = []selenium.WebDriver{}
  94. count = 0
  95. }
  96. }
  97. if count != 0 {
  98. wg.Wait()
  99. /*for _, v := range wds {
  100. v.Quit()
  101. }*/
  102. for _, v := range serviceArray {
  103. v.Stop()
  104. }
  105. serviceArray = []*selenium.Service{}
  106. wds = []selenium.WebDriver{}
  107. }
  108. fmt.Printf("over\n")
  109. time.Sleep(60 * time.Second)
  110. }
  111. }
  112. func main() {
  113. err := config.LoadConfig(*configFile)
  114. if err != nil {
  115. panic(err)
  116. }
  117. if config.Conf.OnlyVisit {
  118. onlyVisit()
  119. return
  120. }
  121. // 取账号
  122. for {
  123. accounts, _ := student.GetAcounts("account.txt")
  124. fmt.Printf("accounts:%v\n", len(accounts))
  125. if len(accounts) == 0 {
  126. return
  127. }
  128. for _, v := range accounts {
  129. if len(v) != 3 {
  130. fmt.Printf("账号不完整\n")
  131. return
  132. }
  133. }
  134. count := 0
  135. var wg sync.WaitGroup
  136. serviceArray := []*selenium.Service{}
  137. wds := []selenium.WebDriver{}
  138. //accounts = [][]string{{"2151101451735", "Ouchn19951103", "罗建琴"}}
  139. for k, _ := range accounts {
  140. wg.Add(1)
  141. count++
  142. go func(account []string) {
  143. defer wg.Done()
  144. fmt.Printf("账号:%v\n", account)
  145. service, wd, err := student.StartOneStudent(account[0], account[1], account[2])
  146. if wd != nil {
  147. wd.Quit()
  148. wds = append(wds, wd)
  149. }
  150. if service != nil {
  151. //service.Stop()
  152. serviceArray = append(serviceArray, service)
  153. }
  154. if err != nil {
  155. fmt.Printf("处理账号%s失败:%v \n", account[2], err)
  156. return
  157. }
  158. writeRecord("record.txt", []byte(fmt.Sprintf("%s %s %s\n", account[0], account[1], account[2])))
  159. }(accounts[k])
  160. //fmt.Printf("count:%d,%d\n", count, config.Conf.Channel)
  161. //go handleCount(accounts[k])
  162. if count == config.Conf.Channel {
  163. wg.Wait()
  164. /*for _, v := range wds {
  165. v.Quit()
  166. }*/
  167. for _, v := range serviceArray {
  168. v.Stop()
  169. }
  170. serviceArray = []*selenium.Service{}
  171. wds = []selenium.WebDriver{}
  172. count = 0
  173. }
  174. }
  175. if count != 0 {
  176. wg.Wait()
  177. /*for _, v := range wds {
  178. v.Quit()
  179. }*/
  180. for _, v := range serviceArray {
  181. v.Stop()
  182. }
  183. serviceArray = []*selenium.Service{}
  184. wds = []selenium.WebDriver{}
  185. }
  186. fmt.Printf("over\n")
  187. time.Sleep(60 * time.Second)
  188. }
  189. }