123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- package main
- import (
- "flag"
- "fmt"
- "os"
- "ouchn/config"
- "ouchn/student"
- "runtime"
- "strings"
- "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("2151101200269", "Ouchn@2021", "方")
- //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() {
- os.Rename("error.txt", fmt.Sprintf("error_%s.txt", time.Now().Format("2006-01-02 15:04:05")))
- err := config.LoadConfig(*configFile)
- if err != nil {
- panic(err)
- }
- student.TokenDbOpen()
- student.TokenDbClear()
- defer func() {
- student.TokenDbClose()
- }()
- fmt.Printf("target:%d\n", config.Conf.Target)
- //stest()
- // 取账号
- for {
- accounts, _ := student.GetAcounts("account.txt")
- //accounts = accounts[0:1]
- //accounts[0][0] = "2051101250176"
- //accounts[0][1] = "Ouchn@2021"
- //accounts[0][2] = "将"
- 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)
- if strings.Contains(err.Error(), "题") || strings.Contains(err.Error(), "panic") {
- writeRecord("error.txt", []byte(fmt.Sprintf("%s %s %s;%s\n", account[0], account[1], account[2], err.Error())))
- }
- 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("等待执行下一批账号\n")
- runtime.GC()
- time.Sleep(5 * time.Second)
- }
- }
|