Skip to content

Commit e4704a7

Browse files
committed
re: reorganize code, simplify data types
1 parent c279d20 commit e4704a7

File tree

6 files changed

+35
-33
lines changed

6 files changed

+35
-33
lines changed

cmd/ytgo/playlist.go

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"strings"
1010
)
1111

12+
type videoCache map[uint32][]Video // cache: content hash -> parsed videos
13+
1214
type Playlist string
1315

1416
func (p Playlist) Create() error {
@@ -57,28 +59,14 @@ func (p Playlist) Create() error {
5759
}
5860

5961
func (p Playlist) Play(m bool) error {
60-
var vs *[]Video
6162
prevVideo := BACK_FLAG
62-
playlistCache := make(map[uint32]*[]Video) // cache: content hash -> parsed videos
63+
c := make(videoCache)
6364
f := string(p)
6465
for {
65-
pl, err := os.ReadFile(f)
66+
vs, err := getPlaylistVideos(f, c)
6667
if err != nil {
6768
return err
6869
}
69-
70-
// check if we've already parsed this playlist content
71-
contentHash := crc32.ChecksumIEEE(pl)
72-
if cached, exists := playlistCache[contentHash]; exists {
73-
vs = cached // use cached parsed videos
74-
} else {
75-
vs, err = getPlaylistVideos(pl) // parse for first time
76-
if err != nil {
77-
return err
78-
}
79-
playlistCache[contentHash] = vs // cache the result
80-
}
81-
8270
v, err := GetVideoFromMenu(vs)
8371
if err != nil {
8472
return err
@@ -97,20 +85,34 @@ func (p Playlist) Play(m bool) error {
9785
}
9886
}
9987

100-
func getPlaylistVideos(pl []byte) (*[]Video, error) {
88+
func getPlaylistVideos(filename string, cache videoCache) ([]Video, error) {
89+
pl, err := os.ReadFile(filename)
90+
if err != nil {
91+
return nil, err
92+
}
93+
94+
// check if we've already parsed this playlist content
95+
contentHash := crc32.ChecksumIEEE(pl)
96+
if cached, exists := cache[contentHash]; exists {
97+
return cached, nil // use cached parsed videos
98+
}
99+
101100
lines := strings.Split(string(pl), "\n")
102101
var vs []Video
103-
for i := 0; i < len(lines)-1; i++ {
104-
id := lines[i]
105-
if len(id) == 11 {
102+
for i, id := range lines {
103+
switch len(id) {
104+
case 0:
105+
continue // skip empty lines
106+
case 11:
106107
v, err := GetVideoFromURL(VID(id).URL())
107108
if err != nil {
108109
return nil, err
109110
}
110111
vs = append(vs, *v)
111-
} else {
112+
default:
112113
log.Printf("%s[WARN]%s Skipped invalid Video ID on line %d: %s\n", C_YELLOW, C_RESET, i+1, id)
113114
}
114115
}
115-
return &vs, nil
116+
cache[contentHash] = vs // cache the result
117+
return vs, nil
116118
}

cmd/ytgo/resparse.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func getVideoFromDetails(j *map[string]any) (*Video, error) {
4848

4949
type SearchRes string // Search Response
5050

51-
func (r SearchRes) Parse() (*[]Video, error) {
51+
func (r SearchRes) Parse() ([]Video, error) {
5252
re := regexp.MustCompile(`var ytInitialData = ({.*?});`)
5353
s := re.FindStringSubmatch(string(r))[1]
5454
var j any
@@ -60,15 +60,15 @@ func (r SearchRes) Parse() (*[]Video, error) {
6060
return getVideoList(&res), nil
6161
}
6262

63-
func getVideoList(j *[]any) *[]Video {
63+
func getVideoList(j *[]any) []Video {
6464
var vs []Video
6565
for _, i := range *j {
6666
v, isVideo := getVideoFromEntry(&i)
6767
if isVideo {
6868
vs = append(vs, *v)
6969
}
7070
}
71-
return &vs
71+
return vs
7272
}
7373

7474
func getVideoFromEntry(j *any) (*Video, bool) {

cmd/ytgo/search.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ func GetVideoFromSearch(query string, n int) (*Video, error) {
2020
if err != nil {
2121
return nil, err
2222
}
23-
if n <= 0 || n > len(*vs) {
23+
if n <= 0 || n > len(vs) {
2424
return nil, errors.New("no video found")
2525
}
26-
return &(*vs)[n-1], nil
26+
return &(vs)[n-1], nil
2727
}
2828

29-
func GetSearchResults(query string) (*[]Video, error) {
29+
func GetSearchResults(query string) ([]Video, error) {
3030
params := url.Values{"search_query": []string{query}}.Encode()
3131
res, err := GetRequest(YtURL + "results?" + params)
3232
if err != nil {

cmd/ytgo/ui.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ import (
55
"github.com/rivo/tview"
66
)
77

8-
func GetVideoFromMenu(vs *[]Video) (*Video, error) {
8+
func GetVideoFromMenu(vs []Video) (*Video, error) {
99
tview.Styles.PrimitiveBackgroundColor = tcell.ColorDefault
1010
app := tview.NewApplication()
1111
return getVideoFromList(app, vs)
1212
}
1313

14-
func getVideoFromList(app *tview.Application, vs *[]Video) (*Video, error) {
14+
func getVideoFromList(app *tview.Application, vs []Video) (*Video, error) {
1515
var selected *Video
1616

1717
list := tview.NewList()
@@ -27,7 +27,7 @@ func getVideoFromList(app *tview.Application, vs *[]Video) (*Video, error) {
2727
app.Stop()
2828
})
2929

30-
for i, v := range *vs {
30+
for i, v := range vs {
3131
list.AddItem(v.Title, v.Desc(), getShortcut(i), func() {
3232
selected = &v
3333
app.Stop()

cmd/ytgo/yt.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"github.com/ergochat/readline"
1212
)
1313

14-
const VERSION string = "v3.4.4"
14+
const VERSION string = "v3.4.5"
1515

1616
const (
1717
C_RED string = "\x1b[31m"

cmd/ytgo/yt_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func TestGetSearchResults(t *testing.T) {
103103
if err != nil {
104104
t.Error(err)
105105
}
106-
for _, v := range *v {
106+
for _, v := range v {
107107
testGottenVideo(&v, t)
108108
}
109109
}(q, t, &wg)

0 commit comments

Comments
 (0)