Files
aliyundrive-Checkin/main.go
Sakurasan 425c600c78 up
2023-03-14 02:28:45 +08:00

159 lines
4.7 KiB
Go

package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"strings"
"github.com/tidwall/gjson"
)
var (
refresh_Token = "d8bdfb926fec4a32b1e81964cb5b7fba"
updateAccesssTokenURL = "https://auth.aliyundrive.com/v2/account/token"
signinURL = "https://member.aliyundrive.com/v1/activity/sign_in_list"
)
type aliyundrive struct {
refreshToken string
accessToken string
}
func New(refreshToken string) *aliyundrive {
return &aliyundrive{refreshToken: refreshToken}
}
func (a *aliyundrive) getAccessToken() {
body := map[string]string{
"grant_type": "refresh_token",
"refresh_token": refresh_Token,
}
b := bytes.NewBuffer(nil)
json.NewEncoder(b).Encode(body)
rsp, err := http.Post(updateAccesssTokenURL, "application/json", b)
if err != nil {
log.Println(err)
return
}
bytersp, _ := io.ReadAll(rsp.Body)
a.accessToken = gjson.GetBytes(bytersp, "access_token").String()
a.refreshToken = gjson.GetBytes(bytersp, "refresh_token").String()
log.Printf("%#v\n", string(bytersp))
}
type refreshToken struct {
GrantType string `json:"grant_Type,omitempty"`
RefreshToken string `json:"refresh_Token,omitempty"`
Phone string
}
type Signrsp struct {
Success bool `json:"success,omitempty"`
Code string `json:"code,omitempty"`
Message string `json:"message,omitempty"`
TotalCount string `json:"totalCount,omitempty"`
NextToken string `json:"nextToken,omitempty"`
MaxResults string `json:"maxResults,omitempty"`
Result struct {
Subject string `json:"subject,omitempty"`
Title string `json:"title,omitempty"`
Description string `json:"description,omitempty"`
IsReward bool `json:"isReward,omitempty"`
Blessing string `json:"blessing,omitempty"`
SignInCount int `json:"signInCount,omitempty"`
SignInCover string `json:"signInCover,omitempty"`
SignInRemindCover string `json:"signInRemindCover,omitempty"`
RewardCover string `json:"rewardCover,omitempty"`
SignInLogs []SignInLogs `json:"signInLogs,omitempty"`
} `json:"result,omitempty"`
Arguments string `json:"arguments,omitempty"`
}
type SignInLogs struct {
Day int `json:"day,omitempty"`
Status string `json:"status,omitempty"`
Icon string `json:"icon,omitempty"`
Notice string `json:"notice,omitempty"`
Type string `json:"type,omitempty"`
Themes string `json:"themes,omitempty"`
CalendarChinese string `json:"calendarChinese,omitempty"`
CalendarDay string `json:"calendarDay,omitempty"`
CalendarMonth string `json:"calendarMonth,omitempty"`
Poster struct {
Name string `json:"name,omitempty"`
Reason string `json:"reason,omitempty"`
Background string `json:"background,omitempty"`
Color string `json:"color,omitempty"`
Action string `json:"action,omitempty"`
} `json:"poster,omitempty"`
Reward struct {
GoodsID int `json:"goodsId,omitempty"`
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
Background string `json:"background,omitempty"`
Color string `json:"color,omitempty"`
Action string `json:"action,omitempty"`
Notice string `json:"notice,omitempty"`
} `json:"reward,omitempty"`
IsReward bool `json:"isReward,omitempty"`
}
func (a aliyundrive) signIn() bool {
req, err := http.NewRequest("POST", signinURL, strings.NewReader("{}"))
if err != nil {
log.Printf("签到失败, 错误信息: %v\n", err)
return false
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", a.accessToken))
req.Header.Set("Content-Type", "application/json")
client := http.DefaultClient
resp, err := client.Do(req)
if err != nil {
log.Printf("签到失败, 错误信息: %v\n", err)
return false
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Printf("签到失败, 错误信息: %v\n", err)
return false
}
log.Printf("%#v\n", string(body))
if gjson.GetBytes(body, "success").Bool() == true {
count := gjson.GetBytes(body, "result.signInCount").Int()
result := gjson.GetBytes(body, fmt.Sprintf("result.signInLogs.%d", count-1))
log.Printf("已签到%d天,本月第%d日\n", count, result.Get("calendarDay"))
if result.Get("isReward").Bool() {
log.Printf("%s\n", result.Get("notice"))
}
log.Println(result.String())
} else {
return false
}
return true
}
func push(config interface{}, phone string, reward string, signInCount int) {
// push notification implementation here
}
func main() {
// rt := os.Getenv("REFRESH_TOKENS")
// if rt == "" {
// panic("未配置阿里云盘REFRESH_TOKENS")
// }
a := New(refresh_Token)
a.getAccessToken()
a.signIn()
}