pof
Proof of Freshness: collate proof of an authorship date.
Log | Files | << Repositories
tree 3296d48fdd9f33da674599a53fc2b10634a269c4 parent 0e6cb19ff3482293ff6efc81b85cc6fbde91e093 author esote <esote.net@gmail.com> 1563329924 -0500 committer esote <esote.net@gmail.com> 1563329924 -0500 gpgsig -----BEGIN PGP SIGNATURE----- iHUEABYIAB0WIQTXAxYDuIzimYoNSPuhTmRAjzzC8gUCXS6FpAAKCRChTmRAjzzC 8hlaAQDaFURp1qcXrKKQs6NB1+xpRveYT9Qqmq4uByoPJ0LE+wD6AhhM2Sn5XUTr gfcj8htrXt+TmI0B5FLt/Y/AYxwXLwE= =JZpK -----END PGP SIGNATURE----- Custom (faster) RSS parser, remove external dependency Use BTC depth variable directly.
.gitignore | 1 + pof.go | 61 ++++++++++++++++++++++++++++++++++++++++++++++++------------- 2 files changed, 49 insertions(+), 13 deletions(-)
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1377554 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.swp diff --git a/pof.go b/pof.go index 34418bc..b41440a 100644 --- a/pof.go +++ b/pof.go @@ -2,27 +2,22 @@ package main import ( "encoding/json" + "encoding/xml" "fmt" "io/ioutil" "log" "net/http" "regexp" "time" - - "github.com/mmcdole/gofeed" ) -var re = regexp.MustCompile(`[^[:ascii:]]+`) - func main() { - parser := gofeed.NewParser() - // date fmt.Printf("Date: %s\n\n", time.Now().UTC().Format("2006-01-02 15:04 MST")) // news feeds - news(parser, 5) + news() // NIST randomness beacons nist() @@ -34,7 +29,19 @@ func main() { monero() } -func news(parser *gofeed.Parser, count int) { +type Rss struct { + XMLName xml.Name `xml:"rss"` + Channel struct { + Title string `xml:"title"` + Items []struct { + Title string `xml:"title"` + } `xml:"item"` + } `xml:"channel"` +} + +func news() { + var re = regexp.MustCompile(`[^[:ascii:]]+`) + urls := []string{ "https://www.spiegel.de/international/index.rss", "https://rss.nytimes.com/services/xml/rss/nyt/World.xml", @@ -43,27 +50,55 @@ func news(parser *gofeed.Parser, count int) { "https://www.economist.com/latest/rss.xml", } + const count = 5 + for _, url := range urls { - feed, err := parser.ParseURL(url) + rss, err := parseRss(url) if err != nil { log.Fatal(err) } - if len(feed.Items) < count { + if len(rss.Channel.Items) < count { log.Fatalf("couldn't find %d items", count) } - fmt.Printf("Src: %s (%s)\n ---\n", re.ReplaceAllString(feed.Title, " "), url) + fmt.Printf("Src: %s (%s)\n ---\n", re.ReplaceAllString(rss.Channel.Title, " "), url) for i := 0; i < count; i++ { - fmt.Printf("%s\n", re.ReplaceAllString(feed.Items[i].Title, " ")) + fmt.Printf("%s\n", re.ReplaceAllString(rss.Channel.Items[i].Title, " ")) } fmt.Println() } } +func parseRss(url string) (*Rss, error) { + resp, err := http.Get(url) + + if err != nil { + return nil, err + } + + rssxml, err := ioutil.ReadAll(resp.Body) + + if err != nil { + return nil, err + } + + if err := resp.Body.Close(); err != nil { + return nil, err + } + + var rss Rss + + if err := xml.Unmarshal(rssxml, &rss); err != nil { + return nil, err + } + + return &rss, nil +} + func nist() { v2URL := "https://beacon.nist.gov/beacon/2.0/pulse/last" @@ -135,7 +170,7 @@ func btc() { fmt.Printf("Src: Blockchain.Info [block depth %d] (%s)\n ---\n", depth, btcURL) - fmt.Printf("%s\n\n", btc.Blocks[10].Hash) + fmt.Printf("%s\n\n", btc.Blocks[depth].Hash) } func monero() {