Keeps track of status of downloads while running
parent
2f43820f6a
commit
e46860adad
|
|
@ -55,7 +55,7 @@ fetch("currentwork").then(response => {
|
|||
parsed = JSON.parse(json)
|
||||
console.log(parsed);
|
||||
document.getElementById("list").innerText = "";
|
||||
Object.entries(parsed).forEach(([key, value]) => {document.getElementById("list").innerText += value});
|
||||
Object.entries(parsed).forEach(([key, value]) => {document.getElementById("list").innerText += value.Status + " - " + value.URL + "\n"});
|
||||
if (json === "{}"){
|
||||
}
|
||||
}).catch( () => {
|
||||
|
|
@ -78,6 +78,14 @@ type ops struct {
|
|||
// List of the operations currently being worked on
|
||||
var currentWork map[int]string
|
||||
|
||||
type WorkItem struct {
|
||||
Status string
|
||||
URL string
|
||||
}
|
||||
|
||||
// CurrentWork
|
||||
var CurrentWork = make(map[int]WorkItem)
|
||||
|
||||
// program configurations
|
||||
var config struct {
|
||||
// Where files should be saved
|
||||
|
|
@ -94,7 +102,7 @@ var opsc = make(chan ops)
|
|||
// handleRequest handles http requests
|
||||
func handleRequest(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method == http.MethodGet && r.URL.Path == "/currentwork" {
|
||||
list, err := json.Marshal(currentWork)
|
||||
list, err := json.Marshal(CurrentWork)
|
||||
if err != nil {
|
||||
log.Panic(err)
|
||||
}
|
||||
|
|
@ -116,41 +124,55 @@ func handleRequest(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
// Modify template and send it to the requester
|
||||
var insert string
|
||||
for _, url := range currentWork {
|
||||
insert += "<br>" + url
|
||||
for _, workItem := range CurrentWork {
|
||||
insert += "<br>" + workItem.URL
|
||||
}
|
||||
temp := strings.Replace(template, "currentWork", insert, 1)
|
||||
io.WriteString(w, temp)
|
||||
}
|
||||
|
||||
func downloadVid(r *http.Request) {
|
||||
}
|
||||
|
||||
// operationHandler waits for operation requests and initiates them
|
||||
func operationHandler() {
|
||||
// Simple index for sorting video work
|
||||
for i := 0; true; i++ {
|
||||
dir := path.Join(config.saveLocation, ".viddown", strconv.Itoa(i))
|
||||
var err error
|
||||
// Check if index exists
|
||||
_, err = os.Stat(dir)
|
||||
if err == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
o := <-opsc
|
||||
log.Print("Start Downloading: " + o.URL)
|
||||
|
||||
// Spin off a go routine to handle a download so the handler can continue to receive requests
|
||||
go func() {
|
||||
var item WorkItem
|
||||
item.URL = o.URL
|
||||
item.Status = "downloading"
|
||||
CurrentWork[i] = item
|
||||
|
||||
// Create a unique directory to download the video in
|
||||
dir := path.Join(config.saveLocation, ".viddown", strconv.Itoa(i))
|
||||
os.MkdirAll(dir, 0755)
|
||||
|
||||
// Build the download command
|
||||
cmd := exec.Command(config.youtubedll, o.URL)
|
||||
cmd.Dir = dir + "/"
|
||||
currentWork[i] = o.URL // Adds the URL to list of current work
|
||||
output, err := cmd.CombinedOutput()
|
||||
log.Print("Finished: " + o.URL)
|
||||
if err != nil {
|
||||
item.Status = "failed"
|
||||
CurrentWork[i] = item
|
||||
|
||||
log.Print("Failed to download")
|
||||
log.Print(err)
|
||||
}
|
||||
|
||||
item.Status = "finished"
|
||||
CurrentWork[i] = item
|
||||
|
||||
log.Print(string(output))
|
||||
delete(currentWork, i) // Remove the URL from list of current work
|
||||
|
||||
// Clean up directory
|
||||
files, err := ioutil.ReadDir(dir + "/")
|
||||
|
|
|
|||
Loading…
Reference in New Issue