This repository has been archived by the owner on Nov 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
129 lines (100 loc) · 2.7 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
package main
import (
"log"
"net/http"
"regexp"
"strings"
"time"
"github.com/PuerkitoBio/goquery"
"github.com/labstack/echo/v4"
)
type Service struct {
Status string `json:"status"`
Now string `json:"now"`
Routes []string `json:"routes"`
}
type News struct {
Text string `json:"text"`
Url string `json:"url"`
}
type LineStatus struct {
Line string `json:"line"`
Text string `json:"text"`
Status string `json:"status"`
}
const URL string = "https://www.atm.it/it/AtmNews/Pagine/default.aspx"
// Create a static JSON and serve it
func mainRoute(c echo.Context) error {
s := Service{
Status: "Up & running",
Now: time.Now().String(),
Routes: []string{"/news", "/status", "/traffic"},
}
return c.JSON(http.StatusOK, s)
}
// handling the /news route
func newsRoute(c echo.Context) error {
news := make([]News, 0)
doc := getDocument()
doc.Find("#atm-comunicati div.news-item a").Each(func(index int, item *goquery.Selection) {
text := item.Text()
url, _ := item.Attr("href")
singleNews := News{
Text: text,
Url: url,
}
news = append(news, singleNews)
})
return c.JSON(http.StatusOK, news)
}
// handling the /status route
func statusRoute(c echo.Context) error {
lines := make([]LineStatus, 0)
doc := getDocument()
re := regexp.MustCompile(`[M][0-9]`)
doc.Find("#StatusLinee tr").Each(func(index int, item *goquery.Selection) {
line, _ := item.Find("div.StatusLinee_Stretch").Attr("id")
text := item.Find("div.StatusLinee_DirezioneScritta").Text()
status := item.Find("div.StatusLinee_Stretch").Text()
if len(line) > 0 {
singleLine := LineStatus{
Text: strings.TrimSpace(text),
Line: re.FindStringSubmatch(line)[0],
Status: strings.TrimSpace(status),
}
lines = append(lines, singleLine)
}
})
return c.JSON(http.StatusOK, lines)
}
// handling the /trffic route
func trafficRoute(c echo.Context) error {
traffic := make([]News, 0)
doc := getDocument()
doc.Find("#subhomepage-cx-infomobilita > div:nth-child(1) > table div.item.link-item a").Each(func(index int, item *goquery.Selection) {
text := item.Text()
url, _ := item.Attr("href")
entry := News{
Text: text,
Url: url,
}
traffic = append(traffic, entry)
})
return c.JSON(http.StatusOK, traffic)
}
func getDocument() *goquery.Document {
doc, err := goquery.NewDocument(URL)
if err != nil {
log.Fatal(err)
}
return doc
}
// Server bootstrapping
func main() {
e := echo.New() // initialize Echo
e.GET("/", mainRoute) // adding routes
e.GET("/news", newsRoute) // adding routes
e.GET("/status", statusRoute) // adding routes
e.GET("/traffic", trafficRoute) // adding routes
e.Logger.Fatal(e.Start(":8080")) // starting server
}