-
Notifications
You must be signed in to change notification settings - Fork 4
/
episode.go
200 lines (176 loc) · 4.23 KB
/
episode.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package misc
import (
"fmt"
"math"
"strconv"
"strings"
)
type EpisodeContainer interface {
// Returns true if this EpisodeContainer is equivalent or a superset of the given EpisodeContainer
ContainsEpisodes(EpisodeContainer) bool
// Returns a channel meant for iterating with for/range.
// Sends all contained episodes in order.
Episodes() chan Episode
}
type Formatter interface {
// Returns a string where the number portion is 0-padded to fit 'width' digits
Format(width int) string
// Returns a string where the number portion is 0-padded to be the same length
// as max.
FormatLog(max int) string
}
type EpisodeType int
const (
EpisodeTypeRegular = EpisodeType(1 + iota)
EpisodeTypeSpecial // "S" episode
EpisodeTypeCredits // "C" episode
EpisodeTypeTrailer // "T" episode
EpisodeTypeParody // "P" episode
EpisodeTypeOther // "O" episode
)
func parseEpisodeType(typ string) EpisodeType {
switch typ {
case "":
return EpisodeTypeRegular
case "S":
return EpisodeTypeSpecial
case "C":
return EpisodeTypeCredits
case "T":
return EpisodeTypeTrailer
case "P":
return EpisodeTypeParody
case "O":
return EpisodeTypeOther
}
return 0
}
func (et EpisodeType) String() string {
switch et {
case EpisodeTypeRegular:
return ""
case EpisodeTypeSpecial:
return "S"
case EpisodeTypeCredits:
return "C"
case EpisodeTypeTrailer:
return "T"
case EpisodeTypeParody:
return "P"
case EpisodeTypeOther:
return "O"
default:
return "!"
}
}
// An episode (duh).
type Episode struct {
Type EpisodeType
Number int
Part int
Parts int
}
// returns how many digits are needed to represent this int
func scale(i int) int {
return 1 + int(math.Floor(math.Log10(float64(i))))
}
// Converts the Episode into AniDB API episode format.
func (ep Episode) String() string {
return ep.Format(1)
}
// returns how many digits are needed to represent this episode
func (ep *Episode) scale() int {
if ep == nil {
return 1
}
return scale(ep.Number)
}
func (ep *Episode) Episodes() chan Episode {
ch := make(chan Episode, 1)
if ep != nil {
ch <- *ep
}
close(ch)
return ch
}
// Returns true if ec is an Episode and is identical to this episode,
// or if ec is a single episode EpisodeRange / EpisodeList that
// contain only this episode.
func (ep *Episode) ContainsEpisodes(ec EpisodeContainer) bool {
switch e := ec.(type) {
case *Episode:
if ep == nil {
return false
}
basic := ep.Type == e.Type && ep.Number == e.Number
if ep.Part < 0 { // a whole episode contains any partial episodes
return basic
}
return basic && ep.Part == e.Part
case *EpisodeRange:
case *EpisodeList:
return EpisodeList{&EpisodeRange{Type: ep.Type, Start: ep, End: ep}}.ContainsEpisodes(ep)
default:
}
return false
}
func (ep Episode) Format(width int) string {
if ep.Part < 0 { // whole episode
return fmt.Sprintf("%s%0"+strconv.Itoa(width)+"d", ep.Type, ep.Number)
}
if ep.Parts != 0 { // part X of Y
frac := float64(ep.Number) + float64(ep.Part)/float64(ep.Parts)
return fmt.Sprintf("%s%0"+strconv.Itoa(width)+".2f", ep.Type, frac)
}
// part N
return fmt.Sprintf("%s%0"+strconv.Itoa(width)+"d.%d", ep.Type, ep.Number, ep.Part)
}
func (ep *Episode) FormatLog(max int) string {
return ep.Format(scale(max))
}
func (ep *Episode) IncPart() {
if ep.Parts > 0 && ep.Part == ep.Parts-1 {
ep.IncNumber()
} else {
ep.Part++
}
}
func (ep *Episode) IncNumber() {
ep.Part = -1
ep.Parts = 0
ep.Number++
}
func (ep *Episode) DecPart() {
if ep.Part > 0 {
ep.Part--
} else {
ep.DecNumber()
}
}
func (ep *Episode) DecNumber() {
ep.Part = -1
ep.Parts = 0
ep.Number--
}
// Parses a string in the usual AniDB API episode format and converts into
// an Episode.
func ParseEpisode(s string) *Episode {
p := int64(-1)
parts := strings.Split(s, ".")
switch len(parts) {
case 1: // no worries
case 2:
s = parts[0]
p, _ = strconv.ParseInt(parts[1], 10, 32)
default: // too many dots
return nil
}
if no, err := strconv.ParseInt(s, 10, 32); err == nil {
return &Episode{Type: EpisodeTypeRegular, Number: int(no), Part: int(p)}
} else if len(s) < 1 {
// s too short
} else if no, err = strconv.ParseInt(s[1:], 10, 30); err == nil {
return &Episode{Type: parseEpisodeType(s[:1]), Number: int(no), Part: int(p)}
}
return nil
}