Skip to content

Commit

Permalink
Merge pull request XiovV#86 from XiovV/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
XiovV authored Jan 12, 2020
2 parents 30b3dae + 5bb1e21 commit 3f9cbe4
Show file tree
Hide file tree
Showing 12 changed files with 124 additions and 80 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,
}
30 changes: 26 additions & 4 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 @@ -187,14 +196,27 @@ func (t DownloadTarget) GetFromDatabase() (out DownloadTarget, err error) {
}

func GetCheckingInterval(file string) (int, error) {
interval, time, err := GetCheckingIntervalConfig(file)
if err != nil {
return 0, err
}

if interval == 0 {
return 0, nil
}
return interval * checkingIntervalMultipliers[time], nil
}

func GetCheckingIntervalConfig(file string) (int, string, error) {
cf, ok := confs[file]
if !ok {
return 0, fmt.Errorf("GetFromDatabase: bad conf type")
return 0, "", fmt.Errorf("GetFromDatabase: bad conf type")
}
cf.RLock()
defer cf.RUnlock()
if len(cf.contents) == 0 {
return 0, fmt.Errorf("GetCheckingInterval: empty target list")
return 0, "", fmt.Errorf("GetCheckingInterval: empty target list")
}
return strconv.Atoi(cf.contents[0].CheckingInterval)
target := cf.contents[0]
return target.CheckingInterval, 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
31 changes: 28 additions & 3 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"encoding/json"
"net/http"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -73,9 +74,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 @@ -210,6 +211,30 @@ func HandleCheckAllTargets(w http.ResponseWriter, r *http.Request) {
return
}

func HandleGetCheckingInterval(w http.ResponseWriter, r *http.Request) {
log.Info("received a request to get the checking interval")
w.Header().Set("Content-Type", "application/json")
var getCheckingInterval GetCheckingIntervalPayload
err := json.NewDecoder(r.Body).Decode(&getCheckingInterval)
if err != nil {
ReturnResponse(w, Response{Type: "Error", Key: "ERROR_PARSING_DATA", Message: "There was an error parsing json: " + err.Error()})
return
}
interval, time, err := GetCheckingIntervalConfig(getCheckingInterval.Type)
if err != nil {
ReturnResponse(w, Response{Type: "Error", Key: "ERROR_GETTING_CHECKING_INTERVAL", Message: "There was an error while getting the checking interval: " + err.Error()})
return
}
result := CheckingIntervalPayload{
CheckingInterval: strconv.Itoa(interval),
Time: time,
Type: getCheckingInterval.Type,
}
json.NewEncoder(w).Encode(result)
log.Info("Returning checking-interval: ", result)
return
}

func HandleUpdateCheckingInterval(w http.ResponseWriter, r *http.Request) {
log.Info("received a request to update the checking interval")
w.Header().Set("Content-Type", "application/json")
Expand All @@ -221,7 +246,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
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ func main() {
r.HandleFunc("/api/get", HandleGetTargets).Methods("POST")
r.HandleFunc("/api/check-all", HandleCheckAllTargets).Methods("POST")

r.HandleFunc("/api/get-checking-interval", HandleGetCheckingInterval).Methods("POST")
r.HandleFunc("/api/update-checking-interval", HandleUpdateCheckingInterval).Methods("POST")

r.HandleFunc("/api/get-videos", HandleGetVideos).Methods("GET")
Expand Down
3 changes: 2 additions & 1 deletion static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<title>GoAutoYT - Dashboard</title>

</head>
<body onload="getChannels()">
<body onload="getChannels(); getCheckingInterval();">
<ul class="nav nav-pills justify-content-center mt-3">
<li class="nav-item">
<a class="nav-link active" href="#">Channels</a>
Expand Down Expand Up @@ -91,6 +91,7 @@
<div class="form-group form-inline">
<label for="checking-interval">Check All Channels Every: </label>
<input type="number" class="form-control ml-1" id="checking-interval">
<input type="hidden" id="list-type" value="Channel">
<div class="form-group ml-2">
<select class="form-control" id="time">
<option value="minutes">minutes</option>
Expand Down
56 changes: 47 additions & 9 deletions static/js/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,53 @@ function displayDownloadHistory(channelName, downloadHistory) {
})
}

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

let getCheckingInterval = {
type
};

const options = {
method: "POST",
body: JSON.stringify(getCheckingInterval),
headers: new Headers({
"Content-Type": "application/json"
})
};

fetch("/api/get-checking-interval", options)
.then(res => res.json())
.then(interval => {
document.getElementById("checking-interval").value = interval.CheckingInterval
document.getElementById("time").value = interval.Time
});
}

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

