sn-rss2tg/rss.go

70 lines
1.3 KiB
Go
Raw Permalink Normal View History

package main
import (
"encoding/xml"
"fmt"
"log"
"net/http"
"time"
)
type Item struct {
Guid string `xml:"guid"`
Title string `xml:"title"`
Link string `xml:"link"`
Description string `xml:"description"`
PubDate RssDate `xml:"pubDate"`
}
type Channel struct {
Title string `xml:"title"`
Description string `xml:"description"`
Link string `xml:"link"`
Items []Item `xml:"item"`
LastBuildDate RssDate `xml:"lastBuildDate"`
}
type Rss struct {
Channel Channel `xml:"channel"`
}
type RssDate struct {
time.Time
}
func (c *RssDate) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var v string
dateFormat := "Mon, 02 Jan 2006 15:04:05 GMT"
d.DecodeElement(&v, &start)
parse, err := time.Parse(dateFormat, v)
if err != nil {
return err
}
*c = RssDate{parse}
return nil
}
var (
StackerNewsRssFeedUrl = "https://stacker.news/rss"
)
func FetchStackerNewsRssFeed() (*Rss, error) {
resp, err := http.Get(StackerNewsRssFeedUrl)
if err != nil {
err = fmt.Errorf("error fetching RSS feed: %w\n", err)
log.Println(err)
return nil, err
}
defer resp.Body.Close()
var rss Rss
err = xml.NewDecoder(resp.Body).Decode(&rss)
if err != nil {
err = fmt.Errorf("error decoding RSS feed XML: %w\n", err)
return nil, err
}
return &rss, nil
}