Compare commits
10 Commits
e4ef9da436
...
0d852ce68c
| Author | SHA1 | Date |
|---|---|---|
|
|
0d852ce68c | |
|
|
498e8ef5e5 | |
|
|
e46860adad | |
|
|
2f43820f6a | |
|
|
f20308f7ce | |
|
|
df57d7a57e | |
|
|
ba1bf0afde | |
|
|
15f7c0c6e1 | |
|
|
716605b3de | |
|
|
19ec9225f5 |
|
|
@ -13,5 +13,9 @@ A Makefile is in progress but currently does not add the version.
|
||||||
|
|
||||||
# Run
|
# Run
|
||||||
|
|
||||||
The default port is 8080. Env VIDDOWNLOC is where videos are saved to.
|
The default port is 8080.
|
||||||
|
|
||||||
|
For help:
|
||||||
|
|
||||||
|
viddown -h
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,4 @@ RUN apt-get clean
|
||||||
|
|
||||||
COPY viddown /usr/bin/viddown
|
COPY viddown /usr/bin/viddown
|
||||||
|
|
||||||
ENV VIDDOWNLOC /srv/
|
CMD ["/usr/bin/viddown", "-o", "/srv"]
|
||||||
|
|
||||||
CMD ["/usr/bin/viddown"]
|
|
||||||
|
|
|
||||||
224
src/viddown.go
224
src/viddown.go
|
|
@ -18,41 +18,103 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/gob"
|
||||||
|
"encoding/json"
|
||||||
|
"flag"
|
||||||
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"io"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"net/http"
|
|
||||||
"path"
|
"path"
|
||||||
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
const template = `
|
// Used to request an operation (download)
|
||||||
<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 {
|
type ops struct {
|
||||||
URL string
|
URL string
|
||||||
Res chan string
|
Res chan string
|
||||||
}
|
}
|
||||||
|
|
||||||
var config struct{
|
type WorkItem struct {
|
||||||
saveLocation string
|
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)
|
var opsc = make(chan ops)
|
||||||
|
|
||||||
|
// 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) {
|
func handleRequest(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.Method == http.MethodGet{
|
if r.Method == http.MethodGet && r.URL.Path == "/currentwork" {
|
||||||
log.Print("New Get request")
|
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 {
|
} else if r.Method == http.MethodPost {
|
||||||
r.ParseForm()
|
r.ParseForm()
|
||||||
go func() {
|
go func() {
|
||||||
|
|
@ -60,41 +122,123 @@ func handleRequest (w http.ResponseWriter, r *http.Request) {
|
||||||
o.URL = r.FormValue("URL")
|
o.URL = r.FormValue("URL")
|
||||||
opsc <- o
|
opsc <- o
|
||||||
}()
|
}()
|
||||||
|
http.Redirect(w, r, "/static", 301)
|
||||||
} else {
|
} else {
|
||||||
}
|
}
|
||||||
io.WriteString (w,template)
|
|
||||||
}
|
|
||||||
|
|
||||||
func downloadVid (r *http.Request) {
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// operationHandler waits for operation requests and initiates them
|
||||||
func operationHandler() {
|
func operationHandler() {
|
||||||
for true {
|
// 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
|
o := <-opsc
|
||||||
log.Print("Start Downloading: " + o.URL)
|
log.Print("Start Downloading: " + o.URL)
|
||||||
go func() {
|
|
||||||
cmd := exec.Command("youtube-dl", o.URL)
|
// Spin off a go routine to handle a download so the handler can continue to receive requests
|
||||||
cmd.Dir = config.saveLocation
|
go download(i, o.URL)
|
||||||
cmd.Start()
|
|
||||||
}()
|
|
||||||
}
|
}
|
||||||
log.Fatal("Exited operationsHandler")
|
log.Fatal("Exited operationsHandler")
|
||||||
}
|
}
|
||||||
|
|
||||||
func main () {
|
func download(i int, url string) {
|
||||||
if os.Getenv("VIDDOWNLOC") == "" {
|
dir := path.Join(config.saveLocation, ".viddown", strconv.Itoa(i))
|
||||||
config.saveLocation = "./"
|
var item WorkItem
|
||||||
} else {
|
item.URL = url
|
||||||
config.saveLocation = os.Getenv("VIDDOWNLOC")
|
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)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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)
|
log.Println("Downloading files to: " + config.saveLocation)
|
||||||
|
|
||||||
go operationHandler()
|
|
||||||
|
|
||||||
handler := http.HandlerFunc(handleRequest)
|
|
||||||
http.Handle("/",handler)
|
|
||||||
log.Printf("Starting server on 8080")
|
|
||||||
http.ListenAndServe(":8080",nil)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -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>
|
||||||
Loading…
Reference in New Issue