From 50ec6deff245920b6cdc58c4c3a6e98eb551c454 Mon Sep 17 00:00:00 2001 From: Akhil Gupta Date: Fri, 22 Jan 2021 17:35:09 +0530 Subject: [PATCH] ability to add date to episode name --- client/settings.html | 9 ++++++++- controllers/pages.go | 1 + controllers/podcast.go | 2 +- db/podcast.go | 1 + service/fileService.go | 5 ++++- service/podcastService.go | 22 +++++++++++++++++----- 6 files changed, 32 insertions(+), 8 deletions(-) diff --git a/client/settings.html b/client/settings.html index e0ba4a7..5465165 100644 --- a/client/settings.html +++ b/client/settings.html @@ -64,6 +64,11 @@ Automatically download new episodes to the disk
+ +
@@ -83,7 +88,8 @@ var app = new Vue({ axios.post("/settings",{ downloadOnAdd: self.downloadOnAdd, initialDownloadCount:self.initialDownloadCount, - autoDownload:self.autoDownload + autoDownload:self.autoDownload, + appendDateToFileName:self.appendDateToFileName }) .then(function(response){ @@ -118,6 +124,7 @@ var app = new Vue({ downloadOnAdd: {{ .setting.DownloadOnAdd }}, initialDownloadCount: {{ .setting.InitialDownloadCount }}, autoDownload: {{ .setting.AutoDownload }}, + appendDateToFileName: {{ .setting.AppendDateToFileName }}, }, diff --git a/controllers/pages.go b/controllers/pages.go index c58a2a7..8c7e117 100644 --- a/controllers/pages.go +++ b/controllers/pages.go @@ -23,6 +23,7 @@ type SettingModel struct { DownloadOnAdd bool `form:"downloadOnAdd" json:"downloadOnAdd" query:"downloadOnAdd"` InitialDownloadCount int `form:"initialDownloadCount" json:"initialDownloadCount" query:"initialDownloadCount"` AutoDownload bool `form:"autoDownload" json:"autoDownload" query:"autoDownload"` + AppendDateToFileName bool `form:"appendDateToFileName" json:"appendDateToFileName" query:"appendDateToFileName"` } func AddPage(c *gin.Context) { diff --git a/controllers/podcast.go b/controllers/podcast.go index e11bc22..d8b814d 100644 --- a/controllers/podcast.go +++ b/controllers/podcast.go @@ -229,7 +229,7 @@ func UpdateSetting(c *gin.Context) { if err == nil { - err = service.UpdateSettings(model.DownloadOnAdd, model.InitialDownloadCount, model.AutoDownload) + err = service.UpdateSettings(model.DownloadOnAdd, model.InitialDownloadCount, model.AutoDownload, model.AppendDateToFileName) if err == nil { c.JSON(200, gin.H{"message": "Success"}) diff --git a/db/podcast.go b/db/podcast.go index 37de859..1161957 100644 --- a/db/podcast.go +++ b/db/podcast.go @@ -60,6 +60,7 @@ type Setting struct { DownloadOnAdd bool `gorm:"default:true"` InitialDownloadCount int `gorm:"default:5"` AutoDownload bool `gorm:"default:true"` + AppendDateToFileName bool `gorm:"default:false"` } type Migration struct { Base diff --git a/service/fileService.go b/service/fileService.go index e0d2e1a..01ea1fe 100644 --- a/service/fileService.go +++ b/service/fileService.go @@ -18,7 +18,7 @@ import ( stringy "github.com/gobeam/stringy" ) -func Download(link string, episodeTitle string, podcastName string) (string, error) { +func Download(link string, episodeTitle string, podcastName string, prefix string) (string, error) { if link == "" { return "", errors.New("Download path empty") } @@ -30,6 +30,9 @@ func Download(link string, episodeTitle string, podcastName string) (string, err } fileName := getFileName(link, episodeTitle, ".mp3") + if prefix != "" { + fileName = fmt.Sprintf("%s-%s", prefix, fileName) + } folder := createIfFoldeDoesntExist(podcastName) finalPath := path.Join(folder, fileName) file, err := os.Create(finalPath) diff --git a/service/podcastService.go b/service/podcastService.go index 8407d89..f6b450b 100644 --- a/service/podcastService.go +++ b/service/podcastService.go @@ -242,6 +242,14 @@ func SetPodcastItemPlayedStatus(id string, isPlayed bool) error { func SetAllEpisodesToDownload(podcastId string) error { return db.SetAllEpisodesToDownload(podcastId) } + +func GetPodcastPrefix(item *db.PodcastItem, setting *db.Setting) string { + prefix := "" + if setting.AppendDateToFileName { + prefix = item.PubDate.Format("2006-01-02") + } + return prefix +} func DownloadMissingEpisodes() error { const JOB_NAME = "DownloadMissingEpisodes" lock := db.GetLock(JOB_NAME) @@ -250,6 +258,7 @@ func DownloadMissingEpisodes() error { return nil } db.Lock(JOB_NAME, 120) + setting := db.GetOrCreateSetting() data, err := db.GetAllPodcastItemsToBeDownloaded() @@ -260,11 +269,11 @@ func DownloadMissingEpisodes() error { var wg sync.WaitGroup for index, item := range *data { wg.Add(1) - go func(item db.PodcastItem) { + go func(item db.PodcastItem, setting db.Setting) { defer wg.Done() - url, _ := Download(item.FileURL, item.Title, item.Podcast.Title) + url, _ := Download(item.FileURL, item.Title, item.Podcast.Title, GetPodcastPrefix(&item, &setting)) SetPodcastItemAsDownloaded(item.ID, url) - }(item) + }(item, *setting) if index%5 == 0 { wg.Wait() @@ -316,7 +325,9 @@ func DownloadSingleEpisode(podcastItemId string) error { return err } - url, err := Download(podcastItem.FileURL, podcastItem.Title, podcastItem.Podcast.Title) + setting := db.GetOrCreateSetting() + + url, err := Download(podcastItem.FileURL, podcastItem.Title, podcastItem.Podcast.Title, GetPodcastPrefix(&podcastItem, setting)) if err != nil { return err } @@ -426,12 +437,13 @@ func GetSearchFromItunes(pod model.ItunesSingleResult) *model.CommonSearchResult return p } -func UpdateSettings(downloadOnAdd bool, initialDownloadCount int, autoDownload bool) error { +func UpdateSettings(downloadOnAdd bool, initialDownloadCount int, autoDownload bool, appendDateToFileName bool) error { setting := db.GetOrCreateSetting() setting.AutoDownload = autoDownload setting.DownloadOnAdd = downloadOnAdd setting.InitialDownloadCount = initialDownloadCount + setting.AppendDateToFileName = appendDateToFileName return db.UpdateSettings(setting) }