2014-12-23 17:38:01 -05:00
|
|
|
// Watch a torrent directory, poll rtorrent, and download completed torrents over SFTP.
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2014-12-23 17:59:25 -05:00
|
|
|
"flag"
|
2014-12-23 17:38:01 -05:00
|
|
|
"github.com/adampresley/sigint"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Load information from a given config file config_path
|
2014-12-29 18:58:23 -05:00
|
|
|
func loadConfig(configPath string) (map[string]string, error) {
|
|
|
|
file, err := os.Open(configPath)
|
2014-12-23 17:38:01 -05:00
|
|
|
if err != nil {
|
2014-12-29 18:58:23 -05:00
|
|
|
log.Println("Failed to open configuration file " + configPath)
|
2014-12-23 17:38:01 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := ioutil.ReadAll(file)
|
|
|
|
if err != nil {
|
2014-12-29 18:58:23 -05:00
|
|
|
log.Println("Failed to read configuration file " + configPath)
|
2014-12-23 17:38:01 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
config := make(map[string]string)
|
|
|
|
|
|
|
|
lines := strings.Split(string(data), "\n")
|
|
|
|
for _, line := range lines {
|
|
|
|
line = strings.TrimSpace(line)
|
|
|
|
// Ignore comments
|
|
|
|
if len(line) <= 2 || line[:2] == "//" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ignore malformed lines
|
2014-12-29 18:58:23 -05:00
|
|
|
sepPosition := strings.Index(line, ": \"")
|
|
|
|
if sepPosition == -1 {
|
2014-12-23 17:38:01 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2014-12-29 18:58:23 -05:00
|
|
|
config[line[:sepPosition]] = line[sepPosition + 3:len(line) - 1]
|
2014-12-23 17:38:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return config, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Checker routine to see if torrents are completed
|
2014-12-29 18:58:23 -05:00
|
|
|
func checker(config map[string]string, checkerChan <- chan map[string]string, com chan <- error) error {
|
2014-12-23 17:38:01 -05:00
|
|
|
for {
|
2014-12-29 18:58:23 -05:00
|
|
|
torrentInfo := <-checkerChan
|
2014-12-23 17:38:01 -05:00
|
|
|
|
2014-12-29 18:58:23 -05:00
|
|
|
log.Println("Started checking " + torrentInfo["torrent_path"])
|
2014-12-23 17:38:01 -05:00
|
|
|
|
2014-12-29 18:58:23 -05:00
|
|
|
torrent, err := NewTorrent(config["xml_user"], config["xml_pass"], config["xml_address"], torrentInfo["torrent_path"])
|
2014-12-23 17:38:01 -05:00
|
|
|
if err != nil {
|
2014-12-29 18:58:23 -05:00
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
log.Println("Failed to initialize torrent for " + torrentInfo["torrent_path"] + ": " + err.Error())
|
|
|
|
}
|
|
|
|
|
2014-12-23 17:38:01 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
syncer, err := NewSync(config["threads"], config["ssh_user"], config["ssh_pass"], config["ssh_server"], config["ssh_port"])
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Failed to create a new sync: " + err.Error())
|
|
|
|
com <- err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
completed, err := torrent.GetTorrentComplete()
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Failed to see if " + torrent.path + " is completed: " + err.Error())
|
|
|
|
com <- err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
name, err := torrent.GetTorrentName()
|
|
|
|
if err != nil {
|
|
|
|
com <- err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if completed {
|
|
|
|
log.Println(name + " is completed, starting download now")
|
|
|
|
|
2014-12-29 18:58:23 -05:00
|
|
|
remoteDownloadPath := filepath.Join(config["remote_download_dir"], name)
|
|
|
|
exists, err := syncer.Exists(remoteDownloadPath)
|
2014-12-23 17:38:01 -05:00
|
|
|
if err != nil {
|
2014-12-29 18:58:23 -05:00
|
|
|
log.Println("Failed to see if " + remoteDownloadPath + " exists: " + err.Error())
|
2014-12-23 17:38:01 -05:00
|
|
|
com <- err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// file/dir to downlaod does not exist!
|
|
|
|
if !exists {
|
2014-12-29 18:58:23 -05:00
|
|
|
err = errors.New(remoteDownloadPath + " does not exist on remote server")
|
2014-12-23 17:38:01 -05:00
|
|
|
com <- err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-12-29 18:58:23 -05:00
|
|
|
completedDestination := filepath.Join(torrentInfo["local_download_dir"], name)
|
2014-12-23 17:38:01 -05:00
|
|
|
|
2014-12-29 18:58:23 -05:00
|
|
|
_, err = os.Stat(completedDestination)
|
2014-12-23 17:38:01 -05:00
|
|
|
if err == nil {
|
2014-12-29 18:58:23 -05:00
|
|
|
err = errors.New(completedDestination + " already exists, not downloading")
|
2014-12-23 17:38:01 -05:00
|
|
|
continue
|
|
|
|
} else if !os.IsNotExist(err) {
|
2014-12-29 18:58:23 -05:00
|
|
|
log.Println("Failed to stat: " + completedDestination + ": " + err.Error())
|
2014-12-23 17:38:01 -05:00
|
|
|
com <- err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-12-29 18:58:23 -05:00
|
|
|
err = syncer.GetPath(remoteDownloadPath, config["temp_download_dir"])
|
2014-12-23 17:38:01 -05:00
|
|
|
if err != nil {
|
2014-12-29 18:58:23 -05:00
|
|
|
log.Println("Failed to download " + remoteDownloadPath + ": " + err.Error())
|
2014-12-23 17:38:01 -05:00
|
|
|
com <- err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("Successfully downloaded " + name)
|
|
|
|
|
2014-12-29 18:58:23 -05:00
|
|
|
tempDestination := filepath.Join(config["temp_download_dir"], name)
|
2014-12-23 17:38:01 -05:00
|
|
|
|
2014-12-29 18:58:23 -05:00
|
|
|
err = os.Rename(tempDestination, completedDestination)
|
2014-12-23 17:38:01 -05:00
|
|
|
if err != nil {
|
2014-12-29 18:58:23 -05:00
|
|
|
log.Println("Failed to move " + tempDestination + " to " + completedDestination + ": " + err.Error())
|
2014-12-23 17:38:01 -05:00
|
|
|
com <- err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = os.Remove(torrent.path)
|
|
|
|
if err != nil && !os.IsNotExist(err) {
|
|
|
|
log.Println("Failed to remove " + torrent.path + ": " + err.Error())
|
|
|
|
com <- err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.Println(name + " is not completed, waiting for it to finish")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
com <- nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scanner routine to see if there are new torrent_files
|
2014-12-29 18:58:23 -05:00
|
|
|
func scanner(config map[string]string, checkerChan chan <- map[string]string, com chan <- error) error {
|
|
|
|
watchDirs := map[string]string{config["local_torrent_dir"]: config["local_download_dir"]}
|
|
|
|
dirContents, err := ioutil.ReadDir(config["local_torrent_dir"])
|
2014-12-23 17:38:01 -05:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
com <- err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-12-29 18:58:23 -05:00
|
|
|
for _, file := range dirContents {
|
2014-12-23 17:38:01 -05:00
|
|
|
if file.IsDir() {
|
2014-12-29 18:58:23 -05:00
|
|
|
watchDirs[filepath.Join(config["local_torrent_dir"], file.Name())] = filepath.Join(config["local_download_dir"], file.Name())
|
2014-12-23 17:38:01 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-29 18:58:23 -05:00
|
|
|
uploaded := make(map[string]bool)
|
|
|
|
downloadingTorrentPath := ""
|
2014-12-23 17:38:01 -05:00
|
|
|
for {
|
2014-12-29 18:58:23 -05:00
|
|
|
for watchDir, downloadDir := range watchDirs {
|
|
|
|
torrentFiles, err := ioutil.ReadDir(watchDir)
|
2014-12-23 17:38:01 -05:00
|
|
|
if err != nil {
|
|
|
|
com <- err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-12-29 18:58:23 -05:00
|
|
|
for _, torrentFile := range torrentFiles {
|
|
|
|
if torrentFile.IsDir() {
|
2014-12-23 17:38:01 -05:00
|
|
|
// skip because we don't do more than one level of watching
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2014-12-29 18:58:23 -05:00
|
|
|
torrentPath := filepath.Join(watchDir, torrentFile.Name())
|
|
|
|
|
|
|
|
if !uploaded[torrentPath] {
|
|
|
|
syncer, err := NewSync("1", config["ssh_user"], config["ssh_pass"], config["ssh_server"], config["ssh_port"])
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Failed to create a new sync: " + err.Error())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
destinationTorrent := filepath.Join(config["remote_torrent_dir"], filepath.Base(torrentPath))
|
|
|
|
exists, err := syncer.Exists(destinationTorrent)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Failed to see if " + torrentPath + " already exists on the server: " + err.Error())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if exists {
|
|
|
|
uploaded[torrentPath] = true
|
|
|
|
} else {
|
|
|
|
err = syncer.SendFiles(map[string]string{torrentPath: destinationTorrent})
|
|
|
|
if err == nil {
|
|
|
|
log.Println("Successfully uploaded " + torrentPath + " to " + destinationTorrent)
|
|
|
|
uploaded[torrentPath] = true
|
|
|
|
} else {
|
|
|
|
log.Println("Failed to upload " + torrentPath + " to " + destinationTorrent + ": " + err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
downloadInfo := map[string]string{
|
|
|
|
"torrent_path": torrentPath,
|
|
|
|
"local_download_dir": downloadDir,
|
|
|
|
}
|
|
|
|
|
|
|
|
// try to send the info to the checker goroutine (nonblocking)
|
|
|
|
select {
|
|
|
|
case checkerChan <- downloadInfo:
|
|
|
|
// don't keep track of completed downloads in the uploaded map
|
|
|
|
if downloadingTorrentPath != "" {
|
|
|
|
delete(uploaded, downloadingTorrentPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
downloadingTorrentPath = torrentPath
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
break
|
2014-12-23 17:38:01 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(time.Second * 30)
|
|
|
|
}
|
|
|
|
|
|
|
|
com <- nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
sigint.ListenForSIGINT(func() {
|
|
|
|
log.Println("Quiting")
|
|
|
|
os.Exit(1)
|
|
|
|
})
|
|
|
|
|
2014-12-29 18:58:23 -05:00
|
|
|
var configPath string
|
|
|
|
flag.StringVar(&configPath, "config", "", "Location of the config file")
|
2014-12-23 17:59:25 -05:00
|
|
|
flag.Parse()
|
|
|
|
|
2014-12-29 18:58:23 -05:00
|
|
|
if configPath == "" {
|
2014-12-23 17:59:25 -05:00
|
|
|
log.Println("Missing argument for configuration file path")
|
|
|
|
flag.PrintDefaults()
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2014-12-23 17:38:01 -05:00
|
|
|
log.Println("Reading configuration file")
|
2014-12-29 18:58:23 -05:00
|
|
|
config, err := loadConfig(configPath)
|
2014-12-23 17:38:01 -05:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("Successfully read configuration file")
|
|
|
|
|
2014-12-29 18:58:23 -05:00
|
|
|
checkerChan := make(chan map[string]string, 50)
|
2014-12-23 17:38:01 -05:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("Starting the scanner routine")
|
2014-12-29 18:58:23 -05:00
|
|
|
scannerCom := make(chan error)
|
|
|
|
go scanner(config, checkerChan, scannerCom)
|
2014-12-23 17:38:01 -05:00
|
|
|
|
|
|
|
log.Println("Starting the checker routine")
|
2014-12-29 18:58:23 -05:00
|
|
|
checkerCom := make(chan error)
|
|
|
|
go checker(config, checkerChan, checkerCom)
|
2014-12-23 17:38:01 -05:00
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
2014-12-29 18:58:23 -05:00
|
|
|
case err := <-scannerCom:
|
2014-12-23 17:38:01 -05:00
|
|
|
if err != nil {
|
|
|
|
log.Println("Scanner failed: " + err.Error())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2014-12-29 18:58:23 -05:00
|
|
|
case err := <-checkerCom:
|
2014-12-23 17:38:01 -05:00
|
|
|
if err != nil {
|
|
|
|
log.Println("Checker failed: " + err.Error())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2014-12-29 18:58:23 -05:00
|
|
|
default:
|
|
|
|
break
|
2014-12-23 17:38:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(time.Second * 5)
|
|
|
|
}
|
|
|
|
}
|