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
/
getStudent.go
74 lines (64 loc) · 2.04 KB
/
getStudent.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
package goil
import (
"github.com/PuerkitoBio/goquery"
"strings"
)
// Get a Student given a relative link to iseplive.fr: "/student/alexandrebezri"
// endpoint: http://iseplive.fr/student/{studentIdentifier}
func (s *Session) GetStudentByLink(link string) (Student, error) {
// Query the student page
resp, err := s.Client.Get(BaseURLString + link)
if err != nil {
return Student{}, err
}
defer resp.Body.Close()
// Parse the response's body
doc, err := goquery.NewDocumentFromReader(resp.Body)
if err != nil {
return Student{}, err
}
// Selection object
sel := doc.Find(".profile-info")
// Parse it
student, err := parseProfileInfo(sel)
return student, err
}
// Parse the profile information scrapped into the Student{} struct
func parseProfileInfo(sel *goquery.Selection) (student Student, err error) {
// Name is in a <h1> tag, so we can extract it
student.Name = sel.ChildrenFiltered("h1").Text()
// Well, didn't think it would work but it does
// It works this way: if an element, as dictated by goquery, is the name of a field, we know the next one is the value of that field, so we mark it for the next iteration of the loop.
// If it is marked, then we match the marking to the value and voila.
var nextIs string
sel.Contents().Each(func(i int, s *goquery.Selection) {
if nextIs == "" {
// If there is no next indicated for the current element, check if the current element indicates a next
fields := [7]string{"Promo", "Numéro", "Adresse ISEP", "Adresse email", "Tél", "Date de naissance", "Ma citation"}
for _, k := range fields {
if strings.Contains(s.Text(), k) {
nextIs = k
}
}
} else {
switch nextIs {
case "Promo":
student.Promo = s.Text()
case "Numéro":
student.ID = s.Text()
case "Adresse ISEP":
student.ISEPEmail = s.Text()
case "Adresse email":
student.Email = s.Text()
case "Tél":
student.Cell = s.Text()
case "Date de naissance":
student.Birthday = s.Text()
case "Ma citation":
student.Quote = s.Text()
}
nextIs = ""
}
})
return
}