新增直播间搜索

用户信息可以获取直播间信息了
This commit is contained in:
2022-12-25 18:07:24 +08:00
parent b2b566f9dd
commit ed9189a313
9 changed files with 2525 additions and 773 deletions

View File

@@ -3,6 +3,7 @@ package biliapi
import (
"io"
"net/http"
"net/url"
"time"
)
@@ -12,25 +13,41 @@ type Root struct {
TTL int `json:"ttl"`
Data interface{} `json:"data"`
}
type Jar struct {
cookies []*http.Cookie
}
func (jar *Jar) SetCookies(u *url.URL, cookies []*http.Cookie) {
jar.cookies = cookies
}
func (jar *Jar) Cookies(u *url.URL) []*http.Cookie {
return jar.cookies
}
type BaseService struct {
client *http.Client
url string
Client *http.Client
Url string
Headers map[string]string
}
func (srv *BaseService) DoRequest() ([]byte, error) {
req, err := http.NewRequest("GET", srv.url, nil)
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,
if srv.Headers != nil {
for k, v := range srv.Headers {
req.Header.Add(k, v)
}
}
resp, err := srv.client.Do(req)
if srv.Client == nil {
srv.Client = &http.Client{
Timeout: 2 * time.Second,
}
}
resp, err := srv.Client.Do(req)
defer resp.Body.Close()
if err != nil {
return nil, err
@@ -40,9 +57,6 @@ func (srv *BaseService) DoRequest() ([]byte, error) {
if err != nil {
return nil, err
}
//if resp.StatusCode != 200 {
// return body, NewAPIError(fmt.Sprintf("非正常返回值StatusCode %d", resp.StatusCode))
//}
return body, nil
}

View File

@@ -9,8 +9,8 @@ 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,
Client: &http.Client{},
Url: url,
}
respBody, err := srv.DoRequest()
if err != nil {

54
biliapi/search.go Normal file
View File

@@ -0,0 +1,54 @@
package biliapi
import (
"fmt"
"net/http"
"time"
)
type SearchParams struct {
Cookie string
Keyword string
Order string
Page uint
}
func SearchVideo(params *SearchParams) ([]byte, error) {
url := fmt.Sprintf("https://api.bilibili.com/x/web-interface/search/type?"+
"page=%d&page_size=42&order=%s&platform=pc&keyword=%s&source_tag=3&search_type=video&dynamic_offset=0",
params.Page, params.Order, params.Keyword)
srv := BaseService{
Client: &http.Client{
Jar: nil,
Timeout: 2 * time.Second,
},
Url: url,
Headers: map[string]string{"Cookie": params.Cookie},
}
respBody, err := srv.DoRequest()
if err != nil {
return nil, err
}
return respBody, nil
}
func SearchLiveRoom(params *SearchParams) ([]byte, error) {
url := fmt.Sprintf("https://api.bilibili.com/x/web-interface/search/type?"+
"page=%d&page_size=42&order=%s&platform=pc&keyword=%s&source_tag=3&search_type=live_room&dynamic_offset=0",
params.Page, params.Order, params.Keyword)
srv := BaseService{
Client: &http.Client{
Jar: nil,
Timeout: 2 * time.Second,
},
Url: url,
Headers: map[string]string{"Cookie": params.Cookie},
}
respBody, err := srv.DoRequest()
if err != nil {
return nil, err
}
return respBody, nil
}

View File

@@ -18,8 +18,8 @@ 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,
Client: &http.Client{},
Url: url,
}
respBody, err := srv.DoRequest()
if err != nil {