Fix torrent handler

Former-commit-id: 2bf1e2e21137e4020d2ace31e4560e9a97684403
master
bel 2019-06-26 17:41:19 -06:00
parent 46fe312222
commit cfea265312
3 changed files with 14 additions and 6 deletions

1
.gitignore vendored
View File

@ -2,3 +2,4 @@ rssmon3
exec-rssmon3
**.sw*
**/testdata
**/._*

View File

@ -25,6 +25,7 @@ type Config struct {
vpntor string
outdir string
interval time.Duration
last time.Time
db storage.DB
ctx context.Context
can context.CancelFunc
@ -35,6 +36,7 @@ func main() {
if err != nil {
panic(err)
}
log.Println(config)
for {
if err := mainLoop(config); err != nil {
@ -43,12 +45,15 @@ func main() {
}
}
func mainLoop(config Config) error {
func mainLoop(config *Config) error {
block := config.interval - time.Since(config.last)
log.Printf("Blocking %v", block)
select {
case <-time.After(config.interval):
case <-time.After(block):
if err := pull(config.db, config.vpntor, config.outdir, config.url); err != nil {
log.Println(err)
}
config.last = time.Now()
case <-config.ctx.Done():
if err := config.ctx.Err(); err != nil {
return err
@ -57,7 +62,7 @@ func mainLoop(config Config) error {
return nil
}
func config() (Config, error) {
func config() (*Config, error) {
as := args.NewArgSet()
as.Append(args.STRING, "url", "url of rss feed", "http://192.168.0.86:33419/api/tag/torrent")
as.Append(args.STRING, "vpntor", "url of vpntor", "http://192.168.0.86:9091/transmission/rpc")
@ -68,7 +73,7 @@ func config() (Config, error) {
as.Append(args.STRING, "user", "db user", "")
as.Append(args.STRING, "pass", "db pass", "")
if err := as.Parse(); err != nil {
return Config{}, err
return &Config{}, err
}
db, err := storage.New(
@ -82,7 +87,7 @@ func config() (Config, error) {
}
ctx, can := context.WithCancel(context.Background())
return Config{
return &Config{
url: as.Get("url").GetString(),
vpntor: as.Get("vpntor").GetString(),
interval: as.Get("interval").GetDuration(),
@ -98,6 +103,7 @@ func pull(db storage.DB, vpntor, outdir, url string) error {
if err != nil {
return err
}
log.Printf("feed: %v", gofeed.Title)
for _, item := range gofeed.Items {
if ok, err := isDone(db, item.Link); err != nil {
return err
@ -156,6 +162,7 @@ func isDone(db storage.DB, url string) (bool, error) {
}
func handle(vpntor, outdir, content string) error {
log.Printf("magnets: %v", findMagnets(content))
for _, magnet := range findMagnets(content) {
resp, err := submit(vpntor, outdir, magnet)
if err != nil {

View File

@ -52,7 +52,7 @@ func fakeRSSServer() *httptest.Server {
func TestMainLoopCtx(t *testing.T) {
ctx, can := context.WithCancel(context.Background())
can()
c := Config{
c := &Config{
interval: time.Hour,
ctx: ctx,
}