初版core,使用http api
This commit is contained in:
48
biliapi/base.go
Normal file
48
biliapi/base.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package biliapi
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Root struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
TTL int `json:"ttl"`
|
||||
Data interface{} `json:"data"`
|
||||
}
|
||||
|
||||
type BaseService struct {
|
||||
client *http.Client
|
||||
url string
|
||||
}
|
||||
|
||||
func (srv *BaseService) DoRequest() ([]byte, error) {
|
||||
req, err := http.NewRequest("GET", srv.url, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36 Edg/108.0.1462.46")
|
||||
|
||||
if srv.client == nil {
|
||||
srv.client = &http.Client{
|
||||
Timeout: time.Duration(5) * time.Second,
|
||||
}
|
||||
}
|
||||
resp, err := srv.client.Do(req)
|
||||
defer resp.Body.Close()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
//if resp.StatusCode != 200 {
|
||||
// return body, NewAPIError(fmt.Sprintf("非正常返回值:StatusCode %d", resp.StatusCode))
|
||||
//}
|
||||
|
||||
return body, nil
|
||||
}
|
||||
17
biliapi/error.go
Normal file
17
biliapi/error.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package biliapi
|
||||
|
||||
type APIError struct {
|
||||
Msg string
|
||||
}
|
||||
|
||||
func (err APIError) Error() string {
|
||||
return err.Msg
|
||||
}
|
||||
|
||||
func NewAPIError(msg string) APIError {
|
||||
return APIError{Msg: msg}
|
||||
}
|
||||
|
||||
func MissingParamError() APIError {
|
||||
return APIError{Msg: "缺少参数"}
|
||||
}
|
||||
20
biliapi/info.go
Normal file
20
biliapi/info.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package biliapi
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func BasicInfo(mid uint) ([]byte, error) {
|
||||
url := fmt.Sprintf("https://api.bilibili.com/x/space/wbi/acc/info?mid=%d", mid)
|
||||
|
||||
srv := BaseService{
|
||||
client: &http.Client{},
|
||||
url: url,
|
||||
}
|
||||
respBody, err := srv.DoRequest()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return respBody, nil
|
||||
}
|
||||
29
biliapi/stat.go
Normal file
29
biliapi/stat.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package biliapi
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type StatData struct {
|
||||
Mid uint64 `json:"mid"`
|
||||
Following uint32 `json:"following"`
|
||||
Whisper uint32 `json:"whisper"`
|
||||
Black uint32 `json:"black"`
|
||||
Follower uint32 `json:"follower"`
|
||||
}
|
||||
|
||||
// UserStat 用户统计信息 例如粉丝数,订阅数等
|
||||
func UserStat(mid uint) ([]byte, error) {
|
||||
url := fmt.Sprintf("https://api.bilibili.com/x/relation/stat?vmid=%d", mid)
|
||||
|
||||
srv := BaseService{
|
||||
client: &http.Client{},
|
||||
url: url,
|
||||
}
|
||||
respBody, err := srv.DoRequest()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return respBody, nil
|
||||
}
|
||||
Reference in New Issue
Block a user