newsbot-api/internal/services/input/rss.go

59 lines
1.2 KiB
Go

package input
import (
"strings"
"git.jamestombleson.com/jtom38/newsbot-api/internal/domain"
"github.com/mmcdole/gofeed"
)
type FeedInput interface {
GetArticles() (domain.ArticleEntity, error)
}
type rssClient struct {
SourceRecord domain.SourceEntity
}
func NewRssClient(sourceRecord domain.SourceEntity) rssClient {
client := rssClient{
SourceRecord: sourceRecord,
}
return client
}
func (rc rssClient) GetArticles() ([]domain.ArticleEntity, error) {
parser := gofeed.NewParser()
feed, err := parser.ParseURL(rc.SourceRecord.Url)
if err != nil {
return nil, err
}
sourceTags := strings.Split(rc.SourceRecord.Tags, ",")
var articles []domain.ArticleEntity
for _, post := range feed.Items {
article := domain.ArticleEntity{
SourceID: rc.SourceRecord.ID,
Title: post.Title,
Description: post.Content,
Url: post.Link,
PubDate: *post.PublishedParsed,
AuthorName: post.Author.Email,
}
var postTags []string
postTags = append(postTags, sourceTags...)
postTags = append(postTags, post.Categories...)
article.Tags = strings.Join(postTags, ",")
if post.Image == nil {
article.Thumbnail = ""
}
articles = append(articles, article)
}
return articles, nil
}