5376b9e271
* New config files As decided, config files are parsed at runtime. I decided to go for YAML config files because there can be comments in it. There are 2 files: * config/default_config.yml <= which shouldn't be edited unless we add a config parameter * config/config.yml <= which is the user-defined config. This file shouldn't be commited Changed every call to config.XXX to config.Conf.XXX (look to the new stucture of config in config/types.go) Of course, putting config parameters in config.yml overrides config in config_default.yml. You don't have to put everything in it, just add what you want to override. * Fixing test Replacing conf.New by config.Conf * Fixing call to config.Conf to config.Config{} in test files * Might have fixed testing with this Printf instead of Fatalf * Renaming config.yml in example file * Forbid commiting config.yml * Should be now fixed * Do not need this file anymore
62 lignes
1,4 Kio
Go
62 lignes
1,4 Kio
Go
package metainfoFetcher
|
|
|
|
import (
|
|
"errors"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/NyaaPantsu/nyaa/config"
|
|
"github.com/NyaaPantsu/nyaa/model"
|
|
"github.com/NyaaPantsu/nyaa/util"
|
|
"github.com/anacrolix/torrent/metainfo"
|
|
)
|
|
|
|
// FetchOperation struct
|
|
type FetchOperation struct {
|
|
fetcher *MetainfoFetcher
|
|
torrent model.Torrent
|
|
done chan int
|
|
}
|
|
|
|
// Result struct
|
|
type Result struct {
|
|
operation *FetchOperation
|
|
err error
|
|
info *metainfo.Info
|
|
}
|
|
|
|
// NewFetchOperation : Creates a new fetchoperation
|
|
func NewFetchOperation(fetcher *MetainfoFetcher, dbEntry model.Torrent) (op *FetchOperation) {
|
|
op = &FetchOperation{
|
|
fetcher: fetcher,
|
|
torrent: dbEntry,
|
|
done: make(chan int, 1),
|
|
}
|
|
return
|
|
}
|
|
|
|
// Start : Should be started from a goroutine somewhere
|
|
func (op *FetchOperation) Start(out chan Result) {
|
|
defer op.fetcher.wg.Done()
|
|
|
|
magnet := util.InfoHashToMagnet(strings.TrimSpace(op.torrent.Hash), op.torrent.Name, config.Conf.Torrents.Trackers.Default...)
|
|
downloadingTorrent, err := op.fetcher.torrentClient.AddMagnet(magnet)
|
|
if err != nil {
|
|
out <- Result{op, err, nil}
|
|
return
|
|
}
|
|
defer downloadingTorrent.Drop()
|
|
|
|
timeoutTimer := time.NewTicker(time.Second * time.Duration(op.fetcher.timeout))
|
|
defer timeoutTimer.Stop()
|
|
select {
|
|
case <-downloadingTorrent.GotInfo():
|
|
out <- Result{op, nil, downloadingTorrent.Info()}
|
|
break
|
|
case <-timeoutTimer.C:
|
|
out <- Result{op, errors.New("Timeout"), nil}
|
|
break
|
|
case <-op.done:
|
|
break
|
|
}
|
|
}
|