xintray/rss.go

89 lines
2.0 KiB
Go
Raw Normal View History

package main
import (
"encoding/xml"
2024-04-26 07:13:28 -06:00
"fmt"
"strings"
"time"
2023-02-16 08:25:50 -07:00
"golang.org/x/net/html"
)
type Feed struct {
XMLName xml.Name `xml:"feed"`
Text string `xml:",chardata"`
Xmlns string `xml:"xmlns,attr"`
Media string `xml:"media,attr"`
Lang string `xml:"lang,attr"`
ID string `xml:"id"`
Link []struct {
Text string `xml:",chardata"`
Type string `xml:"type,attr"`
Rel string `xml:"rel,attr"`
Href string `xml:"href,attr"`
} `xml:"link"`
Title string `xml:"title"`
Updated time.Time `xml:"updated"`
Entry []struct {
Text string `xml:",chardata"`
ID string `xml:"id"`
Link struct {
Text string `xml:",chardata"`
Type string `xml:"type,attr"`
Rel string `xml:"rel,attr"`
Href string `xml:"href,attr"`
} `xml:"link"`
Title string `xml:"title"`
Updated time.Time `xml:"updated"`
Thumbnail struct {
Text string `xml:",chardata"`
Height string `xml:"height,attr"`
Width string `xml:"width,attr"`
URL string `xml:"url,attr"`
} `xml:"thumbnail"`
Author struct {
Text string `xml:",chardata"`
Name string `xml:"name"`
URI string `xml:"uri"`
} `xml:"author"`
Content struct {
Text string `xml:",chardata"`
Type string `xml:"type,attr"`
} `xml:"content"`
} `xml:"entry"`
}
2023-02-16 08:25:50 -07:00
func (f *Feed) LatestHash() (*commit, error) {
doc, err := html.Parse(strings.NewReader(f.Entry[0].Content.Text))
if err != nil {
return nil, err
}
cmitMsg := ""
var h func(*html.Node)
h = func(n *html.Node) {
if n.Type == html.ElementNode && n.Data == "pre" {
cmitMsg = n.FirstChild.Data
return
}
for child := n.FirstChild; child != nil; child = child.NextSibling {
h(child)
}
}
h(doc)
2024-04-26 07:13:28 -06:00
hashParts := strings.Split(f.Entry[0].ID, "/")
if len(hashParts) != 2 {
return nil, fmt.Errorf("invalid hash")
}
hash := hashParts[1]
2023-02-16 08:25:50 -07:00
return &commit{
2024-04-26 07:13:28 -06:00
hash: hash,
// TODO: use x/html to pull out the info?
2023-02-16 08:25:50 -07:00
message: cmitMsg,
//message: html.UnescapeString(f.Entry[0].Content.Text),
date: f.Entry[0].Updated,
}, nil
}