10 Commits

11 changed files with 190 additions and 16 deletions

View File

@ -10,6 +10,8 @@ Create a `config.toml` file in the same directory as the drmdtool executable:
BaseDir = "/path/to/save/downloads" BaseDir = "/path/to/save/downloads"
Format = "mkv" Format = "mkv"
TempBaseDir = "/tmp/nre" TempBaseDir = "/tmp/nre"
EnableConsole = true
WatchedFolder = "/path/to/watched/folder"
[N_m3u8DL-RE] [N_m3u8DL-RE]
Path = "/path/to/N_m3u8DL-RE" Path = "/path/to/N_m3u8DL-RE"

View File

@ -1,6 +1,8 @@
BaseDir = "/mnt/media" BaseDir = "/mnt/media"
Format = "mkv" Format = "mkv"
TempBaseDir = "/tmp/nre" TempBaseDir = "/tmp/nre"
EnableConsole = true
WatchedFolder = "/mnt/watched"
[N_m3u8DLRE] [N_m3u8DLRE]
Path = "nre" Path = "nre"

View File

@ -15,6 +15,8 @@ type Config struct {
N_m3u8DLRE struct { N_m3u8DLRE struct {
Path string Path string
} }
EnableConsole bool
WatchedFolder string
} }
var config Config var config Config
@ -39,4 +41,8 @@ func loadConfig() {
return return
} }
if config.WatchedFolder == "" {
fmt.Println("Error: Watched folder is not specified in the config file")
return
}
} }

View File

