-
Notifications
You must be signed in to change notification settings - Fork 0
/
go-tvdb-cli.go
147 lines (130 loc) · 3.15 KB
/
go-tvdb-cli.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package main
import (
"bytes"
"fmt"
"log"
"os"
"text/template"
"time"
"github.com/codegangsta/cli"
"github.com/nsf/termbox-go"
"github.com/yanfali/go-tvdb"
)
var (
RowTemplate = `{{.SeriesName | printf "%-40s"}} {{.Language | printf "%-10s"}} {{.FirstAired | printf "%-15s"}} {{.Genre}}`
RowCompiled = template.Must(template.New("row").Parse(RowTemplate))
config tvdb.TvdbConfig
)
func ellipsisString(source string, length int) string {
if len(source) > length {
return fmt.Sprintf("%."+string(length-4)+"s...", source)
}
return source
}
func printSeries(series *tvdb.Series) string {
w := bytes.NewBuffer([]byte{})
series.SeriesName = ellipsisString(series.SeriesName, 40)
err := RowCompiled.Execute(w, series)
if err != nil {
panic(err)
}
return w.String()
}
func init() {
cli.AppHelpTemplate = `NAME:
{{.Name}} - {{.Usage}}
USAGE:
{{.Name}} {{if .Flags}}[global options] {{end}} "search title"
VERSION:
{{.Version}}{{if or .Author .Email}}
AUTHOR:{{if .Author}}
{{.Author}}{{if .Email}} - <{{.Email}}>{{end}}{{else}}
{{.Email}}{{end}}{{end}}
COMMANDS:
{{range .Commands}}{{.Name}}{{with .ShortName}}, {{.}}{{end}}{{ "\t" }}{{.Usage}}
{{end}}{{if .Flags}}
GLOBAL OPTIONS:
{{range .Flags}}{{.}}
{{end}}{{end}}
`
}
func main() {
app := cli.NewApp()
app.Name = "go-tvdb-cli"
app.Usage = "make CLI queries against the thetvdb.com"
app.Flags = []cli.Flag{
cli.IntFlag{
Name: "max-results, m",
Value: 10,
Usage: "Maximum Number of Results to Show",
},
cli.StringFlag{
Name: "apikey, k",
Value: "90CCCAB7A2B7509E",
Usage: "thetvdb.com API key",
},
cli.StringFlag{
Name: "language, l",
Value: "en",
Usage: "Default Language to search using",
},
}
app.Action = func(c *cli.Context) {
if len(c.Args()) == 0 {
fmt.Printf("Error: Not enough parameters\n")
cli.ShowAppHelp(c)
return
}
config = tvdb.NewDefaultTvdbConfig()
if apiKey := c.String("apikey"); apiKey != "" {
log.Printf("Using APIKEY %q\n", apiKey)
config.ApiKey = apiKey
}
if lang := c.String("language"); lang != "en" {
log.Printf("Using Language %q", lang)
config.Language = lang
}
myTvdb := tvdb.NewTvdbWithConfig(config)
results, err := myTvdb.SearchSeries(c.Args()[0], c.Int("max-results"))
if err != nil {
fmt.Errorf("error", err)
}
var tx = &termboxState{results: &results}
tx.consoleFn = func(*termboxState) string {
return fmt.Sprintf("Displaying %d results out of %d", len(results.Series), c.Int("max-results"))
}
if len(results.Series) == 0 {
log.Printf("No results found for %q", c.Args()[0])
return
}
if err := termbox.Init(); err != nil {
panic(err)
}
defer termbox.Close()
updateScreen(tx, drawSeries)
currentState := SeriesEventHandler
ch := getPollEventChan()
loop:
for {
select {
case tx.ev = <-ch:
if currentState = currentState(tx); currentState == nil {
break loop
}
case <-time.After(time.Second / 2):
cursorBlink(tx)
termbox.Flush()
}
}
}
app.Run(os.Args)
}
func getPollEventChan() chan termbox.Event {
var ch = make(chan termbox.Event)
go func() {
for {
ch <- termbox.PollEvent()
}
}()
return ch
}