99 "strings"
1010)
1111
12+ type videoCache map [uint32 ][]Video // cache: content hash -> parsed videos
13+
1214type Playlist string
1315
1416func (p Playlist ) Create () error {
@@ -57,28 +59,14 @@ func (p Playlist) Create() error {
5759}
5860
5961func (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}
0 commit comments