Skip to content

Commit

Permalink
拓展支持从17bushu、shuabu和api三中方式实现部署调用
Browse files Browse the repository at this point in the history
  • Loading branch information
ns-cn committed Jun 5, 2023
1 parent 1061743 commit bbecc7f
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 83 deletions.
12 changes: 10 additions & 2 deletions env/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@ package env

import "github.com/ns-cn/goter"

const (
COM_17BUSHU = "17bushu"
COM_SHUABU = "shuabu"
API = "api"
)

var (
CfgFile = goter.NewCmdFlagString("istep.json", "load", "l", "config file")
CfgHost = goter.NewCmdFlagString("http://118.195.237.33/", "host", "H", "服务器")
CfgFile = goter.NewCmdFlagString("istep.json", "load", "l", "加载的json配置文件")
Cfg17BushuHost = goter.NewCmdFlagString("http://118.195.237.33/", "17bushu-host", "H", "使用17bushu方式时配置的服务器")
CfgShuabuHost = goter.NewCmdFlagString("https://shuabu.org/", "shuabu-host", "S", "使用shuabu方式时配置的服务器")
CfgWay = goter.NewCmdFlagString(COM_17BUSHU, "way", "W", "指定使用的方式,支持(17bushu、shuabu、api)三种方式")
)
8 changes: 7 additions & 1 deletion istep.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ var wg = sync.WaitGroup{}
func main() {
root := goter.NewRootCmdWithAction("istep", "基于zepp life修改步数的程序", env.VERSION, func(command *cobra.Command, strings []string) {
fmt.Printf("【istep】%v读取配置文件%s\n", time.Now(), env.CfgFile.Value)
way := env.CfgWay.Value
if way != env.COM_17BUSHU && way != env.COM_SHUABU && way != env.API {
fmt.Println("不被支持的方式")
_ = command.Help()
return
}
// 读取配置文件
data, err := os.ReadFile(env.CfgFile.Value)
if err != nil {
Expand Down Expand Up @@ -51,6 +57,6 @@ func main() {
}
wg.Wait()
})
root.Bind(&env.CfgFile, &env.CfgHost)
root.Bind(&env.CfgFile, &env.Cfg17BushuHost, &env.CfgShuabuHost, &env.CfgWay)
_ = root.Execute()
}
64 changes: 64 additions & 0 deletions task_change.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package main

import (
"context"
"github.com/chromedp/chromedp"
"istep/env"
"log"
)

func Client(keeper chan int, commands chan ChangeCommand, showClient bool) {
keeper <- 1
var ctx context.Context
var cancel context.CancelFunc
defer func() {
<-keeper
}()
if showClient {
// 创建一个chrome配置实例
opts := append(chromedp.DefaultExecAllocatorOptions[:],
chromedp.Flag("headless", false),
)
ctx, cancel = chromedp.NewExecAllocator(context.Background(), opts...)
defer cancel()
ctx, cancel = chromedp.NewContext(ctx)
defer cancel()
} else {
ctx, cancel = chromedp.NewContext(context.Background())
defer cancel()
}
// 访问网站并等待页面加载完成
if env.CfgWay.Value == env.COM_SHUABU {
if err := chromedp.Run(ctx,
//chromedp.Emulate(device.IPadPro11),
chromedp.Navigate(env.CfgShuabuHost.Value),
chromedp.WaitVisible("app", chromedp.ByID),
); err != nil {
log.Fatal(err)
}
} else if env.CfgWay.Value == env.COM_17BUSHU {
if err := chromedp.Run(ctx,
//chromedp.Emulate(device.IPadPro11),
chromedp.Navigate(env.Cfg17BushuHost.Value),
chromedp.WaitVisible("submitBtn", chromedp.ByID),
); err != nil {
log.Fatal(err)
}
}
defer func() {
err := recover()
if err != nil {
log.Fatal(err)
}
}()
for {
cmd := <-commands
if env.CfgWay.Value == env.COM_SHUABU {
changeStepNumberWithShuabu(ctx, cmd)
} else if env.CfgWay.Value == env.API {
changeStepNumberWithApi(cmd)
} else {
changeStepNumberWith17bushu(ctx, cmd)
}
}
}
80 changes: 0 additions & 80 deletions task_change_step.go

This file was deleted.

40 changes: 40 additions & 0 deletions task_change_with_17bushu.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package main

import (
"context"
"fmt"
"github.com/chromedp/chromedp"
"istep/env"
"log"
"strconv"
"time"
)

func changeStepNumberWith17bushu(ctx context.Context, cmd ChangeCommand) {
defer func() {
wg.Done()
}()
fmt.Printf("【修改步数任务】: 为%s修改步数为%d", cmd.UserID, cmd.StepNumber)
err := chromedp.Run(ctx,
chromedp.Navigate(env.Cfg17BushuHost.Value),
chromedp.WaitVisible("submitBtn", chromedp.ByID),
chromedp.SendKeys("#phone", cmd.UserID),
chromedp.SendKeys("#password", cmd.Password, chromedp.ByID),
chromedp.SendKeys("#steps", strconv.Itoa(cmd.StepNumber)),
chromedp.Click("#submitBtn", chromedp.ByID),
chromedp.Sleep(5*time.Second),
chromedp.ActionFunc(func(ctx context.Context) error {
var failMsg string
_ = chromedp.Run(ctx, chromedp.Text("#failMsg", &failMsg, chromedp.ByID))
if failMsg == "" {
fmt.Print("\t修改步数成功\n")
} else {
fmt.Printf("\t修改步数失败: %s(请注意密码编码)\n", failMsg)
}
return nil
}),
)
if err != nil {
log.Fatal(err)
}
}
44 changes: 44 additions & 0 deletions task_change_with_shuabu.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package main

import (
"context"
"fmt"
"github.com/chromedp/chromedp"
"istep/env"
"log"
"strconv"
"time"
)

/*
*
使用shuabu网站进行shuabu
*/
func changeStepNumberWithShuabu(ctx context.Context, cmd ChangeCommand) {
defer func() {
wg.Done()
}()
fmt.Printf("【修改步数任务】: 为%s修改步数为%d", cmd.UserID, cmd.StepNumber)
err := chromedp.Run(ctx,
chromedp.Navigate(env.Cfg17BushuHost.Value),
chromedp.WaitVisible("submitBtn", chromedp.ByID),
chromedp.SendKeys("#app > div > div > div > div > div.w-11\\/12.mx-auto > div.ant-spin-nested-loading > div > div > div:nth-child(1) > div.form-input > input", cmd.UserID),
chromedp.SendKeys("#app > div > div > div > div > div.w-11\\/12.mx-auto > div.ant-spin-nested-loading > div > div > div:nth-child(2) > div.form-input > span > input", cmd.Password, chromedp.ByID),
chromedp.SendKeys("#app > div > div > div > div > div.w-11\\/12.mx-auto > div.ant-spin-nested-loading > div > div > div:nth-child(3) > div.form-input > input", strconv.Itoa(cmd.StepNumber)),
chromedp.Click("#app > div > div > div > div > div.w-11\\/12.mx-auto > button", chromedp.ByQuery),
chromedp.Sleep(5*time.Second),
//chromedp.ActionFunc(func(ctx context.Context) error {
// var failMsg string
// _ = chromedp.Run(ctx, chromedp.Text("#failMsg", &failMsg, chromedp.ByID))
// if failMsg == "" {
// fmt.Print("\t修改步数成功\n")
// } else {
// fmt.Printf("\t修改步数失败: %s(请注意密码编码)\n", failMsg)
// }
// return nil
//}),
)
if err != nil {
log.Fatal(err)
}
}

0 comments on commit bbecc7f

Please sign in to comment.