add support get bilibili-live url

pull/15/head
肥羊 3 years ago
parent 71c5ebe11d
commit b776cb2514
  1. 3
      Golang/go.mod
  2. 6
      Golang/go.sum
  3. 118
      Golang/liveurls/bilibili.go
  4. 7
      Golang/main.go

@ -6,6 +6,7 @@ require (
github.com/dop251/goja v0.0.0-20230203172422-5460598cfa32
github.com/forgoer/openssl v1.5.0
github.com/gin-gonic/gin v1.8.2
github.com/tidwall/gjson v1.14.4
)
require (
@ -22,6 +23,8 @@ require (
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.0.6 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
github.com/ugorji/go/codec v1.2.7 // indirect
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3 // indirect
golang.org/x/net v0.4.0 // indirect

@ -69,6 +69,12 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/tidwall/gjson v1.14.4 h1:uo0p8EbA09J7RQaflQ1aBRffTR7xedD2bcIVSYxLnkM=
github.com/tidwall/gjson v1.14.4/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/ugorji/go v1.2.7/go.mod h1:nF9osbDWLy6bDVv/Rtoh6QgnvNDpmCalQV5urGCCS6M=
github.com/ugorji/go/codec v1.2.7 h1:YPXUKf7fYbp/y8xloBqZOw2qaVggbfwMlI8WM3wZUJ0=
github.com/ugorji/go/codec v1.2.7/go.mod h1:WGN1fab3R1fzQlVQTkfxVtIBhWDRqOviHU95kRgeqEY=

@ -0,0 +1,118 @@
// Package liveurls
// @Time:2023/02/10 01:03
// @File:bilibili.go
// @SoftWare:Goland
// @Author:feiyang
// @Contact:TG@feiyangdigital
package liveurls
import (
"encoding/json"
"fmt"
"github.com/tidwall/gjson"
"io"
"net/http"
)
type BiliBili struct {
Rid string
Line string
Quality string
Platform string
}
func (b *BiliBili) GetRealRoomID() any {
var firstmap = make(map[string]any)
var realroomid string
apiurl := "https://api.live.bilibili.com/room/v1/Room/room_init?id=" + b.Rid
client := &http.Client{}
r, _ := http.NewRequest("GET", apiurl, nil)
r.Header.Add("user-agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 16_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.3 Mobile/15E148 Safari/604.1")
resp, _ := client.Do(r)
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
json.Unmarshal(body, &firstmap)
if firstmap["msg"] == "直播间不存在" {
return nil
}
if newmap, ok := firstmap["data"].(map[string]any); ok {
if newmap["live_status"] != float64(1) {
return nil
} else {
if flt, ok := newmap["room_id"].(float64); ok {
realroomid = fmt.Sprintf("%v", int(flt))
}
}
}
return realroomid
}
func (b *BiliBili) GetPlayUrl() any {
var roomid string
var realurl string
if str, ok := b.GetRealRoomID().(string); ok {
roomid = str
} else {
return nil
}
client := &http.Client{}
params := map[string]string{
"room_id": roomid,
"protocol": "0,1",
"format": "0,1,2",
"codec": "0,1",
"qn": b.Quality,
"platform": b.Platform,
"ptype": "8",
}
r, _ := http.NewRequest("GET", "https://api.live.bilibili.com/xlive/web-room/v2/index/getRoomPlayInfo", nil)
q := r.URL.Query()
for k, v := range params {
q.Add(k, v)
}
r.URL.RawQuery = q.Encode()
resp, _ := client.Do(r)
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
var json = string(body)
value := gjson.Get(json, "data.playurl_info.playurl.stream")
value.ForEach(func(key, value gjson.Result) bool {
newvalue := gjson.Get(value.String(), "format.0.format_name")
if newvalue.String() == "ts" {
nnvalue := gjson.Get(value.String(), "format.#")
valuelast := fmt.Sprintf("%v", nnvalue.Int()-1)
base_url := gjson.Get(value.String(), "format."+valuelast+".codec.0.base_url")
url_info := gjson.Get(value.String(), "format."+valuelast+".codec.0.url_info")
url_info.ForEach(func(key, value gjson.Result) bool {
keyval := fmt.Sprintf("%v", key)
switch b.Line {
case "first":
if keyval == "0" {
host := gjson.Get(value.String(), "host")
extra := gjson.Get(value.String(), "extra")
realurl = fmt.Sprintf("%v%v%v", host, base_url, extra)
}
case "second":
if keyval == "1" {
host := gjson.Get(value.String(), "host")
extra := gjson.Get(value.String(), "extra")
realurl = fmt.Sprintf("%v%v%v", host, base_url, extra)
}
case "third":
if keyval == "2" {
host := gjson.Get(value.String(), "host")
extra := gjson.Get(value.String(), "extra")
realurl = fmt.Sprintf("%v%v%v", host, base_url, extra)
}
}
return true
})
}
return true
})
return realurl
}

@ -57,7 +57,12 @@ func setupRouter(adurl string) *gin.Engine {
huyaobj.Rid = rid
c.Redirect(http.StatusMovedPermanently, duanyan(adurl, huyaobj.GetLiveUrl()))
case "bilibili":
c.String(http.StatusOK, "即将发布")
biliobj := &liveurls.BiliBili{}
biliobj.Rid = rid
biliobj.Platform = c.DefaultQuery("platform", "web")
biliobj.Quality = c.DefaultQuery("quality", "10000")
biliobj.Line = c.DefaultQuery("line", "second")
c.Redirect(http.StatusMovedPermanently, duanyan(adurl, biliobj.GetPlayUrl()))
}
})
return r

Loading…
Cancel
Save