99 lines
2.8 KiB
Go
99 lines
2.8 KiB
Go
package main
|
|
|
|
// Reading a news feed. Only the standard library, no Windows calls, so it can
|
|
// be tested on its own.
|
|
//
|
|
// Local headlines come from Google News' place feed: it takes a place name such
|
|
// as "Huntington, Indiana" and returns what local papers, TV and radio stations
|
|
// near it are publishing. The place name comes from the ZIP code (the weather
|
|
// lookup already finds it), so the same ZIP code serves both panels.
|
|
|
|
import (
|
|
"encoding/xml"
|
|
"fmt"
|
|
"html"
|
|
"net/url"
|
|
"sort"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
type newsItem struct {
|
|
title string
|
|
source string
|
|
link string
|
|
when time.Time
|
|
}
|
|
|
|
const newsKeep = 30 // headlines kept per lookup
|
|
|
|
func newsFeedURL(place string) string {
|
|
return "https://news.google.com/rss/headlines/section/geo/" + url.PathEscape(place) + "?hl=en-US&gl=US&ceid=US:en"
|
|
}
|
|
|
|
// newsParse turns the feed into headlines, newest first. Only web links are
|
|
// kept, so nothing odd can be opened from the panel.
|
|
func newsParse(data []byte) ([]newsItem, error) {
|
|
var feed struct {
|
|
Items []struct {
|
|
Title string `xml:"title"`
|
|
Link string `xml:"link"`
|
|
PubDate string `xml:"pubDate"`
|
|
Source string `xml:"source"`
|
|
} `xml:"channel>item"`
|
|
}
|
|
if err := xml.Unmarshal(data, &feed); err != nil {
|
|
return nil, fmt.Errorf("The news feed could not be read.")
|
|
}
|
|
var out []newsItem
|
|
seen := map[string]bool{}
|
|
for _, it := range feed.Items {
|
|
link := strings.TrimSpace(it.Link)
|
|
if !strings.HasPrefix(link, "https://") && !strings.HasPrefix(link, "http://") {
|
|
continue
|
|
}
|
|
title := strings.TrimSpace(html.UnescapeString(it.Title))
|
|
src := strings.TrimSpace(html.UnescapeString(it.Source))
|
|
// Titles end with " - Source Name"; the source is shown on its own line.
|
|
if src != "" && strings.HasSuffix(title, " - "+src) {
|
|
title = strings.TrimSuffix(title, " - "+src)
|
|
} else if src == "" {
|
|
if i := strings.LastIndex(title, " - "); i > 0 {
|
|
src = strings.TrimSpace(title[i+3:])
|
|
title = strings.TrimSpace(title[:i])
|
|
}
|
|
}
|
|
key := strings.ToLower(title)
|
|
if title == "" || seen[key] {
|
|
continue
|
|
}
|
|
seen[key] = true
|
|
when, _ := time.Parse(time.RFC1123, strings.TrimSpace(it.PubDate))
|
|
out = append(out, newsItem{title: title, source: src, link: link, when: when})
|
|
}
|
|
sort.SliceStable(out, func(i, j int) bool { return out[i].when.After(out[j].when) })
|
|
if len(out) > newsKeep {
|
|
out = out[:newsKeep]
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// newsAge says how old a headline is: "12 min ago", "3 hr ago", "2 days ago".
|
|
func newsAge(t time.Time, now time.Time) string {
|
|
if t.IsZero() {
|
|
return ""
|
|
}
|
|
d := now.Sub(t)
|
|
switch {
|
|
case d < 0, d < time.Minute:
|
|
return "just now"
|
|
case d < time.Hour:
|
|
return fmt.Sprintf("%d min ago", int(d.Minutes()))
|
|
case d < 24*time.Hour:
|
|
return fmt.Sprintf("%d hr ago", int(d.Hours()))
|
|
case d < 48*time.Hour:
|
|
return "yesterday"
|
|
}
|
|
return fmt.Sprintf("%d days ago", int(d.Hours()/24))
|
|
}
|