This repository has been archived by the owner on May 9, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
getStudentList.go
79 lines (66 loc) · 1.74 KB
/
getStudentList.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
package goil
import (
"encoding/json"
"fmt"
"github.com/PuerkitoBio/goquery"
"github.com/gocarina/gocsv"
"io"
)
// Return list of every student with data
func (s *Session) GetStudentList() (StudentList, error) {
body, err := s.getStudentsListPage()
if err != nil {
return StudentList{}, err
}
links, err := extractLinksToStudentPages(body)
if err != nil {
return StudentList{}, err
}
body.Close() // Closing the open body
studentList := make(StudentList, len(links))
for i, k := range links {
student, err := s.GetStudentByLink(k)
if err != nil {
fmt.Printf("ERROR: %v", err.Error())
}
studentList[i] = student
}
return studentList, err
}
// Get the page with student links
// http://iseplive.fr/students
func (s *Session) getStudentsListPage() (io.ReadCloser, error) {
resp, err := s.Client.Get("http://iseplive.fr/students")
if err != nil {
return nil, err
}
return resp.Body, nil
}
// Extract links to each student page
// --> http://iseplive.fr/student/example
func extractLinksToStudentPages(body io.ReadCloser) ([]string, error) {
doc, err := goquery.NewDocumentFromReader(body)
if err != nil {
return nil, err
}
// Selection object
sel := doc.Find(".students-promo").ChildrenFiltered("a")
// Make the slice the length of the selection
links := make([]string, sel.Length())
// For each, append the link to the slice
sel.Each(func(i int, s *goquery.Selection) {
//link := s.Find
link, _ := s.Attr("href")
links[i] = link
})
return links, nil
}
// Export StudentList to CSV
func (sl StudentList) WriteCSV(writer io.Writer) error {
return gocsv.Marshal(sl, writer)
}
// Export StudentList to JSON
func (sl StudentList) WriteJSON(writer io.Writer) error {
enc := json.NewEncoder(writer)
return enc.Encode(&sl)
}