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

59 lines
1.2 KiB
Go
Raw Normal View History

package input
import (
"strings"
2024-04-23 07:15:38 -07:00
"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
}