Skip to content

Commit

Permalink
Merge pull request XiaoMengXinX#37 from LinYuanovo/master
Browse files Browse the repository at this point in the history
增加推送
  • Loading branch information
XiaoMengXinX authored Mar 29, 2023
2 parents 4a77ea0 + dcef1d3 commit f0ca5da
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 2 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,9 @@ $ ./Fuck163MusicTasks
"LagMin": 600,
"LagMax": 3600
}
}
},
"PushPlusToken": "", // PushPlus Token, 用于推送运行日志(随便填一个就好)
"ServerSendKey": "" // Server酱 SendKey
}
```

Expand Down
4 changes: 3 additions & 1 deletion config_example.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,5 +86,7 @@
"LagMin": 600,
"LagMax": 3600
}
}
},
"PushPlusToken": "",
"ServerSendKey": ""
}
56 changes: 56 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package main

import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io"
"io/ioutil"
"math/rand"
"net/http"
"net/url"
"os"
"path"
"runtime"
Expand Down Expand Up @@ -38,6 +41,7 @@ var msgLag RandomNum
var mlogLag RandomNum
var processingUser int
var circleID string
var pushMsg string
var configFileName = flag.String("c", "config.json", "Config filename") // 从 cli 参数读取配置文件名
var printVersion = flag.Bool("v", false, "Print version")
var isDEBUG = flag.Bool("d", false, "DEBUG mode")
Expand Down Expand Up @@ -128,6 +132,7 @@ Build ARCH: %s

startTasks()
startCron()
startPushMsg()
}

func startCron() {
Expand Down Expand Up @@ -187,6 +192,52 @@ func startTasks() {
}
}

// 推送消息
func startPushMsg() {
// PushPlus
if config.PushPlusToken != "" {
// 消息内容
content := fmt.Sprintf("网易云音乐自动任务已完成")
content += pushMsg
// 推送相关
data := url.Values{}
data.Set("token", config.PushPlusToken)
data.Set("title", "网易云音乐自动任务")
data.Set("content", content)
res, err := http.PostForm("http://www.pushplus.plus/send", data)
if err != nil {
log.Errorln(err)
}
defer res.Body.Close()
log.Println("PushPlus推送:", res.Status)
}
// Server酱
if config.ServerSendKey != "" {
sendUrl := fmt.Sprintf("https://sc.ftqq.com/%s.send", config.ServerSendKey)
// 消息内容
type Message struct {
Title string `json:"title"`
Desp string `json:"desp"`
}
title := fmt.Sprintf("网易云音乐自动任务")
content := fmt.Sprintf("网易云音乐自动任务已完成")
content += pushMsg
content = strings.ReplaceAll(content, "\n", "\n\n")
message := Message{
Title: title,
Desp: content,
}
messageJson, err := json.Marshal(message)
// 推送相关
res, err := http.Post(sendUrl, "application/json", bytes.NewReader(messageJson))
if err != nil {
log.Errorln(err)
}
defer res.Body.Close()
log.Println("Server酱推送:", res.Status)
}
}

func autoTasks(userData types.LoginStatusData, data utils.RequestData) error {
defer func() {
err := recover()
Expand Down Expand Up @@ -373,6 +424,7 @@ func userSignTask(userData types.LoginStatusData, data utils.RequestData) error
log.Printf("[%s] %s (%s)", userData.Profile.Nickname, result.Msg, "Android")
} else {
log.Printf("[%s] 签到成功 (%s)", userData.Profile.Nickname, "Android")
pushMsg += fmt.Sprintf("\n[%s] 签到成功 (%s)", userData.Profile.Nickname, "Android")
}

result, err = api.UserSign(data, 1)
Expand All @@ -383,6 +435,7 @@ func userSignTask(userData types.LoginStatusData, data utils.RequestData) error
log.Printf("[%s] %s (%s)", userData.Profile.Nickname, result.Msg, "web/PC")
} else {
log.Printf("[%s] 签到成功 (%s)", userData.Profile.Nickname, "Android")
pushMsg += fmt.Sprintf("\n[%s] 签到成功 (%s)", userData.Profile.Nickname, "Android")
}
return nil
}
Expand Down Expand Up @@ -613,6 +666,7 @@ func checkCloudBean(userData types.LoginStatusData, data utils.RequestData) ([]s
return nil, err
}
log.Printf("[%s] 账号当前云豆数: %d", userData.Profile.Nickname, cloudBeanData.Data.CloudBean)
pushMsg += fmt.Sprintf("\n[%s] 当前云豆数: %d", userData.Profile.Nickname, cloudBeanData.Data.CloudBean)
log.Printf("[%s] 获取音乐人任务中...", userData.Profile.Nickname)
dailyTasks, err := api.GetMusicianDailyTasks(data)
if err != nil {
Expand All @@ -634,6 +688,7 @@ func checkCloudBean(userData types.LoginStatusData, data utils.RequestData) ([]s
}
if result.Code == 200 {
log.Printf("[%s] 领取「%s」任务云豆成功, 云豆+%s", userData.Profile.Nickname, task.Description, task.RewardWorth)
pushMsg += fmt.Sprintf("\n[%s] 完成「%s」任务云豆+%s", userData.Profile.Nickname, task.Description, task.RewardWorth)
} else {
log.Errorf("[%s] 领取「%s」任务云豆失败: %s", userData.Profile.Nickname, task.Description, result.Message)
}
Expand All @@ -654,6 +709,7 @@ func checkCloudBean(userData types.LoginStatusData, data utils.RequestData) ([]s
}
if result.Code == 200 {
log.Printf("[%s] 领取「%s」任务云豆成功, 云豆+%d", userData.Profile.Nickname, task.Description, s.Worth)
pushMsg += fmt.Sprintf("\n[%s] 完成「%s」任务云豆+%d", userData.Profile.Nickname, task.Description, s.Worth)
} else {
log.Errorf("[%s] 领取「%s」任务云豆失败: %s", userData.Profile.Nickname, task.Description, result.Message)
}
Expand Down
2 changes: 2 additions & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ type Config struct {
EnableLag bool `json:"EnableLag"`
LagConfig LagConfig `json:"LagConfig"`
} `json:"Cron"`
PushPlusToken string `json:"PushPlusToken"`
ServerSendKey string `json:"ServerSendKey"`
}

// LagConfig 延迟设置
Expand Down

0 comments on commit f0ca5da

Please sign in to comment.