Compare commits

...

10 Commits

5 changed files with 268 additions and 50 deletions

View File

@ -13,5 +13,9 @@ A Makefile is in progress but currently does not add the version.
# Run
The default port is 8080. Env VIDDOWNLOC is where videos are saved to.
The default port is 8080.
For help:
viddown -h

View File

@ -6,6 +6,4 @@ RUN apt-get clean
COPY viddown /usr/bin/viddown
ENV VIDDOWNLOC /srv/
CMD ["/usr/bin/viddown"]
CMD ["/usr/bin/viddown", "-o", "/srv"]

View File

@ -18,83 +18,227 @@
*/
package main
import (
"encoding/gob"
"encoding/json"
"flag"
"io/ioutil"
"log"
"io"
"net/http"
"os"
"os/exec"
"net/http"
"path"
"strconv"
)
const template = `
<html>
<head>
<title>Download</title>
</head>
<body>
<form method="POST">
<label>URL: <input name="URL" type="text""></label>
<input type="submit" value="Download">
</form>
</body>
</html>
`
type ops struct{
// Used to request an operation (download)
type ops struct {
URL string
Res chan string
}
var config struct{
saveLocation string
type WorkItem struct {
Status string
URL string
}
type CurrentWork map[int]WorkItem
// CurrentWork
var currentWork CurrentWork = make(map[int]WorkItem)
// program configurations
var config struct {
// Where files should be saved
saveLocation string
// Where to find youtube-dl program, must be full name
youtubedll string
// The port the server listens on
port int
}
// Global channel to send operation requests to
var opsc = make(chan ops)
func handleRequest (w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet{
log.Print("New Get request")
}else if r.Method == http.MethodPost {
// read the saved currentWork file
func (cw *CurrentWork) read() (err error) {
dir := path.Join(config.saveLocation, ".viddown", "currentWork")
f, err := os.Open(dir)
defer f.Close()
if os.IsNotExist(err) {
return nil
} else if err != nil {
return
}
// Decode data
decoder := gob.NewDecoder(f)
err = decoder.Decode(cw)
return
}
// save the currentWork to the save file
func (cw *CurrentWork) save() (err error) {
dir := path.Join(config.saveLocation, ".viddown", "currentWork")
f, err := os.Create(dir)
defer f.Close()
if err != nil {
return
}
encoder := gob.NewEncoder(f)
err = encoder.Encode(cw)
return
}
// apply an item then save it
func (cw *CurrentWork) apply(i int, item WorkItem) (err error) {
(*cw)[i] = item
cw.save()
return
}
// 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)
if err != nil {
log.Panic(err)
}
w.Write(list)
return
} else if r.Method == http.MethodGet {
log.Print("New Get request: " + r.URL.Path)
http.Redirect(w, r, "/static", 301)
} else if r.Method == http.MethodPost {
r.ParseForm()
go func() {
var o ops
o.URL = r.FormValue("URL")
opsc <- o
} ()
} else{
}
io.WriteString (w,template)
}
func downloadVid (r *http.Request) {
}
func operationHandler(){
for true {
o := <- opsc
log.Print("Start Downloading: " + o.URL)
go func() {
cmd := exec.Command("youtube-dl", o.URL)
cmd.Dir = config.saveLocation
cmd.Start()
}()
http.Redirect(w, r, "/static", 301)
} else {
}
}
// 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
_, ok := currentWork[i]
if ok {
continue
}
// Check if directory already 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 download(i, o.URL)
}
log.Fatal("Exited operationsHandler")
}
func main () {
if os.Getenv("VIDDOWNLOC") == "" {
config.saveLocation = "./"
} else {
config.saveLocation = os.Getenv("VIDDOWNLOC")
func download(i int, url string) {
dir := path.Join(config.saveLocation, ".viddown", strconv.Itoa(i))
var item WorkItem
item.URL = url
item.Status = "downloading"
currentWork.apply(i, item)
// Create a unique directory to download the video in
os.MkdirAll(dir, 0755)
// Build the download command
cmd := exec.Command(config.youtubedll, url)
cmd.Dir = dir + "/"
output, err := cmd.CombinedOutput()
log.Print("Finished: " + url)
if err != nil {
item.Status = "failed"
currentWork[i] = item
log.Print("Failed to download")
log.Print(err)
}
log.Println("Downloading files to: " + config.saveLocation)
go operationHandler()
item.Status = "finished"
currentWork.apply(i, item)
log.Print(string(output))
// Clean up directory
files, err := ioutil.ReadDir(dir + "/")
if err != nil {
log.Print(err)
}
for _, file := range files {
log.Print("Moving: " + file.Name())
err := os.Rename(dir+"/"+file.Name(), file.Name())
if err != nil {
log.Print(err)
log.Print("Failed to move: " + dir + "/" + file.Name() + " to: " + file.Name())
}
}
err = os.RemoveAll(dir)
if err != nil {
log.Print("Failed to remove: " + dir)
}
handler := http.HandlerFunc(handleRequest)
http.Handle("/",handler)
log.Printf("Starting server on 8080")
http.ListenAndServe(":8080",nil)
}
// handleConfigs configures the program at start, handling any flags or env variables
func handleConfigs() {
flag.StringVar(&config.saveLocation, "o", "./", "The output location for videos")
flag.StringVar(&config.youtubedll, "y", "youtube-dl", "The full location of youtube-dl")
flag.IntVar(&config.port, "p", 8080, "The port the server listens on")
flag.Parse()
log.Println("Downloading files to: " + config.saveLocation)
}
func main() {
handleConfigs()
// initialize currentWork
err := currentWork.read()
if err != nil {
log.Fatal(err)
}
// Continue old Downloads
for i, item := range currentWork {
if item.Status == "downloading" {
go download(i, item.URL)
}
}
// Create the work directory
os.MkdirAll(path.Join(config.saveLocation, ".viddown"), 0755)
// Start the operations handler
go operationHandler()
// Start the http server
handler := http.HandlerFunc(handleRequest)
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.FS(Content))))
http.Handle("/", handler)
log.Printf("Starting server on " + strconv.Itoa(config.port))
http.ListenAndServe(":"+strconv.Itoa(config.port), nil)
}

39
src/web.go Normal file
View File

@ -0,0 +1,39 @@
/*
* Copyright 2022 Justin Reichardt
*
* This file is part of viddown.
*
* fedset is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* viddown is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with fedset. If not, see <https://www.gnu.org/licenses/>.
*/
package main
import (
"embed"
"io/fs"
"log"
)
//go:embed web/*
var content embed.FS
var Content fs.FS
func init() {
var err error
Content, err = fs.Sub(content, "web")
if err != nil {
log.Fatal(err)
}
}

33
src/web/index.html Normal file
View File

@ -0,0 +1,33 @@
<!DOCTYPE html>
<html>
<head>
<title>Download</title>
</head>
<body>
<form action="/" method="POST">
<label>URL: <input name="URL" type="text""></label>
<input type="submit" value="Download">
</form>
<script>
setInterval(()=>{
fetch("/currentwork").then(response => {
if (!response.ok){
throw new Error("HTTP error " + response.status);
}
return response.text();
}).then(json => {
parsed = JSON.parse(json)
console.log(parsed);
document.getElementById("list").innerText = "";
Object.entries(parsed).forEach(([key, value]) => {document.getElementById("list").innerText += value.Status + " - " + value.URL + "\n"});
if (json === "{}"){
}
}).catch( () => {
console.log("Error retrieving setup status");
});
}
, 1000);
</script>
<p id=list></p>
</body>
</html>