diff --git a/main.go b/main.go index 455744f..0654a1e 100644 --- a/main.go +++ b/main.go @@ -11,17 +11,21 @@ import ( "net/url" "os" "regexp" + "strings" "time" "github.com/jinzhu/now" + "p83.nl/go/ekster/pkg/jf2" "p83.nl/go/ekster/pkg/microsub" "willnorris.com/go/microformats" - - "p83.nl/go/ekster/pkg/jf2" ) type Summary struct { - Items []microsub.Item + PostTypes map[string]string + Name string + Published string + Items []microsub.Item + Grouped map[string][]microsub.Item } func init() { @@ -40,7 +44,7 @@ func main() { log.Fatal(err) } - f, err := openAndCreate(filename) + f, err := forceCreate(filename) data, err := ioutil.ReadAll(f) if err != nil { @@ -55,9 +59,10 @@ func main() { log.Fatal(err) } - items = filterEntriesAfter(items, now.With(time.Now()).Monday()) + monday := now.With(time.Now()).Monday().AddDate(0, 0, -10) + items = filterEntriesAfter(items, monday) - summary := Summary{Items: items} + summary := getSummary(monday, items) err = outputEntries(summary, &buf) if err != nil { @@ -71,36 +76,74 @@ func main() { } } -func openAndCreate(filename string) (io.ReadCloser, error) { - f, err := os.Open(filename) - if err != nil && !os.IsExist(err) { - t, err := template.ParseFiles("templates/full.html") - if err != nil { - return nil, err - } - f, err = os.Create(filename) - if err != nil { - return nil, err - } - err = t.Execute(f, nil) - if err != nil { - return nil, err - } - err = f.Close() - if err != nil { - return nil, err - } - f, err = os.Open(filename) - if err != nil { - return nil, err - } - } else if err != nil { +func getSummary(monday time.Time, items []microsub.Item) Summary { + year, week := monday.ISOWeek() + grouped := make(map[string][]microsub.Item) + for _, item := range items { + itemType := postTypeDiscovery(&item) + grouped[itemType] = append(grouped[itemType], item) + } + summary := Summary{ + Name: fmt.Sprintf("Digest for Week %d-%d", week, year), + Published: monday.AddDate(0, 0, 7).Format("2 Jan 2006"), + Items: items, + Grouped: grouped, + } + return summary +} + +func forceCreate(filename string) (io.ReadCloser, error) { + t, err := template.ParseFiles("templates/full.html") + if err != nil { + return nil, err + } + f, err := os.Create(filename) + if err != nil { + return nil, err + } + err = t.Execute(f, nil) + if err != nil { + return nil, err + } + err = f.Close() + if err != nil { + return nil, err + } + f, err = os.Open(filename) + if err != nil { return nil, err } return f, nil } +func openAndCreate(filename string) (io.ReadCloser, error) { + var f io.ReadCloser + var err error + + f, err = os.Open(filename) + + if err != nil { + if !os.IsExist(err) { + f, err = forceCreate(filename) + } + } + + return f, err +} + func outputEntries(summary Summary, w io.Writer) error { + summary.PostTypes = map[string]string{ + "checkin": "Checkins", + "event": "Events", + "repost-of": "Reposts", + "like-of": "Likes", + "in-reply-to": "Replies", + "bookmark": "Bookmarks", + "photo": "Photos", + "note": "Notes", + "article": "Articles", + } + t, err := template.ParseFiles("templates/weekly.html") if err != nil { log.Fatal(err) @@ -112,6 +155,67 @@ func outputEntries(summary Summary, w io.Writer) error { return nil } +func postTypeDiscovery(item *microsub.Item) string { + if item == nil { + return "" + } + if item.Checkin != nil { + return "checkin" + } + if item.Type == "event" { + return "event" + } + if len(item.RepostOf) > 0 && validUrl(item.RepostOf[0]) { + return "repost-of" + } + if len(item.LikeOf) > 0 && validUrl(item.LikeOf[0]) { + return "like-of" + } + if len(item.InReplyTo) > 0 && validUrl(item.InReplyTo[0]) { + return "in-reply-to" + } + if len(item.BookmarkOf) > 0 && validUrl(item.BookmarkOf[0]) { + return "bookmark" + } + if len(item.Photo) > 0 && validUrl(item.Photo[0]) { + return "photo" + } + + var content, name string + if item.Content != nil { + content = item.Content.Text + } + if content == "" { + if item.Summary != "" { + content = item.Summary + } + } + + if content == "" { + return "note" + } + + name = item.Name + + if name == "" { + return "note" + } + + name = strings.Join(strings.Fields(strings.TrimSpace(name)), " ") + content = strings.Join(strings.Fields(strings.TrimSpace(content)), " ") + + if !strings.HasPrefix(content, name) { + return "article" + } + + return "note" +} + +func validUrl(u string) bool { + _, err := url.Parse(u) + return err == nil +} + func getEntriesForFeed(u *url.URL) ([]microsub.Item, error) { resp, err := http.Get(u.String()) if err != nil { diff --git a/templates/weekly.html b/templates/weekly.html index f7a2962..0d73ec2 100644 --- a/templates/weekly.html +++ b/templates/weekly.html @@ -1,14 +1,24 @@
- {{ range .Items }} -

{{ .Name }}

- {{ if .Content }} - {{ if .Content.HTML }} -
{{ .Content.HTML }}
- {{ else }} -
{{ .Content.Text }}
+

{{ .Name }}

+ {{ range $k, $items := .Grouped }} +

{{ index $.PostTypes $k }}

+ {{ end }}
\ No newline at end of file