mirror of
https://github.com/AynaLivePlayer/AynaLivePlayer.git
synced 2025-12-14 14:08:22 +08:00
add diange source command & fix bilibili video
This commit is contained in:
@@ -220,6 +220,10 @@
|
|||||||
"en": "Custom Command (Default one still works)",
|
"en": "Custom Command (Default one still works)",
|
||||||
"zh-CN": "自定义命令 (默认的依然可用)"
|
"zh-CN": "自定义命令 (默认的依然可用)"
|
||||||
},
|
},
|
||||||
|
"plugin.diange.source_cmd": {
|
||||||
|
"en": "Source Command",
|
||||||
|
"zh-CN": "来源点歌命令"
|
||||||
|
},
|
||||||
"plugin.diange.description": {
|
"plugin.diange.description": {
|
||||||
"en": "Basic Diange Configuration",
|
"en": "Basic Diange Configuration",
|
||||||
"zh-CN": "点歌基本设置"
|
"zh-CN": "点歌基本设置"
|
||||||
|
|||||||
@@ -59,17 +59,21 @@ func Add(keyword string, user interface{}) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func AddWithProvider(keyword string, pname string, user interface{}) {
|
func AddWithProvider(keyword string, pname string, user interface{}) {
|
||||||
medias, err := provider.Search(pname, keyword)
|
media := provider.MatchMedia(pname, keyword)
|
||||||
if err != nil {
|
if media == nil {
|
||||||
l().Warnf("search for %s, got error %s", keyword, err)
|
medias, err := provider.Search(pname, keyword)
|
||||||
return
|
if err != nil {
|
||||||
|
l().Warnf("search for %s, got error %s", keyword, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if len(medias) == 0 {
|
||||||
|
l().Infof("search for %s, got no result", keyword)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
media = medias[0]
|
||||||
}
|
}
|
||||||
if len(medias) == 0 {
|
|
||||||
l().Info("search for %s, got no result", keyword)
|
|
||||||
}
|
|
||||||
media := medias[0]
|
|
||||||
media.User = user
|
media.User = user
|
||||||
l().Info("add media %s (%s)", media.Title, media.Artist)
|
l().Infof("add media %s (%s)", media.Title, media.Artist)
|
||||||
UserPlaylist.Insert(-1, media)
|
UserPlaylist.Insert(-1, media)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import (
|
|||||||
"AynaLivePlayer/i18n"
|
"AynaLivePlayer/i18n"
|
||||||
"AynaLivePlayer/liveclient"
|
"AynaLivePlayer/liveclient"
|
||||||
"AynaLivePlayer/logger"
|
"AynaLivePlayer/logger"
|
||||||
|
"fmt"
|
||||||
"fyne.io/fyne/v2"
|
"fyne.io/fyne/v2"
|
||||||
"fyne.io/fyne/v2/container"
|
"fyne.io/fyne/v2/container"
|
||||||
"fyne.io/fyne/v2/data/binding"
|
"fyne.io/fyne/v2/data/binding"
|
||||||
@@ -29,6 +30,7 @@ type Diange struct {
|
|||||||
QueueMax int
|
QueueMax int
|
||||||
UserCoolDown int
|
UserCoolDown int
|
||||||
CustomCMD string
|
CustomCMD string
|
||||||
|
SourceCMD []string
|
||||||
cooldowns map[string]int
|
cooldowns map[string]int
|
||||||
panel fyne.CanvasObject
|
panel fyne.CanvasObject
|
||||||
}
|
}
|
||||||
@@ -41,6 +43,7 @@ func NewDiange() *Diange {
|
|||||||
QueueMax: 128,
|
QueueMax: 128,
|
||||||
UserCoolDown: -1,
|
UserCoolDown: -1,
|
||||||
CustomCMD: "add",
|
CustomCMD: "add",
|
||||||
|
SourceCMD: make([]string, 0),
|
||||||
cooldowns: make(map[string]int),
|
cooldowns: make(map[string]int),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -51,18 +54,42 @@ func (d *Diange) Name() string {
|
|||||||
|
|
||||||
func (d *Diange) Enable() error {
|
func (d *Diange) Enable() error {
|
||||||
config.LoadConfig(d)
|
config.LoadConfig(d)
|
||||||
|
d.initCMD()
|
||||||
controller.AddCommand(d)
|
controller.AddCommand(d)
|
||||||
gui.AddConfigLayout(d)
|
gui.AddConfigLayout(d)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Diange) Match(command string) bool {
|
func (d *Diange) initCMD() {
|
||||||
for _, c := range []string{"点歌", d.CustomCMD} {
|
if len(d.SourceCMD) == len(config.Provider.Priority) {
|
||||||
if command == c {
|
return
|
||||||
return true
|
}
|
||||||
|
if len(d.SourceCMD) > len(config.Provider.Priority) {
|
||||||
|
d.SourceCMD = d.SourceCMD[:len(config.Provider.Priority)]
|
||||||
|
return
|
||||||
|
}
|
||||||
|
for i := len(d.SourceCMD); i < len(config.Provider.Priority); i++ {
|
||||||
|
d.SourceCMD = append(d.SourceCMD, "点歌"+config.Provider.Priority[i])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// isCMD return int if the commmand name matches our command
|
||||||
|
// -1 = not match, 0 = normal command, 1+ = source command
|
||||||
|
func (d *Diange) isCMD(cmd string) int {
|
||||||
|
if cmd == "点歌" || cmd == d.CustomCMD {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
fmt.Println(d.SourceCMD)
|
||||||
|
for index, c := range d.SourceCMD {
|
||||||
|
if cmd == c {
|
||||||
|
return index + 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false
|
return -1
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Diange) Match(command string) bool {
|
||||||
|
return d.isCMD(command) >= 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Diange) Execute(command string, args []string, danmu *liveclient.DanmuMessage) {
|
func (d *Diange) Execute(command string, args []string, danmu *liveclient.DanmuMessage) {
|
||||||
@@ -78,6 +105,7 @@ func (d *Diange) Execute(command string, args []string, danmu *liveclient.DanmuM
|
|||||||
l().Infof("User %s(%s) still in cool down period, diange failed", danmu.User.Username, danmu.User.Uid)
|
l().Infof("User %s(%s) still in cool down period, diange failed", danmu.User.Username, danmu.User.Uid)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
cmdType := d.isCMD(command)
|
||||||
keyword := strings.Join(args, " ")
|
keyword := strings.Join(args, " ")
|
||||||
perm := d.UserPermission
|
perm := d.UserPermission
|
||||||
l().Trace("user permission check: ", perm)
|
l().Trace("user permission check: ", perm)
|
||||||
@@ -85,10 +113,15 @@ func (d *Diange) Execute(command string, args []string, danmu *liveclient.DanmuM
|
|||||||
l().Trace("privilege permission check: ", perm)
|
l().Trace("privilege permission check: ", perm)
|
||||||
perm = perm || (d.AdminPermission && (danmu.User.Admin))
|
perm = perm || (d.AdminPermission && (danmu.User.Admin))
|
||||||
l().Trace("admin permission check: ", perm)
|
l().Trace("admin permission check: ", perm)
|
||||||
if perm {
|
if !perm {
|
||||||
// reset cool down
|
return
|
||||||
d.cooldowns[danmu.User.Uid] = ct
|
}
|
||||||
|
// reset cool down
|
||||||
|
d.cooldowns[danmu.User.Uid] = ct
|
||||||
|
if cmdType == 0 {
|
||||||
controller.Add(keyword, &danmu.User)
|
controller.Add(keyword, &danmu.User)
|
||||||
|
} else {
|
||||||
|
controller.AddWithProvider(keyword, config.Provider.Priority[cmdType-1], &danmu.User)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -122,6 +155,17 @@ func (d *Diange) CreatePanel() fyne.CanvasObject {
|
|||||||
widget.NewLabel(i18n.T("plugin.diange.custom_cmd")), nil,
|
widget.NewLabel(i18n.T("plugin.diange.custom_cmd")), nil,
|
||||||
widget.NewEntryWithData(binding.BindString(&d.CustomCMD)),
|
widget.NewEntryWithData(binding.BindString(&d.CustomCMD)),
|
||||||
)
|
)
|
||||||
d.panel = container.NewVBox(dgPerm, dgQueue, dgCoolDown, dgShortCut)
|
sourceCmds := []fyne.CanvasObject{}
|
||||||
|
for i, _ := range d.SourceCMD {
|
||||||
|
sourceCmds = append(
|
||||||
|
sourceCmds,
|
||||||
|
container.NewBorder(
|
||||||
|
nil, nil, widget.NewLabel(config.Provider.Priority[i]), nil,
|
||||||
|
widget.NewEntryWithData(binding.BindString(&d.SourceCMD[i]))))
|
||||||
|
}
|
||||||
|
dgSourceCMD := container.NewBorder(
|
||||||
|
nil, nil, widget.NewLabel(i18n.T("plugin.diange.source_cmd")), nil,
|
||||||
|
container.NewVBox(sourceCmds...))
|
||||||
|
d.panel = container.NewVBox(dgPerm, dgQueue, dgCoolDown, dgShortCut, dgSourceCMD)
|
||||||
return d.panel
|
return d.panel
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -116,7 +116,6 @@ func (b *Bilibili) UpdateMediaUrl(media *player.Media) error {
|
|||||||
media.Header = map[string]string{
|
media.Header = map[string]string{
|
||||||
"user-agent": "BiliMusic/2.233.3",
|
"user-agent": "BiliMusic/2.233.3",
|
||||||
}
|
}
|
||||||
fmt.Println(fmt.Sprintf(b.InfoApi, media.Meta.(Meta).Id))
|
|
||||||
uri := gjson.Get(resp, "data.cdns.0").String()
|
uri := gjson.Get(resp, "data.cdns.0").String()
|
||||||
if uri == "" {
|
if uri == "" {
|
||||||
return ErrorExternalApi
|
return ErrorExternalApi
|
||||||
|
|||||||
@@ -82,7 +82,7 @@ func (b *BilibiliVideo) Search(keyword string) ([]*player.Media, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (b *BilibiliVideo) UpdateMedia(media *player.Media) error {
|
func (b *BilibiliVideo) UpdateMedia(media *player.Media) error {
|
||||||
resp := httpGetString(fmt.Sprintf(b.InfoApi, media.Meta.(Meta).Id), nil)
|
resp := httpGetString(fmt.Sprintf(b.InfoApi, b.getBv(media.Meta.(Meta).Id)), nil)
|
||||||
if resp == "" {
|
if resp == "" {
|
||||||
return ErrorExternalApi
|
return ErrorExternalApi
|
||||||
}
|
}
|
||||||
@@ -98,12 +98,12 @@ func (b *BilibiliVideo) UpdateMedia(media *player.Media) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (b *BilibiliVideo) UpdateMediaUrl(media *player.Media) error {
|
func (b *BilibiliVideo) UpdateMediaUrl(media *player.Media) error {
|
||||||
resp := httpGetString(fmt.Sprintf(b.InfoApi, media.Meta.(Meta).Id), nil)
|
resp := httpGetString(fmt.Sprintf(b.InfoApi, b.getBv(media.Meta.(Meta).Id)), nil)
|
||||||
if resp == "" {
|
if resp == "" {
|
||||||
return ErrorExternalApi
|
return ErrorExternalApi
|
||||||
}
|
}
|
||||||
jresp := gjson.Parse(resp)
|
jresp := gjson.Parse(resp)
|
||||||
page := b.getPage(media.Meta.(Meta).Id)
|
page := b.getPage(media.Meta.(Meta).Id) - 1
|
||||||
cid := jresp.Get(fmt.Sprintf("data.View.pages.%d.cid", page)).String()
|
cid := jresp.Get(fmt.Sprintf("data.View.pages.%d.cid", page)).String()
|
||||||
if cid == "" {
|
if cid == "" {
|
||||||
cid = jresp.Get("data.View.cid").String()
|
cid = jresp.Get("data.View.cid").String()
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package provider
|
|||||||
import (
|
import (
|
||||||
"AynaLivePlayer/player"
|
"AynaLivePlayer/player"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"regexp"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -42,3 +43,44 @@ func TestBV_GetMusic(t *testing.T) {
|
|||||||
//fmt.Println(media)
|
//fmt.Println(media)
|
||||||
fmt.Println(media.Url)
|
fmt.Println(media.Url)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestBV_Regex(t *testing.T) {
|
||||||
|
fmt.Println(regexp.MustCompile("^BV[0-9A-Za-z]+(\\?p=[0-9]+)?").FindString("BV1gA411P7ir?p=3"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBV_GetMusicMeta2(t *testing.T) {
|
||||||
|
var api MediaProvider = BilibiliVideoAPI
|
||||||
|
|
||||||
|
media := player.Media{
|
||||||
|
Meta: Meta{
|
||||||
|
Name: api.GetName(),
|
||||||
|
Id: "BV1gA411P7ir?p=3",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
err := api.UpdateMedia(&media)
|
||||||
|
fmt.Println(err)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fmt.Println(media)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBV_GetMusic2(t *testing.T) {
|
||||||
|
var api MediaProvider = BilibiliVideoAPI
|
||||||
|
media := player.Media{
|
||||||
|
Meta: Meta{
|
||||||
|
Name: api.GetName(),
|
||||||
|
Id: "BV1gA411P7ir?p=3",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
err := api.UpdateMedia(&media)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
err = api.UpdateMediaUrl(&media)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
//fmt.Println(media)
|
||||||
|
fmt.Println(media.Url)
|
||||||
|
}
|
||||||
|
|||||||
@@ -44,6 +44,13 @@ func FormatPlaylistUrl(pname, uri string) (string, error) {
|
|||||||
return "", ErrorNoSuchProvider
|
return "", ErrorNoSuchProvider
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func MatchMedia(provider string, keyword string) *player.Media {
|
||||||
|
if v, ok := Providers[provider]; ok {
|
||||||
|
return v.MatchMedia(keyword)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func Search(provider string, keyword string) ([]*player.Media, error) {
|
func Search(provider string, keyword string) ([]*player.Media, error) {
|
||||||
if v, ok := Providers[provider]; ok {
|
if v, ok := Providers[provider]; ok {
|
||||||
return v.Search(keyword)
|
return v.Search(keyword)
|
||||||
|
|||||||
@@ -38,3 +38,9 @@ func StringToInt(s string) int {
|
|||||||
i, _ := strconv.Atoi(s)
|
i, _ := strconv.Atoi(s)
|
||||||
return i
|
return i
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func StringSliceCopy(src []string) []string {
|
||||||
|
x := make([]string, len(src))
|
||||||
|
copy(x, src)
|
||||||
|
return x
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user