Skip to content

Commit

Permalink
Merge pull request #5 from dvgamerr/feat/notifi-line
Browse files Browse the repository at this point in the history
Feature Notify message.
  • Loading branch information
dvgamerr authored May 10, 2023
2 parents 7b1a622 + bdf5613 commit 5ad4404
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 31 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[![Dependency Review](https://github.com/dvgamerr/go-hoyolab/actions/workflows/review.yml/badge.svg)](https://github.com/dvgamerr/go-hoyolab/actions/workflows/review.yml)

Genshin Impact's Hoyolab Daily Check-in Bot is here!. You only need to run it once, then it will continue to run forever.
![example.png](./docs/example.png)
![example.png](./docs/example-logs.jpg)

## How to use
1. Open chrome browser open [https://www.hoyolab.com/home](https://www.hoyolab.com/home)
Expand Down
7 changes: 7 additions & 0 deletions act/act.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ type BrowserProfile struct {
UserAgent string `yaml:"userAgent"`
}

type LineNotify struct {
Token string `yaml:"token"`
Mini bool `yaml:"mini"`
}

type Hoyolab struct {
Client *resty.Client `yaml:"client,omitempty"`
Notify LineNotify `yaml:"notify"`
Expand Down Expand Up @@ -85,6 +90,8 @@ func (hoyo *Hoyolab) ReadHoyoConfig(configPath string) error {
return nil
} else {
log.Println("Configuration readed.")
hoyo.Notify = readHoyo.Notify
hoyo.Delay = readHoyo.Delay
hoyo.Browser = readHoyo.Browser
hoyo.Daily = readHoyo.Daily
return nil
Expand Down
45 changes: 28 additions & 17 deletions act/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ type ActInfo struct {

type ActSign map[string]any

type ActUser struct {
UserInfo ActUserInfo `json:"user_info"`
}

type ActUserInfo struct {
UID string `json:"uid"`
NickName string `json:"nickname"`
}

// var json jsoniter.API = jsoniter.ConfigCompatibleWithStandardLibrary

func (e *DailyHoyolab) generateHeaders() map[string]string {
Expand Down Expand Up @@ -107,15 +116,15 @@ func actResponse[T any](raw *resty.Response, actData *T) error {
func (e *DailyHoyolab) GetMonthAward(hoyo *Hoyolab) (*ActAward, error) {
raw, err := hoyo.ActRequest(e).Get(fmt.Sprintf("%s%s", e.API.Endpoint, e.API.Award))
if err != nil {
return nil, fmt.Errorf("hoyo::%+v", err)
return nil, fmt.Errorf("api::%+v", err)
}
if raw.StatusCode() != 200 {
return nil, fmt.Errorf("hoyo::%s", raw.Status())
return nil, fmt.Errorf("api::%s", raw.Status())
}

var res ActAward
if err := actResponse(raw, &res); err != nil {
return nil, fmt.Errorf("hoyo::%+v", err)
return nil, fmt.Errorf("api::%+v", err)
}

if IsDebug {
Expand All @@ -129,19 +138,22 @@ func (e *DailyHoyolab) GetMonthAward(hoyo *Hoyolab) (*ActAward, error) {
return &res, nil
}

func (e *DailyHoyolab) GetUserFull(hoyo *Hoyolab) (*ActSign, error) {
time.Sleep(1 * time.Second)
raw, err := hoyo.ActRequest(e).SetBody(map[string]string{"act_id": e.ActID}).Post(fmt.Sprintf("%s%s", e.API.Endpoint, e.API.Sign))
func (e *DailyHoyolab) GetAccountUserInfo(hoyo *Hoyolab) (*ActUser, error) {
// https://bbs-api-os.hoyolab.com/community/painter/wapi/user/full?t=1683734340156
raw, err := hoyo.ActRequest(e).
SetQueryParams(map[string]string{"t": fmt.Sprint(time.Now().Unix() * 1000)}).
SetBody(map[string]string{"act_id": e.ActID}).
Get("https://bbs-api-os.hoyolab.com/community/painter/wapi/user/full")
if err != nil {
return nil, fmt.Errorf("hoyo::%+v", err)
return nil, fmt.Errorf("api::%+v", err)
}
if raw.StatusCode() != 200 {
return nil, fmt.Errorf("hoyo::%s", raw.Status())
return nil, fmt.Errorf("api::%s", raw.Status())
}

var res ActSign
var res ActUser
if err := actResponse(raw, &res); err != nil {
return nil, fmt.Errorf("hoyo::%+v", err)
return nil, fmt.Errorf("api::%+v", err)
}

if IsDebug {
Expand All @@ -151,18 +163,17 @@ func (e *DailyHoyolab) GetUserFull(hoyo *Hoyolab) (*ActSign, error) {
}

func (e *DailyHoyolab) GetCheckInInfo(hoyo *Hoyolab) (*ActInfo, error) {
time.Sleep(550 * time.Millisecond)
raw, err := hoyo.ActRequest(e).Get(fmt.Sprintf("%s%s", e.API.Endpoint, e.API.Info))
if err != nil {
return nil, fmt.Errorf("hoyo::%+v", err)
return nil, fmt.Errorf("api::%+v", err)
}
if raw.StatusCode() != 200 {
return nil, fmt.Errorf("hoyo::%s", raw.Status())
return nil, fmt.Errorf("api::%s", raw.Status())
}

var res ActInfo
if err := actResponse(raw, &res); err != nil {
return nil, fmt.Errorf("hoyo::%+v", err)
return nil, fmt.Errorf("api::%+v", err)
}

if IsDebug {
Expand All @@ -175,15 +186,15 @@ func (e *DailyHoyolab) DailySignIn(hoyo *Hoyolab) (*ActSign, error) {
time.Sleep(1 * time.Second)
raw, err := hoyo.ActRequest(e).SetBody(map[string]string{"act_id": e.ActID}).Post(fmt.Sprintf("%s%s", e.API.Endpoint, e.API.Sign))
if err != nil {
return nil, fmt.Errorf("hoyo::%+v", err)
return nil, fmt.Errorf("api::%+v", err)
}
if raw.StatusCode() != 200 {
return nil, fmt.Errorf("hoyo::%s", raw.Status())
return nil, fmt.Errorf("api::%s", raw.Status())
}

var res ActSign
if err := actResponse(raw, &res); err != nil {
return nil, fmt.Errorf("hoyo::%+v", err)
return nil, fmt.Errorf("api::%+v", err)
}

if IsDebug {
Expand Down
27 changes: 24 additions & 3 deletions act/notify.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,27 @@
package act

type LineNotify struct {
Token string `yaml:"token"`
Minitify bool `yaml:"minitify"`
import (
"fmt"
"log"

"github.com/go-resty/resty/v2"
)

func (hoyo *Hoyolab) NotifyMessage(message string) error {
if hoyo.Notify.Token == "" || message == "" {
return nil
}

raw, err := resty.New().R().
SetHeaders(map[string]string{"Authorization": fmt.Sprintf("Bearer %s", hoyo.Notify.Token)}).
SetFormData(map[string]string{"message": message}).
Post("https://notify-api.line.me/api/notify")
if err != nil {
return fmt.Errorf("notify::%+v", err)
}
if raw.StatusCode() != 200 {
return fmt.Errorf("notify::%s", raw.Status())
}
log.Println("NotifyMessage Sending...")
return nil
}
Binary file added docs/example-logs.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed docs/example.png
Binary file not shown.
67 changes: 57 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,24 @@ var logPath string = ""
var logfile *os.File

func init() {
filename, err := os.Executable()
execFilename, err := os.Executable()
if err != nil {
log.Fatal(err)
}
filename = strings.ReplaceAll(filepath.Base(filename), filepath.Ext(filename), "")
baseFilename := strings.ReplaceAll(filepath.Base(execFilename), filepath.Ext(execFilename), "")

configPath = fmt.Sprintf("%s.%s", filename, configExt)
logPath = fmt.Sprintf("%s.%s", filename, logExt)
configPath = fmt.Sprintf("%s.%s", baseFilename, configExt)
logPath = fmt.Sprintf("%s.%s", baseFilename, logExt)

dirname, err := os.UserHomeDir()
if err != nil {
log.Fatal(err)
}

if !act.IsDev {
dirname = filepath.Dir(execFilename)
}

if _, err := os.Stat(path.Join(dirname, configExt)); err == nil {
configPath = path.Join(dirname, configPath)
logPath = path.Join(dirname, logPath)
Expand Down Expand Up @@ -66,6 +70,8 @@ func main() {

cookieStore := kooky.FindAllCookieStores()
log.Printf("Browser total %d sessions", len(cookieStore))

var notifyMessage []string
for _, store := range cookieStore {
if _, err := os.Stat(store.FilePath()); os.IsNotExist(err) {
continue
Expand All @@ -87,14 +93,27 @@ func main() {
continue
}

var resAcc *act.ActUser
hoyo.Client = resty.New()

var getDaySign int32 = -1
var getAward []string = []string{}
for i := 0; i < len(hoyo.Daily); i++ {
act := hoyo.Daily[i]
act.UserAgent = profile.UserAgent

if resAcc == nil {
resAcc, err = act.GetAccountUserInfo(hoyo)
if err != nil {
log.Printf("%s::GetUserInfo : %v", act.Label, err)
continue
}
log.Printf("%s::GetUserInfo : Hi, '%s", act.Label, resAcc.UserInfo.NickName)
}

resAward, err := act.GetMonthAward(hoyo)
if err != nil {
log.Printf("%s::GetMonthAward :%v", act.Label, err)
log.Printf("%s::GetMonthAward : %v", act.Label, err)
continue
}

Expand All @@ -103,22 +122,50 @@ func main() {
log.Printf("%s::GetCheckInInfo :%v", act.Label, err)
continue
}
log.Printf("%s::GetCheckInInfo : Total %d Claims", act.Label, resInfo.TotalSignDay+1)

log.Printf("%s::GetCheckInInfo : Checked in for %d days", act.Label, resInfo.TotalSignDay)
if resInfo.IsSign {
log.Printf("%s::DailySignIn : Claimed %s", act.Label, resInfo.Today)
continue
}

_, err = act.DailySignIn(hoyo)
if err != nil {
log.Printf("%s::DailySignIn :%v", act.Label, err)
log.Printf("%s::DailySignIn : %v", act.Label, err)
continue
}

if getDaySign < 0 {
getDaySign = resInfo.TotalSignDay + 1
}

award := resAward.Awards[resInfo.TotalSignDay+1]
log.Printf("%s::GetMonthAward : Claimed %s x%d", act.Label, award.Name, award.Count)
log.Printf("%s::GetMonthAward : Today's received %s x%d", act.Label, award.Name, award.Count)

if hoyo.Notify.Mini {
getAward = append(getAward, fmt.Sprintf("*%s x%d* (%s)", award.Name, award.Count, act.Label))
} else {
getAward = append(getAward, fmt.Sprintf("*[%s]* at day %d received %s x%d", act.Label, resInfo.TotalSignDay+1, award.Name, award.Count))
}

}
if len(getAward) > 0 {
if len(hoyo.Browser) > 1 {
notifyMessage = append(notifyMessage, "\n")
}

if hoyo.Notify.Mini {
notifyMessage = append(notifyMessage, fmt.Sprintf("%s, at day %d your got %s", resAcc.UserInfo.NickName, getDaySign, strings.Join(getAward, ", ")))
} else {
notifyMessage = append(notifyMessage, fmt.Sprintf("\nHi, %s Checked in for %d days.\n%s", resAcc.UserInfo.NickName, 1, strings.Join(getAward, "\n")))
}
}
}
}

if err := hoyo.NotifyMessage(strings.Join(notifyMessage, "\n")); err != nil {
log.Printf("NotifyMessage : %v", err)
}
}

func ContainsStrings(a []string, x string) bool {
Expand Down Expand Up @@ -193,8 +240,8 @@ func GenerateDefaultConfig() *act.Hoyolab {
}
return &act.Hoyolab{
Notify: act.LineNotify{
Token: "",
Minitify: true,
Token: "",
Mini: true,
},
Delay: 150,
Browser: []act.BrowserProfile{
Expand Down

0 comments on commit 5ad4404

Please sign in to comment.