From 52da2c5bb19e6b1939d4baef36db76ddd4d40013 Mon Sep 17 00:00:00 2001 From: Aaron Bieber Date: Thu, 16 Feb 2023 08:25:50 -0700 Subject: [PATCH] parse html from rss feed --- flake.nix | 2 +- go.mod | 2 +- main.go | 6 +++++- rss.go | 32 ++++++++++++++++++++++++++------ 4 files changed, 33 insertions(+), 9 deletions(-) diff --git a/flake.nix b/flake.nix index e0a1f95..0ee479d 100644 --- a/flake.nix +++ b/flake.nix @@ -16,7 +16,7 @@ in { xintray = pkgs.buildGoModule { pname = "xintray"; - version = "v0.1.6"; + version = "v0.1.7"; src = ./.; vendorSha256 = diff --git a/go.mod b/go.mod index 2c5ec24..1d9798a 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.17 require ( fyne.io/fyne/v2 v2.3.1 golang.org/x/crypto v0.6.0 + golang.org/x/net v0.6.0 ) require ( @@ -31,7 +32,6 @@ require ( github.com/yuin/goldmark v1.5.3 // indirect golang.org/x/image v0.3.0 // indirect golang.org/x/mobile v0.0.0-20221110043201-43a038452099 // indirect - golang.org/x/net v0.6.0 // indirect golang.org/x/sys v0.5.0 // indirect golang.org/x/text v0.7.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect diff --git a/main.go b/main.go index 9a5694e..07c6321 100644 --- a/main.go +++ b/main.go @@ -172,7 +172,11 @@ func (x *xinStatus) updateRepoInfo() error { if err = xml.NewDecoder(res.Body).Decode(&resp); err != nil { return err } - x.repoCommit = resp.LatestHash() + cmit, err := resp.LatestHash() + if err != nil { + return err + } + x.repoCommit = *cmit } return nil diff --git a/rss.go b/rss.go index cb5242a..41b322f 100644 --- a/rss.go +++ b/rss.go @@ -2,9 +2,10 @@ package main import ( "encoding/xml" - "html" "strings" "time" + + "golang.org/x/net/html" ) type Feed struct { @@ -51,11 +52,30 @@ type Feed struct { } `xml:"entry"` } -func (f *Feed) LatestHash() commit { - return commit{ +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) + + return &commit{ hash: strings.Split(f.Entry[0].ID, "/")[1], // TODO: use x/html to pull out the info? - message: html.UnescapeString(f.Entry[0].Content.Text), - date: f.Entry[0].Updated, - } + message: cmitMsg, + //message: html.UnescapeString(f.Entry[0].Content.Text), + date: f.Entry[0].Updated, + }, nil }