@ -20,7 +20,7 @@ func removeBOM(input []byte) []byte {
return input return input
} }
func downloadFile(item Item, jobInfo *JobInfo) error { func downloadFile(drmdFilename string, item Item, jobInfo *JobInfo) error {
logger.LogInfo("Download File", fmt.Sprintf("Starting download for: %s", item.Filename)) logger.LogInfo("Download File", fmt.Sprintf("Starting download for: %s", item.Filename))
tempDir := filepath.Join(config.TempBaseDir, sanitizeFilename(item.Filename)) tempDir := filepath.Join(config.TempBaseDir, sanitizeFilename(item.Filename))
@ -131,7 +131,10 @@ func downloadFile(item Item, jobInfo *JobInfo) error {
go func() { go func() {
for { for {
if outputBuffer.Len() > 0 { if outputBuffer.Len() > 0 {
broadcast(outputBuffer.Bytes()) message := outputBuffer.Bytes()
if config.EnableConsole {
broadcast(drmdFilename, message)
}
outputBuffer.Reset() outputBuffer.Reset()
} }
time.Sleep(1 * time.Second) time.Sleep(1 * time.Second)

View File

@ -8,9 +8,12 @@ require (
github.com/beevik/etree v1.4.1 github.com/beevik/etree v1.4.1
) )
require golang.org/x/sys v0.4.0 // indirect
require ( require (
github.com/asticode/go-astikit v0.20.0 // indirect github.com/asticode/go-astikit v0.20.0 // indirect
github.com/asticode/go-astits v1.8.0 // indirect github.com/asticode/go-astits v1.8.0 // indirect
github.com/fsnotify/fsnotify v1.7.0
github.com/gorilla/websocket v1.5.3 github.com/gorilla/websocket v1.5.3
golang.org/x/net v0.0.0-20200904194848-62affa334b73 // indirect golang.org/x/net v0.0.0-20200904194848-62affa334b73 // indirect
golang.org/x/text v0.3.2 // indirect golang.org/x/text v0.3.2 // indirect

View File

@ -10,6 +10,8 @@ github.com/beevik/etree v1.4.1 h1:PmQJDDYahBGNKDcpdX8uPy1xRCwoCGVUiW669MEirVI=
github.com/beevik/etree v1.4.1/go.mod h1:gPNJNaBGVZ9AwsidazFZyygnd+0pAU38N4D+WemwKNs= github.com/beevik/etree v1.4.1/go.mod h1:gPNJNaBGVZ9AwsidazFZyygnd+0pAU38N4D+WemwKNs=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg= github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/pkg/exec v0.0.0-20150614095509-0bd164ad2a5a h1:EN123kAtAAE2pg/+TvBsUBZfHCWNNFyL2ZBPPfNWAc0= github.com/pkg/exec v0.0.0-20150614095509-0bd164ad2a5a h1:EN123kAtAAE2pg/+TvBsUBZfHCWNNFyL2ZBPPfNWAc0=
@ -28,6 +30,8 @@ golang.org/x/net v0.0.0-20200904194848-62affa334b73/go.mod h1:/O7V0waA8r7cgGh81R
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.4.0 h1:Zr2JFtRQNX3BCZ8YtxRE9hNJYC8J6I1MVbMg6owUp18=
golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs= golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=

View File

@ -382,19 +382,36 @@ func updateProgress(filename string, value float64, currentFile string) {
} }
var upgrader = websocket.Upgrader{} var upgrader = websocket.Upgrader{}
var clients = make(map[*websocket.Conn]bool) var clients = make(map[string]map[*websocket.Conn]bool)
var mu sync.Mutex var mu sync.Mutex
func handleWebSocket(w http.ResponseWriter, r *http.Request) { func handleWebSocket(w http.ResponseWriter, r *http.Request) {
fmt.Println(config.EnableConsole)
if !config.EnableConsole {
http.Error(w, "Console output is disabled", http.StatusForbidden)
return
}
filename := r.URL.Query().Get("filename")
if filename == "" {
http.Error(w, "Filename is required", http.StatusBadRequest)
return
}
conn, err := upgrader.Upgrade(w, r, nil) conn, err := upgrader.Upgrade(w, r, nil)
if err != nil { if err != nil {
fmt.Println("Error while upgrading connection:", err) logger.LogError("WebSocket", fmt.Sprintf("Error while upgrading connection: %v", err))
return return
} }
defer conn.Close() defer conn.Close()
logger.LogInfo("WebSocket", fmt.Sprintf("WebSocket connection established for filename: %s", filename))
mu.Lock() mu.Lock()
clients[conn] = true if clients[filename] == nil {
clients[filename] = make(map[*websocket.Conn]bool)
}
clients[filename][conn] = true
mu.Unlock() mu.Unlock()
for { for {
@ -404,18 +421,25 @@ func handleWebSocket(w http.ResponseWriter, r *http.Request) {
} }
mu.Lock() mu.Lock()
delete(clients, conn) delete(clients[filename], conn)
mu.Unlock() mu.Unlock()
logger.LogInfo("WebSocket", fmt.Sprintf("WebSocket connection closed for filename: %s", filename))
} }
func broadcast(message []byte) { func broadcast(filename string, message []byte) {
if !config.EnableConsole {
return
}
mu.Lock() mu.Lock()
defer mu.Unlock() defer mu.Unlock()
for client := range clients { for client := range clients[filename] {
if err := client.WriteMessage(websocket.TextMessage, message); err != nil { if err := client.WriteMessage(websocket.TextMessage, message); err != nil {
client.Close() client.Close()
delete(clients, client) delete(clients[filename], client)
logger.LogError("Broadcast", fmt.Sprintf("Error writing message to client: %v", err))
} }
} }
} }

View File

@ -60,6 +60,7 @@ func main() {
flag.Parse() flag.Parse()
if *inputFile == "" { if *inputFile == "" {
go watchFolder()
startWebServer() startWebServer()
} else { } else {
items, err := parseInputFile(*inputFile) items, err := parseInputFile(*inputFile)

View File

@ -145,9 +145,10 @@
<div style="display: none;" id="console"></div> <div style="display: none;" id="console"></div>
<script> <script>
let isPaused = false; let isPaused = false;
const filename = "{{.Filename}}";
function updateProgress() { function updateProgress() {
fetch('/progress?filename={{.Filename}}', { fetch(`/progress?filename=${filename}`, {
headers: { headers: {
'Accept': 'application/json' 'Accept': 'application/json'
} }
@ -179,7 +180,7 @@
} }
function abortDownload() { function abortDownload() {
fetch('/abort?filename={{.Filename}}', { method: 'POST' }) fetch(`/abort?filename=${filename}`, { method: 'POST' })
.then(response => { .then(response => {
if (response.ok) { if (response.ok) {
console.log('Abort signal sent. The download will stop soon.'); console.log('Abort signal sent. The download will stop soon.');
@ -190,7 +191,7 @@
} }
function pauseDownload() { function pauseDownload() {
fetch('/pause?filename={{.Filename}}', { method: 'POST' }) fetch(`/pause?filename=${filename}`, { method: 'POST' })
.then(response => { .then(response => {
if (response.ok) { if (response.ok) {
console.log('Pause signal sent. The download will pause soon.'); console.log('Pause signal sent. The download will pause soon.');
@ -203,7 +204,7 @@
} }
function resumeDownload() { function resumeDownload() {
fetch('/resume?filename={{.Filename}}', { method: 'POST' }) fetch(`/resume?filename=${filename}`, { method: 'POST' })
.then(response => { .then(response => {
if (response.ok) { if (response.ok) {
console.log('Resume signal sent. The download will resume soon.'); console.log('Resume signal sent. The download will resume soon.');
@ -217,13 +218,22 @@
} }
const consoleDiv = document.getElementById('console'); const consoleDiv = document.getElementById('console');
const ws = new WebSocket(`ws://${window.location.host}/ws`); const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
const ws = new WebSocket(`${protocol}//${window.location.host}/ws?filename=${filename}`);
ws.onmessage = function(event) { ws.onmessage = function(event) {
consoleDiv.textContent += event.data; consoleDiv.textContent += event.data;
consoleDiv.scrollTop = consoleDiv.scrollHeight; consoleDiv.scrollTop = consoleDiv.scrollHeight;
}; };
ws.onclose = function() {
console.log('WebSocket connection closed');
};
ws.onerror = function(error) {
console.error('WebSocket error:', error);
};
document.getElementById('toggle-console').onclick = function() { document.getElementById('toggle-console').onclick = function() {
if (consoleDiv.style.display === "none") { if (consoleDiv.style.display === "none") {
consoleDiv.style.display = "block"; consoleDiv.style.display = "block";

View File

@ -259,21 +259,25 @@ func processItems(filename string, items []Item) error {
select { select {
case <-jobInfo.AbortChan: case <-jobInfo.AbortChan:
updateProgress(filename, 100, "Aborted") updateProgress(filename, 100, "Aborted")
logger.LogJobState(filename, "aborted")
return fmt.Errorf("download aborted") return fmt.Errorf("download aborted")
default: default:
if jobInfo.Paused { if jobInfo.Paused {
select { select {
case <-jobInfo.ResumeChan: case <-jobInfo.ResumeChan:
jobInfo.Paused = false jobInfo.Paused = false
logger.LogJobState(filename, "resumed")
case <-jobInfo.AbortChan: case <-jobInfo.AbortChan:
updateProgress(filename, 100, "Aborted") updateProgress(filename, 100, "Aborted")
logger.LogJobState(filename, "aborted")
return fmt.Errorf("download aborted") return fmt.Errorf("download aborted")
} }
} }
updateProgress(filename, float64(i)/float64(len(items))*100, items[i].Filename) updateProgress(filename, float64(i)/float64(len(items))*100, items[i].Filename)
err := downloadFile(items[i], jobInfo) err := downloadFile(filename, items[i], jobInfo)
if err != nil { if err != nil {
if err.Error() == "download paused" { if err.Error() == "download paused" {
logger.LogJobState(filename, "paused")
removeCompletedEpisodes(filename, items[:i]) removeCompletedEpisodes(filename, items[:i])
i-- i--
continue continue
@ -282,6 +286,7 @@ func processItems(filename string, items []Item) error {
} }
} }
updateProgress(filename, 100, "") updateProgress(filename, 100, "")
logger.LogJobState(filename, "completed successfully")
return nil return nil
} }

114
src/watcher.go Normal file
View File

@ -0,0 +1,114 @@
package main
import (
"fmt"
"io"
"log"
"os"
"path/filepath"
"strings"
"time"
"github.com/fsnotify/fsnotify"
)
func watchFolder() {
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
defer watcher.Close()
done := make(chan bool)
go func() {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
if event.Op&fsnotify.Create == fsnotify.Create {
if strings.HasSuffix(event.Name, ".drmd") {
fmt.Println("New .drmd detected:", event.Name)
processWatchedFile(event.Name)
}
}
case err, ok := <-watcher.Errors:
if !ok {
return
}
log.Println("Error:", err)
}
}
}()
err = watcher.Add(config.WatchedFolder)
if err != nil {
log.Fatal(err)
}
<-done
}
func processWatchedFile(filePath string) {
for {
initialSize, err := getFileSize(filePath)
if err != nil {
logger.LogError("Watcher", fmt.Sprintf("Error getting file size: %v", err))
return
}
time.Sleep(1 * time.Second)
currentSize, err := getFileSize(filePath)
if err != nil {
logger.LogError("Watcher", fmt.Sprintf("Error getting file size: %v", err))
return
}
if initialSize == currentSize {
break
}
}
file, err := os.Open(filePath)
if err != nil {
logger.LogError("Watcher", fmt.Sprintf("Error opening file: %v", err))
return
}
defer file.Close()
originalFilename := filepath.Base(filePath)
tempFile, err := os.CreateTemp(uploadDir, originalFilename)
if err != nil {
logger.LogError("Watcher", fmt.Sprintf("Error creating temporary file: %v", err))
return
}
defer tempFile.Close()
_, err = io.Copy(tempFile, file)
if err != nil {
logger.LogError("Watcher", fmt.Sprintf("Error copying file: %v", err))
return
}
if err := os.Remove(filePath); err != nil {
logger.LogError("Watcher", fmt.Sprintf("Error deleting original file: %v", err))
}
items, err := parseInputFile(tempFile.Name())
if err != nil {
logger.LogError("Watcher", fmt.Sprintf("Error parsing input file: %v", err))
return
}
go processItems(filepath.Base(tempFile.Name()), items)
}
func getFileSize(filePath string) (int64, error) {
fileInfo, err := os.Stat(filePath)
if err != nil {
return 0, err
}
return fileInfo.Size(), nil
}