2021-04-10 10:17:38 +02:00
|
|
|
package rss
|
|
|
|
|
|
|
|
import (
|
|
|
|
"brainbaking.com/go-jamming/common"
|
|
|
|
"encoding/xml"
|
|
|
|
"errors"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2021-04-13 09:10:32 +02:00
|
|
|
// someone already did this for me, yay!
|
2021-04-10 10:17:38 +02:00
|
|
|
type Rss2 struct {
|
|
|
|
XMLName xml.Name `xml:"rss"`
|
|
|
|
Version string `xml:"version,attr"`
|
|
|
|
// Required
|
|
|
|
Title string `xml:"channel>title"`
|
|
|
|
Link string `xml:"channel>link"`
|
|
|
|
Description string `xml:"channel>description"`
|
|
|
|
// Optional
|
|
|
|
PubDate string `xml:"channel>pubDate"`
|
|
|
|
ItemList []Item `xml:"channel>item"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Item struct {
|
|
|
|
// Required
|
2021-04-10 16:16:08 +02:00
|
|
|
Title string `xml:"title"`
|
|
|
|
Link string `xml:"link"`
|
|
|
|
Description string `xml:"description"` // could also be template.HTML, not interested in that
|
2021-04-10 10:17:38 +02:00
|
|
|
// Optional
|
2021-04-10 16:16:08 +02:00
|
|
|
Content string `xml:"encoded"`
|
|
|
|
PubDate string `xml:"pubDate"`
|
|
|
|
Comments string `xml:"comments"`
|
2021-04-10 10:17:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (itm Item) PubDateAsTime() time.Time {
|
|
|
|
// format: Tue, 16 Mar 2021 17:07:14 +0000
|
|
|
|
t, err := time.Parse("Mon, 02 Jan 2006 15:04:05 +0000", itm.PubDate)
|
|
|
|
if err != nil {
|
|
|
|
log.Warn().Str("pubDate", itm.PubDate).Msg("Incorrectly formatted RSS date, reverting to now")
|
|
|
|
return common.Now()
|
|
|
|
}
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
|
|
|
type Link struct {
|
|
|
|
Href string `xml:"href,attr"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Author struct {
|
|
|
|
Name string `xml:"name"`
|
|
|
|
Email string `xml:"email"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Entry struct {
|
|
|
|
Title string `xml:"title"`
|
|
|
|
Summary string `xml:"summary"`
|
|
|
|
Content string `xml:"content"`
|
|
|
|
Id string `xml:"id"`
|
|
|
|
Updated string `xml:"updated"`
|
|
|
|
Link Link `xml:"link"`
|
|
|
|
Author Author `xml:"author"`
|
|
|
|
}
|
|
|
|
|
2021-04-13 09:10:32 +02:00
|
|
|
// Based on https://siongui.github.io/2015/03/03/go-parse-web-feed-rss-atom/
|
2021-04-10 16:16:08 +02:00
|
|
|
func ParseFeed(content []byte) (*Rss2, error) {
|
|
|
|
v := &Rss2{}
|
|
|
|
err := xml.Unmarshal(content, v)
|
2021-04-10 10:17:38 +02:00
|
|
|
if err != nil {
|
|
|
|
return v, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if v.Version == "2.0" {
|
|
|
|
for i, _ := range v.ItemList {
|
|
|
|
if v.ItemList[i].Content != "" {
|
|
|
|
v.ItemList[i].Description = v.ItemList[i].Content
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return v, nil
|
|
|
|
}
|
|
|
|
|
2021-04-13 09:10:32 +02:00
|
|
|
return v, errors.New("ParseFeed: not RSS 2.0")
|
2021-04-10 10:17:38 +02:00
|
|
|
}
|