ability to add date to episode name

pull/18/merge
Akhil Gupta 4 years ago
parent 8a0878f1a5
commit 50ec6deff2

@ -64,6 +64,11 @@
<span class="label-body">Automatically download new episodes to the disk</span>
</label>
<br>
<label for="appendDateToFileName">
<input type="checkbox" name="appendDateToFileName" v-model="appendDateToFileName">
<span class="label-body">Append episode date to episode file name</span>
</label>
<br>
<input type="submit" value="Save" class="button button-primary">
</form>
</div>
@ -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 }},
},

@ -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) {

@ -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"})

@ -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

@ -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)

@ -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)
}

Loading…
Cancel
Save