[Golang] 纯文本查看 复制代码
package main
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"strings"
)
const GET_TOKEN = "https://auth.aliyundrive.com/v2/account/token"
const GET_SINGINCOUNT = "https://member.aliyundrive.com/v1/activity/sign_in_list"
const GET_SINGINREWARD = "https://member.aliyundrive.com/v1/activity/sign_in_reward?_rx-s=mobile"
type AliSign struct {
GrantType string
RefreshToken string
AccessToken string
UserName string
SignInCount int
ReadyRewardDays []int
}
func NewAliSign(refreshToken string) *AliSign {
return &AliSign{RefreshToken: refreshToken}
}
func (a *AliSign) getToken() bool {
b, _ := json.Marshal(map[string]interface{}{
"grant_type": "refresh_token",
"refresh_token": a.RefreshToken,
})
payload := strings.NewReader(string(b))
req, _ := http.NewRequest("POST", GET_TOKEN, payload)
req.Header.Add("content-type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
type AutoGenerated struct {
UserName string `json:"user_name"`
AccessToken string `json:"access_token"`
}
result := &AutoGenerated{}
if err := json.Unmarshal(body, result); err != nil {
log.Printf("请求token失败,err:%v", err)
return false
}
a.AccessToken = result.AccessToken
a.UserName = result.UserName
return a.AccessToken != ""
}
func (a *AliSign) getSignCount() bool {
payload := strings.NewReader("{}")
req, _ := http.NewRequest("POST", GET_SINGINCOUNT, payload)
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", a.AccessToken))
req.Header.Add("content-type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
type AutoGenerated struct {
Result struct {
SignInCount int `json:"signInCount"`
SignInLogs []struct {
Day int `json:"day"`
IsReward bool `json:"isReward"`
} `json:"signInLogs"`
} `json:"result"`
}
result := &AutoGenerated{}
if err := json.Unmarshal(body, result); err != nil {
log.Printf("请求signInCount失败,err:%v", err)
return false
}
a.SignInCount = result.Result.SignInCount
log.Printf("用户 %s 本月累计签到%d次", a.UserName, a.SignInCount)
for _, v := range result.Result.SignInLogs {
if !v.IsReward {
a.ReadyRewardDays = append(a.ReadyRewardDays, v.Day)
} else {
log.Printf("用户 %s 已经获得第%d天的奖励", a.UserName, v.Day)
}
}
return a.SignInCount != 0
}
func (a *AliSign) getSignInReward(day int) bool {
b, _ := json.Marshal(map[string]interface{}{
"signInDay": day,
})
payload := strings.NewReader(string(b))
req, _ := http.NewRequest("POST", GET_SINGINREWARD, payload)
req.Header.Add("content-type", "application/json")
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", a.AccessToken))
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
type AutoGenerated struct {
Success bool `json:"success"`
Code interface{} `json:"code"`
Message interface{} `json:"message"`
Result struct {
Name string `json:"name"`
Description string `json:"description"`
Notice string `json:"notice"`
SubNotice string `json:"subNotice"`
} `json:"result"`
Arguments interface{} `json:"arguments"`
}
result := &AutoGenerated{}
if err := json.Unmarshal(body, result); err != nil {
log.Printf("请求领奖失败,err:%v", err)
return false
}
if result.Success {
log.Printf("用户 %s 获得第%d天的签到奖励 %s %s %s %s", a.UserName, day, result.Result.Name, result.Result.Description, result.Result.Notice, result.Result.SubNotice)
} else {
log.Printf("请求领奖失败,%v", string(body))
}
return result.Success
}
func (a *AliSign) Sign() {
defer func() {
if err := recover(); err != nil {
log.Printf("签到出现panic:%v", err)
}
}()
if !a.getToken() {
log.Printf("token 获取失败,请检查输入 %s", a.RefreshToken)
return
}
if !a.getSignCount() {
return
}
for _, day := range a.ReadyRewardDays {
if day <= a.SignInCount {
a.getSignInReward(day)
}
}
}
func main() {
args := os.Args
for i := 1; i < len(args); i++ {
a := NewAliSign(args[i])
a.Sign()
}
}