forked from kkdai/youtube
-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.go
75 lines (65 loc) · 1.56 KB
/
list.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package main
import (
"fmt"
"io"
"os"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
)
type PlaylistInfo struct {
Title string
Author string
Videos []VideoInfo
}
var (
// listCmd represents the list command
listCmd = &cobra.Command{
Use: "list",
Short: "Print metadata of the desired playlist",
Args: cobra.ExactArgs(1),
PreRunE: func(_ *cobra.Command, _ []string) error {
return checkOutputFormat()
},
Run: func(_ *cobra.Command, args []string) {
playlist, err := getDownloader().GetPlaylist(args[0])
exitOnError(err)
playlistInfo := PlaylistInfo{
Title: playlist.Title,
Author: playlist.Author,
}
for _, v := range playlist.Videos {
playlistInfo.Videos = append(playlistInfo.Videos, VideoInfo{
ID: v.ID,
Title: v.Title,
Author: v.Author,
Duration: v.Duration.String(),
})
}
exitOnError(writeOutput(os.Stdout, &playlistInfo, func(w io.Writer) {
writePlaylistOutput(w, &playlistInfo)
}))
},
}
)
func writePlaylistOutput(w io.Writer, info *PlaylistInfo) {
fmt.Println("Title: ", info.Title)
fmt.Println("Author: ", info.Author)
fmt.Println("# Videos: ", len(info.Videos))
fmt.Println()
table := tablewriter.NewWriter(w)
table.SetAutoWrapText(false)
table.SetHeader([]string{"ID", "Author", "Title", "Duration"})
for _, vid := range info.Videos {
table.Append([]string{
vid.ID,
vid.Author,
vid.Title,
fmt.Sprintf("%v", vid.Duration),
})
}
table.Render()
}
func init() {
rootCmd.AddCommand(listCmd)
addFormatFlag(listCmd.Flags())
}