44 lines
785 B
Go
44 lines
785 B
Go
package lib
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/mmcdole/gofeed"
|
|
)
|
|
|
|
type Video struct {
|
|
ID string
|
|
Title string
|
|
URL string
|
|
}
|
|
|
|
type Youtube struct {
|
|
channelID string
|
|
parser *gofeed.Parser
|
|
}
|
|
|
|
func NewYoutube(channelID string) *Youtube {
|
|
return &Youtube{
|
|
channelID: channelID,
|
|
parser: gofeed.NewParser(),
|
|
}
|
|
}
|
|
|
|
func (y *Youtube) FetchLatest() ([]Video, error) {
|
|
feedURL := fmt.Sprintf("https://www.youtube.com/feeds/videos.xml?channel_id=%s", y.channelID)
|
|
feed, err := y.parser.ParseURL(feedURL)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("parse feed: %w", err)
|
|
}
|
|
|
|
videos := make([]Video, 0, len(feed.Items))
|
|
for _, item := range feed.Items {
|
|
videos = append(videos, Video{
|
|
ID: item.GUID,
|
|
Title: item.Title,
|
|
URL: item.Link,
|
|
})
|
|
}
|
|
return videos, nil
|
|
}
|