Skip to content

Commit

Permalink
fix: play correct entry from list
Browse files Browse the repository at this point in the history
others: limit list length, add list item shortcuts, mainly use Video instead of VID, respect terminal background colour in list
  • Loading branch information
cybardev committed Mar 3, 2024
1 parent 82727c3 commit e5eefb5
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 59 deletions.
44 changes: 22 additions & 22 deletions cmd/ytgo/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,18 @@ import (
"slices"
)

func fetch(u string) (string, error) {
res, err := http.Get(u)
func NthVID(query string, n int) (VID, error) {
vids, err := getVIDs(query)
if err != nil {
return "", err
return VID(""), err
}
defer res.Body.Close()

body, err := io.ReadAll(res.Body)
if err != nil {
return "", err
if n <= 0 || n > len(vids) {
return "", errors.New("no video found")
}
return string(body), nil
}

func search(query string) (string, error) {
params := url.Values{"search_query": []string{query}}.Encode()
return fetch(YtURL + "results?" + params)
return vids[n-1], nil
}

func getVideos(query string) ([]VID, error) {
func getVIDs(query string) ([]VID, error) {
res, err := search(query)
if err != nil {
return nil, err
Expand All @@ -48,20 +40,28 @@ func getVideos(query string) ([]VID, error) {
return vids, nil
}

func NthVideo(query string, n int) (VID, error) {
vids, err := getVideos(query)
func search(query string) (string, error) {
params := url.Values{"search_query": []string{query}}.Encode()
return fetch(YtURL + "results?" + params)
}

func fetch(u string) (string, error) {
res, err := http.Get(u)
if err != nil {
return "", err
}
if n <= 0 || n > len(vids) {
return "", errors.New("no video found")
defer res.Body.Close()

body, err := io.ReadAll(res.Body)
if err != nil {
return "", err
}
return vids[n-1], nil
return string(body), nil
}

func fetchVideoInfo(id VID) (Video, error) {
j := "%(.{id,title,channel,duration_string,original_url})#j"
out, err := exec.Command("yt-dlp", "-O", j, YtURL+"watch?v="+string(id)).Output()
j := "%(.{id,title,channel,duration_string})#j"
out, err := exec.Command("yt-dlp", "-O", j, id.URL()).Output()
if err != nil {
return Video{}, err
}
Expand Down
45 changes: 24 additions & 21 deletions cmd/ytgo/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,22 @@ import (
"fmt"
"sync"

"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)

func VIDFromMenu(query string) (VID, error) {
vids, err := getVideos(query)
func VideoFromMenu(query string) (Video, error) {
vids, err := getVIDs(query)
if err != nil {
return "", err
return Video{}, err
}
m := make(VideoMap, len(vids))
return menuUI(&vids, &m)
n := min(10, len(vids))
v := vids[:n]
m := make(VideoMap, n)
return menuUI(&v, &m)
}

func menuUI(vids *[]VID, m *VideoMap) (VID, error) {
func menuUI(vids *[]VID, m *VideoMap) (Video, error) {
var wg sync.WaitGroup
wg.Add(len(*vids))

Expand All @@ -27,14 +30,9 @@ func menuUI(vids *[]VID, m *VideoMap) (VID, error) {

wg.Wait()

var v VID
tview.Styles.PrimitiveBackgroundColor = tcell.ColorDefault
app := tview.NewApplication()
list := videoList(app, &v, m, vids)
err := app.SetRoot(list, true).EnableMouse(true).Run()
if err != nil {
return "", err
}
return v, nil
return chooseFromList(app, m, vids)
}

func mapVideos(id VID, m *VideoMap, wg *sync.WaitGroup) error {
Expand All @@ -47,22 +45,27 @@ func mapVideos(id VID, m *VideoMap, wg *sync.WaitGroup) error {
return nil
}

func videoList(app *tview.Application, selected *VID, m *VideoMap, vids *[]VID) *tview.List {
func chooseFromList(app *tview.Application, m *VideoMap, vids *[]VID) (Video, error) {
var selected Video

l := tview.NewList()
l = l.
AddItem("Quit", "Press to exit", 'q', func() {
*selected = ""
selected = Video{}
app.Stop()
})

var v Video
for _, vid := range *vids {
v = (*m)[vid]
l.AddItem("\n"+v.Title, v.Desc(), 0, func() {
*selected = vid
for i, vid := range *vids {
v := (*m)[vid]
l.AddItem(v.Title, v.Desc(), rune(i+48), func() {
selected = v
app.Stop()
})
}

return l
err := app.SetRoot(l, true).EnableMouse(true).Run()
if err != nil {
return Video{}, err
}
return selected, nil
}
19 changes: 9 additions & 10 deletions cmd/ytgo/vid.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,25 @@ const YtURL = "https://www.youtube.com/"

type VideoMap map[VID]Video

type VID string

type Video struct {
Id VID `json:"id"`
Title string `json:"title"`
Channel string `json:"channel"`
Duration string `json:"duration_string"`
Url string `json:"original_url"`
}

func (v Video) Desc() string {
return fmt.Sprintf("(%s) [%s]", v.Channel, v.Duration)
}

type VID string

func (v VID) URL() string {
return YtURL + "watch?v=" + string(v)
}

func (v VID) Play(m bool) error {
func (v Video) Play(m bool) error {
bestaudio, novideo := "", ""
if m {
bestaudio, novideo = "--ytdl-format=bestaudio", "--no-video"
}
cmd := exec.Command("mpv", bestaudio, novideo, v.URL())
cmd := exec.Command("mpv", bestaudio, novideo, v.Id.URL())
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
Expand All @@ -45,10 +40,14 @@ func (v VID) Play(m bool) error {
return nil
}

func (v VID) URL() string {
return YtURL + "watch?v=" + string(v)
}

func VIDfromURL(s string) (VID, error) {
u, err := url.Parse(s)
if err != nil {
return "", err
return VID(""), err
}
return VID(u.Query().Get("v")), nil
}
12 changes: 6 additions & 6 deletions cmd/ytgo/yt.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,26 @@ func main() {
}

// play media from YT or display URL
var v VID
var v Video
var err error
if f {
v, err = VIDfromURL(query)
v.Id, err = VIDfromURL(query)
} else {
if l {
v, err = VIDFromMenu(query)
if err == nil && v == "" {
v, err = VideoFromMenu(query)
if err == nil && (v == Video{}) {
fmt.Println("No video selected.\nExiting...")
return
}
} else {
v, err = NthVideo(query, n)
v.Id, err = NthVID(query, n)
}
}
if err != nil {
log.Fatalln(err)
}
if u {
fmt.Println(v.URL())
fmt.Println(v.Id.URL())
} else {
err = v.Play(m)
if err != nil {
Expand Down

0 comments on commit e5eefb5

Please sign in to comment.