Skip to content

Commit

Permalink
add bilibili bangumi support
Browse files Browse the repository at this point in the history
  • Loading branch information
ankikong committed Mar 13, 2020
1 parent e73a6b8 commit 5455087
Show file tree
Hide file tree
Showing 15 changed files with 382 additions and 111 deletions.
6 changes: 3 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.vscode/*
*.exe
bin/*
.vscode/*
*.exe
bin/*
cookie/*
Binary file modified README.md
Binary file not shown.
9 changes: 9 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module github.com/ankikong/goMusic

go 1.13

require (
github.com/cheggaaa/pb/v3 v3.0.4
github.com/go-openapi/strfmt v0.19.5 // indirect
github.com/jedib0t/go-pretty v4.3.0+incompatible
)
41 changes: 41 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
github.com/VividCortex/ewma v1.1.1 h1:MnEK4VOv6n0RSY4vtRe3h11qjxL3+t0B8yOL8iMXdcM=
github.com/VividCortex/ewma v1.1.1/go.mod h1:2Tkkvm3sRDVXaiyucHiACn4cqf7DpdyLvmxzcbUokwA=
github.com/asaskevich/govalidator v0.0.0-20190424111038-f61b66f89f4a h1:idn718Q4B6AGu/h5Sxe66HYVdqdGu2l9Iebqhi/AEoA=
github.com/asaskevich/govalidator v0.0.0-20190424111038-f61b66f89f4a/go.mod h1:lB+ZfQJz7igIIfQNfa7Ml4HSf2uFQQRzpGGRXenZAgY=
github.com/cheggaaa/pb v2.0.7+incompatible h1:gLKifR1UkZ/kLkda5gC0K6c8g+jU2sINPtBeOiNlMhU=
github.com/cheggaaa/pb/v3 v3.0.4 h1:QZEPYOj2ix6d5oEg63fbHmpolrnNiwjUsk+h74Yt4bM=
github.com/cheggaaa/pb/v3 v3.0.4/go.mod h1:7rgWxLrAUcFMkvJuv09+DYi7mMUYi8nO9iOWcvGJPfw=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/go-openapi/errors v0.19.2 h1:a2kIyV3w+OS3S97zxUndRVD46+FhGOUBDFY7nmu4CsY=
github.com/go-openapi/errors v0.19.2/go.mod h1:qX0BLWsyaKfvhluLejVpVNwNRdXZhEbTA4kxxpKBC94=
github.com/go-openapi/strfmt v0.19.5 h1:0utjKrw+BAh8s57XE9Xz8DUBsVvPmRUB6styvl9wWIM=
github.com/go-openapi/strfmt v0.19.5/go.mod h1:eftuHTlB/dI8Uq8JJOyRlieZf+WkkxUuk0dgdHXr2Qk=
github.com/go-stack/stack v1.8.0 h1:5SgMzNM5HxrEjV0ww2lTmX6E2Izsfxas4+YHWRs3Lsk=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/jedib0t/go-pretty v4.3.0+incompatible h1:CGs8AVhEKg/n9YbUenWmNStRW2PHJzaeDodcfvRAbIo=
github.com/jedib0t/go-pretty v4.3.0+incompatible/go.mod h1:XemHduiw8R651AF9Pt4FwCTKeG3oo7hrHJAoznj9nag=
github.com/mattn/go-colorable v0.1.2 h1:/bC9yWikZXAL9uJdulbSfyVNIR3n3trXl+v8+1sx8mU=
github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-isatty v0.0.10 h1:qxFzApOv4WsAL965uUPIsXzAKCZxN2p9UqdhFS4ZW10=
github.com/mattn/go-isatty v0.0.10/go.mod h1:qgIWMr58cqv1PHHyhnkY9lrL7etaEgOFcMEpPG5Rm84=
github.com/mattn/go-runewidth v0.0.7 h1:Ei8KR0497xHyKJPAv59M1dkC+rOZCMBJ+t3fZ+twI54=
github.com/mattn/go-runewidth v0.0.7/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE=
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
go.mongodb.org/mongo-driver v1.0.3 h1:GKoji1ld3tw2aC+GX1wbr/J2fX13yNacEYoJ8Nhr0yU=
go.mongodb.org/mongo-driver v1.0.3/go.mod h1:u7ryQJ+DOzQmeO7zB6MHyr8jkEQvC8vH7qLUO4lqsUM=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20191008105621-543471e840be/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191128015809-6d18c012aee9 h1:ZBzSG/7F4eNKz2L3GE9o300RX0Az1Bw5HF7PDraD+qU=
golang.org/x/sys v0.0.0-20191128015809-6d18c012aee9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
8 changes: 7 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,13 @@ func main() {
} else if strings.Contains(URL, "qq.com") {
GetByQQId(URL)
} else if strings.Contains(URL, "bilibili") {
bilibili.Deal(URL)
if strings.Contains(URL, "video") {
bilibili.Deal(URL)
} else if strings.Contains(URL, "bangumi") {
bilibili.BangumiDeal(URL)
} else {
fmt.Println("unsupport url")
}
}
}
}
68 changes: 34 additions & 34 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
package main

import (
"encoding/json"
"fmt"
"testing"

"github.com/ankikong/goMusic/kugou"
)

func TestKuGou(t *testing.T) {
rs := kugou.Search("Hello Alone")
for _, per := range rs {
fmt.Println(per.GetUrl(320))
}
fmt.Println(rs)
}

type tt struct {
Key string `json:"key"`
}

func TestSearch(t *testing.T) {
a := new(tt)
a.Key = "claris"
rs, _ := json.Marshal(a)
fmt.Println(string(rs), rs)
// search("トゥルーエンド プレイヤー")
}

func TestQQId(t *testing.T) {
GetByQQId("https://y.qq.com/n/yqq/song/0031Jhwu0ryf6Q.html")
// GetByQQId("https://i.y.qq.com/v8/playsong.html?songid=105603683&source=yqq#wechat_redirect")
}
package main

import (
"encoding/json"
"fmt"
"testing"

"github.com/ankikong/goMusic/kugou"
)

func TestKuGou(t *testing.T) {
rs := kugou.Search("Hello Alone")
for _, per := range rs {
fmt.Println(per.GetUrl(320))
}
fmt.Println(rs)
}

type tt struct {
Key string `json:"key"`
}

func TestSearch(t *testing.T) {
a := new(tt)
a.Key = "claris"
rs, _ := json.Marshal(a)
fmt.Println(string(rs), rs)
// search("トゥルーエンド プレイヤー")
}

func TestQQId(t *testing.T) {
GetByQQId("https://y.qq.com/n/yqq/song/0031Jhwu0ryf6Q.html")
// GetByQQId("https://i.y.qq.com/v8/playsong.html?songid=105603683&source=yqq#wechat_redirect")
}
48 changes: 21 additions & 27 deletions makefile
Original file line number Diff line number Diff line change
@@ -1,62 +1,56 @@
# Go parameters
GOCMD=go
GOBUILD=$(GOCMD) build
GOBUILD=$(GOCMD) build -ldflags "-s -w"
GOCLEAN=$(GOCMD) clean
GOTEST=$(GOCMD) test
GOGET=$(GOCMD) get

RELEASE_VERSION=V0.1.0
RELEASE_VERSION=V0.2.0
BINARY_NAME=goMusic-$(RELEASE_VERSION)
LOCAL_BINARY_NAME=goMusic
BINARY_DIR=bin


all: test build
build:
$(GOBUILD) -o D:/cmd/$(LOCAL_BINARY_NAME).exe
$(GOBUILD)
test:
$(GOTEST) -v ./...
$(GOTEST) -v ./...
clean:
$(GOCLEAN)
rm -f $(BINARY_NAME)
rm -f $(BINARY_UNIX)
$(GOCLEAN)
rm -f $(BINARY_NAME)
run:
$(GOBUILD) -o $(BINARY_NAME) -v ./...
./$(BINARY_NAME)
$(GOBUILD) -o $(BINARY_NAME) -v ./...
./$(BINARY_NAME)

deps:
$(GOGET) github.com/jedib0t/go-pretty/table

mybuild:
$(GOBUILD) -o D:/cmd/$(LOCAL_BINARY_NAME).exe

# Cross compilation

# windows
build-win64:
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 $(GOBUILD) -o $(BINARY_DIR)/$(BINARY_NAME)_win64.exe
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 $(GOBUILD) -o $(BINARY_DIR)/$(BINARY_NAME)_win64.exe
build-win32:
CGO_ENABLED=0 GOOS=windows GOARCH=386 $(GOBUILD) -o $(BINARY_DIR)/$(BINARY_NAME)_win32.exe
CGO_ENABLED=0 GOOS=windows GOARCH=386 $(GOBUILD) -o $(BINARY_DIR)/$(BINARY_NAME)_win32.exe

# Linux
build-linux-amd64:
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 $(GOBUILD) -o $(BINARY_DIR)/$(BINARY_NAME)_linux_amd64
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 $(GOBUILD) -o $(BINARY_DIR)/$(BINARY_NAME)_linux_amd64
build-linux-X86:
CGO_ENABLED=0 GOOS=linux GOARCH=386 $(GOBUILD) -o $(BINARY_DIR)/$(BINARY_NAME)_linux_X86
CGO_ENABLED=0 GOOS=linux GOARCH=386 $(GOBUILD) -o $(BINARY_DIR)/$(BINARY_NAME)_linux_X86
build-linux-arm64:
CGO_ENABLED=0 GOOS=linux GOARCH=arm64 $(GOBUILD) -o $(BINARY_DIR)/$(BINARY_NAME)_linux_arm64
CGO_ENABLED=0 GOOS=linux GOARCH=arm64 $(GOBUILD) -o $(BINARY_DIR)/$(BINARY_NAME)_linux_arm64
build-linux-armV7:
CGO_ENABLED=0 GOOS=linux GOARCH=arm GOARM=7 $(GOBUILD) -o $(BINARY_DIR)/$(BINARY_NAME)_linux_armV7
CGO_ENABLED=0 GOOS=linux GOARCH=arm GOARM=7 $(GOBUILD) -o $(BINARY_DIR)/$(BINARY_NAME)_linux_armV7

# Mac
build-darwin-X86:
CGO_ENABLED=0 GOOS=darwin GOARCH=386 $(GOBUILD) -o $(BINARY_DIR)/$(BINARY_NAME)_darwin_X86 -v
CGO_ENABLED=0 GOOS=darwin GOARCH=386 $(GOBUILD) -o $(BINARY_DIR)/$(BINARY_NAME)_darwin_X86
build-darwin-amd64:
CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 $(GOBUILD) -o $(BINARY_DIR)/$(BINARY_NAME)_darwin_amd64 -v

build-all:
make build-win64
make build-win32
make build-linux-amd64
make build-linux-X86
make build-linux-arm64
make build-linux-armV7
make build-darwin-X86
make build-darwin-amd64
CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 $(GOBUILD) -o $(BINARY_DIR)/$(BINARY_NAME)_darwin_amd64

build-all: build-win64 build-win32 build-linux-amd64 build-linux-X86 build-linux-arm64 build-linux-armV7 build-darwin-X86 build-darwin-amd64
169 changes: 169 additions & 0 deletions provider/bilibili/bangumi.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
package bilibili

import (
"encoding/json"
"fmt"
"regexp"
"strings"

"github.com/ankikong/goMusic/tool"
)

const (
epsURL = "https://api.bilibili.com/pgc/web/season/section?season_id="
originURL = "https://bangumi.bilibili.com/player/web_api/playurl/?"
bangumiParams = "appkey=%s&cid=%d&module=bangumi&otype=json&platform=flash&player=1&qn=%s&type="
)

// Episode 番剧每一集的信息
type Episode struct {
Aid int `json:"aid"`
Cid int `json:"cid"`
Title string `json:"long_title"`
Index string `json:"title"`
}

// URL 保存某个番剧的一集的某一个块
// 因为发起的是flash请求,所以应该只有一个完整的块
// Length 视频的时间长度
// Order 第几个分块
// Size 此分块的物理大小
// URL 视频的链接
type URL struct {
Length int `json:"length"`
Order int `json:"order"`
Size int `json:"size"`
URL string `json:"url"`
Format string
}

type rawURL struct {
Code int `json:"code"`
Format string `json:"format"`
TimeLength int `json:"timelength"`
DURL []URL `json:"durl"`
Message string `json:"message"`
}
type epsStruct struct {
Code int `json:"code"`
Message string `json:"message"`
Result struct {
Data struct {
Episodes []Episode `json:"episodes"`
} `json:"main_section"`
} `json:"result"`
}

// SeasonInfo 一季番剧的信息
type SeasonInfo struct {
SeasonID int `json:"season_id"`
SeasonTitle string `json:"season_title"`
Title string `json:"title"`
}

type midStrut struct {
MediaInfo struct {
Seasons []SeasonInfo `json:"seasons"`
} `json:"mediaInfo"`
}

// GetEpisodesBySeasonID 通过season ID获取该番剧的所有分集
func GetEpisodesBySeasonID(seasonID string) []Episode {
rs, err := tool.DoHTTP("GET", epsURL+seasonID, "", "", "", "")
if err != nil {
panic(err)
}
var data epsStruct
if err := json.Unmarshal([]byte(rs), &data); err != nil {
panic(err)
}
if data.Code != 0.0 {
panic(data.Message)
}
return data.Result.Data.Episodes
}

// GetBangumiURL 通过cid获取番剧的实际地址
func GetBangumiURL(cid int) []URL {
param := fmt.Sprintf(bangumiParams, appkey, cid, "112")
// param = getSign(param)
// fmt.Println(originURL + param)
rs, err := tool.DoHTTP("GET", originURL+param, "", "", "", "")
if err != nil {
panic(err)
}
var data rawURL
if err := json.Unmarshal([]byte(rs), &data); err != nil {
panic(err)
}
if data.Code != 0 {
panic(data.Message)
}
format := data.Format
for i := range data.DURL {
data.DURL[i].Format = format
}
return data.DURL
}

// GetAllSeasonByMIDURL 通过总集页获取番剧所有季的信息
func GetAllSeasonByMIDURL(url string) []SeasonInfo {
rs, err := tool.DoHTTP("GET", url, "", "", "", "")
if err != nil {
panic(err)
}
reg := regexp.MustCompile(`__INITIAL_STATE__=(.*?);\(`)
data := reg.Find([]byte(rs))
data = data[18 : len(data)-2]
var rawData midStrut
if err := json.Unmarshal(data, &rawData); err != nil {
panic(err)
}
return rawData.MediaInfo.Seasons
}

// GetSeasonIDByPlayPage 通过播放页获取此番剧的season ID
func GetSeasonIDByPlayPage(url string) string {
rs, err := tool.DoHTTP("GET", url, "", "", "", "")
if err != nil {
panic(err)
}
reg := regexp.MustCompile(`"ssId":\d+`)
seasonID := string(reg.Find([]byte(rs)))[7:]
return seasonID
}

// BangumiDeal 处理番剧链接
func BangumiDeal(url string) {
var seasonID string
if strings.Contains(url, "media") {
data := GetAllSeasonByMIDURL(url)
for j, i := range data {
fmt.Printf("%d:%s-%s\n", j, i.SeasonTitle, i.Title)
}
var ind int
fmt.Printf("select which season:")
fmt.Scan(&ind)
if ind > 0 && ind < len(data) {
seasonID = fmt.Sprint(data[ind].SeasonID)
} else {
panic("error input")
}
} else if strings.Contains(url, "play") {
seasonID = GetSeasonIDByPlayPage(url)
} else {
panic("unsupport url")
}
rs := GetEpisodesBySeasonID(seasonID)
for i, j := range rs {
fmt.Printf("%d:%s\n", i, j.Title)
}
fmt.Printf("select which episode to download:")
var ind int
fmt.Scan(&ind)
// ind := 0
if ind >= 0 && ind < len(rs) {
tmp := GetBangumiURL(rs[ind].Cid)[0]
tool.Download(tmp.URL, fmt.Sprintf("%s-%d", seasonID, tmp.Order), "")
}
}
Loading

0 comments on commit 5455087

Please sign in to comment.