-
Notifications
You must be signed in to change notification settings - Fork 1
/
feed.go
94 lines (83 loc) · 2.12 KB
/
feed.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
package main
import (
"log"
"time"
"github.com/securityfirst/umbrella-api/feed"
"strings"
"github.com/securityfirst/umbrella-api/models"
"github.com/securityfirst/umbrella-api/utils"
"github.com/gin-gonic/gin"
"github.com/gosexy/to"
)
func (um *Umbrella) UpdateFeeds() error {
countries, err := um.getCountries()
if err != nil {
return err
}
var feeds = make([]feed.Fetcher, 0, len(countries)+4)
for i := range countries {
feeds = append(feeds, &feed.ReliefWebFetcher{
Country: &countries[i],
})
}
feeds = append(feeds, &feed.GdascFetcher{},
&feed.FCOFetcher{}, &feed.CadataFetcher{}, &feed.CDCFetcher{})
for _, f := range feeds {
results, err := f.Fetch()
if err != nil {
utils.CheckErr(err)
continue
}
for _, item := range results {
item.Update(um.Db)
}
}
return nil
}
func (um *Umbrella) getFeed(c *gin.Context) {
since := to.Int64(c.Request.URL.Query().Get("since"))
// twoMonthsAgo := time.Now().AddDate(0, -2, 0).Unix()
// if since < twoMonthsAgo {
// since = twoMonthsAgo
// }
country, err := um.getCountryInfo(c.Request.URL.Query().Get("country"))
um.checkErr(c, err)
if err != nil {
log.Println("country", err)
c.JSON(200, []interface{}{})
return
}
sources := strings.Split(c.Request.URL.Query().Get("sources"), ",")
feedItems, err := um.getDbFeedItems(sources, country.Iso2, since)
um.checkErr(c, err)
if err != nil {
log.Println("sources", err)
c.JSON(200, feedItems)
return
}
feedLog := models.FeedRequestLog{
Country: country.Iso2,
Sources: c.Request.URL.Query().Get("sources"),
CheckedAt: time.Now().Unix(),
}
if err != nil {
utils.CheckErr(err)
feedLog.Status = 500
go utils.CheckErr(um.Db.Insert(&feedLog))
c.JSON(500, gin.H{"error": "Internal server error"})
return
}
feedLog.Status = 200
go utils.CheckErr(um.Db.Insert(&feedLog))
c.JSON(200, feedItems)
}
type SortFeedByDate []models.FeedItem
func (slice SortFeedByDate) Len() int {
return len(slice)
}
func (slice SortFeedByDate) Less(i, j int) bool {
return slice[i].UpdatedAt > slice[j].UpdatedAt
}
func (slice SortFeedByDate) Swap(i, j int) {
slice[i], slice[j] = slice[j], slice[i]
}