]> Sergey Matveev's repositories - btrtrc.git/blob - cmd/tracker-announce/main.go
adding http user-agent setters and usage
[btrtrc.git] / cmd / tracker-announce / main.go
1 package main
2
3 import (
4         "flag"
5         "log"
6         "math"
7         "strings"
8
9         "github.com/davecgh/go-spew/spew"
10
11         "github.com/anacrolix/torrent"
12         "github.com/anacrolix/torrent/metainfo"
13         "github.com/anacrolix/torrent/tracker"
14 )
15
16 func argSpec(arg string) (ts *torrent.TorrentSpec, err error) {
17         if strings.HasPrefix(arg, "magnet:") {
18                 return torrent.TorrentSpecFromMagnetURI(arg)
19         }
20         mi, err := metainfo.LoadFromFile(arg)
21         if err != nil {
22                 return
23         }
24         ts = torrent.TorrentSpecFromMetaInfo(mi)
25         return
26 }
27
28 func main() {
29         flag.Parse()
30         ar := tracker.AnnounceRequest{
31                 NumWant: -1,
32                 Left:    math.MaxUint64,
33         }
34         for _, arg := range flag.Args() {
35                 ts, err := argSpec(arg)
36                 if err != nil {
37                         log.Fatal(err)
38                 }
39                 ar.InfoHash = ts.InfoHash
40                 for _, tier := range ts.Trackers {
41                         for _, tURI := range tier {
42                                 resp, err := tracker.Announce(torrent.DefaultHTTPClient, torrent.DefaultHTTPUserAgent, tURI, &ar)
43                                 if err != nil {
44                                         log.Print(err)
45                                         continue
46                                 }
47                                 log.Printf("%q: %s", tURI, spew.Sdump(resp))
48                         }
49                 }
50         }
51 }