Add --news to --print

This commit is contained in:
Jonas Thiem 2018-05-06 02:26:03 +02:00
parent f9723f67b7
commit 8dd08327b1
2 changed files with 55 additions and 0 deletions

2
cmd.go
View file

@ -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"):

View file

@ -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)