-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
341 lines (319 loc) · 10.1 KB
/
main.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
package main
import (
"code.google.com/p/go.net/html"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"reflect"
"strings"
"time"
)
type Movie struct {
Name string
ObjectId string
URL *url.URL
Schedule []ScheduleItem
Pitch string
Programme string
Description string
}
type ScheduleItem struct {
DateStr string
Date string
TimeStr string
Venue string
VenueRoom string
QAndA bool
Premium bool
Press bool
School bool
Industry bool
}
type Movies struct {
Movies []*Movie
}
func (self Movies) String() string {
str := ""
for _, item := range self.Movies {
str = str + fmt.Sprintf("%v", item) + "\n"
}
return str
}
var ProgrammeUrls = []string{
"http://www.tiff.net/festivals/thefestival/programmes/future-projections",
"http://www.tiff.net/festivals/thefestival/programmes/tiff-docs",
"http://www.tiff.net/festivals/thefestival/programmes/discovery",
"http://www.tiff.net/festivals/thefestival/programmes/midnight-madness",
"http://www.tiff.net/festivals/thefestival/programmes/galapresentations",
"http://www.tiff.net/festivals/thefestival/programmes/masters",
"http://www.tiff.net/festivals/thefestival/programmes/specialpresentations",
"http://www.tiff.net/festivals/thefestival/programmes/mavericks",
"http://www.tiff.net/festivals/thefestival/programmes/contemporary-world-cinema",
"http://www.tiff.net/festivals/thefestival/programmes/contemporary-world-speakers",
"http://www.tiff.net/festivals/thefestival/programmes/wavelengths-all",
"http://www.tiff.net/festivals/thefestival/programmes/kids",
"http://www.tiff.net/festivals/thefestival/programmes/city-to-city",
"http://www.tiff.net/festivals/thefestival/programmes/short-cuts-canada",
"http://www.tiff.net/festivals/thefestival/programmes/short-cuts-international",
"http://www.tiff.net/festivals/thefestival/programmes/cinematheque",
"http://www.tiff.net/festivals/thefestival/programmes/vanguard",
"http://www.tiff.net/festivals/thefestival/programmes/next-wave",
"http://www.tiff.net/festivals/thefestival/programmes/special-events",
}
var ProgrammeNames = []string{
"future-projections",
"tiff-docs",
"discovery",
"midnight-madness",
"galapresentations",
"masters",
"specialpresentations",
"mavericks",
"contemporary-world-cinema",
"contemporary-world-speakers",
"wavelengths-all",
"kids",
"city-to-city",
"short-cuts-canada",
"short-cuts-international",
"cinematheque",
"vanguard",
"next-wave",
"special-events",
}
var CurrentMovie *Movie
var CurrentProgram string
var AllMovies Movies
func main() {
AllMovies.Movies = make([]*Movie, 0)
for index, programmeUrl := range ProgrammeUrls {
log.Printf("<-----Parsing Program: %v----->", programmeUrl)
CurrentProgram = ProgrammeNames[index]
ParseProgramme(programmeUrl)
}
jsonOutput, err := json.Marshal(&AllMovies)
if err != nil {
log.Fatalf("Error marshalling json: %v", err)
}
if err := ioutil.WriteFile("/tmp/Tiff2014.json", jsonOutput, 0775); err != nil {
log.Fatalf("Error writting file: %v", err)
}
log.Printf("Success")
}
func ParseProgramme(programmeUrl string) {
resp, err := http.Get(programmeUrl)
if err != nil {
log.Fatalf("Error getting programme: %v", err)
}
defer resp.Body.Close()
doc, err := html.Parse(resp.Body)
if err != nil {
log.Fatalf("Error parsing html: %v", err)
}
CurrentMovie = new(Movie)
ParseProgrammeHTML(doc)
}
func ParseMovie(movieUrl *url.URL) {
time.Sleep(5 * time.Second)
resp, err := http.Get(movieUrl.String())
if err != nil {
log.Fatalf("Error getting movie %v: %v", CurrentMovie.Name, err)
}
defer resp.Body.Close()
doc, err := html.Parse(resp.Body)
if err != nil {
log.Fatalf("Error parsing html: %v", err)
}
ParseMovieHTML(doc)
}
func ParseProgrammeHTML(n *html.Node) {
if n.Type == html.ElementNode && n.Data == "h1" {
h1Node := n.FirstChild
if !strings.Contains(h1Node.Data, "\n") {
CurrentMovie.Name = h1Node.Data
}
}
if n.Type == html.ElementNode && n.Data == "a" {
if url_suffix, ok := LinkIsListItem(n); ok {
var err error
CurrentMovie.URL, err = url.Parse("http://www.tiff.net" + url_suffix)
if err != nil {
log.Fatalf("Error while trying to parse URL: %v", err)
}
}
}
if CurrentMovie.Name != "" && CurrentMovie.URL != nil {
//We've got all we need here, parse the page for the Movie
log.Printf("Parsing details for %v (%v)", CurrentMovie.Name, CurrentMovie.URL.String())
ParseMovie(CurrentMovie.URL)
CurrentMovie.Programme = CurrentProgram
AllMovies.Movies = append(AllMovies.Movies, CurrentMovie)
log.Printf("Done with %v (%v)", CurrentMovie.Name, CurrentMovie.ObjectId)
CurrentMovie = new(Movie)
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
ParseProgrammeHTML(c)
}
}
func ParseMovieHTML(n *html.Node) {
if n.Type == html.ElementNode && n.Data == "script" {
scriptNode := n.FirstChild
if scriptNode != nil && strings.Contains(scriptNode.Data, "var objectId = '") {
indexOfObjectId := strings.Index(scriptNode.Data, "objectId") + 12
CurrentMovie.ObjectId = scriptNode.Data[indexOfObjectId : indexOfObjectId+10]
GetScreeningSchedule(CurrentMovie.ObjectId)
}
}
if n.Type == html.ElementNode && n.Data == "p" {
if pitch, ok := ParagraphIsAPitch(n); ok {
CurrentMovie.Pitch = pitch
}
if desc, ok := ParagraphIsADescription(n); ok {
CurrentMovie.Description = CurrentMovie.Description + "\n" + desc
}
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
ParseMovieHTML(c)
}
}
func GetScreeningSchedule(objectId string) {
screeningScheduleUrl := "http://tiff.net//ajax/whats-on-film/" + objectId
resp, err := http.Get(screeningScheduleUrl)
if err != nil {
log.Fatalf("Error getting movie screening schedule %v (%v): %v", CurrentMovie.Name, CurrentMovie.ObjectId, err)
}
defer resp.Body.Close()
jsonSchedule, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatalf("Error reading movie screening schedule %v (%v): %v", CurrentMovie.Name, CurrentMovie.ObjectId, err)
}
schedule := new(interface{})
if objectId == "2330049689" {
log.Printf("The JSON: %v", string(jsonSchedule))
}
if strings.Contains(string(jsonSchedule), "whats-on-film") {
log.Printf("Warning: %v (%v) does not have a schedule", CurrentMovie.Name, CurrentMovie.ObjectId)
} else {
if err := json.Unmarshal(jsonSchedule, schedule); err != nil {
log.Fatalf("Error unmarshalling schedule json: %v", err)
}
ParseSchedule(schedule)
}
}
func ParseSchedule(ptrToSchedule interface{}) {
// We know that this is a pointer to an interface,
// so get past the pointer
valueOfSchedule := reflect.ValueOf(ptrToSchedule).Elem()
// We now have an interface, so get past that
valueOfSchedule = valueOfSchedule.Elem()
// Now our value should be a map
// Get the schedule array ready
CurrentMovie.Schedule = make([]ScheduleItem, 0)
for _, dateKey := range valueOfSchedule.MapKeys() {
scheduleItem := valueOfSchedule.MapIndex(dateKey).Elem()
scheduleObject := new(ScheduleItem)
for _, itemKey := range scheduleItem.MapKeys() {
if itemKey.String() == "date" {
scheduleObject.DateStr = scheduleItem.MapIndex(itemKey).Elem().String()
} else if itemKey.String() == "eventformat" {
scheduleObject.Date = scheduleItem.MapIndex(itemKey).Elem().String()
} else if itemKey.String() == "timekeys" {
//This will be an array of structs
timeKeys := scheduleItem.MapIndex(itemKey).Elem()
for i := 0; i < timeKeys.Len(); i++ {
timeItem := timeKeys.Index(i).Elem()
for _, itemKey := range timeItem.MapKeys() {
if itemKey.String() == "starttime" {
scheduleObject.TimeStr = timeItem.MapIndex(itemKey).Elem().String()
} else if itemKey.String() == "venue_name" {
scheduleObject.Venue = timeItem.MapIndex(itemKey).Elem().String()
} else if itemKey.String() == "room_name" {
scheduleObject.VenueRoom = timeItem.MapIndex(itemKey).Elem().String()
} else if itemKey.String() == "extended_q_and_a" {
if timeItem.MapIndex(itemKey).Elem().String() == "0" {
scheduleObject.QAndA = false
} else {
scheduleObject.QAndA = true
}
} else if itemKey.String() == "premium" {
if timeItem.MapIndex(itemKey).Elem().String() == "0" {
scheduleObject.Premium = false
} else {
scheduleObject.Premium = true
}
} else if itemKey.String() == "press" {
if timeItem.MapIndex(itemKey).Elem().String() == "0" {
scheduleObject.Press = false
} else {
scheduleObject.Press = true
}
} else if itemKey.String() == "industry" {
if timeItem.MapIndex(itemKey).Elem().String() == "0" {
scheduleObject.Industry = false
} else {
scheduleObject.Industry = true
}
} else if itemKey.String() == "school" {
if timeItem.MapIndex(itemKey).Elem().String() == "0" {
scheduleObject.School = false
} else {
scheduleObject.School = true
}
}
}
}
}
}
CurrentMovie.Schedule = append(CurrentMovie.Schedule, *scheduleObject)
}
}
func LinkIsListItem(n *html.Node) (string, bool) {
href := ""
isListItem := false
for _, value := range n.Attr {
if value.Key == "class" && strings.Contains(value.Val, "list-item") {
isListItem = true
} else if value.Key == "href" {
href = value.Val
}
}
return href, isListItem
}
func ParagraphIsAPitch(n *html.Node) (string, bool) {
for _, value := range n.Attr {
if value.Key == "class" && strings.Contains(value.Val, "pitch") {
if n.FirstChild != nil {
return n.FirstChild.Data, true
}
}
}
return "", false
}
func ParagraphIsADescription(n *html.Node) (string, bool) {
parent := n.Parent
var grandparent *html.Node
if parent != nil {
grandparent = parent.Parent
}
if grandparent != nil {
desc := ""
for _, value := range grandparent.Attr {
if value.Key == "class" && strings.Contains(value.Val, "film-note") {
for c := n.FirstChild; c != nil; c = c.NextSibling {
if c.Type == html.TextNode {
desc = desc + c.Data
} else if c.Type == html.ElementNode {
if c.FirstChild != nil {
desc = desc + c.FirstChild.Data
}
}
}
return desc, true
}
}
}
return "", false
}