This commit is contained in:
akiba
2023-09-14 20:44:46 +08:00
parent 258bd0c72f
commit 6833089dc9
2 changed files with 25 additions and 115 deletions

View File

@@ -3,6 +3,7 @@ package api
import (
"errors"
"fmt"
"github.com/tidwall/gjson"
"net/http"
"strconv"
)
@@ -55,128 +56,18 @@ type DanmuInfo struct {
} `json:"data"`
}
// UserInfo
// api https://api.bilibili.com/x/web-interface/nav
type UserInfo struct {
Code int `json:"code"`
Message string `json:"message"`
TTL int `json:"ttl"`
Data struct {
IsLogin bool `json:"isLogin"`
EmailVerified int `json:"email_verified"`
Face string `json:"face"`
FaceNft int `json:"face_nft"`
FaceNftType int `json:"face_nft_type"`
LevelInfo struct {
CurrentLevel int `json:"current_level"`
CurrentMin int `json:"current_min"`
CurrentExp int `json:"current_exp"`
NextExp string `json:"next_exp"`
} `json:"level_info"`
Mid int `json:"mid"`
MobileVerified int `json:"mobile_verified"`
Money float64 `json:"money"`
Moral int `json:"moral"`
Official struct {
Role int `json:"role"`
Title string `json:"title"`
Desc string `json:"desc"`
Type int `json:"type"`
} `json:"official"`
OfficialVerify struct {
Type int `json:"type"`
Desc string `json:"desc"`
} `json:"officialVerify"`
Pendant struct {
Pid int `json:"pid"`
Name string `json:"name"`
Image string `json:"image"`
Expire int `json:"expire"`
ImageEnhance string `json:"image_enhance"`
ImageEnhanceFrame string `json:"image_enhance_frame"`
} `json:"pendant"`
Scores int `json:"scores"`
Uname string `json:"uname"`
VipDueDate int64 `json:"vipDueDate"`
VipStatus int `json:"vipStatus"`
VipType int `json:"vipType"`
VipPayType int `json:"vip_pay_type"`
VipThemeType int `json:"vip_theme_type"`
VipLabel struct {
Path string `json:"path"`
Text string `json:"text"`
LabelTheme string `json:"label_theme"`
TextColor string `json:"text_color"`
BgStyle int `json:"bg_style"`
BgColor string `json:"bg_color"`
BorderColor string `json:"border_color"`
UseImgLabel bool `json:"use_img_label"`
ImgLabelURIHans string `json:"img_label_uri_hans"`
ImgLabelURIHant string `json:"img_label_uri_hant"`
ImgLabelURIHansStatic string `json:"img_label_uri_hans_static"`
ImgLabelURIHantStatic string `json:"img_label_uri_hant_static"`
} `json:"vip_label"`
VipAvatarSubscript int `json:"vip_avatar_subscript"`
VipNicknameColor string `json:"vip_nickname_color"`
Vip struct {
Type int `json:"type"`
Status int `json:"status"`
DueDate int64 `json:"due_date"`
VipPayType int `json:"vip_pay_type"`
ThemeType int `json:"theme_type"`
Label struct {
Path string `json:"path"`
Text string `json:"text"`
LabelTheme string `json:"label_theme"`
TextColor string `json:"text_color"`
BgStyle int `json:"bg_style"`
BgColor string `json:"bg_color"`
BorderColor string `json:"border_color"`
UseImgLabel bool `json:"use_img_label"`
ImgLabelURIHans string `json:"img_label_uri_hans"`
ImgLabelURIHant string `json:"img_label_uri_hant"`
ImgLabelURIHansStatic string `json:"img_label_uri_hans_static"`
ImgLabelURIHantStatic string `json:"img_label_uri_hant_static"`
} `json:"label"`
AvatarSubscript int `json:"avatar_subscript"`
NicknameColor string `json:"nickname_color"`
Role int `json:"role"`
AvatarSubscriptURL string `json:"avatar_subscript_url"`
TvVipStatus int `json:"tv_vip_status"`
TvVipPayType int `json:"tv_vip_pay_type"`
TvDueDate int `json:"tv_due_date"`
} `json:"vip"`
Wallet struct {
Mid int `json:"mid"`
BcoinBalance int `json:"bcoin_balance"`
CouponBalance int `json:"coupon_balance"`
CouponDueTime int `json:"coupon_due_time"`
} `json:"wallet"`
HasShop bool `json:"has_shop"`
ShopURL string `json:"shop_url"`
AllowanceCount int `json:"allowance_count"`
AnswerStatus int `json:"answer_status"`
IsSeniorMember int `json:"is_senior_member"`
WbiImg struct {
ImgURL string `json:"img_url"`
SubURL string `json:"sub_url"`
} `json:"wbi_img"`
IsJury bool `json:"is_jury"`
} `json:"data"`
}
func GetUid(cookie string) (int, error) {
result := &UserInfo{}
headers := &http.Header{}
headers.Set("cookie", cookie)
err := GetJsonWithHeader("https://api.bilibili.com/x/web-interface/nav", headers, result)
resp, err := HttpGet("https://api.bilibili.com/x/web-interface/nav", headers)
if err != nil {
return 0, err
}
if result.Code != 0 || !result.Data.IsLogin {
return 0, errors.New(result.Message)
j := gjson.ParseBytes(resp)
if j.Get("code").Int() != 0 || !j.Get("data.isLogin").Bool() {
return 0, errors.New(j.Get("message").String())
}
return result.Data.Mid, nil
return int(j.Get("data.mid").Int()), nil
}
func GetDanmuInfo(roomID int, cookie string) (*DanmuInfo, error) {

View File

@@ -2,9 +2,28 @@ package api
import (
"encoding/json"
"io"
"net/http"
)
func HttpGet(url string, headers *http.Header) ([]byte, error) {
req, err := http.NewRequest("GET", url, nil)
req.Header = *headers
c := &http.Client{}
resp, err := c.Do(req)
if err != nil {
return nil, err
}
defer func() {
_ = resp.Body.Close()
}()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return body, nil
}
func GetJson(url string, result interface{}) error {
resp, err := http.Get(url)
if err != nil {