diff --git a/cmd.go b/cmd.go index a6957d2d..381573c5 100644 --- a/cmd.go +++ b/cmd.go @@ -318,6 +318,8 @@ func handlePrint() (err error) { err = printNumberOfUpdates() case cmdArgs.existsArg("u", "upgrades"): err = printUpdateList(cmdArgs) + case cmdArgs.existsArg("w", "news"): + err = printNewsFeed() case cmdArgs.existsArg("c", "complete"): switch { case cmdArgs.existsArg("f", "fish"): diff --git a/print.go b/print.go index 00ccd44f..1fe7ae41 100644 --- a/print.go +++ b/print.go @@ -1,7 +1,11 @@ package main import ( + "bytes" + "encoding/xml" "fmt" + "io/ioutil" + "net/http" "os" "strconv" "strings" @@ -398,6 +402,55 @@ outer: return nil } +type item struct { + Title string `xml:"title"` + Link string `xml:"link"` + Description string `xml:"description"` + PubDate string `xml:"pubDate"` + Creator string `xml:"dc:creator"` +} + +type channel struct { + Title string `xml:"title"` + Link string `xml:"link"` + Description string `xml:"description"` + Language string `xml:"language"` + Lastbuilddate string `xml:"lastbuilddate"` + Item []item `xml:"item"` +} + +type rss struct { + Channel channel `xml:"channel"` +} + +func printNewsFeed() error { + resp, err := http.Get("https://archlinux.org/feeds/news") + if err != nil { + return err + } + + defer resp.Body.Close() + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return err + } + + rss := rss{} + + p := xml.NewDecoder(bytes.NewReader(body)) + + err = p.Decode(&rss) + if err != nil { + return err + } + + for _, item := range rss.Channel.Item { + fmt.Println(item.PubDate, item.Title) + } + + return nil +} + // Formats a unix timestamp to ISO 8601 date (yyyy-mm-dd) func formatTime(i int) string { t := time.Unix(int64(i), 0)