Added useful comments
parent
716605b3de
commit
15f7c0c6e1
|
|
@ -46,18 +46,25 @@ const template = `
|
||||||
</html>
|
</html>
|
||||||
`
|
`
|
||||||
|
|
||||||
|
// Used to request an operation (download)
|
||||||
type ops struct {
|
type ops struct {
|
||||||
URL string
|
URL string
|
||||||
Res chan string
|
Res chan string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// List of the operations currently being worked on
|
||||||
var currentWork map[int]string
|
var currentWork map[int]string
|
||||||
|
|
||||||
|
// program configurations
|
||||||
var config struct {
|
var config struct {
|
||||||
|
// Where files should be saved
|
||||||
saveLocation string
|
saveLocation string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Global channel to send operation requests to
|
||||||
var opsc = make(chan ops)
|
var opsc = make(chan ops)
|
||||||
|
|
||||||
|
// 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 {
|
if r.Method == http.MethodGet {
|
||||||
log.Print("New Get request")
|
log.Print("New Get request")
|
||||||
|
|
@ -70,6 +77,8 @@ func handleRequest(w http.ResponseWriter, r *http.Request) {
|
||||||
}()
|
}()
|
||||||
} else {
|
} else {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Modify template and send it to the requester
|
||||||
var insert string
|
var insert string
|
||||||
for _, url := range currentWork {
|
for _, url := range currentWork {
|
||||||
insert += "<br>" + url
|
insert += "<br>" + url
|
||||||
|
|
@ -81,22 +90,28 @@ func handleRequest(w http.ResponseWriter, r *http.Request) {
|
||||||
func downloadVid(r *http.Request) {
|
func downloadVid(r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// operationHandler waits for operation requests and initiates them
|
||||||
func operationHandler() {
|
func operationHandler() {
|
||||||
|
// Simple index for sorting video work
|
||||||
for i := 0; true; i++ {
|
for i := 0; true; i++ {
|
||||||
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
|
||||||
go func() {
|
go func() {
|
||||||
|
// Create a unique directory to download the video in
|
||||||
dir := path.Join(config.saveLocation, ".viddown", strconv.Itoa(i))
|
dir := path.Join(config.saveLocation, ".viddown", strconv.Itoa(i))
|
||||||
os.MkdirAll(dir, 0755)
|
os.MkdirAll(dir, 0755)
|
||||||
|
|
||||||
|
// Build the download command
|
||||||
cmd := exec.Command("youtube-dl", o.URL)
|
cmd := exec.Command("youtube-dl", o.URL)
|
||||||
cmd.Dir = dir + "/"
|
cmd.Dir = dir + "/"
|
||||||
currentWork[i] = o.URL
|
currentWork[i] = o.URL // Adds the URL to list of current work
|
||||||
cmd.Run()
|
cmd.Run()
|
||||||
|
|
||||||
log.Print("Finished: " + o.URL)
|
log.Print("Finished: " + o.URL)
|
||||||
|
delete(currentWork, i) // Remove the URL from list of current work
|
||||||
|
|
||||||
delete(currentWork, i)
|
// Clean up directory
|
||||||
|
|
||||||
files, err := ioutil.ReadDir(dir + "/")
|
files, err := ioutil.ReadDir(dir + "/")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Print(err)
|
log.Print(err)
|
||||||
|
|
@ -122,6 +137,7 @@ func operationHandler() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
// Set the location to save videos
|
||||||
if os.Getenv("VIDDOWNLOC") == "" {
|
if os.Getenv("VIDDOWNLOC") == "" {
|
||||||
config.saveLocation = "./"
|
config.saveLocation = "./"
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -129,12 +145,16 @@ func main() {
|
||||||
}
|
}
|
||||||
log.Println("Downloading files to: " + config.saveLocation)
|
log.Println("Downloading files to: " + config.saveLocation)
|
||||||
|
|
||||||
|
// initialize currentWork with a blank map
|
||||||
currentWork = make(map[int]string)
|
currentWork = make(map[int]string)
|
||||||
|
|
||||||
|
// Create the work directory
|
||||||
os.MkdirAll(path.Join(config.saveLocation, ".viddown"), 0755)
|
os.MkdirAll(path.Join(config.saveLocation, ".viddown"), 0755)
|
||||||
|
|
||||||
|
// Start the operations handler
|
||||||
go operationHandler()
|
go operationHandler()
|
||||||
|
|
||||||
|
// Start the http server
|
||||||
handler := http.HandlerFunc(handleRequest)
|
handler := http.HandlerFunc(handleRequest)
|
||||||
http.Handle("/", handler)
|
http.Handle("/", handler)
|
||||||
log.Printf("Starting server on 8080")
|
log.Printf("Starting server on 8080")
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue