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

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/davecgh/go-spew/spew"
     7  
     8  	"github.com/anacrolix/torrent"
     9  	"github.com/anacrolix/torrent/tracker"
    10  	"github.com/anacrolix/torrent/tracker/udp"
    11  )
    12  
    13  type AnnounceCmd struct {
    14  	Event    udp.AnnounceEvent
    15  	Port     *uint16
    16  	Tracker  string           `arg:"positional"`
    17  	InfoHash torrent.InfoHash `arg:"positional"`
    18  }
    19  
    20  func announceErr(flags AnnounceCmd) error {
    21  	req := tracker.AnnounceRequest{
    22  		InfoHash: flags.InfoHash,
    23  		Port:     uint16(torrent.NewDefaultClientConfig().ListenPort),
    24  		NumWant:  -1,
    25  		Event:    flags.Event,
    26  		Left:     -1,
    27  	}
    28  	if flags.Port != nil {
    29  		req.Port = *flags.Port
    30  	}
    31  	response, err := tracker.Announce{
    32  		TrackerUrl: flags.Tracker,
    33  		Request:    req,
    34  	}.Do()
    35  	if err != nil {
    36  		return fmt.Errorf("doing announce: %w", err)
    37  	}
    38  	spew.Dump(response)
    39  	return nil
    40  }