Skip to content

Commit

Permalink
Store checking interval time in server
Browse files Browse the repository at this point in the history
Make calculations of the time multiplier on server not on the client,
this will allow to revtrieve the checking interval later
  • Loading branch information
mkolesnik committed Jan 6, 2020
1 parent 44cd705 commit 0206029
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 24 deletions.
6 changes: 6 additions & 0 deletions consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,9 @@ var (
errRes Response
okRes Response
)

var checkingIntervalMultipliers = map[string]int{
"minutes": 1,
"hours": 60,
"days": 1440,
}
17 changes: 15 additions & 2 deletions database_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (db *Database) load() error {
return nil
}

func UpdateCheckingInterval(file, interval string) error {
func UpdateCheckingInterval(file, time, intervalStr string) error {
log.Info("update checking interval")
cf, ok := confs[file]
if !ok {
Expand All @@ -78,8 +78,17 @@ func UpdateCheckingInterval(file, interval string) error {
if len(cf.contents) == 0 {
return fmt.Errorf("UpdateCheckingInterval: empty config list")
}
interval, err := strconv.Atoi(intervalStr)
if err != nil {
return fmt.Errorf("UpdateCheckingInterval: non-numeric interval %w", err)
}
_, ok = checkingIntervalMultipliers[time]
if !ok {
return fmt.Errorf("UpdateCheckingInterval: bad multiplier")
}
cf.Lock()
cf.contents[0].CheckingInterval = interval
cf.contents[0].CheckingIntervalTime = time
cf.write()
cf.Unlock()
return nil
Expand Down Expand Up @@ -196,5 +205,9 @@ func GetCheckingInterval(file string) (int, error) {
if len(cf.contents) == 0 {
return 0, fmt.Errorf("GetCheckingInterval: empty target list")
}
return strconv.Atoi(cf.contents[0].CheckingInterval)
target := cf.contents[0]
if target.CheckingInterval == 0 {
return 0, nil
}
return target.CheckingInterval * checkingIntervalMultipliers[target.CheckingIntervalTime], nil
}
5 changes: 2 additions & 3 deletions database_handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ var targets = []DownloadTarget{
PreferredExtensionForVideo: "",
DownloadHistory: nil,
LastChecked: "",
CheckingInterval: "",
Type: "Channel",
},
{
Expand All @@ -28,7 +27,6 @@ var targets = []DownloadTarget{
PreferredExtensionForVideo: "mp4",
DownloadHistory: nil,
LastChecked: "",
CheckingInterval: "",
Type: "Channel",
},
{
Expand All @@ -40,7 +38,8 @@ var targets = []DownloadTarget{
PreferredExtensionForVideo: "mp4",
DownloadHistory: nil,
LastChecked: "",
CheckingInterval: "5",
CheckingInterval: 5,
CheckingIntervalTime: "minutes",
Type: "Playlist",
},
}
Expand Down
6 changes: 3 additions & 3 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ func HandleAddTarget(w http.ResponseWriter, r *http.Request) {
}

if targetData.DownloadMode == "Audio Only" {
target = DownloadTarget{URL: targetData.URL, DownloadMode: targetData.DownloadMode, Name: targetMetadata.Playlist, PreferredExtensionForAudio: targetData.FileExtension, DownloadHistory: []string{}, LastChecked: time.Now().Format("01-02-2006 15:04:05"), CheckingInterval: "", Type: targetData.Type, DownloadPath: targetData.DownloadPath}
target = DownloadTarget{URL: targetData.URL, DownloadMode: targetData.DownloadMode, Name: targetMetadata.Playlist, PreferredExtensionForAudio: targetData.FileExtension, DownloadHistory: []string{}, LastChecked: time.Now().Format("01-02-2006 15:04:05"), Type: targetData.Type, DownloadPath: targetData.DownloadPath}
} else if targetData.DownloadMode == "Video And Audio" {
target = DownloadTarget{URL: targetData.URL, DownloadMode: targetData.DownloadMode, Name: targetMetadata.Playlist, PreferredExtensionForVideo: targetData.FileExtension, DownloadHistory: []string{}, LastChecked: time.Now().Format("01-02-2006 15:04:05"), CheckingInterval: "", Type: targetData.Type, DownloadPath: targetData.DownloadPath}
target = DownloadTarget{URL: targetData.URL, DownloadMode: targetData.DownloadMode, Name: targetMetadata.Playlist, PreferredExtensionForVideo: targetData.FileExtension, DownloadHistory: []string{}, LastChecked: time.Now().Format("01-02-2006 15:04:05"), Type: targetData.Type, DownloadPath: targetData.DownloadPath}
}

err = target.Download(targetData.DownloadQuality, targetData.FileExtension, targetData.DownloadEntire)
Expand Down Expand Up @@ -221,7 +221,7 @@ func HandleUpdateCheckingInterval(w http.ResponseWriter, r *http.Request) {
ReturnResponse(w, errRes)
return
}
err = UpdateCheckingInterval(interval.Type, interval.CheckingInterval)
err = UpdateCheckingInterval(interval.Type, interval.Time, interval.CheckingInterval)
if err != nil {
errRes = Response{Type: "Error", Key: "ERROR_UPDATING_CHECKING_INTERVAL", Message: "There was an updating the checking interval: " + err.Error()}
ReturnResponse(w, errRes)
Expand Down
17 changes: 3 additions & 14 deletions static/js/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,26 +60,15 @@ function displayDownloadHistory(channelName, downloadHistory) {
})
}

function setCheckingInterval() {
let checkingIntervalInput = document.getElementById("checking-interval").value;
let time = document.getElementById("time").value;

if (time === "minutes") {
return checkingIntervalInput
} else if (time === "hours") {
return checkingIntervalInput * 60
} else if (time === "days") {
return checkingIntervalInput * 1440
}
}

function updateCheckingInterval() {
startSpinner("update-checking-interval-spinner");
let checkingInterval = setCheckingInterval();
let checkingInterval = document.getElementById("checking-interval").value;
let time = document.getElementById("time").value;
let type = document.getElementById("list-type").value;

let interval = {
checkingInterval: checkingInterval.toString(),
time: time,
type
};

Expand Down
6 changes: 4 additions & 2 deletions structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ type DownloadTarget struct {
PreferredExtensionForVideo string
DownloadHistory []string
LastChecked string
CheckingInterval string
CheckingInterval int
CheckingIntervalTime string
Type string
DownloadPath string
}
Expand All @@ -156,6 +157,7 @@ type DeleteTargetPayload struct {

type CheckingIntervalPayload struct {
CheckingInterval string
Time string
Type string
}

Expand Down Expand Up @@ -206,4 +208,4 @@ type Database struct {
file string
contents []DownloadTarget
lookup map[string]*DownloadTarget
}
}

0 comments on commit 0206029

Please sign in to comment.