goticker实现计划任务,gjson来解析json数据,干货满满[建议收藏]

golang代码如下:

package main

import (
   "fmt"
   "github.com/aWildProgrammer/goticker"
   "github.com/tidwall/gjson"
   "io/ioutil"
   "net/http"
   "os"
   "os/exec"
   "time"
)

func isAppNotFinish() bool {
   url := "https://你的域名/api/uploadGoodsDetail?type=app"
   method := "GET"

   client := &http.Client{}
   req, err := http.NewRequest(method, url, nil)

   if err != nil {
      fmt.Println(err)
      return false
   }
   req.Header.Add("Cookie", "PHPSESSID=3075ec176a43543f3995ba0f0c0b797a")

   res, err := client.Do(req)
   if err != nil {
      fmt.Println(err)
      return false
   }
   defer res.Body.Close()

   body, err := ioutil.ReadAll(res.Body)
   if err != nil {
      fmt.Println(err)
      return false
   }
   jsonStr := string(body)
   product_id := gjson.Get(jsonStr, "data.product_id").String()

   if len(product_id) > 7 {
      return true
   } else {
      return false
   }

}

func main() {

   task := goticker.New(100, false)
   ch2 := make(chan bool, 0)
   ch02 := make(chan bool, 0)
   ch03 := make(chan bool, 0)
   ch04 := make(chan bool, 0)
   ch05 := make(chan bool, 0)
   ch06 := make(chan bool, 0)
   //每隔1800秒执行
   //task1 := task.AddTaskCallBackFunc(taskone, 300, "proxy-h5循环采集")
   task1 := task.AddTaskCallBackFunc(taskone, 1, "proxy-h5循环采集")
   task2 := task.AddTaskCallBackChannel(ch2, 3000)
   task02 := task.AddCycleTaskCallBackChannel(ch02, "02:18:55")
   task03 := task.AddCycleTaskCallBackChannel(ch03, "03:18:55")
   task04 := task.AddCycleTaskCallBackChannel(ch04, "04:18:55")
   task05 := task.AddCycleTaskCallBackChannel(ch05, "05:18:55")
   task06 := task.AddCycleTaskCallBackChannel(ch06, "06:18:55")
   fmt.Println(task1, task2, task02, task03, task04, task05, task06)
   for {
      select {
      case <-ch2:
         fmt.Println("每间隔3000秒任务task2")
         rebootRouterNow()
         exeonlyH5()
      case <-ch02:
         fmt.Println("每天 02:18:55任务task02")
         rebootRouterNow()
         exeonlyH5()
      case <-ch03:
         fmt.Println("每天 03:18:55任务task03")
         rebootRouterNow()
         exeonlyH5()
      case <-ch04:
         fmt.Println("每天 04:18:55任务task04")
         rebootRouterNow()
         exeonlyH5()
      case <-ch05:
         fmt.Println("每天 05:18:55任务task05")
         rebootRouterNow()
         exeonlyH5()
      case <-ch06:
         fmt.Println("每天 06:18:55任务task06")
         rebootRouterNow()
         exeonlyH5()

      }

   }
}
func rebootRouterNow() {
   //先关闭全部浏览器

   cmdLine2 := "taskkill /F /IM chrome.exe"
   cmd2 := exec.Command("cmd.exe", "/c", "start "+cmdLine2)
   err2 := cmd2.Run()
   fmt.Printf("%s, error:%v \n", cmdLine2, err2)

   cmdLine1 := "selenium_chrome/onlyH5Root.exe"

   if PathExists(cmdLine1) {
      fmt.Println(cmdLine1)
      cmd1 := exec.Command("cmd.exe", "/c", "start "+cmdLine1)
      err1 := cmd1.Run()
      fmt.Printf("%s, error:%v \n", cmdLine1, err1)
      time.Sleep(3 * time.Minute)
   }

}
func exeonlyH5() {
   pythonFile := "ProxyH5NoLimit"
   cmdLine1 := "fetch_log/douyinproxy.exe"
   fmt.Println(cmdLine1)
   cmd1 := exec.Command("cmd.exe", "/c", "start "+cmdLine1)
   err1 := cmd1.Run()
   fmt.Printf("%s, error:%v \n", cmdLine1, err1)

   if isAppNotFinish() {
      //app未采集完成就开启开个窗口进行采集
      cmdLine := "python ./" + pythonFile + ".py"
      fmt.Println(cmdLine)
      cmd := exec.Command("cmd.exe", "/c", "start "+cmdLine)
      err := cmd.Run()
      fmt.Printf("%s, error:%v \n", cmdLine, err)
      time.Sleep(5 * time.Minute)

      cmdLine2 := "python ./" + pythonFile + ".py"
      fmt.Println(cmdLine2)
      cmd2 := exec.Command("cmd.exe", "/c", "start "+cmdLine2)
      err2 := cmd2.Run()
      fmt.Printf("%s, error:%v \n", cmdLine2, err2)
      time.Sleep(5 * time.Minute)

      cmdLine3 := "python ./" + pythonFile + ".py"
      fmt.Println(cmdLine3)
      cmd3 := exec.Command("cmd.exe", "/c", "start "+cmdLine3)
      err3 := cmd3.Run()
      fmt.Printf("%s, error:%v \n", cmdLine3, err3)
      time.Sleep(5 * time.Minute)

   } else {
      //app采集完成,就只打开一个页面进行采集

      cmdLine := "python ./" + pythonFile + ".py"
      fmt.Println(cmdLine)
      cmd := exec.Command("cmd.exe", "/c", "start "+cmdLine)
      err := cmd.Run()
      fmt.Printf("%s, error:%v \n", cmdLine, err)
      time.Sleep(time.Duration(2) * time.Second)
   }

}

func taskone(args interface{}) {
   fmt.Println(args)

   fmt.Println("存在selenium_chrome/onlyH5Root.exe,就会采集前重启路由器" + time.Now().String())
}
func test(args interface{}) {
   fmt.Println(args)
}

func PathExists(path string) bool {

   _, err := os.Stat(path)

   if err == nil {

      return true

   }

   if os.IsNotExist(err) {

      return false

   }

   return false

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

逆向导师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值