owl-blogs/webmention.go

105 lines
2.3 KiB
Go
Raw Normal View History

2022-08-27 21:01:14 +00:00
package owl
import (
"bytes"
"errors"
"net/http"
"strings"
2022-09-01 19:53:06 +00:00
"time"
2022-08-27 21:01:14 +00:00
"golang.org/x/net/html"
)
2022-09-04 13:03:16 +00:00
type WebmentionIn struct {
2022-09-01 19:53:06 +00:00
Source string `yaml:"source"`
Title string `yaml:"title"`
ApprovalStatus string `yaml:"approval_status"`
RetrievedAt time.Time `yaml:"retrieved_at"`
2022-09-01 19:34:33 +00:00
}
2022-09-04 13:03:16 +00:00
type WebmentionOut struct {
Target string `yaml:"target"`
Supported bool `yaml:"supported"`
ScannedAt time.Time `yaml:"scanned_at"`
LastSentAt time.Time `yaml:"last_sent_at"`
}
2022-08-27 21:01:14 +00:00
type HttpRetriever interface {
Get(url string) ([]byte, error)
}
type MicroformatParser interface {
ParseHEntry(data []byte) (ParsedHEntry, error)
}
2022-08-27 21:01:14 +00:00
type OwlHttpRetriever struct{}
type OwlMicroformatParser struct{}
2022-08-27 21:01:14 +00:00
type ParsedHEntry struct {
Title string
}
func (OwlHttpRetriever) Get(url string) ([]byte, error) {
2022-08-27 21:01:14 +00:00
resp, err := http.Get(url)
if err != nil {
return []byte{}, err
}
var data []byte
_, err = resp.Body.Read(data)
// TODO: encoding
return data, err
}
func collectText(n *html.Node, buf *bytes.Buffer) {
if n.Type == html.TextNode {
buf.WriteString(n.Data)
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
collectText(c, buf)
}
}
func (OwlMicroformatParser) ParseHEntry(data []byte) (ParsedHEntry, error) {
2022-08-27 21:01:14 +00:00
doc, err := html.Parse(strings.NewReader(string(data)))
if err != nil {
return ParsedHEntry{}, err
}
var interpretHFeed func(*html.Node, *ParsedHEntry, bool) (ParsedHEntry, error)
interpretHFeed = func(n *html.Node, curr *ParsedHEntry, parent bool) (ParsedHEntry, error) {
attrs := n.Attr
for _, attr := range attrs {
if attr.Key == "class" && strings.Contains(attr.Val, "p-name") {
buf := &bytes.Buffer{}
collectText(n, buf)
curr.Title = buf.String()
return *curr, nil
}
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
interpretHFeed(c, curr, false)
}
return *curr, nil
}
var findHFeed func(*html.Node) (ParsedHEntry, error)
findHFeed = func(n *html.Node) (ParsedHEntry, error) {
attrs := n.Attr
for _, attr := range attrs {
if attr.Key == "class" && strings.Contains(attr.Val, "h-entry") {
return interpretHFeed(n, &ParsedHEntry{}, true)
}
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
entry, err := findHFeed(c)
if err == nil {
return entry, nil
}
}
return ParsedHEntry{}, errors.New("no h-entry found")
}
return findHFeed(doc)
}