2022-06-30 14:54:58 -07:00
|
|
|
package input
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
|
2022-12-10 11:13:45 -08:00
|
|
|
"github.com/jtom38/newsbot/collector/domain/models"
|
2022-06-30 14:54:58 -07:00
|
|
|
"github.com/jtom38/newsbot/collector/services/cache"
|
|
|
|
"github.com/mmcdole/gofeed"
|
|
|
|
)
|
|
|
|
|
|
|
|
type rssClient struct {
|
2022-12-10 11:13:45 -08:00
|
|
|
SourceRecord models.Sources
|
2022-06-30 14:54:58 -07:00
|
|
|
}
|
|
|
|
|
2022-12-10 11:13:45 -08:00
|
|
|
func NewRssClient(sourceRecord models.Sources) rssClient {
|
2022-06-30 14:54:58 -07:00
|
|
|
client := rssClient{
|
|
|
|
SourceRecord: sourceRecord,
|
|
|
|
}
|
|
|
|
|
|
|
|
return client
|
|
|
|
}
|
|
|
|
|
|
|
|
//func (rc rssClient) ReplaceSourceRecord(source model.Sources) {
|
2022-12-04 08:49:17 -08:00
|
|
|
//rc.SourceRecord = source
|
2022-06-30 14:54:58 -07:00
|
|
|
//}
|
|
|
|
|
|
|
|
func (rc rssClient) getCacheGroup() string {
|
|
|
|
return fmt.Sprintf("rss-%v", rc.SourceRecord.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rc rssClient) GetContent() error {
|
|
|
|
feed, err := rc.PullFeed()
|
2022-12-04 08:49:17 -08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-06-30 14:54:58 -07:00
|
|
|
|
|
|
|
cacheClient := cache.NewCacheClient(rc.getCacheGroup())
|
|
|
|
|
|
|
|
for _, item := range feed.Items {
|
|
|
|
log.Println(item)
|
|
|
|
|
|
|
|
cacheClient.FindByValue(item.Link)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rc rssClient) PullFeed() (*gofeed.Feed, error) {
|
|
|
|
feedUri := fmt.Sprintf("%v", rc.SourceRecord.Url)
|
|
|
|
fp := gofeed.NewParser()
|
|
|
|
feed, err := fp.ParseURL(feedUri)
|
2022-12-04 08:49:17 -08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-06-30 14:54:58 -07:00
|
|
|
|
|
|
|
return feed, nil
|
2022-12-04 08:49:17 -08:00
|
|
|
}
|