watchFolder #8
@@ -11,6 +11,7 @@ BaseDir = "/path/to/save/downloads"
 | 
			
		||||
Format = "mkv"
 | 
			
		||||
TempBaseDir = "/tmp/nre"
 | 
			
		||||
EnableConsole = true
 | 
			
		||||
WatchedFolder = "/path/to/watched/folder"
 | 
			
		||||
 | 
			
		||||
[N_m3u8DL-RE]
 | 
			
		||||
Path = "/path/to/N_m3u8DL-RE"
 | 
			
		||||
 
 | 
			
		||||
@@ -2,6 +2,7 @@ BaseDir = "/mnt/media"
 | 
			
		||||
Format = "mkv" 
 | 
			
		||||
TempBaseDir = "/tmp/nre"
 | 
			
		||||
EnableConsole = true
 | 
			
		||||
WatchedFolder = "/mnt/watched"
 | 
			
		||||
 | 
			
		||||
[N_m3u8DLRE]
 | 
			
		||||
Path = "nre"
 | 
			
		||||
@@ -16,6 +16,7 @@ type Config struct {
 | 
			
		||||
		Path string
 | 
			
		||||
	}
 | 
			
		||||
	EnableConsole bool
 | 
			
		||||
	WatchedFolder string
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
var config Config
 | 
			
		||||
@@ -40,4 +41,8 @@ func loadConfig() {
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if config.WatchedFolder == "" {
 | 
			
		||||
		fmt.Println("Error: Watched folder is not specified in the config file")
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -8,9 +8,12 @@ require (
 | 
			
		||||
	github.com/beevik/etree v1.4.1
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
require golang.org/x/sys v0.4.0 // indirect
 | 
			
		||||
 | 
			
		||||
require (
 | 
			
		||||
	github.com/asticode/go-astikit v0.20.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
 | 
			
		||||
	golang.org/x/net v0.0.0-20200904194848-62affa334b73 // indirect
 | 
			
		||||
	golang.org/x/text v0.3.2 // indirect
 | 
			
		||||
 
 | 
			
		||||
@@ -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/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/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/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
 | 
			
		||||
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-20190412213103-97732733099d/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.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
 | 
			
		||||
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
 | 
			
		||||
 
 | 
			
		||||
@@ -60,6 +60,7 @@ func main() {
 | 
			
		||||
	flag.Parse()
 | 
			
		||||
 | 
			
		||||
	if *inputFile == "" {
 | 
			
		||||
		go watchFolder()
 | 
			
		||||
		startWebServer()
 | 
			
		||||
	} else {
 | 
			
		||||
		items, err := parseInputFile(*inputFile)
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										114
									
								
								src/watcher.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										114
									
								
								src/watcher.go
									
									
									
									
									
										Normal 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
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user