Browse Source

Merge branch 'micropub' of peter/weekly into master

master
Peter Stuifzand 2 years ago
committed by Gitea
parent
commit
3911aa91fe
  1. 2
      go.mod
  2. 12
      go.sum
  3. 158
      main.go
  4. 62
      templates/weekly.html
  5. 24
      templates/weekly.md.tmpl

2
go.mod

@ -4,7 +4,9 @@ go 1.12
require (
github.com/jinzhu/now v1.1.1
github.com/kylelemons/godebug v1.1.0 // indirect
github.com/pkg/errors v0.8.1 // indirect
github.com/stretchr/testify v1.4.0 // indirect
golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553 // indirect
p83.nl/go/ekster v0.0.0-20191119211024-4511657daa0b
willnorris.com/go/microformats v1.0.0

12
go.sum

@ -1,12 +1,24 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/jinzhu/now v1.1.1 h1:g39TucaRWyV3dwDO++eEc6qf8TVIQ/Da48WmqjZ3i7E=
github.com/jinzhu/now v1.1.1/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553 h1:efeOvDhwQ29Dj3SdAV/MJf8oukgn+8D8WgaCaRMchF8=
golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
p83.nl/go/ekster v0.0.0-20191119211024-4511657daa0b h1:MzIItcq83xZn1apSjCNovs55CZE/R/7GUnlOeIJ9HRI=
p83.nl/go/ekster v0.0.0-20191119211024-4511657daa0b/go.mod h1:NDCrcUlnixnkQ4u36jm3Hhvq0WcayEjg8SpAsFdQWLE=
willnorris.com/go/microformats v1.0.0 h1:II6uDIJBPp6RpJQqRWm+6IN9lI00mN/jQAC5OHuF4HA=

158
main.go

@ -2,16 +2,16 @@ package main
import (
"bytes"
"flag"
"fmt"
"html/template"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"regexp"
"os/exec"
"strings"
tt "text/template"
"time"
"github.com/jinzhu/now"
@ -20,6 +20,12 @@ import (
"willnorris.com/go/microformats"
)
var (
show = flag.Bool("show", false, "Show the output")
server = flag.String("endpoint", "", "Micropub endpoint specified in shpub")
)
// Summary is a summary of the last few posts on a blog
type Summary struct {
PostTypes map[string]string
Name string
@ -30,28 +36,21 @@ type Summary struct {
func init() {
log.SetFlags(log.LstdFlags | log.Lshortfile)
}
func main() {
u, err := url.Parse(os.Args[1])
if err != nil {
log.Fatal(err)
}
filename := os.Args[2]
flag.Parse()
re, err := regexp.Compile(`<span class="next-entry"></span>`)
if err != nil {
log.Fatal(err)
if !*show && *server == "" {
log.Println("Specify -endpoint when using micropub")
return
}
f, err := forceCreate(filename)
args := flag.Args()
data, err := ioutil.ReadAll(f)
u, err := url.Parse(args[0])
if err != nil {
log.Fatal(err)
}
f.Close()
var buf bytes.Buffer
items, err := getEntriesForFeed(u)
if err != nil {
@ -68,10 +67,14 @@ func main() {
log.Fatal(err)
}
output := re.ReplaceAll(data, buf.Bytes())
err = ioutil.WriteFile(filename, output, 0666)
if err != nil {
log.Fatal(err)
if *show {
io.Copy(os.Stdout, &buf)
} else {
fmt.Println("micropub")
err = micropub(summary.Name, buf.String(), *server)
if err != nil {
log.Fatal(err)
}
}
}
@ -83,73 +86,36 @@ func getSummary(monday time.Time, items []microsub.Item) Summary {
grouped[itemType] = append(grouped[itemType], item)
}
summary := Summary{
Name: fmt.Sprintf("Digest for Week %d-%d", week, year),
Items: items,
Grouped: grouped,
Name: fmt.Sprintf("Digest for Week %d-%d", week, year),
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")
"checkin": "Checkins",
"event": "Events",
"repost": "Reposts",
"like": "Likes",
"reply": "Replies",
"bookmark": "Bookmarks",
"photo": "Photos",
"note": "Notes",
"article": "Articles",
}
t, err := tt.ParseFiles("templates/weekly.md.tmpl")
if err != nil {
log.Fatal(err)
}
err = t.Execute(w, summary)
if err != nil {
return err
}
return nil
}
@ -163,19 +129,19 @@ func postTypeDiscovery(item *microsub.Item) string {
if item.Type == "event" {
return "event"
}
if len(item.RepostOf) > 0 && validUrl(item.RepostOf[0]) {
return "repost-of"
if len(item.RepostOf) > 0 && validURL(item.RepostOf[0]) {
return "repost"
}
if len(item.LikeOf) > 0 && validUrl(item.LikeOf[0]) {
return "like-of"
if len(item.LikeOf) > 0 && validURL(item.LikeOf[0]) {
return "like"
}
if len(item.InReplyTo) > 0 && validUrl(item.InReplyTo[0]) {
return "in-reply-to"
if len(item.InReplyTo) > 0 && validURL(item.InReplyTo[0]) {
return "reply"
}
if len(item.BookmarkOf) > 0 && validUrl(item.BookmarkOf[0]) {
if len(item.BookmarkOf) > 0 && validURL(item.BookmarkOf[0]) {
return "bookmark"
}
if len(item.Photo) > 0 && validUrl(item.Photo[0]) {
if len(item.Photo) > 0 && validURL(item.Photo[0]) {
return "photo"
}
@ -209,7 +175,7 @@ func postTypeDiscovery(item *microsub.Item) string {
return "note"
}
func validUrl(u string) bool {
func validURL(u string) bool {
_, err := url.Parse(u)
return err == nil
}
@ -236,9 +202,31 @@ func filterEntriesAfter(items []microsub.Item, from time.Time) []microsub.Item {
if err != nil {
continue
}
if published.After(from) {
result = append(result, item)
if !published.After(from) {
continue
}
found := false
for _, cat := range item.Category {
if cat == "weekly-digest" {
found = true
}
}
if found {
continue
}
result = append(result, item)
}
return result
}
func micropub(name, content, server string) error {
var args []string
if server != "" {
args = append(args, "-s", server)
}
args = append(args, "-d", "article", "-c", "weekly-digest", "--json", "-x", "p3k-content-type=text/markdown", name, content)
cmd := exec.Command("shpub", args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}

62
templates/weekly.html

@ -1,32 +1,36 @@
<span class="next-entry"></span>
<div class="h-entry">
<h2 class="p-name">{{ .Name }}</h2>
{{ range $k, $items := .Grouped }}
<h3>{{ index $.PostTypes $k }}</h3>
<ul class="{{ $k }}">
{{ range $items }}
{{ if eq $k "like-of" }}
{{ range .LikeOf }}
<li><a href="{{ . }}">{{ . }}</a></li>
{{ end }}
{{ else if eq $k "repost-of" }}
{{ range .RepostOf }}
<li><a href="{{ . }}">{{ . }}</a></li>
{{ end }}
{{ else if eq $k "in-reply-to" }}
{{ range .InReplyTo }}
<li><a href="{{ . }}">{{ . }}</a></li>
{{ end }}
{{ range $k, $items := .Grouped }}
<h3>{{ index $.PostTypes $k }}</h3>
<ul class="{{ $k }}">
{{ range $items }}
{{ $item := . }}
{{ if eq $k "like-of" }}
{{ range .LikeOf }}
<li><a href="{{ . }}">{{ . }}</a></li>
{{ end }}
{{ else if eq $k "repost-of" }}
{{ range .RepostOf }}
<li><a href="{{ . }}">{{ . }}</a></li>
{{ end }}
{{ else if eq $k "bookmark" }}
{{ range .BookmarkOf }}
<li><a href="{{ . }}">{{ $item.Name }}</a></li>
{{ end }}
{{ else if eq $k "in-reply-to" }}
{{ range .InReplyTo }}
<li><a href="{{ . }}">{{ . }}</a></li>
{{ end }}
{{ else if eq $k "article" }}
<li><a href="{{ .URL }}">{{ .Name }}</a></li>
{{ else }}
{{ if .Checkin }}
<li><a href="{{ .URL }}">Checkin at {{ .Checkin.Name }} in {{ .Checkin.Locality }}
, {{ .Checkin.Region }}</a></li>
{{ else if .Content }}
<li><a href="{{ .URL }}">{{ .Content.Text }}</a></li>
{{ else }}
{{ if .Checkin }}
<li><a href="{{ .URL }}">Checkin at {{ .Checkin.Name }} in {{ .Checkin.Locality }}, {{ .Checkin.Region }}</a></li>
{{ else if .Content }}
<li><a href="{{ .URL }}">{{ .Content.Text }}</a></li>
{{ else }}
<li><a href="{{ .URL }}">{{ .Name }}</a></li>
{{ end }}
<li><a href="{{ .URL }}">{{ .Name }}</a></li>
{{ end }}
{{ end }}
</ul>
{{ end }}
</div>
{{ end }}
</ul>
{{ end }}

24
templates/weekly.md.tmpl

@ -0,0 +1,24 @@
{{ range $k, $items := .Grouped }}
### {{ index $.PostTypes $k }}
{{ range $items -}}
{{- $item := . -}}
{{- if eq $k "like" -}}
{{ range .LikeOf }}* [{{ . }}]({{ . }}){{ end }}
{{ else if eq $k "repost" -}}
{{ range .RepostOf }}* [{{ . }}]({{ . }}){{ end }}
{{ else if eq $k "bookmark" -}}
{{ range .BookmarkOf }}* [{{ $item.Name }}]({{ . }}){{ end }}
{{ else if eq $k "reply" -}}
{{ range .InReplyTo }}* [{{ . }}]({{ . }}){{ end }}
{{ else if eq $k "article" -}}
* [{{ .Name }}]({{ .URL }})
{{ else if .Checkin -}}
* [Checkin at {{ .Checkin.Name }} in {{ .Checkin.Locality }}, {{ .Checkin.Region }}]({{ .URL }})
{{ else if .Content -}}
* {{ .Content.Text }} [link]({{ .URL }})
{{ else -}}
* [{{ .Name }}]({{ .URL }})
{{- end -}}
{{ end -}}
{{ end -}}
Loading…
Cancel
Save