-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.go
163 lines (133 loc) · 3.48 KB
/
parse.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
package treksum
import (
"net/http"
"strings"
"time"
"github.com/antchfx/xquery/html"
"go.uber.org/zap"
"golang.org/x/net/html"
)
var (
fmts = []string{
"2 Jan, 2006",
"2 Jan,2006",
"2 Jan 2006",
"2 Jan. 2006",
}
lineCorrections = map[string]string{
"\xe0": "a",
"\xe4": "a",
"\xe7": "c",
"\xe8": "e",
"\xe9": "e",
"\xea": "e",
"\xef": "i",
"\x91": "^",
"\x92": "'",
"\xdf": "ss",
}
)
// CleanUnicode replaces problematic characters from the Star Trek transcripts with suitable alternatives.
func CleanUnicode(in string) string {
for old, new := range lineCorrections {
in = strings.Replace(in, old, new, -1)
}
return in
}
// SwitchPage replaces the final "index.htm" portion of a URL (if present) with a new page.
func SwitchPage(url, page string) string {
return url[:strings.LastIndex(url, "/")+1] + page
}
// FindEpisodesLink searches a web page for a link to a list of episodes in a particular series and returns the URL for that page.
func FindEpisodesLink(url string) (next string, err error) {
var (
resp *http.Response
doc *html.Node
)
if resp, err = http.Get(url); err != nil {
return
}
defer resp.Body.Close()
if doc, err = htmlquery.Parse(resp.Body); err != nil {
return
}
a := htmlquery.FindOne(doc, "//a[contains(., 'Episode')]")
url = SwitchPage(url, htmlquery.SelectAttr(a, "href"))
return url, nil
}
// ParseEpisodeList parses out each episode from a series of tables on a page that lists all episodes for a specific TV
// series.
func ParseEpisodeList(log *zap.Logger, series *Series) (episodes []*Episode, err error) {
var (
resp *http.Response
doc *html.Node
episode *Episode
url string
season int
epNum int
)
log.Info("finding episodes")
if url, err = FindEpisodesLink(series.Url); err != nil {
return
}
if resp, err = http.Get(url); err != nil {
return
}
defer resp.Body.Close()
if doc, err = htmlquery.Parse(resp.Body); err != nil {
return
}
// everything EXCEPT Voyager uses the same nested table format
path := "//td/table"
if strings.Contains(url, "Voyager") {
path = "//div/table"
}
// find all tables with all episodes in a particular season
for _, table := range htmlquery.Find(doc, path) {
season++
epNum = 0
// find all cells in each season table
for _, td := range htmlquery.Find(table, "//tr/td") {
text := strings.TrimSpace(htmlquery.InnerText(td))
text = strings.Replace(text, "\n", " ", -1)
// ignore table headers
if strings.Contains(text, "Episode Name") {
continue
}
// make sure there's a link to the episode transcript
a := htmlquery.FindOne(td, "//a/@href")
if a != nil && episode == nil {
epNum++
episode = &Episode{
Series: series,
Season: season,
Episode: epNum,
Title: text,
Url: SwitchPage(series.Url, htmlquery.SelectAttr(a, "href")),
}
episode.Log = log.With(zap.String("episode", episode.GetAbbrev()))
// go to the next cell
continue
}
if episode != nil && episode.Airdate == nil {
// fix some dates
text = strings.Replace(text, "Sept", "Sep", -1)
// try matching against a series of date formats
for _, f := range fmts {
if t, err := time.Parse(f, text); err == nil {
episode.Airdate = &t
// found a matching date format
break
}
}
// skip if we don't have a valid airdate
if episode.Airdate == nil {
continue
}
episodes = append(episodes, episode)
episode = nil
}
}
}
return episodes, nil
}