-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfco.go
More file actions
70 lines (65 loc) · 1.69 KB
/
Copy pathfco.go
File metadata and controls
70 lines (65 loc) · 1.69 KB
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
package feed
import (
"encoding/xml"
"log"
"net/http"
"strings"
"time"
"github.com/securityfirst/umbrella-api/country"
"github.com/securityfirst/umbrella-api/models"
)
type FCOFetcher struct{}
func (g *FCOFetcher) Fetch() ([]models.FeedItem, error) {
body, err := makeRequest("https://www.gov.uk/government/organisations/latest.atom?organisations%5B%5D=foreign-commonwealth-office", http.MethodGet, nil)
if err != nil {
return nil, err
}
var v fcoResp
if err := xml.Unmarshal(body, &v); err != nil {
return nil, err
}
var feeds = make([]models.FeedItem, 0, len(v.Entry))
for _, e := range v.Entry {
if !strings.HasSuffix(e.Title, " travel advice") {
continue
}
name := strings.TrimSpace(strings.TrimSuffix(e.Title, " travel advice"))
c, err := country.ByName(name)
if err != nil {
log.Printf("FCO - Country %q: %s", name, err)
continue
}
t, err := time.Parse(time.RFC3339, e.Updated)
if err != nil {
log.Printf("FCO - Time %q: %s", e.Updated, err)
continue
}
feeds = append(feeds, models.FeedItem{
Title: e.Title,
Description: e.Summary.Text,
URL: e.Link.Href,
Country: strings.ToLower(c.Codes.Alpha2),
Source: models.FCO,
UpdatedAt: t.Unix(),
})
}
return feeds, nil
}
type fcoResp struct {
Entry []struct {
Text string `xml:",chardata"`
ID string `xml:"id"`
Updated string `xml:"updated"`
Link struct {
Text string `xml:",chardata"`
Rel string `xml:"rel,attr"`
Type string `xml:"type,attr"`
Href string `xml:"href,attr"`
} `xml:"link"`
Title string `xml:"title"`
Summary struct {
Text string `xml:",chardata"`
Type string `xml:"type,attr"`
} `xml:"summary"`
} `xml:"entry"`
}