-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathseason.go
126 lines (117 loc) · 3.45 KB
/
season.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
package imdb
import (
"encoding/json"
"errors"
"fmt"
"html"
"io"
"net/http"
"regexp"
"strconv"
)
// Episode represents a single episode from a TV series.
type Episode struct {
SeasonNumber, EpisodeNumber int
ImageURL, ID, Name string
}
// Season represents a season from a TV Series, with a list of episodes.
type Season struct {
ID string `json:",omitempty"`
SeasonNumber int `json:",omitempty"`
Episodes []Episode `json:",omitempty"`
}
const seasonURL = "https://www.imdb.com/title/%s/episodes/?season=%d"
// NewSeason gets, parses and returns a Season by its ID and season number.
func NewSeason(c *http.Client, id string, season int) (*Season, error) {
if !ttRE.MatchString(id) {
return nil, ErrInvalidID
}
resp, err := c.Get(fmt.Sprintf(seasonURL, id, season))
if err != nil {
return nil, err
}
defer func(Body io.ReadCloser) {
_ = Body.Close()
}(resp.Body)
if resp.StatusCode != http.StatusOK {
if resp.StatusCode == http.StatusForbidden {
return nil, errors.New("forbidden (e.g. denied by AWS WAF)")
}
return nil, fmt.Errorf("imdb: status not ok: %v", resp.Status)
}
page, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
s := Season{
ID: id,
SeasonNumber: season,
}
if err := s.Parse(c, page); err != nil {
return nil, err
}
return &s, nil
}
var (
seasonInfoJSONRE = regexp.MustCompile(`],("episodes":.*}),"currentSeason`)
)
// Parse parses a Season from its page.
func (s *Season) Parse(c *http.Client, page []byte) error {
episodesMatch := seasonInfoJSONRE.FindSubmatch(page)
jsonData := "{" + html.UnescapeString(string(episodesMatch[1])) + "}"
var r struct {
Episodes struct {
Items []struct {
ID string `json:"id"`
Type string `json:"type"`
Season string `json:"season"`
Episode string `json:"episode"`
TitleText string `json:"titleText"`
ReleaseDate struct {
Month int `json:"month"`
Day int `json:"day"`
Year int `json:"year"`
Typename string `json:"__typename"`
} `json:"releaseDate"`
ReleaseYear int `json:"releaseYear"`
Image struct {
URL string `json:"url"`
MaxHeight int `json:"maxHeight"`
MaxWidth int `json:"maxWidth"`
Caption string `json:"caption"`
} `json:"image"`
Plot string `json:"plot"`
AggregateRating float64 `json:"aggregateRating"`
VoteCount int `json:"voteCount"`
CanRate bool `json:"canRate"`
ContributionURL string `json:"ContributionUrl"`
} `json:"items"`
Total int `json:"total"`
HasNextPage bool `json:"hasNextPage"`
EndCursor string `json:"endCursor"`
HasRatedEpisode bool `json:"hasRatedEpisode"`
} `json:"episodes"`
}
if err := json.Unmarshal([]byte(jsonData), &r); err != nil {
return err
}
for _, e := range r.Episodes.Items {
if n, err := strconv.Atoi(e.Season); err != nil {
return fmt.Errorf("Season(%v, %v): atoi(season %v): %v", s.ID, s.SeasonNumber, e.Season, err)
} else if n != s.SeasonNumber {
continue
}
episodeNumber, err := strconv.Atoi(e.Episode)
if err != nil {
return fmt.Errorf("Season(%v, %v): atoi(episode %v): %v", s.ID, s.SeasonNumber, e.Episode, err)
}
s.Episodes = append(s.Episodes, Episode{
ID: e.ID,
SeasonNumber: s.SeasonNumber,
EpisodeNumber: episodeNumber,
ImageURL: e.Image.URL,
Name: e.TitleText,
})
}
return nil
}