package main import ( "flag" "fmt" "os" "ouchn/config" "ouchn/student" "sync" "time" "github.com/tebeka/selenium" ) var ( configFile = flag.String("config", "./common.yaml", "config file location") version = flag.Bool("version", false, "print the version") GitCommit = "library-import" Version = "library-import" ) /*func handleCount(account []string) { fmt.Printf("账号:%v\n", account) service, wd, err := student.StartOneStudent(account[0], account[1], account[2]) wd.Quit() service.Stop() if err != nil { fmt.Printf("%s %v \n", account[2], err) return } ioutil.WriteFile("record.txt", []byte(fmt.Sprintf("%s %s %s",account[0], account[1], account[2])), 0666) }*/ func writeRecord(filename string, data []byte) { fi, err := os.OpenFile(filename, os.O_APPEND|os.O_CREATE, 0666) if err != nil { return } defer fi.Close() fi.Write(data) } func stest() { service, wd, err := student.StartOneStudent("2251101200243", "Ouchn@2021", "肖") if wd != nil { wd.Quit() } if service != nil { service.Stop() } if err != nil { fmt.Printf("处理账号失败:%v \n", err) return } } func main() { err := config.LoadConfig(*configFile) if err != nil { panic(err) } // 取账号 for { accounts, _ := student.GetAcounts("account.txt") fmt.Printf("accounts:%v\n", len(accounts)) if len(accounts) == 0 { return } for _, v := range accounts { if len(v) != 3 { fmt.Printf("账号不完整\n") return } } count := 0 var wg sync.WaitGroup serviceArray := []*selenium.Service{} wds := []selenium.WebDriver{} //accounts = [][]string{{"2151101451735", "Ouchn19951103", "罗建琴"}} for k, _ := range accounts { wg.Add(1) count++ go func(account []string) { defer wg.Done() fmt.Printf("账号:%v\n", account) service, wd, err := student.StartOneStudent(account[0], account[1], account[2]) if wd != nil { wd.Quit() wds = append(wds, wd) } if service != nil { //service.Stop() serviceArray = append(serviceArray, service) } if err != nil { fmt.Printf("处理账号%s失败:%v \n", account[2], err) return } writeRecord("record.txt", []byte(fmt.Sprintf("%s %s %s\n", account[0], account[1], account[2]))) }(accounts[k]) //fmt.Printf("count:%d,%d\n", count, config.Conf.Channel) //go handleCount(accounts[k]) if count == config.Conf.Channel { wg.Wait() /*for _, v := range wds { v.Quit() }*/ for _, v := range serviceArray { v.Stop() } serviceArray = []*selenium.Service{} wds = []selenium.WebDriver{} count = 0 } } if count != 0 { wg.Wait() /*for _, v := range wds { v.Quit() }*/ for _, v := range serviceArray { v.Stop() } serviceArray = []*selenium.Service{} wds = []selenium.WebDriver{} } fmt.Printf("over\n") time.Sleep(60 * time.Second) } }