|
|
|
@ -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,11 +67,15 @@ func main() {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
output := re.ReplaceAll(data, buf.Bytes())
|
|
|
|
|
err = ioutil.WriteFile(filename, output, 0666)
|
|
|
|
|
if *show {
|
|
|
|
|
io.Copy(os.Stdout, &buf)
|
|
|
|
|
} else {
|
|
|
|
|
fmt.Println("micropub")
|
|
|
|
|
err = micropub(summary.Name, buf.String(), *server)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getSummary(monday time.Time, items []microsub.Item) Summary {
|
|
|
|
@ -90,66 +93,29 @@ 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)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
}
|
|
|
|
|