1 // Downloads torrents from the command-line.
14 "github.com/anacrolix/dht"
15 "github.com/anacrolix/envpprof"
16 "github.com/anacrolix/tagflag"
17 "github.com/dustin/go-humanize"
18 "github.com/gosuri/uiprogress"
19 "golang.org/x/time/rate"
21 "github.com/anacrolix/torrent"
22 "github.com/anacrolix/torrent/metainfo"
23 "github.com/anacrolix/torrent/storage"
26 func torrentBar(t *torrent.Torrent) {
27 bar := uiprogress.AddBar(1)
29 bar.AppendFunc(func(*uiprogress.Bar) (ret string) {
37 } else if t.BytesCompleted() == t.Info().TotalLength() {
40 return fmt.Sprintf("downloading (%s/%s)", humanize.Bytes(uint64(t.BytesCompleted())), humanize.Bytes(uint64(t.Info().TotalLength())))
43 bar.PrependFunc(func(*uiprogress.Bar) string {
48 tl := int(t.Info().TotalLength())
55 bc := t.BytesCompleted()
57 time.Sleep(time.Second)
62 func addTorrents(client *torrent.Client) {
63 for _, arg := range flags.Torrent {
64 t := func() *torrent.Torrent {
65 if strings.HasPrefix(arg, "magnet:") {
66 t, err := client.AddMagnet(arg)
68 log.Fatalf("error adding magnet: %s", err)
71 } else if strings.HasPrefix(arg, "http://") || strings.HasPrefix(arg, "https://") {
72 response, err := http.Get(arg)
74 log.Fatalf("Error downloading torrent file: %s", err)
77 metaInfo, err := metainfo.Load(response.Body)
78 defer response.Body.Close()
80 fmt.Fprintf(os.Stderr, "error loading torrent file %q: %s\n", arg, err)
83 t, err := client.AddTorrent(metaInfo)
88 } else if strings.HasPrefix(arg, "infohash:") {
89 t, _ := client.AddTorrentInfoHash(metainfo.NewHashFromHex(strings.TrimPrefix(arg, "infohash:")))
92 metaInfo, err := metainfo.LoadFromFile(arg)
94 fmt.Fprintf(os.Stderr, "error loading torrent file %q: %s\n", arg, err)
97 t, err := client.AddTorrent(metaInfo)
105 t.AddPeers(func() (ret []torrent.Peer) {
106 for _, ta := range flags.TestPeer {
107 ret = append(ret, torrent.Peer{
122 Mmap bool `help:"memory-map torrent data"`
123 TestPeer []*net.TCPAddr `help:"addresses of some starting peers"`
124 Seed bool `help:"seed after download is complete"`
125 Addr *net.TCPAddr `help:"network listen addr"`
126 UploadRate tagflag.Bytes `help:"max piece bytes to send per second"`
127 DownloadRate tagflag.Bytes `help:"max bytes per second down from peers"`
130 Torrent []string `arity:"+" help:"torrent file path or magnet uri"`
137 log.SetFlags(log.LstdFlags | log.Lshortfile)
138 tagflag.Parse(&flags)
139 clientConfig := torrent.Config{
140 DHTConfig: dht.ServerConfig{
141 StartingNodes: dht.GlobalBootstrapAddrs,
145 clientConfig.DefaultStorage = storage.NewMMap("")
147 if flags.Addr != nil {
148 clientConfig.ListenAddr = flags.Addr.String()
151 clientConfig.Seed = true
153 if flags.UploadRate != -1 {
154 clientConfig.UploadRateLimiter = rate.NewLimiter(rate.Limit(flags.UploadRate), 256<<10)
156 if flags.DownloadRate != -1 {
157 clientConfig.DownloadRateLimiter = rate.NewLimiter(rate.Limit(flags.DownloadRate), 1<<20)
160 client, err := torrent.NewClient(&clientConfig)
162 log.Fatalf("error creating client: %s", err)
165 // Write status on the root path on the default HTTP muxer. This will be
166 // bound to localhost somewhere if GOPPROF is set, thanks to the envpprof
168 http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
169 client.WriteStatus(w)
173 if client.WaitAll() {
174 log.Print("downloaded ALL the torrents")
176 log.Fatal("y u no complete torrents?!")
181 expvar.Do(func(kv expvar.KeyValue) {
182 fmt.Printf("%s: %s\n", kv.Key, kv.Value)