-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscrapper.go
108 lines (84 loc) · 1.94 KB
/
scrapper.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
package main
import (
// "fmt"
"strings"
"github.com/gocolly/colly"
)
var url = "http://wpvitassuds01.itap.purdue.edu/washalertweb/washalertweb.aspx"
func GetLoc() Rooms {
var rooms = Rooms{}
c := colly.NewCollector()
c.OnHTML("h2 a[href]", func(e *colly.HTMLElement) {
room := Room{
Name: e.Text,
Url: e.Attr("href"),
}
rooms = append(rooms, room)
})
c.Visit(url)
return rooms
}
func GetInfo(room Room) Room {
c := colly.NewCollector()
var machines = Machines{}
var availWash int32 = 0
var availDry int32 = 0
var wash int32 = 0
var dry int32 = 0
c.OnHTML("tr", func(e *colly.HTMLElement) {
if strings.Compare(e.ChildText("td.name"), "") != 0 {
machine := Machine{
Name: e.ChildText("td.name"),
Status: e.ChildText("td.status"),
TimeRemaining: e.ChildText("td.time"),
}
machines = append(machines, machine)
if strings.Compare(e.ChildText("td.type"), "Dryer") == 0 {
if strings.Compare(machine.Status, "Available") == 0 {
availDry++
}
dry++
} else {
if strings.Compare(machine.Status, "Available") == 0 {
availWash++
}
wash++
}
}
})
c.Visit(url + room.Url)
room.AvailableWashers = String(availWash)
room.TotalWashers = String(wash)
room.AvailableDryers = String(availDry)
room.TotalDryers = String(dry)
room.Machines = machines
return room
}
func Scrape() Rooms {
var rooms = GetLoc()
var scrape = Rooms{}
for _, room := range rooms {
scrape = append(scrape, GetInfo(room))
}
return scrape
}
func String(n int32) string {
buf := [11]byte{}
pos := len(buf)
i := int64(n)
signed := i < 0
if signed {
i = -i
}
for {
pos--
buf[pos], i = '0' + byte(i % 10), i / 10
if i == 0 {
if signed {
pos--
buf[pos] = '-'
}
return string(buf[pos:])
}
}
}