if (time === "minutes") {
return checkingIntervalInput
} else if (time === "hours") {
return checkingIntervalInput * 60
} else if (time === "days") {
return checkingIntervalInput * 1440
}
let interval = {
checkingInterval: checkingInterval.toString(),
time: time,
type
};

const options = {
method: "POST",
body: JSON.stringify(interval),
headers: new Headers({
"Content-Type": "application/json"
})
};

fetch("/api/update-checking-interval", options)
.then(res => res.json())
.then(res => {
handleResponse(res);
stopSpinner("update-checking-interval-spinner")
});
}
27 changes: 0 additions & 27 deletions static/js/index.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,5 @@
let channels = [];

function updateCheckingInterval() {
startSpinner("update-checking-interval-spinner");
let checkingInterval = setCheckingInterval();

let type = "Channel";

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

const options = {
method: "POST",
body: JSON.stringify(interval),
headers: new Headers({
"Content-Type": "application/json"
})
};

fetch("/api/update-checking-interval", options)
.then(res => res.json())
.then(res => {
handleResponse(res);
stopSpinner("update-checking-interval-spinner")
});
}

function addChannel() {
startSpinner("add-channel-spinner");
let downloadEntire = document.querySelector('#download-entire-channel').checked;
Expand Down
28 changes: 0 additions & 28 deletions static/js/playlists.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,34 +36,6 @@ function addPlaylist() {
});
}

function updateCheckingInterval() {
startSpinner("update-checking-interval-spinner");
let checkingInterval = setCheckingInterval();


let type = "Playlist";

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

const options = {
method: "POST",
body: JSON.stringify(interval),
headers: new Headers({
"Content-Type": "application/json"
})
};

fetch("/api/update-checking-interval", options)
.then(res => res.json())
.then(res => {
handleResponse(res)
stopSpinner("update-checking-interval-spinner")
});
}

function checkPlaylist(id) {
startSpinner(id+"-spinner");
let URL = id;
Expand Down
3 changes: 2 additions & 1 deletion static/playlists.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<title>GoAutoYT - Dashboard</title>

</head>
<body onload="getPlaylists()">
<body onload="getPlaylists(); getCheckingInterval();">
<ul class="nav nav-pills justify-content-center mt-3">
<li class="nav-item">
<a class="nav-link" href="/">Channels</a>
Expand Down Expand Up @@ -91,6 +91,7 @@
<div class="form-group form-inline">
<label for="checking-interval">Check All Playlists Every: </label>
<input type="number" class="form-control ml-1" id="checking-interval">
<input type="hidden" id="list-type" value="Playlist">
<div class="form-group ml-2">
<select class="form-control" id="time">
<option value="minutes">minutes</option>
Expand Down
4 changes: 2 additions & 2 deletions static/videos.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
</div>

<div class="form-group mt-4">
<label for="playlist-url">Playlist URL</label>
<label for="playlist-url">Video URL</label>
<input type="text" class="form-control" id="video-url" placeholder="ex. https://www.youtube.com/watch?v=wOMwO5T3yT4">
</div>
<div class="form-group form-inline">
Expand Down Expand Up @@ -99,4 +99,4 @@ <h2 class="mt-2">Download History: </h2>
<script src="/static/js/helpers.js"></script>
<script src="/static/js/version.js"></script>
</body>
</html>
</html>
10 changes: 8 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 @@ -154,8 +155,13 @@ type DeleteTargetPayload struct {
Type string
}

type GetCheckingIntervalPayload struct {
Type string
}

type CheckingIntervalPayload struct {
CheckingInterval string
Time string
Type string
}

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

0 comments on commit 3f9cbe4

Please sign in to comment.