2022-05-15 21:48:23 -07:00
|
|
|
package cron
|
2022-04-02 12:05:32 -07:00
|
|
|
|
|
|
|
import (
|
2022-06-08 21:17:08 -07:00
|
|
|
"context"
|
|
|
|
"database/sql"
|
2022-04-29 13:02:25 -07:00
|
|
|
"log"
|
2022-06-08 21:17:08 -07:00
|
|
|
"time"
|
2022-04-29 13:02:25 -07:00
|
|
|
|
2022-06-08 21:17:08 -07:00
|
|
|
"github.com/google/uuid"
|
|
|
|
_ "github.com/lib/pq"
|
2022-04-02 12:05:32 -07:00
|
|
|
"github.com/robfig/cron/v3"
|
2022-04-29 13:02:25 -07:00
|
|
|
|
|
|
|
"github.com/jtom38/newsbot/collector/database"
|
|
|
|
"github.com/jtom38/newsbot/collector/services"
|
2022-06-08 21:17:08 -07:00
|
|
|
"github.com/jtom38/newsbot/collector/services/config"
|
2022-04-02 12:05:32 -07:00
|
|
|
)
|
|
|
|
|
2022-06-19 22:02:44 -07:00
|
|
|
type Cron struct {
|
|
|
|
Db *database.Queries
|
|
|
|
ctx *context.Context
|
|
|
|
timer *cron.Cron
|
|
|
|
}
|
2022-04-29 13:02:25 -07:00
|
|
|
|
2022-06-19 22:02:44 -07:00
|
|
|
func openDatabase() (*database.Queries, error) {
|
|
|
|
_env := config.New()
|
|
|
|
connString := _env.GetConfig(config.Sql_Connection_String)
|
|
|
|
db, err := sql.Open("postgres", connString)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2022-04-29 13:02:25 -07:00
|
|
|
|
2022-06-19 22:02:44 -07:00
|
|
|
queries := database.New(db)
|
|
|
|
return queries, err
|
2022-04-29 13:02:25 -07:00
|
|
|
}
|
|
|
|
|
2022-06-19 22:02:44 -07:00
|
|
|
func New(ctx context.Context) *Cron {
|
|
|
|
c := &Cron{
|
|
|
|
ctx: &ctx,
|
|
|
|
}
|
|
|
|
|
|
|
|
timer := cron.New()
|
|
|
|
queries, err := openDatabase()
|
2022-06-08 21:17:08 -07:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2022-06-19 22:02:44 -07:00
|
|
|
c.Db = queries
|
|
|
|
|
|
|
|
//timer.AddFunc("*/5 * * * *", func() { go CheckCache() })
|
|
|
|
//timer.AddFunc("* */30 * * *", func() { go c.CheckReddit(ctx) })
|
|
|
|
//timer.AddFunc("* */1 * * *", func() { go CheckYoutube() })
|
|
|
|
//timer.AddFunc("* */1 * * *", func() { go CheckFfxiv() })
|
|
|
|
//timer.AddFunc("* */1 * * *", func() { go CheckTwitch() })
|
|
|
|
c.timer = timer
|
|
|
|
return c
|
|
|
|
}
|
2022-04-29 13:02:25 -07:00
|
|
|
|
2022-06-19 22:02:44 -07:00
|
|
|
func (c *Cron) Start() {
|
|
|
|
c.timer.Start()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Cron) Stop() {
|
|
|
|
c.timer.Stop()
|
2022-04-29 13:02:25 -07:00
|
|
|
}
|
|
|
|
|
2022-06-08 21:17:08 -07:00
|
|
|
// This is the main entry point to query all the reddit services
|
2022-06-19 22:02:44 -07:00
|
|
|
func (c *Cron) CheckReddit(ctx context.Context) {
|
|
|
|
sources, err := c.Db.ListSourcesBySource(*c.ctx, "reddit")
|
2022-06-08 21:17:08 -07:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("No defines sources for reddit to query - %v\r", err)
|
|
|
|
}
|
2022-04-29 13:02:25 -07:00
|
|
|
|
2022-06-08 21:17:08 -07:00
|
|
|
for _, source := range sources {
|
|
|
|
if !source.Enabled {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
rc := services.NewRedditClient(source)
|
|
|
|
raw, err := rc.GetContent()
|
2022-04-29 13:02:25 -07:00
|
|
|
if err != nil {
|
2022-06-08 21:17:08 -07:00
|
|
|
log.Println(err)
|
2022-04-29 13:02:25 -07:00
|
|
|
}
|
2022-06-08 21:17:08 -07:00
|
|
|
redditArticles := rc.ConvertToArticles(raw)
|
2022-06-19 22:02:44 -07:00
|
|
|
c.checkPosts(*c.ctx, redditArticles)
|
2022-04-29 13:02:25 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-19 22:02:44 -07:00
|
|
|
func (c *Cron) CheckYoutube(ctx context.Context) {
|
2022-04-29 13:02:25 -07:00
|
|
|
// Add call to the db to request youtube sources.
|
2022-06-19 22:02:44 -07:00
|
|
|
sources, err := c.Db.ListSourcesBySource(*c.ctx, "youtube")
|
2022-06-08 21:17:08 -07:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("Youtube - No sources found to query - %v\r", err)
|
|
|
|
}
|
2022-04-29 13:02:25 -07:00
|
|
|
|
2022-06-08 21:17:08 -07:00
|
|
|
for _, source := range sources {
|
|
|
|
if !source.Enabled {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
yc := services.NewYoutubeClient(source)
|
|
|
|
raw, err := yc.GetContent()
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
2022-06-19 22:02:44 -07:00
|
|
|
c.checkPosts(*c.ctx, raw)
|
2022-06-08 21:17:08 -07:00
|
|
|
}
|
2022-04-29 13:02:25 -07:00
|
|
|
}
|
|
|
|
|
2022-06-19 22:02:44 -07:00
|
|
|
func (c *Cron) CheckFfxiv(ctx context.Context) {
|
|
|
|
sources, err := c.Db.ListSourcesBySource(*c.ctx, "ffxiv")
|
2022-06-08 21:17:08 -07:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("Final Fantasy XIV - No sources found to query - %v\r", err)
|
|
|
|
}
|
2022-04-29 13:02:25 -07:00
|
|
|
|
2022-06-08 21:17:08 -07:00
|
|
|
for _, source := range sources {
|
|
|
|
if !source.Enabled {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
fc := services.NewFFXIVClient(source)
|
|
|
|
items, err := fc.CheckSource()
|
2022-04-29 13:02:25 -07:00
|
|
|
if err != nil {
|
2022-06-08 21:17:08 -07:00
|
|
|
log.Println(err)
|
2022-04-29 13:02:25 -07:00
|
|
|
}
|
2022-06-19 22:02:44 -07:00
|
|
|
c.checkPosts(*c.ctx, items)
|
2022-04-29 13:02:25 -07:00
|
|
|
}
|
2022-05-15 21:48:23 -07:00
|
|
|
}
|
|
|
|
|
2022-06-19 22:02:44 -07:00
|
|
|
func (c *Cron) CheckTwitch(ctx context.Context) error {
|
|
|
|
sources, err := c.Db.ListSourcesBySource(*c.ctx, "twitch")
|
2022-06-08 21:17:08 -07:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("Twitch - No sources found to query - %v\r", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tc, err := services.NewTwitchClient()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-05-15 21:48:23 -07:00
|
|
|
|
2022-06-08 21:17:08 -07:00
|
|
|
for _, source := range sources {
|
|
|
|
if !source.Enabled {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
tc.ReplaceSourceRecord(source)
|
|
|
|
items, err := tc.GetContent()
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
2022-06-19 22:02:44 -07:00
|
|
|
c.checkPosts(*c.ctx, items)
|
2022-06-08 21:17:08 -07:00
|
|
|
}
|
2022-05-15 21:48:23 -07:00
|
|
|
|
2022-06-08 21:17:08 -07:00
|
|
|
return nil
|
|
|
|
}
|
2022-05-15 21:48:23 -07:00
|
|
|
|
2022-06-19 22:02:44 -07:00
|
|
|
func (c *Cron) checkPosts(ctx context.Context, posts []database.Article) {
|
2022-06-08 21:17:08 -07:00
|
|
|
for _, item := range posts {
|
2022-06-19 22:02:44 -07:00
|
|
|
_, err := c.Db.GetArticleByUrl(*c.ctx, item.Url)
|
2022-06-08 21:17:08 -07:00
|
|
|
if err != nil {
|
2022-06-19 22:02:44 -07:00
|
|
|
err = c.postArticle(ctx, item)
|
2022-05-15 21:48:23 -07:00
|
|
|
if err != nil {
|
2022-06-08 21:17:08 -07:00
|
|
|
log.Printf("Reddit - Failed to post article - %v - %v.\r", item.Url, err)
|
|
|
|
} else {
|
|
|
|
log.Printf("Reddit - Posted article - %v\r", item.Url)
|
2022-05-15 21:48:23 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-06-08 21:17:08 -07:00
|
|
|
time.Sleep(30 * time.Second)
|
|
|
|
}
|
2022-05-15 21:48:23 -07:00
|
|
|
|
2022-06-19 22:02:44 -07:00
|
|
|
func (c *Cron) postArticle(ctx context.Context, item database.Article) error {
|
|
|
|
err := c.Db.CreateArticle(*c.ctx, database.CreateArticleParams{
|
2022-06-08 21:17:08 -07:00
|
|
|
ID: uuid.New(),
|
|
|
|
Sourceid: item.Sourceid,
|
|
|
|
Tags: item.Tags,
|
|
|
|
Title: item.Title,
|
|
|
|
Url: item.Url,
|
|
|
|
Pubdate: item.Pubdate,
|
|
|
|
Video: item.Video,
|
|
|
|
Videoheight: item.Videoheight,
|
|
|
|
Videowidth: item.Videowidth,
|
|
|
|
Thumbnail: item.Thumbnail,
|
|
|
|
Description: item.Description,
|
|
|
|
Authorname: item.Authorname,
|
|
|
|
Authorimage: item.Authorimage,
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
}
|