From 197aaeab016024c7aada187c3d112e9c995546ee Mon Sep 17 00:00:00 2001 From: Akhil Gupta Date: Wed, 7 Oct 2020 05:45:00 +0530 Subject: [PATCH] timer logic to minutes and que download on add --- main.go | 34 ++++------------------------------ service/podcastService.go | 5 ++--- 2 files changed, 6 insertions(+), 33 deletions(-) diff --git a/main.go b/main.go index 6e5d035..631b34a 100644 --- a/main.go +++ b/main.go @@ -40,33 +40,6 @@ func main() { r.GET("/podcastitems", controllers.GetAllPodcastItems) r.GET("/podcastitems/:id", controllers.GetPodcastItemById) - r.GET("/ping", func(c *gin.Context) { - - data, err := service.AddPodcast(c.Query("url")) - go service.AddPodcastItems(&data) - //data, err := db.GetAllPodcastItemsToBeDownloaded() - if err == nil { - c.JSON(200, data) - } else { - c.JSON(http.StatusInternalServerError, err.Error()) - } - }) - r.GET("/pong", func(c *gin.Context) { - - data, err := db.GetAllPodcastItemsToBeDownloaded() - - for _, item := range *data { - url, _ := service.Download(item.FileURL, item.Title, item.Podcast.Title) - service.SetPodcastItemAsDownloaded(item.ID, url) - } - - if err == nil { - c.JSON(200, data) - } else { - c.JSON(http.StatusInternalServerError, err.Error()) - } - }) - r.GET("/", func(c *gin.Context) { //var podcasts []db.Podcast podcasts := service.GetAllPodcasts() @@ -81,6 +54,7 @@ func main() { _, err = service.AddPodcast(addPodcastData.Url) if err == nil { + go service.RefreshEpisodes() c.Redirect(http.StatusFound, "/") } else { @@ -108,13 +82,13 @@ func intiCron() { checkFrequency = 10 log.Print(err) } - gocron.Every(uint64(checkFrequency)).Hours().Do(service.DownloadMissingEpisodes) - gocron.Every(uint64(checkFrequency)).Hours().Do(service.RefreshEpisodes) + //gocron.Every(uint64(checkFrequency)).Minutes().Do(service.DownloadMissingEpisodes) + gocron.Every(uint64(checkFrequency)).Minutes().Do(service.RefreshEpisodes) <-gocron.Start() } func assetEnv() { log.Println("Config Dir: ", os.Getenv("CONFIG")) log.Println("Assets Dir: ", os.Getenv("DATA")) - log.Println("Check Frequency (hrs): ", os.Getenv("CHECK_FREQUENCY")) + log.Println("Check Frequency (mins): ", os.Getenv("CHECK_FREQUENCY")) } diff --git a/service/podcastService.go b/service/podcastService.go index 53c86bd..4b0a6d1 100644 --- a/service/podcastService.go +++ b/service/podcastService.go @@ -5,7 +5,6 @@ import ( "errors" "fmt" "io/ioutil" - "log" "net/http" "strconv" "time" @@ -61,7 +60,7 @@ func AddPodcastItems(podcast *db.Podcast) error { fmt.Println("Creating: " + podcast.ID) data, err := FetchURL(podcast.URL) if err != nil { - log.Fatal(err) + //log.Fatal(err) return err } p := bluemonday.StripTagsPolicy() @@ -109,7 +108,6 @@ func DownloadMissingEpisodes() error { url, _ := Download(item.FileURL, item.Title, item.Podcast.Title) SetPodcastItemAsDownloaded(item.ID, url) - return nil } return nil } @@ -125,6 +123,7 @@ func RefreshEpisodes() error { AddPodcastItems(&item) } + go DownloadMissingEpisodes() return nil }