initial commit
This commit is contained in:
43
lib/youtube.go
Normal file
43
lib/youtube.go
Normal file
@@ -0,0 +1,43 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user