forked from 0x263b/Porygon2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tvmaze.go
98 lines (87 loc) · 2.08 KB
/
tvmaze.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
package tvmaze
import (
"fmt"
"github.com/Seventy-Two/Cara"
"github.com/Seventy-Two/Cara/web"
"net/url"
)
const (
tvMazeURL = "http://api.tvmaze.com/singlesearch/shows?q=%s"
)
type Showinfo struct {
Name string `json:"name"`
Status string `json:"status"`
Schedule struct {
Time string `json:"time"`
Days []string `json:"days"`
} `json:"schedule"`
Network struct {
Name string `json:"name"`
} `json:"network"`
Links struct {
Self struct {
Href string `json:"href"`
} `json:"self"`
Previousepisode struct {
Href string `json:"href"`
} `json:"previousepisode"`
Nextepisode struct {
Href string `json:"href"`
} `json:"nextepisode"`
} `json:"_links"`
}
type Nextepisode struct {
Season int `json:"season"`
Number int `json:"number"`
Airdate string `json:"airdate"`
Airtime string `json:"airtime"`
}
func tvmaze(command *bot.Cmd, matches []string) (msg string, err error) {
results := &Showinfo{}
err = web.GetJSON(fmt.Sprintf(tvMazeURL, url.QueryEscape(matches[1])), results)
if err != nil {
return "TVmaze | Could not find show", nil
}
if len(results.Links.Nextepisode.Href) != 0 {
next := &Nextepisode{}
err = web.GetJSON(results.Links.Nextepisode.Href, next)
if err != nil {
return "TVmaze | Could not find show", nil
}
if len(results.Schedule.Days) == 0 {
results.Schedule.Days = []string{"???"}
}
output := fmt.Sprintf("TVmaze | %s | Airtime: %s %s on %s | Status: %s | Next Ep: S%vE%v at %s %s",
results.Name,
results.Schedule.Days[0],
results.Schedule.Time,
results.Network.Name,
results.Status,
next.Season,
next.Number,
next.Airtime,
next.Airdate,
)
return output, nil
}
if len(results.Schedule.Days) == 0 {
output := fmt.Sprintf("TVmaze | %s | Status: %s",
results.Name,
results.Status,
)
return output, nil
}
output := fmt.Sprintf("TVmaze | %s | Airtime: %s %s on %s | Status: %s",
results.Name,
results.Schedule.Days[0],
results.Schedule.Time,
results.Network.Name,
results.Status,
)
return output, nil
}
func init() {
bot.RegisterCommand(
"^tv (.+)$",
tvmaze)
}