diff --git a/.gitignore b/.gitignore index 7de26a2..1db0303 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ conf.json session_id dist/ -main \ No newline at end of file +main +.idea/ \ No newline at end of file diff --git a/app/internal/client/helper.go b/app/internal/client/kook.go similarity index 100% rename from app/internal/client/helper.go rename to app/internal/client/kook.go diff --git a/app/internal/client/onge.go b/app/internal/client/onge.go new file mode 100644 index 0000000..3bde4d3 --- /dev/null +++ b/app/internal/client/onge.go @@ -0,0 +1,46 @@ +package client + +import ( + jsoniter "github.com/json-iterator/go" + log "github.com/sirupsen/logrus" + "github/aimerny/elix/app/internal/dto" + "io" + "net/http" +) + +const ( + DivingFishPlayerScoreQuery = "https://www.diving-fish.com/api/maimaidxprober/player/records" +) + +func Get(req *http.Request) ([]byte, error) { + client := http.Client{} + resp, err := client.Do(req) + if err != nil { + log.WithError(err).Error("do get failed! err") + return nil, err + } + defer resp.Body.Close() + data, err := io.ReadAll(resp.Body) + if err != nil { + log.WithError(err).Error("action read body failed!") + return nil, err + } + return data, nil +} + +func QueryRecord(importToken string) (*dto.DivingPlayerRecordsResp, error) { + req, err := http.NewRequest("GET", DivingFishPlayerScoreQuery, nil) + if err != nil { + log.Errorf("Http request construct failed! err: %e", err) + return nil, err + } + req.Header.Add("Import-Token", importToken) + body, err := Get(req) + resp := &dto.DivingPlayerRecordsResp{} + err = jsoniter.Unmarshal(body, resp) + if err != nil { + log.WithError(err).WithField("resp", string(body)).Error("unmarshal record failed") + return nil, err + } + return resp, nil +} diff --git a/app/internal/common/flag.go b/app/internal/common/flag.go index 55373b6..bec533c 100644 --- a/app/internal/common/flag.go +++ b/app/internal/common/flag.go @@ -3,11 +3,12 @@ package common import "flag" var ( - ConfigPathParam *string + ConfigPathParam *string + UpgradeOngeDatabase *bool ) func InitFlag() { ConfigPathParam = flag.String("conf", "conf.json", "config file path of elix") - + UpgradeOngeDatabase = flag.Bool("db-update", false, "update database of onge service") flag.Parse() } diff --git a/app/internal/dto/diving_fish_api.go b/app/internal/dto/diving_fish_api.go index 0d1ec1e..aaeaa27 100644 --- a/app/internal/dto/diving_fish_api.go +++ b/app/internal/dto/diving_fish_api.go @@ -40,3 +40,28 @@ type DivingMusicBasicInfo struct { From string `json:"from"` IsNew bool `json:"is_new"` } + +type DivingPlayerRecordsResp struct { + Username string `json:"username,omitempty"` + AdditionalRating int `json:"additional_rating,omitempty"` + Nickname string `json:"nickname,omitempty"` + Plate string `json:"plate,omitempty"` + Rating int `json:"rating"` + Records []DivingPlayerRecordInfo `json:"records"` +} + +type DivingPlayerRecordInfo struct { + Achievements float64 `json:"achievements,omitempty"` + Ds float64 `json:"ds,omitempty"` + DxScore int `json:"dx_score,omitempty"` + Fc string `json:"fc"` + Fs string `json:"fs"` + Level string `json:"level"` + LevelIndex int `json:"level_index"` + LevelLabel string `json:"level_label"` + Ra int `json:"ra"` + Rate string `json:"rate"` + SongID int `json:"song_id"` + Title string `json:"title"` + Type string `json:"type"` +} diff --git a/app/internal/dto/onge.go b/app/internal/dto/onge.go index ef127ff..838fd55 100644 --- a/app/internal/dto/onge.go +++ b/app/internal/dto/onge.go @@ -49,3 +49,9 @@ type ChuniChartInfo struct { Charter string Combo int } + +type OngeUserInfo struct { + gorm.Model + KookId string + DivingFishToken string +} diff --git a/app/internal/service/onge/base.go b/app/internal/service/onge/base.go index f8eefef..cf37118 100644 --- a/app/internal/service/onge/base.go +++ b/app/internal/service/onge/base.go @@ -5,6 +5,7 @@ import ( log "github.com/sirupsen/logrus" "github/aimerny/elix/app/internal/client" "github/aimerny/elix/app/internal/common" + "github/aimerny/elix/app/internal/dto" "gorm.io/gorm" ) @@ -31,4 +32,18 @@ func InitOngeService(dbConf *common.DatasourceConf) { OngeStatus = true } log.Infof("init onge service finished") + if *common.UpgradeOngeDatabase { + log.Infof("onge database upgrading ...") + err := OngeServiceDS.AutoMigrate( + &dto.MaiMusicInfo{}, + &dto.MaiChartInfo{}, + &dto.ChuniMusicInfo{}, + &dto.ChuniChartInfo{}, + &dto.OngeUserInfo{}, + ) + if err != nil { + log.WithError(err).Panicf("onge database upgrade failed") + } + log.Infof("onge database upgrade success") + } }