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

78 lines
1.5 KiB
Go
Raw Normal View History

package input
import (
"strings"
2024-05-09 18:59:50 -07:00
"git.jamestombleson.com/jtom38/newsbot-api/internal/entity"
"github.com/mmcdole/gofeed"
)
type FeedInput interface {
2024-05-09 18:59:50 -07:00
GetArticles() (entity.ArticleEntity, error)
}
type rssClient struct {
2024-05-09 18:59:50 -07:00
SourceRecord entity.SourceEntity
}
2024-05-09 18:59:50 -07:00
func NewRssClient(sourceRecord entity.SourceEntity) rssClient {
client := rssClient{
SourceRecord: sourceRecord,
}
return client
}
2024-05-09 18:59:50 -07:00
func (rc rssClient) GetArticles() ([]entity.ArticleEntity, error) {
parser := gofeed.NewParser()
feed, err := parser.ParseURL(rc.SourceRecord.Url)
if err != nil {
return nil, err
}
sourceTags := strings.Split(rc.SourceRecord.Tags, ",")
2024-05-09 18:59:50 -07:00
var articles []entity.ArticleEntity
for _, post := range feed.Items {
2024-05-09 18:59:50 -07:00
article := entity.ArticleEntity{
SourceID: rc.SourceRecord.ID,
Title: post.Title,
Description: post.Content,
Url: post.Link,
PubDate: *post.PublishedParsed,
2024-05-02 17:37:18 -07:00
//AuthorName: post.Authors[0].Email,
}
if len(post.Authors) != 0 {
article.AuthorName = post.Authors[0].Email
}
var postTags []string
postTags = append(postTags, sourceTags...)
postTags = append(postTags, post.Categories...)
article.Tags = strings.Join(postTags, ",")
2024-05-02 17:37:18 -07:00
/*
pageContent, err := getHttpContent(article.Url)
if err != nil {
continue
}
htmlNode, err := html.Parse(bytes.NewReader(pageContent))
if err != nil {
continue
}
htmlNode.
fmt.Println(htmlNode)
*/
if post.Image == nil {
article.Thumbnail = ""
}
articles = append(articles, article)
}
return articles, nil
}