2022-04-17 07:25:49 -07:00
|
|
|
package services
|
|
|
|
|
|
|
|
import (
|
2022-06-08 21:17:08 -07:00
|
|
|
"database/sql"
|
2022-04-17 07:25:49 -07:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
2022-05-15 21:48:23 -07:00
|
|
|
|
2022-04-17 07:25:49 -07:00
|
|
|
//"strconv"
|
|
|
|
|
|
|
|
"github.com/PuerkitoBio/goquery"
|
|
|
|
"github.com/go-rod/rod"
|
|
|
|
"github.com/mmcdole/gofeed"
|
|
|
|
|
2022-06-08 21:17:08 -07:00
|
|
|
"github.com/jtom38/newsbot/collector/database"
|
2022-04-17 07:25:49 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
type YoutubeClient struct {
|
2022-06-08 21:17:08 -07:00
|
|
|
record database.Source
|
|
|
|
|
|
|
|
// internal variables at time of collection
|
|
|
|
channelID string
|
|
|
|
avatarUri string
|
2022-04-29 13:02:25 -07:00
|
|
|
|
2022-05-15 21:48:23 -07:00
|
|
|
// config
|
|
|
|
//debug bool
|
2022-04-17 07:25:49 -07:00
|
|
|
|
2022-05-15 21:48:23 -07:00
|
|
|
// cache config
|
|
|
|
cacheGroup string
|
2022-04-17 07:25:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
// This is a local slice to store what URI's have been seen to remove extra calls to the DB
|
|
|
|
YoutubeUriCache []*string
|
2022-05-15 21:48:23 -07:00
|
|
|
ErrYoutubeChannelIdMissing = errors.New("unable to find the channelId on the requested page")
|
2022-04-17 07:25:49 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
const YOUTUBE_FEED_URL string = "https://www.youtube.com/feeds/videos.xml?channel_id="
|
|
|
|
|
2022-06-08 21:17:08 -07:00
|
|
|
func NewYoutubeClient(Record database.Source) YoutubeClient {
|
2022-04-17 07:25:49 -07:00
|
|
|
yc := YoutubeClient{
|
2022-06-08 21:17:08 -07:00
|
|
|
record: Record,
|
2022-04-29 13:02:25 -07:00
|
|
|
cacheGroup: "youtube",
|
2022-04-17 07:25:49 -07:00
|
|
|
}
|
|
|
|
/*
|
2022-05-15 21:48:23 -07:00
|
|
|
cc := NewConfigClient()
|
2022-04-17 07:25:49 -07:00
|
|
|
|
2022-05-15 21:48:23 -07:00
|
|
|
debug, err := strconv.ParseBool(cc.GetConfig(YOUTUBE_DEBUG))
|
|
|
|
if err != nil { panic("'YOUTUBE_DEBUG' was not a bool value")}
|
|
|
|
yc.Config.Debug = debug
|
2022-04-17 07:25:49 -07:00
|
|
|
*/
|
|
|
|
return yc
|
|
|
|
}
|
|
|
|
|
|
|
|
// CheckSource will go and run all the commands needed to process a source.
|
2022-06-08 21:17:08 -07:00
|
|
|
func (yc *YoutubeClient) GetContent() ([]database.Article, error) {
|
|
|
|
var items []database.Article
|
|
|
|
docParser, err := yc.GetParser(yc.record.Url)
|
2022-05-15 21:48:23 -07:00
|
|
|
if err != nil {
|
2022-06-08 21:17:08 -07:00
|
|
|
return items, err
|
2022-05-15 21:48:23 -07:00
|
|
|
}
|
2022-04-17 07:25:49 -07:00
|
|
|
|
|
|
|
// Check cache/db for existing value
|
|
|
|
// If we have the value, skip
|
2022-04-29 13:02:25 -07:00
|
|
|
//channelId, err := yc.extractChannelId()
|
2022-04-17 07:25:49 -07:00
|
|
|
channelId, err := yc.GetChannelId(docParser)
|
2022-05-15 21:48:23 -07:00
|
|
|
if err != nil {
|
2022-06-08 21:17:08 -07:00
|
|
|
return items, err
|
2022-05-15 21:48:23 -07:00
|
|
|
}
|
|
|
|
if channelId == "" {
|
2022-06-08 21:17:08 -07:00
|
|
|
return items, ErrYoutubeChannelIdMissing
|
2022-05-15 21:48:23 -07:00
|
|
|
}
|
2022-06-08 21:17:08 -07:00
|
|
|
yc.channelID = channelId
|
2022-05-15 21:48:23 -07:00
|
|
|
|
2022-04-17 07:25:49 -07:00
|
|
|
// Check the cache/db forthe value.
|
|
|
|
// if we have the value, skip
|
|
|
|
avatar, err := yc.GetAvatarUri()
|
2022-05-15 21:48:23 -07:00
|
|
|
if err != nil {
|
2022-06-08 21:17:08 -07:00
|
|
|
return items, err
|
2022-05-15 21:48:23 -07:00
|
|
|
}
|
|
|
|
if avatar == "" {
|
2022-06-08 21:17:08 -07:00
|
|
|
return items, ErrMissingAuthorImage
|
2022-05-15 21:48:23 -07:00
|
|
|
}
|
2022-06-08 21:17:08 -07:00
|
|
|
yc.avatarUri = avatar
|
2022-04-17 07:25:49 -07:00
|
|
|
|
|
|
|
feed, err := yc.PullFeed()
|
2022-05-15 21:48:23 -07:00
|
|
|
if err != nil {
|
2022-06-08 21:17:08 -07:00
|
|
|
return items, err
|
2022-05-15 21:48:23 -07:00
|
|
|
}
|
|
|
|
|
2022-04-17 07:25:49 -07:00
|
|
|
newPosts, err := yc.CheckForNewPosts(feed)
|
2022-05-15 21:48:23 -07:00
|
|
|
if err != nil {
|
2022-06-08 21:17:08 -07:00
|
|
|
return items, err
|
2022-05-15 21:48:23 -07:00
|
|
|
}
|
2022-04-17 07:25:49 -07:00
|
|
|
|
|
|
|
for _, item := range newPosts {
|
|
|
|
|
|
|
|
article := yc.ConvertToArticle(item)
|
2022-06-08 21:17:08 -07:00
|
|
|
items = append(items, article)
|
2022-04-17 07:25:49 -07:00
|
|
|
|
|
|
|
YoutubeUriCache = append(YoutubeUriCache, &item.Link)
|
|
|
|
|
|
|
|
// Add the post to local cache
|
|
|
|
log.Println(article)
|
|
|
|
}
|
2022-05-15 21:48:23 -07:00
|
|
|
|
2022-06-08 21:17:08 -07:00
|
|
|
return items, nil
|
2022-04-17 07:25:49 -07:00
|
|
|
}
|
|
|
|
|
2022-04-29 13:02:25 -07:00
|
|
|
func (yc *YoutubeClient) GetBrowser() *rod.Browser {
|
|
|
|
browser := rod.New().MustConnect()
|
|
|
|
return browser
|
|
|
|
}
|
|
|
|
|
|
|
|
func (yc *YoutubeClient) GetPage(parser *rod.Browser, url string) *rod.Page {
|
|
|
|
page := parser.MustPage(url)
|
|
|
|
return page
|
|
|
|
}
|
|
|
|
|
2022-04-17 07:25:49 -07:00
|
|
|
func (yc *YoutubeClient) GetParser(uri string) (*goquery.Document, error) {
|
|
|
|
html, err := http.Get(uri)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
defer html.Body.Close()
|
|
|
|
|
|
|
|
doc, err := goquery.NewDocumentFromReader(html.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return doc, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// This pulls the youtube page and finds the ChannelID.
|
|
|
|
// This value is required to generate the RSS feed URI
|
|
|
|
func (yc *YoutubeClient) GetChannelId(doc *goquery.Document) (string, error) {
|
|
|
|
meta := doc.Find("meta")
|
|
|
|
for _, item := range meta.Nodes {
|
|
|
|
|
|
|
|
if item.Attr[0].Val == "channelId" {
|
2022-06-08 21:17:08 -07:00
|
|
|
yc.channelID = item.Attr[1].Val
|
|
|
|
return yc.channelID, nil
|
2022-04-17 07:25:49 -07:00
|
|
|
}
|
|
|
|
}
|
2022-05-15 21:48:23 -07:00
|
|
|
return "", ErrYoutubeChannelIdMissing
|
2022-04-17 07:25:49 -07:00
|
|
|
}
|
|
|
|
|
2022-04-29 13:02:25 -07:00
|
|
|
// This pulls the youtube page and finds the ChannelID.
|
|
|
|
// This value is required to generate the RSS feed URI
|
|
|
|
//func (yc *YoutubeClient) extractChannelId(page *rod.Page) (string, error) {
|
|
|
|
|
|
|
|
//}
|
|
|
|
|
2022-04-17 07:25:49 -07:00
|
|
|
// This will parse the page to find the current Avatar of the channel.
|
|
|
|
func (yc *YoutubeClient) GetAvatarUri() (string, error) {
|
|
|
|
var AvatarUri string
|
|
|
|
|
|
|
|
browser := rod.New().MustConnect()
|
2022-06-08 21:17:08 -07:00
|
|
|
page := browser.MustPage(yc.record.Url)
|
2022-05-15 21:48:23 -07:00
|
|
|
|
2022-04-17 07:25:49 -07:00
|
|
|
res := page.MustElement("#channel-header-container > yt-img-shadow:nth-child(1) > img:nth-child(1)").MustAttribute("src")
|
|
|
|
|
|
|
|
if *res == "" || res == nil {
|
2022-05-15 21:48:23 -07:00
|
|
|
return AvatarUri, ErrMissingAuthorImage
|
2022-04-17 07:25:49 -07:00
|
|
|
}
|
2022-05-15 21:48:23 -07:00
|
|
|
|
2022-04-17 07:25:49 -07:00
|
|
|
AvatarUri = *res
|
|
|
|
|
|
|
|
defer browser.Close()
|
|
|
|
defer page.Close()
|
|
|
|
return AvatarUri, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// This will parse and look for the tags that has been defined by the user.
|
|
|
|
func (yc *YoutubeClient) GetTags(parser *goquery.Document) (string, error) {
|
|
|
|
meta := parser.Find("meta")
|
|
|
|
|
|
|
|
for _, item := range meta.Nodes {
|
|
|
|
if item.Attr[0].Val == "keywords" {
|
|
|
|
res := item.Attr[1].Val
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
}
|
2022-05-15 21:48:23 -07:00
|
|
|
return "", ErrMissingTags
|
2022-04-17 07:25:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (yc *YoutubeClient) GetVideoThumbnail(parser *goquery.Document) (string, error) {
|
|
|
|
meta := parser.Find("meta")
|
|
|
|
|
|
|
|
for _, item := range meta.Nodes {
|
|
|
|
if item.Attr[0].Val == "og:image" {
|
|
|
|
res := item.Attr[1].Val
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
}
|
2022-05-15 21:48:23 -07:00
|
|
|
return "", ErrMissingThumbnail
|
2022-04-17 07:25:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// This will pull the RSS feed items and return the results
|
|
|
|
func (yc *YoutubeClient) PullFeed() (*gofeed.Feed, error) {
|
2022-06-08 21:17:08 -07:00
|
|
|
feedUri := fmt.Sprintf("%v%v", YOUTUBE_FEED_URL, yc.channelID)
|
2022-04-17 07:25:49 -07:00
|
|
|
fp := gofeed.NewParser()
|
|
|
|
feed, err := fp.ParseURL(feedUri)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return feed, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CheckForNewPosts will talk to the Database to see if it has a record for the posts that have been extracted.
|
|
|
|
// If the post does not exist, it will be added.
|
|
|
|
func (yc *YoutubeClient) CheckForNewPosts(feed *gofeed.Feed) ([]*gofeed.Item, error) {
|
|
|
|
var newPosts []*gofeed.Item
|
|
|
|
for _, item := range feed.Items {
|
|
|
|
|
|
|
|
// Check the cache/db to see if this URI has been seen already
|
|
|
|
uriExists := yc.CheckUriCache(&item.Link)
|
2022-05-15 21:48:23 -07:00
|
|
|
if uriExists {
|
|
|
|
continue
|
|
|
|
}
|
2022-04-17 07:25:49 -07:00
|
|
|
|
|
|
|
//TODO Check the DB if the cache is not aware
|
|
|
|
//TODO If the db knew about it, append it to the local cache
|
|
|
|
|
|
|
|
// if its new, append it.
|
|
|
|
newPosts = append(newPosts, item)
|
|
|
|
}
|
|
|
|
|
|
|
|
return newPosts, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (yc *YoutubeClient) CheckUriCache(uri *string) bool {
|
|
|
|
for _, item := range YoutubeUriCache {
|
|
|
|
if item == uri {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-06-08 21:17:08 -07:00
|
|
|
func (yc *YoutubeClient) ConvertToArticle(item *gofeed.Item) database.Article {
|
2022-04-17 07:25:49 -07:00
|
|
|
parser, err := yc.GetParser(item.Link)
|
2022-05-15 21:48:23 -07:00
|
|
|
if err != nil {
|
2022-04-17 07:25:49 -07:00
|
|
|
log.Printf("Unable to process %v, submit this link as an issue.\n", item.Link)
|
|
|
|
}
|
|
|
|
|
|
|
|
tags, err := yc.GetTags(parser)
|
|
|
|
if err != nil {
|
2022-05-15 21:48:23 -07:00
|
|
|
msg := fmt.Sprintf("%v. %v", err, item.Link)
|
2022-04-17 07:25:49 -07:00
|
|
|
log.Println(msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
thumb, err := yc.GetVideoThumbnail(parser)
|
2022-05-15 21:48:23 -07:00
|
|
|
if err != nil {
|
|
|
|
msg := fmt.Sprintf("%v. %v", err, item.Link)
|
2022-04-17 07:25:49 -07:00
|
|
|
log.Println(msg)
|
|
|
|
}
|
|
|
|
|
2022-06-08 21:17:08 -07:00
|
|
|
var article = database.Article{
|
|
|
|
Sourceid: yc.record.ID,
|
2022-04-17 07:25:49 -07:00
|
|
|
Tags: tags,
|
|
|
|
Title: item.Title,
|
|
|
|
Url: item.Link,
|
2022-06-08 21:17:08 -07:00
|
|
|
Pubdate: *item.PublishedParsed,
|
2022-04-17 07:25:49 -07:00
|
|
|
Thumbnail: thumb,
|
|
|
|
Description: item.Description,
|
2022-06-08 21:17:08 -07:00
|
|
|
Authorname: sql.NullString{String: item.Author.Name},
|
|
|
|
Authorimage: sql.NullString{String: yc.avatarUri},
|
2022-04-17 07:25:49 -07:00
|
|
|
}
|
|
|
|
return article
|
|
|
|
}
|