use the Source, interface
parent
4017f94b56
commit
2f3ae0c09b
63
main.go
63
main.go
|
@ -17,10 +17,11 @@ import (
|
||||||
const UserAgent = "feedget/0.1"
|
const UserAgent = "feedget/0.1"
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var sources = []*FeedSource{ // TODO: interface Source
|
var sources = []Source{
|
||||||
NewFeed("https://tilde.team/~dozens/dreams/rss.xml"),
|
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/xkcd.xml"), // "https://xkcd.com/atom.xml",
|
||||||
//NewFeed("https://tilde.town/~magical/404.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)
|
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
|
||||||
|
@ -38,36 +39,29 @@ func main() {
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
|
|
||||||
for _, src := range sources {
|
for _, src := range sources {
|
||||||
fmt.Println(src.Title, src.Error, src.LastStatus)
|
fmt.Println(src.GetTitle(), src.GetError())
|
||||||
for i, x := range src.Items {
|
for i, x := range src.GetItems() {
|
||||||
if i > 5 {
|
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 {
|
type Source interface {
|
||||||
Title() string
|
GetTitle() string
|
||||||
Link() string
|
//GetLink() string
|
||||||
Error() error
|
GetError() error
|
||||||
Update(context.Context)
|
GetItems() []Item
|
||||||
|
update(context.Context)
|
||||||
|
}
|
||||||
|
|
||||||
|
type Item struct {
|
||||||
|
Date time.Time
|
||||||
|
Link string
|
||||||
|
Text string
|
||||||
}
|
}
|
||||||
|
|
||||||
// want to keep track of:
|
// want to keep track of:
|
||||||
|
@ -94,6 +88,8 @@ type FeedSource struct {
|
||||||
mu sync.Mutex
|
mu sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var _ Source = &FeedSource{}
|
||||||
|
|
||||||
func NewFeed(url string) *FeedSource {
|
func NewFeed(url string) *FeedSource {
|
||||||
return &FeedSource{
|
return &FeedSource{
|
||||||
URL: url,
|
URL: url,
|
||||||
|
@ -158,3 +154,24 @@ func (src *FeedSource) update(ctx context.Context) {
|
||||||
src.LastFetch = time.Now()
|
src.LastFetch = time.Now()
|
||||||
src.Error = nil
|
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
|
||||||
|
}
|
||||||
|
|
22
mastodon.go
22
mastodon.go
|
@ -69,6 +69,8 @@ type MastoSource struct {
|
||||||
mu sync.Mutex
|
mu sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var _ Source = &MastoSource{}
|
||||||
|
|
||||||
type MastoFeed struct {
|
type MastoFeed struct {
|
||||||
Title string
|
Title string
|
||||||
Items []*MastoItem
|
Items []*MastoItem
|
||||||
|
@ -160,6 +162,7 @@ func parseMicroformats(r io.Reader) (*MastoFeed, error) {
|
||||||
feed.Title = doc.Find(".h-feed > .p-name").First().AttrOr("value", "")
|
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) {
|
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()))
|
cw := strings.TrimSpace(text(elem.Find(".p-summary").First()))
|
||||||
|
// TODO: move this logic to GetItems
|
||||||
if cw != "" {
|
if cw != "" {
|
||||||
cw = "[" + cw + "] "
|
cw = "[" + cw + "] "
|
||||||
}
|
}
|
||||||
|
@ -207,3 +210,22 @@ func text(s *goquery.Selection) string {
|
||||||
|
|
||||||
return buf.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
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue