forked from purdyk/tvdb
/
next.go
61 lines (45 loc) · 1.07 KB
/
next.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
package main
import (
"fmt"
"strconv"
"gopkg.in/urfave/cli.v2"
"github.com/purdyk/tvdb"
)
// Finds the next episode of a show by ID
func Next(c *cli.Context) error {
var idString string = c.Args().First()
id, err := strconv.ParseInt(idString, 10, 64)
if err != nil {
return err
}
if id == 0 {
return fmt.Errorf("Id parameter is required")
}
series, err := client.Series.Get(int32(id))
if series.Status != "Continuing" {
fmt.Fprintf(c.App.Writer, "Series has ended")
}
links := &tvdb.Links{}
links.Next = 1
params := &tvdb.EpisodeSearchParams{}
for {
page := fmt.Sprintf("%d", links.Next)
params.Page = &page
results, err := client.Episodes.ListEpisodes(int32(id), params)
if err != nil {
return err
}
for _, ep := range results.Data {
if (ep.IsInFuture()) {
fmt.Fprintf(c.App.Writer, "Next Episode\n\t%s\n\t%s at %s\n", ep.EpisodeName, ep.FirstAired, series.AirsTime)
return nil
}
}
links := results.Links
if !links.HasNext() {
break
}
}
fmt.Fprintf(c.App.Writer, "Couldn't find a next episode")
return nil
}