Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add setting for max downloaded episodes per podcast #285

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions client/settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ <h3>Settings</h3>
<span class="label-body">Limit the number of podcasts that can be downloaded simultaneously</span>
<input type="number" name="maxDownloadConcurrency" v-model.number="maxDownloadConcurrency" min="1">
</label>
<label for="maxDownloadKeep" style="display: inline-block;" >
<span class="label-body">Limit the number of podcast episodes that are kept (auto deletes older episodes beyond this number)</span>
<input type="number" name="maxDownloadKeep" v-model.number="maxDownloadKeep" min="1">
</label>
<label for="userAgent" style="display: inline-block;" >
<span class="label-body">The <code>User-Agent</code> header used when downloading podcasts</span>
<input type="text" class="u-full-width" name="userAgent" v-model="userAgent">
Expand Down Expand Up @@ -190,6 +194,7 @@ <h3>More Info</h3>
dontDownloadDeletedFromDisk:self.dontDownloadDeletedFromDisk,
baseUrl:self.baseUrl,
maxDownloadConcurrency:self.maxDownloadConcurrency,
maxDownloadKeep:self.maxDownloadKeep,
userAgent:self.userAgent,
})
.then(function(response){
Expand Down Expand Up @@ -236,6 +241,7 @@ <h3>More Info</h3>
dontDownloadDeletedFromDisk:{{ .setting.DontDownloadDeletedFromDisk }},
baseUrl: {{ .setting.BaseUrl }},
maxDownloadConcurrency:{{ .setting.MaxDownloadConcurrency }},
maxDownloadKeep:{{ .setting.MaxDownloadKeep }},
userAgent:{{ .setting.UserAgent}},
},

Expand Down
1 change: 1 addition & 0 deletions controllers/pages.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type SettingModel struct {
DontDownloadDeletedFromDisk bool `form:"dontDownloadDeletedFromDisk" json:"dontDownloadDeletedFromDisk" query:"dontDownloadDeletedFromDisk"`
BaseUrl string `form:"baseUrl" json:"baseUrl" query:"baseUrl"`
MaxDownloadConcurrency int `form:"maxDownloadConcurrency" json:"maxDownloadConcurrency" query:"maxDownloadConcurrency"`
MaxDownloadKeep int `form:"maxDownloadKeep" json:"maxDownloadKeep" query:"maxDownloadKeep"`
UserAgent string `form:"userAgent" json:"userAgent" query:"userAgent"`
}

Expand Down
2 changes: 1 addition & 1 deletion controllers/podcast.go
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,7 @@ func UpdateSetting(c *gin.Context) {
err = service.UpdateSettings(model.DownloadOnAdd, model.InitialDownloadCount,
model.AutoDownload, model.AppendDateToFileName, model.AppendEpisodeNumberToFileName,
model.DarkMode, model.DownloadEpisodeImages, model.GenerateNFOFile, model.DontDownloadDeletedFromDisk, model.BaseUrl,
model.MaxDownloadConcurrency, model.UserAgent,
model.MaxDownloadConcurrency, model.MaxDownloadKeep, model.UserAgent,
)
if err == nil {
c.JSON(200, gin.H{"message": "Success"})
Expand Down
2 changes: 1 addition & 1 deletion db/dbfunctions.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func DeleteTagById(id string) error {

func GetAllPodcastItemsByPodcastId(podcastId string, podcastItems *[]PodcastItem) error {

result := DB.Preload(clause.Associations).Where(&PodcastItem{PodcastID: podcastId}).Find(&podcastItems)
result := DB.Preload(clause.Associations).Where(&PodcastItem{PodcastID: podcastId}).Order("pub_date desc").Find(&podcastItems)
return result.Error
}
func GetAllPodcastItemsByPodcastIds(podcastIds []string, podcastItems *[]PodcastItem) error {
Expand Down
4 changes: 4 additions & 0 deletions db/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ var migrations = []localMigration{
Name: "2020_11_03_04_42_SetDefaultDownloadStatus",
Query: "update podcast_items set download_status=2 where download_path!='' and download_status=0",
},
{
Name: "2023_10_17_AddMaxDownloadKeepColumn",
Query: "ALTER TABLE settings ADD max_download_keep INT default 5",
},
}

func RunMigrations() {
Expand Down
1 change: 1 addition & 0 deletions db/podcast.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ type Setting struct {
BaseUrl string
MaxDownloadConcurrency int `gorm:"default:5"`
UserAgent string
MaxDownloadKeep int `gorm:"default:5"`
}
type Migration struct {
Base
Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ func intiCron() {
gocron.Every(uint64(checkFrequency) * 2).Minutes().Do(service.UnlockMissedJobs)
gocron.Every(uint64(checkFrequency) * 3).Minutes().Do(service.UpdateAllFileSizes)
gocron.Every(uint64(checkFrequency)).Minutes().Do(service.DownloadMissingImages)
gocron.Every(uint64(checkFrequency)).Minutes().Do(service.ClearEpisodeFiles)
gocron.Every(2).Days().Do(service.CreateBackup)
<-gocron.Start()
}
Expand Down
32 changes: 31 additions & 1 deletion service/podcastService.go
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,35 @@ func CheckMissingFiles() error {
return nil
}

func ClearEpisodeFiles() error {
fmt.Println("Clearning Episode Files")
var podcasts []db.Podcast
err := db.GetAllPodcasts(&podcasts, "")
setting := db.GetOrCreateSetting()
var maxDownloadKeep int = setting.MaxDownloadKeep

if err != nil {
return err
}
for _, pod := range podcasts {
var episodes []db.PodcastItem
err = db.GetAllPodcastItemsByPodcastId(pod.ID, &episodes)
if err != nil {
return err
}
downloadedCount := 0
for _, episode := range episodes {
if (downloadedCount >= maxDownloadKeep && episode.DownloadStatus == 2) {
DeleteEpisodeFile(episode.ID)
}
if (episode.DownloadStatus == 2) {
downloadedCount = downloadedCount + 1
}
}
}
return nil
}

func DeleteEpisodeFile(podcastItemId string) error {
var podcastItem db.PodcastItem
err := db.GetPodcastItemById(podcastItemId, &podcastItem)
Expand Down Expand Up @@ -764,7 +793,7 @@ func GetSearchFromPodcastIndex(pod *podcastindex.Podcast) *model.CommonSearchRes

func UpdateSettings(downloadOnAdd bool, initialDownloadCount int, autoDownload bool,
appendDateToFileName bool, appendEpisodeNumberToFileName bool, darkMode bool, downloadEpisodeImages bool,
generateNFOFile bool, dontDownloadDeletedFromDisk bool, baseUrl string, maxDownloadConcurrency int, userAgent string) error {
generateNFOFile bool, dontDownloadDeletedFromDisk bool, baseUrl string, maxDownloadConcurrency int, maxDownloadKeep int, userAgent string) error {
setting := db.GetOrCreateSetting()

setting.AutoDownload = autoDownload
Expand All @@ -778,6 +807,7 @@ func UpdateSettings(downloadOnAdd bool, initialDownloadCount int, autoDownload b
setting.DontDownloadDeletedFromDisk = dontDownloadDeletedFromDisk
setting.BaseUrl = baseUrl
setting.MaxDownloadConcurrency = maxDownloadConcurrency
setting.MaxDownloadKeep = maxDownloadKeep
setting.UserAgent = userAgent

return db.UpdateSettings(setting)
Expand Down