-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
56 lines (43 loc) · 1.16 KB
/
api.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
package client
import (
"crypto/md5"
"fmt"
"io"
"github.com/antchfx/xmlquery"
)
type Message struct {
Title string
Description string
}
// GetStatusAPIResponse Incident status
type GetStatusAPIResponse struct {
HasMessage bool
Messages []Message
Hash string
}
// GetStatus gets current incident status message
func GetStatus(streetID int, building string) (*GetStatusAPIResponse, error) {
status := &GetStatusAPIResponse{}
url := fmt.Sprintf("http://www.1562.kharkov.ua/uk/ppr/rss?street_id=%d&building=%s", streetID, building)
node, err := xmlquery.LoadURL(url)
if err != nil {
return nil, err
}
items := xmlquery.Find(node, "//rss/channel/item")
messages := make([]Message, 0)
hash := md5.New()
for _, item := range items {
status.HasMessage = true
title := xmlquery.FindOne(item, "//title").InnerText()
description := xmlquery.FindOne(item, "//description").InnerText()
io.WriteString(hash, title)
io.WriteString(hash, description)
messages = append(messages, Message{
Title: title,
Description: description,
})
}
status.Messages = messages
status.Hash = fmt.Sprintf("%x", hash.Sum(nil))
return status, nil
}