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