github.com/anacrolix/torrent@v1.61.0/cmd/torrent/scrape.go (about)

     1  package main
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/davecgh/go-spew/spew"
     8  
     9  	"github.com/anacrolix/torrent"
    10  	"github.com/anacrolix/torrent/tracker"
    11  )
    12  
    13  type scrapeCfg struct {
    14  	Tracker    string             `arg:"positional"`
    15  	InfoHashes []torrent.InfoHash `arity:"+" arg:"positional"`
    16  }
    17  
    18  func scrape(flags scrapeCfg) error {
    19  	cc, err := tracker.NewClient(flags.Tracker, tracker.NewClientOpts{})
    20  	if err != nil {
    21  		err = fmt.Errorf("creating new tracker client: %w", err)
    22  		return err
    23  	}
    24  	defer cc.Close()
    25  	scrapeOut, err := cc.Scrape(context.TODO(), flags.InfoHashes)
    26  	if err != nil {
    27  		return fmt.Errorf("scraping: %w", err)
    28  	}
    29  	spew.Dump(scrapeOut)
    30  	return nil
    31  }