Compare commits

...

No commits in common. "e9601931519587f43361a5e5eeabf2c6ec47b68f" and "main" have entirely different histories.

3 changed files with 19 additions and 61 deletions

View File

@ -1,40 +1,21 @@
package main
import (
"fmt"
"log"
"time"
"week/moonshot"
"week/qywechat"
"week/tools"
"github.com/jasonlvhit/gocron"
)
var maxRetries = 0
var now = time.Now()
var maxRetries = 3
func main() {
fmt.Println("-----------------------------------------------------------------------------------------")
fmt.Println("-----------------------------------------------------------------------------------------")
log.Println(`
_____
_/ ____\______ ____ _____ _____ ____ __ _ _______ ____ ____
\ __\\_ __ \/ _ \ / \ \__ \ / _ \ \ \/ \/ /\__ \ / \ / ___\
| | | | \( <_> ) Y Y \ / __ \( <_> ) \ / / __ \| | \/ /_/ >
|__| |__| \____/|__|_| / (____ /\____/ \/\_/ (____ /___| /\___ /
\/ \/ \/ \//_____/`)
fmt.Println("-----------------------------------------------------------------------------------------")
fmt.Println("-----------------------------------------------------------------------------------------")
if maxRetries < 3 {
for {
remainingTime := tools.RemainingTimeUntilNextFriday17()
if remainingTime <= 0 {
break // 当剩余时间小于等于0时跳出循环
}
fmt.Printf("\r距离执行定时任务还剩 %d小时 %d分钟 %d秒", int(remainingTime.Hours()), int(remainingTime.Minutes())%60, int(remainingTime.Seconds())%60)
time.Sleep(time.Second) // 等待一秒钟,然后更新
}
now := time.Now()
nowDate := now.Format("2006-01-02 15:04:05") + " " + now.Weekday().String()
log.Printf("等待执行定时任务中...当前时间是:%s", nowDate)
if maxRetries != 0 {
// 定义任务,每周五的五点执行
gocron.Every(1).Friday().At("17:00").Do(job)
// 开始定时任务
@ -46,23 +27,15 @@ _/ ____\______ ____ _____ _____ ____ __ _ _______ ____ ____
}
func job() {
nowDate := now.Format("2006-01-02 15:04:05") + " " + now.Weekday().String()
log.Printf("开始执行任务,当前时间是:%v", nowDate)
err := moonshot.CreateExcel()
if err != nil {
maxRetries++
log.Printf("执行失败,开始第%v次重试...", maxRetries)
log.Println("----------------------------------------------------------------------")
job()
maxRetries--
}
repContent, err := moonshot.AiChat()
if err != nil {
maxRetries++
log.Printf("执行失败,开始第%v次重试...", maxRetries)
log.Println("----------------------------------------------------------------------")
job()
maxRetries--
}
qywechat.Send(repContent)

View File

@ -24,10 +24,10 @@ func Send(repContent string) {
// 发送HTTP POST请求到企业微信机器人Webhook
resp, err := http.Post(webhookURL, "application/json", bytes.NewBuffer(requestBody))
if err != nil {
log.Fatalf("发送企业微信机器人失败,错误是: %v", err)
log.Fatalf("Failed to send message: %v", err)
}
defer resp.Body.Close()
log.Println("发送企业微信机器人成功")
log.Println("Message sent successfully!")
}

View File

@ -1,33 +1,18 @@
package tools
import (
"fmt"
"time"
)
// 计算时间差值
func RemainingTimeUntilNextFriday17() time.Duration {
// 获取当前时间
now := time.Now()
// 计算下一个周五的时间
nextFriday := GetNextWeekday(now, time.Friday)
// 构造下一个周五 17 点的时间
nextFriday17 := time.Date(nextFriday.Year(), nextFriday.Month(), nextFriday.Day(), 17, 0, 0, 0, nextFriday.Location())
// 计算时间差
duration := nextFriday17.Sub(now)
return duration
}
// 获取下一个指定星期几的时间
func GetNextWeekday(t time.Time, weekday time.Weekday) time.Time {
daysUntil := (int(weekday) - int(t.Weekday()) + 7) % 7
if daysUntil == 0 {
daysUntil = 7
// 当前时间是否为周五5点
func CheckTime() bool {
nowTime := time.Now()
if nowTime.Weekday().String() == "Friday" && nowTime.Format("15:04:05") == "17:00:00" {
fmt.Println("当前时间是周五5点")
return true
} else {
fmt.Println("当前时间不是周五5点")
return false
}
return t.AddDate(0, 0, daysUntil)
}