Add scrape subcommand to cmd/torrent

This commit is contained in:
Matt Joiner 2022-02-20 16:16:57 +11:00
parent fe43ce07c8
commit 0e4546b738
4 changed files with 57 additions and 2 deletions

View File

@ -41,6 +41,16 @@ func mainErr() error {
}
return announceErr(cmd)
}),
args.Subcommand("scrape", func(p args.SubCmdCtx) error {
var cmd ScrapeCmd
err := p.NewParser().AddParams(
args.Pos("tracker", &cmd.Tracker),
args.Pos("infohash", &cmd.InfoHashes, args.Arity('+'))).Parse()
if err != nil {
return err
}
return scrape(cmd)
}),
args.Subcommand("download", func(p args.SubCmdCtx) error {
var dlc DownloadCmd
err := p.NewParser().AddParams(

44
cmd/torrent/scrape.go Normal file
View File

@ -0,0 +1,44 @@
package main
import (
"context"
"fmt"
"net/url"
"github.com/anacrolix/torrent/tracker/udp"
"github.com/davecgh/go-spew/spew"
"github.com/anacrolix/torrent"
)
type ScrapeCmd struct {
Tracker string `arg:"positional"`
InfoHashes []torrent.InfoHash
}
func scrape(flags ScrapeCmd) error {
trackerUrl, err := url.Parse(flags.Tracker)
if err != nil {
return fmt.Errorf("parsing tracker url: %w", err)
}
cc, err := udp.NewConnClient(udp.NewConnClientOpts{
Network: trackerUrl.Scheme,
Host: trackerUrl.Host,
//Ipv6: nil,
//Logger: log.Logger{},
})
if err != nil {
return fmt.Errorf("creaing new udp tracker conn client: %w", err)
}
defer cc.Close()
var ihs []udp.InfoHash
for _, ih := range flags.InfoHashes {
ihs = append(ihs, ih)
}
scrapeOut, err := cc.Client.Scrape(context.TODO(), ihs)
if err != nil {
return fmt.Errorf("scraping: %w", err)
}
spew.Dump(scrapeOut)
return nil
}

2
go.mod
View File

@ -5,7 +5,7 @@ go 1.16
require (
github.com/RoaringBitmap/roaring v0.9.4
github.com/alexflint/go-arg v1.4.2
github.com/anacrolix/args v0.4.1-0.20211104085705-59f0fe94eb8f
github.com/anacrolix/args v0.5.0
github.com/anacrolix/chansync v0.3.0
github.com/anacrolix/dht/v2 v2.15.2-0.20220123034220-0538803801cb
github.com/anacrolix/envpprof v1.1.1

3
go.sum
View File

@ -77,8 +77,9 @@ github.com/alexflint/go-scalar v1.0.0/go.mod h1:GpHzbCOZXEKMEcygYQ5n/aa4Aq84zbxj
github.com/alexflint/go-scalar v1.1.0 h1:aaAouLLzI9TChcPXotr6gUhq+Scr8rl0P9P4PnltbhM=
github.com/alexflint/go-scalar v1.1.0/go.mod h1:LoFvNMqS1CPrMVltza4LvnGKhaSpc3oyLEBUZVhhS2o=
github.com/anacrolix/args v0.3.0/go.mod h1:41JBnF8sKExNVLHPkCdL74jkZc3dSxAkGsk1TuKOUFI=
github.com/anacrolix/args v0.4.1-0.20211104085705-59f0fe94eb8f h1:NEHQxPja+2pvaFtFMUC4k14X1kwInDtX2DSZUNcxJKQ=
github.com/anacrolix/args v0.4.1-0.20211104085705-59f0fe94eb8f/go.mod h1:41JBnF8sKExNVLHPkCdL74jkZc3dSxAkGsk1TuKOUFI=
github.com/anacrolix/args v0.5.0 h1:bZCkbtn4QMB4ow2g34R/oqTFwrI6IojysKop6+ZcOSs=
github.com/anacrolix/args v0.5.0/go.mod h1:Fj/N2PehEwTBE5t/V/9xgTcxDkuYQ+5IBoFw/8gkldI=
github.com/anacrolix/chansync v0.0.0-20210524073341-a336ebc2de92/go.mod h1:DZsatdsdXxD0WiwcGl0nJVwyjCKMDv+knl1q2iBjA2k=
github.com/anacrolix/chansync v0.1.0/go.mod h1:DZsatdsdXxD0WiwcGl0nJVwyjCKMDv+knl1q2iBjA2k=
github.com/anacrolix/chansync v0.2.1-0.20210910114620-14955c95ded9/go.mod h1:DZsatdsdXxD0WiwcGl0nJVwyjCKMDv+knl1q2iBjA2k=