-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtvdb.go
292 lines (254 loc) · 8.27 KB
/
tvdb.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package main
import (
"errors"
"html"
"io/ioutil"
"log"
"net/http"
"net/url"
"regexp"
"strconv"
"strings"
"github.com/xrash/smetrics"
)
const baseURL = "https://www.thetvdb.com"
const posterBaseURL = "https://www.thetvdb.com/banners/posters/"
func chop(str string, start string, end string) string {
indexA := strings.Index(str, start)
if indexA < 0 {
return ""
}
remainder := str[indexA+len(start):]
indexB := strings.Index(remainder, end)
if indexB < 0 {
return ""
}
return remainder[:indexB]
}
/// Finds the last occurrence of 'start' and return the text after it.
func chopLast(str string, start string) string {
index := strings.LastIndex(str, start)
if index < 0 {
return ""
}
return str[index+len(start):]
}
/// Finds the first occurrence of 'start' and return the text after it.
func chopFirst(str string, start string) string {
index := strings.Index(str, start)
if index < 0 {
return ""
}
return str[index+len(start):]
}
func searchUrl(name string) string {
u, _ := url.Parse(baseURL + "/search") // Assume it'll work.
values := url.Values{}
values.Add("q", name)
u.RawQuery = values.Encode()
return u.String()
}
func get(url string) string {
resp, respErr := http.Get(url)
if respErr != nil {
return ""
}
defer resp.Body.Close()
body, bodyErr := ioutil.ReadAll(resp.Body)
if bodyErr != nil {
return ""
}
return string(body)
}
/// Returns the show url slug / id. "" if can't find. eg 'i-dream-of-jeannie'.
func tvdbSearchForSeries(name string) string {
resp := get(searchUrl(name))
results := chop(resp, "<h2>Search Results</h2>", "</table>")
regex := regexp.MustCompile(`<a href="/series/(.*?)">(.*?)</a>`)
matches := regex.FindAllStringSubmatch(results, -1)
closestDistance := 99999
var closestSlug string
log.Println("tvdbSearchForSeries got # results:", len(matches))
for _, match := range matches {
if len(match) >= 3 {
slug := match[1]
thisName := match[2]
thisDistance := smetrics.WagnerFischer(
strings.ToLower(thisName),
strings.ToLower(name),
1, 3, 2)
log.Println("Possibility, id:", slug, ", name:", thisName, "; score:", thisDistance)
if thisDistance < closestDistance {
closestDistance = thisDistance
closestSlug = slug
}
}
}
log.Println("Best match", closestSlug)
return closestSlug
}
func unescapeTrim(str string) string {
return strings.TrimSpace(html.UnescapeString(str))
}
// Some of these fields below have json names for compatability with old TMDB metadata.
type TVDBSeries struct {
TVDBID string // Eg 'i-dream-of-jeannie' - thetvdb url slug / id
Name string
Overview string
Art string // The 16:9 background full url.
Poster string // The portrait dvd cover full url.
FirstAirDate string `json:"first_air_date"`
Seasons []TVDBSeason
}
type TVDBSeason struct {
TVDBID int // Eg 1 - the season number (old tvdb used to make this different from season number)
Season int `json:"season_number"` // Eg 1,2,3, or 0 for specials
Name string // Eg 'Specials' or 'Season 1'
/// Following are only filled in when requesting season details.
Episodes []TVDBEpisode
Image string // Full url.
}
/// Finds all the seasons in an html for a tv series details page.
func seasonsForResponse(resp string) []TVDBSeason {
section := chop(resp, "<h2>Seasons</h2>", "</div>")
regex := regexp.MustCompile(`(?s)seasons/([0-9]+)">(.*?)</a>`) // s flag means . can span newlines.
seasons := make([]TVDBSeason, 0)
matches := regex.FindAllStringSubmatch(section, -1)
for _, match := range matches {
if len(match) >= 3 {
number, _ := strconv.Atoi(match[1])
name := unescapeTrim(match[2])
seasons = append(seasons, TVDBSeason{
TVDBID: number,
Season: number,
Name: name,
})
}
}
return seasons
}
func tvdbSeriesDetails(id string) (TVDBSeries, error) {
seriesUrl := baseURL + "/series/" + id
resp := get(seriesUrl)
// Name and details.
nameRaw := chop(resp, "<title>", " @ TheTVDB</title>")
name := unescapeTrim(nameRaw)
detailsArea := chop(resp, `<div class="row">`, "</div>")
detailsRaw := chop(detailsArea, `<p>`, `</p>`)
details := unescapeTrim(detailsRaw)
// The 16:9 background.
artArea := chop(resp, "<h2>Backgrounds (Fan Art)</h2>", "</div>")
artURL := chop(artArea, `src="`, `"`)
// The portrait dvd cover.
postersArea := chop(resp, "<h2>Posters</h2>", "</div>")
posterURL := chop(postersArea, `src="`, `"`)
firstAiredArea := chop(resp, `First Aired`, `</li>`)
firstAirDate := chop(firstAiredArea, `<span>`, `</span>`)
// The seasons.
seasons := seasonsForResponse(resp)
if name == "" || len(seasons) == 0 {
return TVDBSeries{}, errors.New("Could not find series")
}
series := TVDBSeries{
TVDBID: id,
Name: name,
Overview: details,
Art: artURL,
Poster: posterURL,
FirstAirDate: firstAirDate,
Seasons: seasons,
}
return series, nil
}
type TVDBEpisode struct {
TVDBID int
SeasonNumber int
Episode int `json:"episode_number"`
Name string
AirDate string `json:"air_date"`
Image string // full url.
// Following are empty until you ask for episode details.
Overview string
}
/// Extracts the episodes from the season screen.
func episodesForSeasonDetails(resp string, seasonNumber int) []TVDBEpisode {
section := chop(resp, `<div class="col-xs-12 col-sm-8 episodes">`, `</table>`)
regex := regexp.MustCompile(`(?s)<tr>.*?/episodes/([0-9]+)">.*?([0-9]+).*?<span.*?>(.*?)</span>.*?<td>.*?([0-9][0-9])/([0-9][0-9])/([0-9][0-9][0-9][0-9]).*?</td>.*?<td>(.*?)</td>.*?</tr>`)
episodes := make([]TVDBEpisode, 0)
matches := regex.FindAllStringSubmatch(section, -1)
for _, match := range matches {
if len(match) >= 8 {
id, _ := strconv.Atoi(match[1])
number, _ := strconv.Atoi(match[2])
name := unescapeTrim(match[3])
mm := match[4] // mm
dd := match[5] // dd
yyyy := match[6] // yyyy
date := yyyy + "-" + mm + "-" + dd
image := chop(match[7], `data-featherlight="`, `"`)
if name != "" && number > 0 && id > 0 {
episodes = append(episodes, TVDBEpisode{
TVDBID: id,
SeasonNumber: seasonNumber,
Episode: number,
Name: name,
AirDate: date,
Image: image,
})
}
}
}
return episodes
}
/// Get the episodes in a season.
func tvdbSeasonDetails(seriesid string, seasonId int, seasonNumber int) (TVDBSeason, error) {
seasonUrl := baseURL + "/series/" + seriesid + "/seasons/" + strconv.Itoa(seasonId)
log.Println("tvdbSeasonDetails: ", seasonUrl)
resp := get(seasonUrl)
titleSection := chop(resp, `<title>`, `</title>`)
titleRaw := chop(titleSection, ` - `, ` @ TheTVDB`)
title := unescapeTrim(titleRaw) // Eg 'Season 2'
postersSection := chop(resp, `<h2>Posters</h2>`, `</div>`)
posterURL := chop(postersSection, `src="`, `"`)
episodes := episodesForSeasonDetails(resp, seasonNumber)
if title == "" {
return TVDBSeason{}, errors.New("Could not find season")
}
season := TVDBSeason{
TVDBID: seasonId,
Season: seasonNumber,
Name: title,
Image: posterURL,
Episodes: episodes,
}
return season, nil
}
func tvdbEpisodeDetails(seriesId string, seasonId int, seasonNumber int, episodeid int) (TVDBEpisode, error) {
episodeUrl := baseURL + "/series/" + seriesId + "/episodes/" + strconv.Itoa(episodeid)
resp := get(episodeUrl)
episodeNumberArea := chop(resp, `<strong>Episode Number</strong>`, `</li>`)
episodeNumberRaw := chop(episodeNumberArea, `<span>`, `</span>`)
episodeNumber, _ := strconv.Atoi(episodeNumberRaw)
titleArea := chop(resp, `<title>`, `</title>`)
titleRaw := chop(titleArea, ` - `, ` @ TheTVDB`)
title := unescapeTrim(titleRaw)
airArea := chop(resp, `<strong>Originally Aired</strong>`, `</li>`)
airDate := chop(airArea, `<span>`, `</span>`) // Eg Saturday, September 18, 1965
overviewArea := chop(resp, `<div class="block" id="translations">`, `</div>`)
overview := unescapeTrim(chop(overviewArea, `<p>`, `</p>`))
imageArea := chop(resp, `<div class="screenshot">`, `</div>`)
image := chop(imageArea, `src="`, `"`)
if title == "" || episodeNumber <= 0 {
return TVDBEpisode{}, errors.New("Could not find episode")
}
episode := TVDBEpisode{
TVDBID: episodeid,
SeasonNumber: seasonNumber,
Episode: episodeNumber,
Name: title,
AirDate: airDate,
Overview: overview,
Image: image,
}
return episode, nil
}