Skip to content

Commit

Permalink
update: add TUI list option for video selection
Browse files Browse the repository at this point in the history
  • Loading branch information
cybardev committed Mar 2, 2024
1 parent 4f72fa9 commit 945fbf7
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 4 deletions.
18 changes: 18 additions & 0 deletions cmd/ytgo/search.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package main

import (
"encoding/json"
"errors"
"io"
"net/http"
"net/url"
"os/exec"
"regexp"
"slices"
)
Expand Down Expand Up @@ -56,3 +58,19 @@ func NthVideo(query string, n int) (VID, error) {
}
return vids[n-1], 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()
if err != nil {
return Video{}, err
}

var v Video
err = json.Unmarshal(out, &v)
if err != nil {
return Video{}, err
}

return v, nil
}
68 changes: 68 additions & 0 deletions cmd/ytgo/ui.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package main

import (
"fmt"
"sync"

"github.com/rivo/tview"
)

func VIDFromMenu(query string) (VID, error) {
vids, err := getVideos(query)
if err != nil {
return "", err
}
m := make(VideoMap, len(vids))
return menuUI(&vids, &m)
}

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

fmt.Println("Loading video list. Please wait...")
for _, vid := range *vids {
go mapVideos(vid, m, &wg)
}

wg.Wait()

var v VID
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
}

func mapVideos(id VID, m *VideoMap, wg *sync.WaitGroup) error {
defer (*wg).Done()
v, err := fetchVideoInfo(id)
if err != nil {
return err
}
(*m)[id] = v
return nil
}

func videoList(app *tview.Application, selected *VID, m *VideoMap, vids *[]VID) *tview.List {
l := tview.NewList()
l = l.
AddItem("Quit", "Press to exit", 'q', func() {
*selected = ""
app.Stop()
})

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

return l
}
15 changes: 15 additions & 0 deletions cmd/ytgo/vid.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
package main

import (
"fmt"
"net/url"
"os"
"os/exec"
)

const YtURL = "https://www.youtube.com/"

type VideoMap map[VID]Video

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 {
Expand Down
17 changes: 13 additions & 4 deletions cmd/ytgo/yt.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ import (
func main() {
// specify available flags
var (
f, m, u bool
n int
query string
f, l, m, u bool
n int
query string
)

// parse CLI args
flag.BoolVar(&f, "f", false, "Play from URL")
flag.BoolVar(&l, "l", false, "Select from list")
flag.BoolVar(&m, "m", false, "Play music only")
flag.BoolVar(&u, "u", false, "Display URL only")
flag.IntVar(&n, "n", 1, "Play nth media")
Expand All @@ -35,7 +36,15 @@ func main() {
if f {
v, err = VIDfromURL(query)
} else {
v, err = NthVideo(query, n)
if l {
v, err = VIDFromMenu(query)
if err == nil && v == "" {
fmt.Println("No video selected.\nExiting...")
return
}
} else {
v, err = NthVideo(query, n)
}
}
if err != nil {
log.Fatalln(err)
Expand Down
13 changes: 13 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
module github.com/cybardev/ytgo

go 1.22

require github.com/rivo/tview v0.0.0-20240225120200-5605142ca62e

require (
github.com/gdamore/encoding v1.0.0 // indirect
github.com/gdamore/tcell/v2 v2.7.1 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
golang.org/x/sys v0.17.0 // indirect
golang.org/x/term v0.17.0 // indirect
golang.org/x/text v0.14.0 // indirect
)
50 changes: 50 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
github.com/gdamore/encoding v1.0.0 h1:+7OoQ1Bc6eTm5niUzBa0Ctsh6JbMW6Ra+YNuAtDBdko=
github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo5dl+VrEg=
github.com/gdamore/tcell/v2 v2.7.1 h1:TiCcmpWHiAU7F0rA2I3S2Y4mmLmO9KHxJ7E1QhYzQbc=
github.com/gdamore/tcell/v2 v2.7.1/go.mod h1:dSXtXTSK0VsW1biw65DZLZ2NKr7j0qP/0J7ONmsraWg=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/rivo/tview v0.0.0-20240225120200-5605142ca62e h1:7ubTieBkl4KCz5ABZzh0zPkBYWPguSOHUundUsorIzQ=
github.com/rivo/tview v0.0.0-20240225120200-5605142ca62e/go.mod h1:02iFIz7K/A9jGCvrizLPvoqr4cEIx7q54RH5Qudkrss=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.3/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/term v0.17.0 h1:mkTF7LCd6WGJNL3K1Ad7kwxNfYAW6a8a8QqtMblp/4U=
golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

0 comments on commit 945fbf7

Please sign in to comment.