diff --git a/feed_fetch.go b/feed_fetch.go index 33e9913..dfcde70 100644 --- a/feed_fetch.go +++ b/feed_fetch.go @@ -22,7 +22,12 @@ func fetchFeed(url string) { d := ParseFeed(b) - b, _ = json.Marshal(d.Channel.ItemsToLinks()) + links := []Link{} + + for _, feedItem := range d.Channel.Items { + links = append(links, Link{Url: feedItem.Link, Title: feedItem.Title, PublishedDate: feedItem.GetPublishedDate()}) + } + b, _ = json.Marshal(links) cacheKey := fmt.Sprintf("feeds:%s", url) SharedCache.Set(cacheKey, string(b)) } diff --git a/render_templates.go b/render_templates.go index 9bbf135..da872f3 100644 --- a/render_templates.go +++ b/render_templates.go @@ -17,6 +17,7 @@ import ( type RenderMeta struct { RenderStart time.Time + Location string } type RenderContext struct { @@ -51,7 +52,7 @@ func Render(w io.Writer, templateName string, data interface{}) error { return err } - renderContext := RenderContext{Data: data, Meta: RenderMeta{RenderStart: start}} + renderContext := RenderContext{Data: data, Meta: RenderMeta{RenderStart: start, Location: "America/New_York"}} return tmpl.ExecuteTemplate(w, "base", renderContext) } diff --git a/routes.go b/routes.go index a04e7b8..67e962a 100644 --- a/routes.go +++ b/routes.go @@ -5,12 +5,22 @@ import ( "fmt" "net/http" "net/url" + "slices" + "time" ) type Link struct { - Url string `json:"url"` - PublishedDate string `json:"publishedDate"` - Title string `json:"title"` + Url string `json:"url"` + PublishedDate time.Time `json:"publishedDate"` + Title string `json:"title"` +} + +func (l Link) GetLocalizedPublishedDate(loc string) time.Time { + if loc == "" { + loc = "America/New_York" + } + location, _ := time.LoadLocation(loc) + return l.PublishedDate.In(location) } // Healthcheck route @@ -39,6 +49,14 @@ func listContent(w http.ResponseWriter, r *http.Request) { links = append(links, formattedItems...) } + slices.SortStableFunc(links, func(a Link, b Link) int { + if a.PublishedDate.After(b.PublishedDate) { + return -1 + } + + return 1 + }) + Render(w, "index", links) } diff --git a/rss.go b/rss.go index a0b5e6e..64c9960 100644 --- a/rss.go +++ b/rss.go @@ -2,6 +2,7 @@ package main import ( "encoding/xml" + "time" ) type Document struct { @@ -12,7 +13,8 @@ type Item struct { Title string `xml:"title"` Link string `xml:"link"` Description string `xml:"description"` - PublishedDate string `xml:"pubDate"` + PubDate string `xml:"pubDate"` + PublishedDate time.Time } type Channel struct { @@ -21,14 +23,11 @@ type Channel struct { Items []Item `xml:"item"` } -func (c Channel) ItemsToLinks() []Link { - formattedItems := []Link{} +const DATE_FORMAT = "Mon, 02 Jan 2006 15:04:05 -0700" - for _, feedItem := range c.Items { - formattedItems = append(formattedItems, Link{Url: feedItem.Link, Title: feedItem.Title, PublishedDate: feedItem.PublishedDate}) - } - - return formattedItems +func (i Item) GetPublishedDate() time.Time { + published, _ := time.Parse(DATE_FORMAT, i.PubDate) + return published } func ParseFeed(raw []byte) Document { diff --git a/templates/index.html.tmpl b/templates/index.html.tmpl index 4b98f3d..50f0c14 100644 --- a/templates/index.html.tmpl +++ b/templates/index.html.tmpl @@ -3,7 +3,7 @@ {{ range .Data }}
  • {{ .Title }} - {{ .PublishedDate }} + {{ .GetLocalizedPublishedDate $.Meta.Location }}
  • {{ end }}