Skip to content

Commit

Permalink
[feature]<main>: support dy keyword suggest ✨✨ (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
o98k-ok committed Jul 3, 2024
1 parent 056f24a commit 4d210ee
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 11 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
data/*.wav
data/*.raw
data/*
54 changes: 54 additions & 0 deletions internal/bilibili/hot.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package bilibili

import (
"net/http"
"net/url"

"github.com/duke-git/lancet/v2/netutil"
"github.com/o98k-ok/voice/internal/pkg"
)

type DouyinHot struct {
StatusCode int `json:"status_code"`
MusicList []struct {
MusicInfo struct {
ID int64 `json:"id"`
IDStr string `json:"id_str"`
Title string `json:"title"`
Author string `json:"author"`
Album string `json:"album"`
} `json:"music_info"`
} `json:"music_list"`
}

type DouyinHelper interface {
HotKeys() []string
}

type Douyin struct {
cli *netutil.HttpClient
}

func NewDouyinHelper(cli *netutil.HttpClient) DouyinHelper {
return &Douyin{cli: cli}
}

func (dy *Douyin) HotKeys() []string {
req := netutil.HttpRequest{
RawURL: "https://aweme.snssdk.com/aweme/v1/chart/music/list/?chart_id=6853972723954146568&count=20",
Method: http.MethodGet,
Headers: make(http.Header),
QueryParams: make(url.Values),
}

hot, err := pkg.Request(dy.cli, &req, func(result *DouyinHot) bool { return result.StatusCode == 0 && len(result.MusicList) > 0 })
if err != nil {
return []string{}
}

var keys []string
for _, music := range hot.MusicList {
keys = append(keys, music.MusicInfo.Title+" "+music.MusicInfo.Author)
}
return keys
}
8 changes: 4 additions & 4 deletions internal/player/play.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,13 @@ func (vp *VoicePlayer) InitPlayList(storage storage.Storage) {
func (vp *VoicePlayer) GetMode() string {
switch vp.Modes[vp.ModeIdx] {
case "sequence":
return "⏯"
return "⏯ 顺序"
case "cycle":
return "🔄"
return "🔄 循环"
case "random":
return "🔀"
return "🔀 随机"
default:
return "⏯"
return "⏯ 顺序"
}
}

Expand Down
20 changes: 16 additions & 4 deletions internal/ui/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/charmbracelet/lipgloss"
"github.com/duke-git/lancet/v2/mathutil"
"github.com/duke-git/lancet/v2/netutil"
"github.com/duke-git/lancet/v2/random"
"github.com/duke-git/lancet/v2/strutil"
"github.com/o98k-ok/voice/internal/bilibili"
"github.com/o98k-ok/voice/internal/convertor"
Expand All @@ -24,12 +25,15 @@ type InputElem struct {
logo *LogoElem
result *ListElem
fetcher bilibili.Fetcher
dyHelper bilibili.DouyinHelper
mconvertor convertor.Convertor

fetcherIdx int
active bool
storage storage.Storage
player *player.VoicePlayer
fetcherIdx int
active bool
storage storage.Storage
player *player.VoicePlayer
suggestKeys []string
suggestKey string
}

func NewInputElem(player *player.VoicePlayer, storage storage.Storage, headers []string, widths []int) *InputElem {
Expand All @@ -43,6 +47,7 @@ func NewInputElem(player *player.VoicePlayer, storage storage.Storage, headers [
logo: &LogoElem{},
result: NewListElem(headers, widths, nil),
fetcher: bilibili.NewBlibliFetcher(netutil.NewHttpClient()),
dyHelper: bilibili.NewDouyinHelper(netutil.NewHttpClient()),
mconvertor: convertor.NewAfconvertConvertor(storage.GetRootPath()),
fetcherIdx: 1,
storage: storage,
Expand All @@ -54,10 +59,14 @@ func (ie *InputElem) Active() bool { return ie.active }
func (ie *InputElem) SetActive(active bool) { ie.active = active }

func (ie *InputElem) Init() tea.Cmd {
keys := ie.dyHelper.HotKeys()
ie.suggestKeys = keys
ie.suggestKey = keys[0]
return textinput.Blink
}

func (ie *InputElem) View() string {
ie.textInput.Placeholder = ie.suggestKey
border := lipgloss.RoundedBorder()
box := lipgloss.NewStyle().
BorderStyle(border).
Expand Down Expand Up @@ -173,6 +182,9 @@ func (ie *InputElem) MsgKeyBindings() map[string]map[string]func(v interface{})
ie.fetcherIdx += 1
pack := ie.fetch(ie.fetcherIdx)
ie.result.ResetList(pack)
case ie.textInput.Focused():
ie.textInput.SetValue(ie.suggestKey)
ie.suggestKey = ie.suggestKeys[random.RandInt(0, len(ie.suggestKeys))]
}
return nil
},
Expand Down
2 changes: 1 addition & 1 deletion internal/ui/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (pe *ProceeLineElem) View() string {
}

music := pe.player.Current()
menu := fmt.Sprintf("p prev • space pause/play • n next • r %s\ntab next menu • left -5s • right +5s", pe.player.GetMode())
menu := fmt.Sprintf("p prev • space pause/play • n next\ntab next menu • left -5s • right +5s\nr change mode • current: %s", pe.player.GetMode())
return lipgloss.JoinVertical(lipgloss.Center,
pkg.RenderWithWidth(music.Name, MaxWindowSize*0.6), "\n",
pkg.RenderWithWidth(music.Desc, MaxWindowSize*0.6), "\n",
Expand Down

0 comments on commit 4d210ee

Please sign in to comment.