weekly/main.go

79 lines
1.4 KiB
Go

package main
import (
"fmt"
"html/template"
"log"
"net/http"
"net/url"
"os"
"time"
"github.com/jinzhu/now"
"p83.nl/go/ekster/pkg/microsub"
"willnorris.com/go/microformats"
"p83.nl/go/ekster/pkg/jf2"
)
func main() {
u, err := url.Parse(os.Args[1])
if err != nil {
log.Fatal(err)
}
items, err := getEntriesForFeed(u)
if err != nil {
log.Fatal(err)
}
items = filterEntriesAfter(items, now.With(time.Now()).Monday())
err = outputEntries(items)
if err != nil {
log.Fatal(err)
}
}
func outputEntries(items []microsub.Item) error {
t, err := template.ParseFiles("templates/weekly.html")
if err != nil {
return err
}
err = t.Execute(os.Stdout, items)
if err != nil {
return err
}
return nil
}
func getEntriesForFeed(u *url.URL) ([]microsub.Item, error) {
resp, err := http.Get(u.String())
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, fmt.Errorf("status is not 200, but %d", resp.StatusCode)
}
md := microformats.Parse(resp.Body, u)
items := jf2.SimplifyMicroformatDataItems(md)
return items, nil
}
func filterEntriesAfter(items []microsub.Item, from time.Time) []microsub.Item {
var result []microsub.Item
for _, item := range items {
published, err := time.Parse(time.RFC3339, item.Published)
if err != nil {
continue
}
if published.After(from) {
result = append(result, item)
}
}
return result
}