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-07-12 15:28:31 -07:00
|
|
|
"fmt"
|
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
|
|
|
|
2024-04-23 07:15:38 -07:00
|
|
|
"git.jamestombleson.com/jtom38/newsbot-api/internal/database"
|
|
|
|
"git.jamestombleson.com/jtom38/newsbot-api/internal/services"
|
|
|
|
"git.jamestombleson.com/jtom38/newsbot-api/internal/services/input"
|
|
|
|
"git.jamestombleson.com/jtom38/newsbot-api/internal/services/output"
|
2022-04-02 12:05:32 -07:00
|
|
|
)
|
|
|
|
|
2022-06-19 22:02:44 -07:00
|
|
|
type Cron struct {
|
2022-06-30 14:54:58 -07:00
|
|
|
Db *database.Queries
|
|
|
|
ctx *context.Context
|
2022-06-19 22:02:44 -07:00
|
|
|
timer *cron.Cron
|
|
|
|
}
|
2022-04-29 13:02:25 -07:00
|
|
|
|
2022-06-19 22:02:44 -07:00
|
|
|
func openDatabase() (*database.Queries, error) {
|
2024-04-23 07:15:38 -07:00
|
|
|
_env := services.NewConfig()
|
|
|
|
connString := _env.GetConfig(services.Sql_Connection_String)
|
2022-07-13 15:52:25 -07:00
|
|
|
if connString == "" {
|
|
|
|
panic("Connection String is null!")
|
|
|
|
}
|
2022-06-19 22:02:44 -07:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-04-23 07:15:38 -07:00
|
|
|
func NewScheduler(ctx context.Context) *Cron {
|
2022-06-19 22:02:44 -07:00
|
|
|
c := &Cron{
|
2022-06-30 14:54:58 -07:00
|
|
|
ctx: &ctx,
|
2022-06-19 22:02:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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() })
|
2024-04-23 07:15:38 -07:00
|
|
|
features := services.NewConfig()
|
2022-06-30 14:54:58 -07:00
|
|
|
|
2024-04-23 07:15:38 -07:00
|
|
|
res, _ := features.GetFeature(services.FEATURE_ENABLE_REDDIT_BACKEND)
|
2022-06-30 14:54:58 -07:00
|
|
|
if res {
|
2022-07-12 15:28:31 -07:00
|
|
|
timer.AddFunc("5 1-23 * * *", func() { go c.CheckReddit() })
|
|
|
|
log.Print("[Input] Reddit backend was enabled")
|
2022-06-30 14:54:58 -07:00
|
|
|
//go c.CheckReddit()
|
|
|
|
}
|
|
|
|
|
2024-04-23 07:15:38 -07:00
|
|
|
res, _ = features.GetFeature(services.FEATURE_ENABLE_YOUTUBE_BACKEND)
|
2022-06-30 14:54:58 -07:00
|
|
|
if res {
|
2022-07-12 15:28:31 -07:00
|
|
|
timer.AddFunc("10 1-23 * * *", func() { go c.CheckYoutube() })
|
|
|
|
log.Print("[Input] YouTube backend was enabled")
|
2022-06-30 14:54:58 -07:00
|
|
|
}
|
|
|
|
|
2024-04-23 07:15:38 -07:00
|
|
|
res, _ = features.GetFeature(services.FEATURE_ENABLE_FFXIV_BACKEND)
|
2022-06-30 14:54:58 -07:00
|
|
|
if res {
|
2022-07-12 15:28:31 -07:00
|
|
|
timer.AddFunc("5 5,10,15,20 * * *", func() { go c.CheckFfxiv() })
|
|
|
|
log.Print("[Input] FFXIV backend was enabled")
|
2022-06-30 14:54:58 -07:00
|
|
|
}
|
|
|
|
|
2024-04-23 07:15:38 -07:00
|
|
|
res, _ = features.GetFeature(services.FEATURE_ENABLE_TWITCH_BACKEND)
|
2022-06-30 14:54:58 -07:00
|
|
|
if res {
|
2022-07-12 15:28:31 -07:00
|
|
|
timer.AddFunc("15 1-23 * * *", func() { go c.CheckTwitch() })
|
|
|
|
log.Print("[Input] Twitch backend was enabled")
|
2022-06-30 14:54:58 -07:00
|
|
|
}
|
2022-07-12 15:28:31 -07:00
|
|
|
|
|
|
|
timer.AddFunc("*/5 * * * *", func() { go c.CheckDiscordQueue() })
|
|
|
|
log.Print("[Output] Discord Output was enabled")
|
|
|
|
|
2022-06-19 22:02:44 -07:00
|
|
|
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-30 14:54:58 -07:00
|
|
|
func (c *Cron) CheckReddit() {
|
2022-06-19 22:02:44 -07:00
|
|
|
sources, err := c.Db.ListSourcesBySource(*c.ctx, "reddit")
|
2022-06-08 21:17:08 -07:00
|
|
|
if err != nil {
|
2022-06-30 14:54:58 -07:00
|
|
|
log.Printf("[Reddit] No sources found to query - %v\r", err)
|
2022-06-08 21:17:08 -07:00
|
|
|
}
|
2022-04-29 13:02:25 -07:00
|
|
|
|
2022-06-08 21:17:08 -07:00
|
|
|
for _, source := range sources {
|
|
|
|
if !source.Enabled {
|
|
|
|
continue
|
|
|
|
}
|
2022-06-30 14:54:58 -07:00
|
|
|
log.Printf("[Reddit] Checking '%v'...", source.Name)
|
|
|
|
rc := input.NewRedditClient(source)
|
2022-06-08 21:17:08 -07:00
|
|
|
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-30 14:54:58 -07:00
|
|
|
c.checkPosts(redditArticles, "Reddit")
|
2022-04-29 13:02:25 -07:00
|
|
|
}
|
2022-06-30 14:54:58 -07:00
|
|
|
log.Print("[Reddit] Done!")
|
2022-04-29 13:02:25 -07:00
|
|
|
}
|
|
|
|
|
2022-06-30 14:54:58 -07:00
|
|
|
func (c *Cron) CheckYoutube() {
|
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 {
|
2022-06-30 14:54:58 -07:00
|
|
|
log.Printf("[Youtube] No sources found to query - %v\r", err)
|
2022-06-08 21:17:08 -07:00
|
|
|
}
|
2022-04-29 13:02:25 -07:00
|
|
|
|
2022-06-08 21:17:08 -07:00
|
|
|
for _, source := range sources {
|
|
|
|
if !source.Enabled {
|
|
|
|
continue
|
|
|
|
}
|
2022-06-30 14:54:58 -07:00
|
|
|
log.Printf("[YouTube] Checking '%v'...", source.Name)
|
|
|
|
yc := input.NewYoutubeClient(source)
|
2022-06-08 21:17:08 -07:00
|
|
|
raw, err := yc.GetContent()
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
2022-06-30 14:54:58 -07:00
|
|
|
c.checkPosts(raw, "YouTube")
|
2022-06-08 21:17:08 -07:00
|
|
|
}
|
2022-06-30 14:54:58 -07:00
|
|
|
log.Print("[YouTube] Done!")
|
2022-04-29 13:02:25 -07:00
|
|
|
}
|
|
|
|
|
2022-06-30 14:54:58 -07:00
|
|
|
func (c *Cron) CheckFfxiv() {
|
2022-06-19 22:02:44 -07:00
|
|
|
sources, err := c.Db.ListSourcesBySource(*c.ctx, "ffxiv")
|
2022-06-08 21:17:08 -07:00
|
|
|
if err != nil {
|
2022-06-30 14:54:58 -07:00
|
|
|
log.Printf("[FFXIV] No sources found to query - %v\r", err)
|
2022-06-08 21:17:08 -07:00
|
|
|
}
|
2022-04-29 13:02:25 -07:00
|
|
|
|
2022-06-08 21:17:08 -07:00
|
|
|
for _, source := range sources {
|
|
|
|
if !source.Enabled {
|
|
|
|
continue
|
|
|
|
}
|
2022-06-30 14:54:58 -07:00
|
|
|
fc := input.NewFFXIVClient(source)
|
2022-06-08 21:17:08 -07:00
|
|
|
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-30 14:54:58 -07:00
|
|
|
c.checkPosts(items, "FFXIV")
|
2022-04-29 13:02:25 -07:00
|
|
|
}
|
2022-06-30 14:54:58 -07:00
|
|
|
log.Printf("[FFXIV Done!]")
|
2022-05-15 21:48:23 -07:00
|
|
|
}
|
|
|
|
|
2022-06-30 14:54:58 -07:00
|
|
|
func (c *Cron) CheckTwitch() error {
|
2022-06-19 22:02:44 -07:00
|
|
|
sources, err := c.Db.ListSourcesBySource(*c.ctx, "twitch")
|
2022-06-08 21:17:08 -07:00
|
|
|
if err != nil {
|
2022-06-30 14:54:58 -07:00
|
|
|
log.Printf("[Twitch] No sources found to query - %v\r", err)
|
2022-06-08 21:17:08 -07:00
|
|
|
}
|
2022-06-30 14:54:58 -07:00
|
|
|
|
|
|
|
tc, err := input.NewTwitchClient()
|
2022-06-08 21:17:08 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-05-15 21:48:23 -07:00
|
|
|
|
2022-07-12 15:28:31 -07:00
|
|
|
err = tc.Login()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-06-08 21:17:08 -07:00
|
|
|
for _, source := range sources {
|
|
|
|
if !source.Enabled {
|
|
|
|
continue
|
|
|
|
}
|
2022-06-30 14:54:58 -07:00
|
|
|
log.Printf("[Twitch] Checking '%v'...", source.Name)
|
2022-06-08 21:17:08 -07:00
|
|
|
tc.ReplaceSourceRecord(source)
|
|
|
|
items, err := tc.GetContent()
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
2022-06-30 14:54:58 -07:00
|
|
|
c.checkPosts(items, "Twitch")
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Print("[Twitch] Done!")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Cron) CheckDiscordQueue() error {
|
|
|
|
// Get items from the table
|
|
|
|
queueItems, err := c.Db.ListDiscordQueueItems(*c.ctx, 50)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-07-12 15:28:31 -07:00
|
|
|
for _, queue := range queueItems {
|
2022-06-30 14:54:58 -07:00
|
|
|
// Get the articleByID
|
|
|
|
article, err := c.Db.GetArticleByID(*c.ctx, queue.Articleid)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var endpoints []string
|
|
|
|
// List Subscription by SourceID
|
|
|
|
subs, err := c.Db.ListSubscriptionsBySourceId(*c.ctx, article.Sourceid)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-07-12 15:28:31 -07:00
|
|
|
// if no one is subscribed to it, remove it from the index.
|
|
|
|
if len(subs) == 0 {
|
|
|
|
log.Printf("No subscriptions found bound to '%v' so it was removed.", article.Sourceid)
|
|
|
|
err = c.Db.DeleteDiscordQueueItem(*c.ctx, queue.ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-06-30 14:54:58 -07:00
|
|
|
// Get the webhhooks to send to
|
2022-07-12 15:28:31 -07:00
|
|
|
for _, sub := range subs {
|
2022-06-30 14:54:58 -07:00
|
|
|
webhook, err := c.Db.GetDiscordWebHooksByID(*c.ctx, sub.Discordwebhookid)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// store them in an array
|
|
|
|
endpoints = append(endpoints, webhook.Url)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create Discord Message
|
2022-07-12 15:28:31 -07:00
|
|
|
dwh := output.NewDiscordWebHookMessage(article)
|
|
|
|
msg, err := dwh.GeneratePayload()
|
2022-06-30 14:54:58 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-07-12 15:28:31 -07:00
|
|
|
|
|
|
|
// Send Message(s)
|
|
|
|
for _, i := range endpoints {
|
|
|
|
err = dwh.SendPayload(msg, i)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-06-30 14:54:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the item from the queue, given we sent our notification.
|
|
|
|
err = c.Db.DeleteDiscordQueueItem(*c.ctx, queue.ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-07-12 15:28:31 -07:00
|
|
|
|
|
|
|
time.Sleep(10 * time.Second)
|
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-07-12 15:28:31 -07:00
|
|
|
func (c *Cron) checkPosts(posts []database.Article, sourceName string) error {
|
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-07-12 15:28:31 -07:00
|
|
|
id := uuid.New()
|
|
|
|
|
|
|
|
err := c.postArticle(id, item)
|
2022-05-15 21:48:23 -07:00
|
|
|
if err != nil {
|
2022-07-12 15:28:31 -07:00
|
|
|
return fmt.Errorf("[%v] Failed to post article - %v - %v.\r", sourceName, item.Url, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = c.addToDiscordQueue(id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-05-15 21:48:23 -07:00
|
|
|
}
|
2022-07-12 15:28:31 -07:00
|
|
|
|
2022-05-15 21:48:23 -07:00
|
|
|
}
|
|
|
|
}
|
2022-06-08 21:17:08 -07:00
|
|
|
time.Sleep(30 * time.Second)
|
2022-07-12 15:28:31 -07:00
|
|
|
return nil
|
2022-06-08 21:17:08 -07:00
|
|
|
}
|
2022-05-15 21:48:23 -07:00
|
|
|
|
2022-11-30 21:43:53 -08:00
|
|
|
func (c *Cron) postArticle(id uuid.UUID, item database.Article) error {
|
2022-06-19 22:02:44 -07:00
|
|
|
err := c.Db.CreateArticle(*c.ctx, database.CreateArticleParams{
|
2022-07-12 15:28:31 -07:00
|
|
|
ID: id,
|
2022-06-08 21:17:08 -07:00
|
|
|
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
|
|
|
|
}
|
2022-07-12 15:28:31 -07:00
|
|
|
|
|
|
|
func (c *Cron) addToDiscordQueue(Id uuid.UUID) error {
|
|
|
|
err := c.Db.CreateDiscordQueue(*c.ctx, database.CreateDiscordQueueParams{
|
2022-11-30 21:43:53 -08:00
|
|
|
ID: uuid.New(),
|
2022-07-12 15:28:31 -07:00
|
|
|
Articleid: Id,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|