-
Notifications
You must be signed in to change notification settings - Fork 3
/
example_test.go
141 lines (131 loc) · 3.94 KB
/
example_test.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
package tvdb_test
import (
"fmt"
"net/url"
"os"
"sort"
"github.com/pioz/tvdb"
)
func ExampleClient_Login() {
c := tvdb.Client{Apikey: os.Getenv("TVDB_APIKEY"), Username: os.Getenv("TVDB_USERNAME"), Userkey: os.Getenv("TVDB_USERKEY"), Language: "en"}
err := c.Login()
if err != nil {
panic(err)
}
}
func ExampleClient_SearchByName() {
c := tvdb.Client{Apikey: os.Getenv("TVDB_APIKEY"), Username: os.Getenv("TVDB_USERNAME"), Userkey: os.Getenv("TVDB_USERKEY"), Language: "en"}
err := c.Login()
if err != nil {
panic(err)
}
res, err := c.SearchByName("Game of Thrones") //Multiple hits and the list is not necessarily ordered to support this test
if err != nil {
panic(err)
}
fmt.Println(res[0].SeriesName)
// Output: Game of Thrones: Cartoon Parody
}
func ExampleClient_BestSearch() {
c := tvdb.Client{Apikey: os.Getenv("TVDB_APIKEY"), Username: os.Getenv("TVDB_USERNAME"), Userkey: os.Getenv("TVDB_USERKEY"), Language: "en"}
err := c.Login()
if err != nil {
panic(err)
}
series, err := c.BestSearch("Game of Thrones")
if err != nil {
// The request response is a 404: this means no results have been found
if tvdb.HaveCodeError(404, err) {
fmt.Println("Series not found")
} else {
panic(err)
}
}
fmt.Println(series.SeriesName)
// Output: Game of Thrones
}
func ExampleClient_GetSeriesEpisodes() {
c := tvdb.Client{Apikey: os.Getenv("TVDB_APIKEY"), Username: os.Getenv("TVDB_USERNAME"), Userkey: os.Getenv("TVDB_USERKEY"), Language: "en"}
err := c.Login()
if err != nil {
panic(err)
}
series, err := c.BestSearch("Game of Thrones")
if err != nil {
panic(err)
}
err = c.GetSeriesEpisodes(&series, url.Values{"airedSeason": {"2"}}) // params can be nil to retrieve all episodes
if err != nil {
panic(err)
}
fmt.Println(series.GetEpisode(2, 10).EpisodeName)
// Output: Valar Morghulis
}
func ExampleClient_GetSeriesFanartImages() {
c := tvdb.Client{Apikey: os.Getenv("TVDB_APIKEY"), Username: os.Getenv("TVDB_USERNAME"), Userkey: os.Getenv("TVDB_USERKEY"), Language: "en"}
err := c.Login()
if err != nil {
panic(err)
}
series, err := c.BestSearch("Game of Thrones")
if err != nil {
panic(err)
}
err = c.GetSeriesFanartImages(&series)
if err != nil {
panic(err)
}
url := tvdb.ImageURL(series.Images[0].FileName)
fmt.Println(url)
// Output: https://thetvdb.com/banners/fanart/original/121361-3.jpg
}
func ExampleSeries_GetSeasonEpisodes() {
c := tvdb.Client{Apikey: os.Getenv("TVDB_APIKEY"), Username: os.Getenv("TVDB_USERNAME"), Userkey: os.Getenv("TVDB_USERKEY"), Language: "en"}
err := c.Login()
if err != nil {
panic(err)
}
series, err := c.BestSearch("Game of Thrones")
if err != nil {
panic(err)
}
err = c.GetSeriesEpisodes(&series, nil)
if err != nil {
panic(err)
}
episodes := series.GetSeasonEpisodes(1)
sort.Slice(episodes, func(i, j int) bool {
return episodes[i].AiredEpisodeNumber < episodes[j].AiredEpisodeNumber
})
for _, ep := range episodes {
fmt.Printf("1x%02d: %s - ", ep.AiredEpisodeNumber, ep.EpisodeName)
}
// Output: 1x01: Winter Is Coming - 1x02: The Kingsroad - 1x03: Lord Snow - 1x04: Cripples, Bastards, and Broken Things - 1x05: The Wolf and the Lion - 1x06: A Golden Crown - 1x07: You Win or You Die - 1x08: The Pointy End - 1x09: Baelor - 1x10: Fire and Blood -
}
func ExampleSeries_GetEpisode() {
c := tvdb.Client{Apikey: os.Getenv("TVDB_APIKEY"), Username: os.Getenv("TVDB_USERNAME"), Userkey: os.Getenv("TVDB_USERKEY"), Language: "en"}
err := c.Login()
if err != nil {
panic(err)
}
series, err := c.BestSearch("Game of Thrones")
if err != nil {
panic(err)
}
err = c.GetSeriesEpisodes(&series, nil)
if err != nil {
panic(err)
}
// Print the title of the episode 4x08 (season 4, episode 8)
fmt.Println(series.GetEpisode(4, 8).EpisodeName)
// Output: The Mountain and the Viper
}
func ExampleHaveCodeError() {
c := tvdb.Client{Apikey: "WRONG APIKEY"}
err := c.Login()
if err == nil {
panic("Impossible!")
}
fmt.Println(tvdb.HaveCodeError(401, err))
// Output: true
}