-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
71 lines (61 loc) · 1.57 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
package main
import (
"encoding/json"
"fmt"
"github.com/tebeka/selenium"
"net/http"
"strings"
)
const (
seleniumPath = "./selenium-server-standalone-3.14.0.jar"
port = 4444
chromeDriver = "./windows/chromedriver.exe"
busStop = "3042"
)
type Parada struct {
Linea string `json:"linea"`
Minutos []string `json:"minutos"`
Destino string `json:"destino"`
}
func main() {
service, err := selenium.NewSeleniumService(seleniumPath, port, selenium.ChromeDriver(chromeDriver))
if err != nil {
panic(err)
}
defer service.Stop()
wd, err := selenium.NewRemote(selenium.Capabilities{"browserName": "chrome"}, "http://localhost:4444/wd/hub")
if err != nil {
panic(err)
}
defer wd.Quit()
// Access to the page for the information of the bus stops
err = wd.Get(fmt.Sprintf("https://busmadrid.welbits.com/stop/%s", busStop))
if err != nil {
panic(err)
}
var filas []selenium.WebElement
err = wd.Wait(func(wd selenium.WebDriver) (bool, error) {
filas, err = wd.FindElements(selenium.ByXPATH, "//table/tbody/tr")
if err != nil {
return false, err
} else {
return len(filas) > 0, nil
}
})
var paradas []Parada
for _, el := range filas {
dato, _ := el.Text()
rs := strings.Split(dato, "\n")
paradas = append(paradas, Parada{
Linea: rs[0],
Minutos: []string{rs[1], rs[2]},
Destino: rs[3],
})
}
//send information to client as push notification
newJson, _ := json.Marshal(paradas)
_, err = http.Post("https://ntfy.sh/information_for_sebas", "application/json", strings.NewReader(string(newJson)))
if err != nil {
panic(err)
}
}