Add -show and -endpoint, remove file processing
This commit is contained in:
parent
aecf1fd6b3
commit
b7f2219d49
2
go.mod
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
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=
|
||||
|
|
160
main.go
160
main.go
|
@ -2,17 +2,16 @@ package main
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"flag"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"os/exec"
|
||||
"regexp"
|
||||
"strings"
|
||||
tt "text/template"
|
||||
"time"
|
||||
|
||||
"github.com/jinzhu/now"
|
||||
|
@ -21,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
|
||||
|
@ -31,33 +36,17 @@ type Summary struct {
|
|||
func init() {
|
||||
log.SetFlags(log.LstdFlags | log.Lshortfile)
|
||||
}
|
||||
|
||||
func micropub(name, content, server string) error {
|
||||
var args []string
|
||||
if server != "" {
|
||||
args = append(args, "-s", server)
|
||||
}
|
||||
args = append(args, "-d", "article", "--html", "--json", name, content)
|
||||
cmd := exec.Command("shpub", args...)
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
return cmd.Run()
|
||||
}
|
||||
|
||||
func main() {
|
||||
u, err := url.Parse(os.Args[1])
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
flag.Parse()
|
||||
|
||||
if !*show && *server == "" {
|
||||
log.Println("Specify -endpoint when using micropub")
|
||||
return
|
||||
}
|
||||
|
||||
var filename string
|
||||
outputFile := false
|
||||
if len(os.Args) >= 3 {
|
||||
filename = os.Args[2]
|
||||
outputFile = true
|
||||
}
|
||||
args := flag.Args()
|
||||
|
||||
re, err := regexp.Compile(`<span class="next-entry"></span>`)
|
||||
u, err := url.Parse(args[0])
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
@ -73,35 +62,16 @@ func main() {
|
|||
|
||||
summary := getSummary(monday, items)
|
||||
|
||||
if outputFile {
|
||||
_, err = buf.Write([]byte(`<span class="next-entry"></span>`))
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
err = outputEntries(summary, &buf)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
if outputFile {
|
||||
f, err := forceCreate(filename)
|
||||
|
||||
data, err := ioutil.ReadAll(f)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
f.Close()
|
||||
|
||||
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 {
|
||||
err = micropub(summary.Name, buf.String(), "p83.nl")
|
||||
fmt.Println("micropub")
|
||||
err = micropub(summary.Name, buf.String(), *server)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
@ -123,59 +93,20 @@ func getSummary(monday time.Time, items []microsub.Item) Summary {
|
|||
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",
|
||||
"repost": "Reposts",
|
||||
"like": "Likes",
|
||||
"reply": "Replies",
|
||||
"bookmark": "Bookmarks",
|
||||
"photo": "Photos",
|
||||
"note": "Notes",
|
||||
"article": "Articles",
|
||||
}
|
||||
|
||||
t, err := template.ParseFiles("templates/weekly.html")
|
||||
t, err := tt.ParseFiles("templates/weekly.md.tmpl")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
@ -184,6 +115,7 @@ func outputEntries(summary Summary, w io.Writer) error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -197,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"
|
||||
}
|
||||
|
||||
|
@ -243,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
|
||||
}
|
||||
|
@ -270,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()
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
<h3>{{ index $.PostTypes $k }}</h3>
|
||||
<ul class="{{ $k }}">
|
||||
{{ range $items }}
|
||||
{{ $item := . }}
|
||||
{{ if eq $k "like-of" }}
|
||||
{{ range .LikeOf }}
|
||||
<li><a href="{{ . }}">{{ . }}</a></li>
|
||||
|
@ -10,10 +11,16 @@
|
|||
{{ 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 }}
|
||||
|
|
24
templates/weekly.md.tmpl
Normal file
24
templates/weekly.md.tmpl
Normal file
|
@ -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…
Reference in New Issue
Block a user