From 2f3ae0c09bdf1dc7d468f0d6dcacf384efba3ce0 Mon Sep 17 00:00:00 2001 From: magical Date: Sat, 1 Jan 2022 00:52:03 +0000 Subject: [PATCH] use the Source, interface --- main.go | 63 ++++++++++++++++++++++++++++++++++------------------- mastodon.go | 22 +++++++++++++++++++ 2 files changed, 62 insertions(+), 23 deletions(-) diff --git a/main.go b/main.go index 89d9af4..59f1093 100644 --- a/main.go +++ b/main.go @@ -17,10 +17,11 @@ import ( const UserAgent = "feedget/0.1" func main() { - var sources = []*FeedSource{ // TODO: interface Source + var sources = []Source{ NewFeed("https://tilde.team/~dozens/dreams/rss.xml"), NewFeed("https://tilde.town/~magical/xkcd.xml"), // "https://xkcd.com/atom.xml", //NewFeed("https://tilde.town/~magical/404.xml"), + NewMastoSource("https://tilde.town/~magical/masto_test.html"), } ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second) @@ -38,36 +39,29 @@ func main() { wg.Wait() for _, src := range sources { - fmt.Println(src.Title, src.Error, src.LastStatus) - for i, x := range src.Items { + fmt.Println(src.GetTitle(), src.GetError()) + for i, x := range src.GetItems() { if i > 5 { - break + //break } - fmt.Println("\t", x.PublishedParsed.Format(time.Stamp), x.Title) + fmt.Println("\t", x.Date.Format("2006 Jan _2 15:04"), x.Text) } } - src := NewMastoSource("https://tilde.town/~magical/masto_test.html") - src.update(ctx) - fmt.Println(src.Title, src.Error, src.LastStatus) - for i, x := range src.Items { - if i > 5 { - //break - } - auth := "" - if x.IsBoost { - auth = "RT @" + x.Author + ": " - } - d, _ := time.Parse(time.RFC3339, x.PublishedString) - fmt.Println("\t", d.Format(time.Stamp), auth+x.Content) - } } type Source interface { - Title() string - Link() string - Error() error - Update(context.Context) + GetTitle() string + //GetLink() string + GetError() error + GetItems() []Item + update(context.Context) +} + +type Item struct { + Date time.Time + Link string + Text string } // want to keep track of: @@ -94,6 +88,8 @@ type FeedSource struct { mu sync.Mutex } +var _ Source = &FeedSource{} + func NewFeed(url string) *FeedSource { return &FeedSource{ URL: url, @@ -158,3 +154,24 @@ func (src *FeedSource) update(ctx context.Context) { src.LastFetch = time.Now() src.Error = nil } + +func (src *FeedSource) GetTitle() string { return src.Title } +func (src *FeedSource) GetError() error { return src.Error } + +func (src *FeedSource) GetItems() (items []Item) { + for _, x := range src.Items { + d := time.Time{} + if x.PublishedParsed != nil { + d = *x.PublishedParsed + } + if x.UpdatedParsed != nil { + d = *x.UpdatedParsed + } + items = append(items, Item{ + Date: d, + Link: x.Link, + Text: x.Title, + }) + } + return +} diff --git a/mastodon.go b/mastodon.go index bd8698f..f8abe06 100644 --- a/mastodon.go +++ b/mastodon.go @@ -69,6 +69,8 @@ type MastoSource struct { mu sync.Mutex } +var _ Source = &MastoSource{} + type MastoFeed struct { Title string Items []*MastoItem @@ -160,6 +162,7 @@ func parseMicroformats(r io.Reader) (*MastoFeed, error) { feed.Title = doc.Find(".h-feed > .p-name").First().AttrOr("value", "") doc.Find(".h-feed").Find(".h-entry, .h-cite").Each(func(i int, elem *goquery.Selection) { cw := strings.TrimSpace(text(elem.Find(".p-summary").First())) + // TODO: move this logic to GetItems if cw != "" { cw = "[" + cw + "] " } @@ -207,3 +210,22 @@ func text(s *goquery.Selection) string { return buf.String() } + +func (src *MastoSource) GetTitle() string { return src.Title } +func (src *MastoSource) GetError() error { return src.Error } + +func (src *MastoSource) GetItems() (items []Item) { + for _, x := range src.Items { + text := x.Content + if x.IsBoost { + text = "RT @" + x.Author + ": " + text + } + d, _ := time.Parse(time.RFC3339, x.PublishedString) + items = append(items, Item{ + Date: d, + Link: x.Link, + Text: text, + }) + } + return +}