Merge pull request #64 from akhilrex/size

pull/65/head
Akhil Gupta 4 years ago committed by GitHub
commit 087d8fbde1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -23,16 +23,20 @@ func GetAllPodcasts(podcasts *[]Podcast, sorting string) error {
if sorting == "" { if sorting == "" {
sorting = "created_at" sorting = "created_at"
} }
result := DB.Debug().Preload("Tags").Order(sorting).Find(&podcasts) result := DB.Preload("Tags").Order(sorting).Find(&podcasts)
return result.Error return result.Error
} }
func GetAllPodcastItems(podcasts *[]PodcastItem) error { func GetAllPodcastItems(podcasts *[]PodcastItem) error {
result := DB.Preload("Podcast").Order("pub_date desc").Find(&podcasts) result := DB.Preload("Podcast").Order("pub_date desc").Find(&podcasts)
return result.Error return result.Error
} }
func GetAllPodcastItemsWithoutSize() (*[]PodcastItem, error) {
var podcasts []PodcastItem
result := DB.Where("file_size<=?", 0).Order("pub_date desc").Find(&podcasts)
return &podcasts, result.Error
}
func GetPaginatedPodcastItems(page int, count int, downloadedOnly *bool, playedOnly *bool, fromDate time.Time, podcasts *[]PodcastItem, total *int64) error { func GetPaginatedPodcastItems(page int, count int, downloadedOnly *bool, playedOnly *bool, fromDate time.Time, podcasts *[]PodcastItem, total *int64) error {
query := DB.Debug().Preload("Podcast") query := DB.Preload("Podcast")
if downloadedOnly != nil { if downloadedOnly != nil {
if *downloadedOnly { if *downloadedOnly {
query = query.Where("download_status=?", Downloaded) query = query.Where("download_status=?", Downloaded)
@ -108,30 +112,35 @@ func GetAllPodcastItemsByPodcastIds(podcastIds []string, podcastItems *[]Podcast
} }
func SetAllEpisodesToDownload(podcastId string) error { func SetAllEpisodesToDownload(podcastId string) error {
result := DB.Debug().Model(PodcastItem{}).Where(&PodcastItem{PodcastID: podcastId, DownloadStatus: Deleted}).Update("download_status", NotDownloaded) result := DB.Model(PodcastItem{}).Where(&PodcastItem{PodcastID: podcastId, DownloadStatus: Deleted}).Update("download_status", NotDownloaded)
return result.Error return result.Error
} }
func UpdateLastEpisodeDateForPodcast(podcastId string, lastEpisode time.Time) error { func UpdateLastEpisodeDateForPodcast(podcastId string, lastEpisode time.Time) error {
result := DB.Debug().Model(Podcast{}).Where("id=?", podcastId).Update("last_episode", lastEpisode) result := DB.Model(Podcast{}).Where("id=?", podcastId).Update("last_episode", lastEpisode)
return result.Error
}
func UpdatePodcastItemFileSize(podcastItemId string, size int64) error {
result := DB.Model(PodcastItem{}).Where("id=?", podcastItemId).Update("file_size", size)
return result.Error return result.Error
} }
func GetAllPodcastItemsWithoutImage() (*[]PodcastItem, error) { func GetAllPodcastItemsWithoutImage() (*[]PodcastItem, error) {
var podcastItems []PodcastItem var podcastItems []PodcastItem
result := DB.Debug().Preload(clause.Associations).Where("local_image is ?", nil).Where("image != ?", "").Where("download_status=?", Downloaded).Order("created_at desc").Find(&podcastItems) result := DB.Preload(clause.Associations).Where("local_image is ?", nil).Where("image != ?", "").Where("download_status=?", Downloaded).Order("created_at desc").Find(&podcastItems)
//fmt.Println("To be downloaded : " + string(len(podcastItems))) //fmt.Println("To be downloaded : " + string(len(podcastItems)))
return &podcastItems, result.Error return &podcastItems, result.Error
} }
func GetAllPodcastItemsToBeDownloaded() (*[]PodcastItem, error) { func GetAllPodcastItemsToBeDownloaded() (*[]PodcastItem, error) {
var podcastItems []PodcastItem var podcastItems []PodcastItem
result := DB.Debug().Preload(clause.Associations).Where("download_status=?", NotDownloaded).Find(&podcastItems) result := DB.Preload(clause.Associations).Where("download_status=?", NotDownloaded).Find(&podcastItems)
//fmt.Println("To be downloaded : " + string(len(podcastItems))) //fmt.Println("To be downloaded : " + string(len(podcastItems)))
return &podcastItems, result.Error return &podcastItems, result.Error
} }
func GetAllPodcastItemsAlreadyDownloaded() (*[]PodcastItem, error) { func GetAllPodcastItemsAlreadyDownloaded() (*[]PodcastItem, error) {
var podcastItems []PodcastItem var podcastItems []PodcastItem
result := DB.Debug().Preload(clause.Associations).Where("download_status=?", Downloaded).Find(&podcastItems) result := DB.Preload(clause.Associations).Where("download_status=?", Downloaded).Find(&podcastItems)
return &podcastItems, result.Error return &podcastItems, result.Error
} }
@ -246,7 +255,7 @@ func Unlock(name string) {
func UnlockMissedJobs() { func UnlockMissedJobs() {
var jobLocks []JobLock var jobLocks []JobLock
result := DB.Debug().Find(&jobLocks) result := DB.Find(&jobLocks)
if result.Error != nil { if result.Error != nil {
return return
} }
@ -269,7 +278,7 @@ func GetAllTags(sorting string) (*[]Tag, error) {
if sorting == "" { if sorting == "" {
sorting = "created_at" sorting = "created_at"
} }
result := DB.Debug().Preload(clause.Associations).Order(sorting).Find(&tags) result := DB.Preload(clause.Associations).Order(sorting).Find(&tags)
return &tags, result.Error return &tags, result.Error
} }
@ -295,7 +304,7 @@ func GetTagByLabel(label string) (*Tag, error) {
} }
func CreateTag(tag *Tag) error { func CreateTag(tag *Tag) error {
tx := DB.Debug().Omit("Podcasts").Create(&tag) tx := DB.Omit("Podcasts").Create(&tag)
return tx.Error return tx.Error
} }
func UpdateTag(tag *Tag) error { func UpdateTag(tag *Tag) error {
@ -303,15 +312,15 @@ func UpdateTag(tag *Tag) error {
return tx.Error return tx.Error
} }
func AddTagToPodcast(id, tagId string) error { func AddTagToPodcast(id, tagId string) error {
tx := DB.Debug().Exec("INSERT INTO `podcast_tags` (`podcast_id`,`tag_id`) VALUES (?,?) ON CONFLICT DO NOTHING", id, tagId) tx := DB.Exec("INSERT INTO `podcast_tags` (`podcast_id`,`tag_id`) VALUES (?,?) ON CONFLICT DO NOTHING", id, tagId)
return tx.Error return tx.Error
} }
func RemoveTagFromPodcast(id, tagId string) error { func RemoveTagFromPodcast(id, tagId string) error {
tx := DB.Debug().Exec("DELETE FROM `podcast_tags` WHERE `podcast_id`=? AND `tag_id`=?", id, tagId) tx := DB.Exec("DELETE FROM `podcast_tags` WHERE `podcast_id`=? AND `tag_id`=?", id, tagId)
return tx.Error return tx.Error
} }
func UntagAllByTagId(tagId string) error { func UntagAllByTagId(tagId string) error {
tx := DB.Debug().Exec("DELETE FROM `podcast_tags` WHERE `tag_id`=?", tagId) tx := DB.Exec("DELETE FROM `podcast_tags` WHERE `tag_id`=?", tagId)
return tx.Error return tx.Error
} }

@ -56,6 +56,8 @@ type PodcastItem struct {
BookmarkDate time.Time BookmarkDate time.Time
LocalImage string LocalImage string
FileSize int64
} }
type DownloadStatus int type DownloadStatus int

@ -196,6 +196,7 @@ func intiCron() {
gocron.Every(uint64(checkFrequency)).Minutes().Do(service.RefreshEpisodes) gocron.Every(uint64(checkFrequency)).Minutes().Do(service.RefreshEpisodes)
gocron.Every(uint64(checkFrequency)).Minutes().Do(service.CheckMissingFiles) gocron.Every(uint64(checkFrequency)).Minutes().Do(service.CheckMissingFiles)
gocron.Every(uint64(checkFrequency) * 2).Minutes().Do(service.UnlockMissedJobs) 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.DownloadMissingImages)
gocron.Every(2).Days().Do(service.CreateBackup) gocron.Every(2).Days().Do(service.CreateBackup)
<-gocron.Start() <-gocron.Start()

@ -136,6 +136,14 @@ func GetAllBackupFiles() ([]string, error) {
return files, err return files, err
} }
func GetFileSize(path string) (int64, error) {
info, err := os.Stat(path)
if err != nil {
return 0, err
}
return info.Size(), nil
}
func deleteOldBackup() { func deleteOldBackup() {
files, err := GetAllBackupFiles() files, err := GetAllBackupFiles()
if err != nil { if err != nil {
@ -152,6 +160,26 @@ func deleteOldBackup() {
} }
} }
func GetFileSizeFromUrl(url string) (int64, error) {
resp, err := http.Head(url)
if err != nil {
return 0, err
}
// Is our request ok?
if resp.StatusCode != http.StatusOK {
return 0, fmt.Errorf("Did not receive 200")
}
size, err := strconv.Atoi(resp.Header.Get("Content-Length"))
if err != nil {
return 0, err
}
return int64(size), nil
}
func CreateBackup() (string, error) { func CreateBackup() (string, error) {
backupFileName := "podgrab_backup_" + time.Now().Format("2006.01.02_150405") + ".tar.gz" backupFileName := "podgrab_backup_" + time.Now().Format("2006.01.02_150405") + ".tar.gz"

@ -239,7 +239,7 @@ func AddPodcastItems(podcast *db.Podcast, newPodcast bool) error {
keyMap[item.GUID] = 1 keyMap[item.GUID] = 1
} }
var latestDate = time.Time{} var latestDate = time.Time{}
var itemsAdded = make(map[string]string)
for i := 0; i < len(data.Channel.Item); i++ { for i := 0; i < len(data.Channel.Item); i++ {
obj := data.Channel.Item[i] obj := data.Channel.Item[i]
var podcastItem db.PodcastItem var podcastItem db.PodcastItem
@ -302,14 +302,45 @@ func AddPodcastItems(podcast *db.Podcast, newPodcast bool) error {
DownloadStatus: downloadStatus, DownloadStatus: downloadStatus,
} }
db.CreatePodcastItem(&podcastItem) db.CreatePodcastItem(&podcastItem)
itemsAdded[podcastItem.ID] = podcastItem.FileURL
} }
} }
if (latestDate != time.Time{}) { if (latestDate != time.Time{}) {
db.UpdateLastEpisodeDateForPodcast(podcast.ID, latestDate) db.UpdateLastEpisodeDateForPodcast(podcast.ID, latestDate)
} }
//go updateSizeFromUrl(itemsAdded)
return err return err
} }
func updateSizeFromUrl(itemUrlMap map[string]string) {
for id, url := range itemUrlMap {
size, err := GetFileSizeFromUrl(url)
if err != nil {
size = 1
}
db.UpdatePodcastItemFileSize(id, size)
}
}
func UpdateAllFileSizes() {
items, err := db.GetAllPodcastItemsWithoutSize()
if err != nil {
return
}
for _, item := range *items {
var size int64 = 1
if item.DownloadStatus == db.Downloaded {
size, _ = GetFileSize(item.DownloadPath)
} else {
size, _ = GetFileSizeFromUrl(item.FileURL)
}
db.UpdatePodcastItemFileSize(item.ID, size)
}
}
func SetPodcastItemAsQueuedForDownload(id string) error { func SetPodcastItemAsQueuedForDownload(id string) error {
var podcastItem db.PodcastItem var podcastItem db.PodcastItem
err := db.GetPodcastItemById(id, &podcastItem) err := db.GetPodcastItemById(id, &podcastItem)
@ -376,6 +407,12 @@ func SetPodcastItemAsDownloaded(id string, location string) error {
fmt.Println("Location", err.Error()) fmt.Println("Location", err.Error())
return err return err
} }
size, err := GetFileSize(location)
if err == nil {
podcastItem.FileSize = size
}
podcastItem.DownloadDate = time.Now() podcastItem.DownloadDate = time.Now()
podcastItem.DownloadPath = location podcastItem.DownloadPath = location
podcastItem.DownloadStatus = db.Downloaded podcastItem.DownloadStatus = db.Downloaded

Loading…
Cancel
Save