Compare commits
No commits in common. "0d852ce68cb1676921dd24e4a352f1800f25ff93" and "e4ef9da4363c1bd959810e405ebcafda48444ed0" have entirely different histories.
0d852ce68c
...
e4ef9da436
|
|
@ -13,9 +13,5 @@ A Makefile is in progress but currently does not add the version.
|
|||
|
||||
# Run
|
||||
|
||||
The default port is 8080.
|
||||
|
||||
For help:
|
||||
|
||||
viddown -h
|
||||
The default port is 8080. Env VIDDOWNLOC is where videos are saved to.
|
||||
|
||||
|
|
|
|||
|
|
@ -6,4 +6,6 @@ RUN apt-get clean
|
|||
|
||||
COPY viddown /usr/bin/viddown
|
||||
|
||||
CMD ["/usr/bin/viddown", "-o", "/srv"]
|
||||
ENV VIDDOWNLOC /srv/
|
||||
|
||||
CMD ["/usr/bin/viddown"]
|
||||
|
|
|
|||
216
src/viddown.go
216
src/viddown.go
|
|
@ -18,103 +18,41 @@
|
|||
*/
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/gob"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"net/http"
|
||||
"path"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// Used to request an operation (download)
|
||||
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{
|
||||
URL string
|
||||
Res chan 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)
|
||||
|
||||
// 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)
|
||||
if r.Method == http.MethodGet{
|
||||
log.Print("New Get request")
|
||||
}else if r.Method == http.MethodPost {
|
||||
r.ParseForm()
|
||||
go func() {
|
||||
|
|
@ -122,123 +60,41 @@ func handleRequest(w http.ResponseWriter, r *http.Request) {
|
|||
o.URL = r.FormValue("URL")
|
||||
opsc <- o
|
||||
} ()
|
||||
http.Redirect(w, r, "/static", 301)
|
||||
} else{
|
||||
}
|
||||
io.WriteString (w,template)
|
||||
}
|
||||
|
||||
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
|
||||
_, ok := currentWork[i]
|
||||
if ok {
|
||||
continue
|
||||
}
|
||||
|
||||
// Check if directory already exists
|
||||
_, err = os.Stat(dir)
|
||||
if err == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
for true {
|
||||
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)
|
||||
go func() {
|
||||
cmd := exec.Command("youtube-dl", o.URL)
|
||||
cmd.Dir = config.saveLocation
|
||||
cmd.Start()
|
||||
}()
|
||||
}
|
||||
log.Fatal("Exited operationsHandler")
|
||||
}
|
||||
|
||||
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)
|
||||
func main () {
|
||||
if os.Getenv("VIDDOWNLOC") == "" {
|
||||
config.saveLocation = "./"
|
||||
} else {
|
||||
config.saveLocation = os.Getenv("VIDDOWNLOC")
|
||||
}
|
||||
|
||||
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)
|
||||
|
||||
}
|
||||
|
||||
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)
|
||||
log.Printf("Starting server on 8080")
|
||||
http.ListenAndServe(":8080",nil)
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
39
src/web.go
39
src/web.go
|
|
@ -1,39 +0,0 @@
|
|||
/*
|
||||
* 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)
|
||||
}
|
||||
}
|
||||
|
|
@ -1,33 +0,0 @@
|
|||
<!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