FedP2P/cmd/torrent/main.go

146 lines
3.1 KiB
Go
Raw Normal View History

2015-04-06 21:04:18 +08:00
// Downloads torrents from the command-line.
2013-09-26 17:49:15 +08:00
package main
import (
"fmt"
2013-09-26 17:49:15 +08:00
"log"
"net"
"net/http"
"os"
"strings"
2015-03-25 14:32:42 +08:00
"time"
_ "github.com/anacrolix/envpprof"
2015-11-05 20:21:39 +08:00
"github.com/anacrolix/tagflag"
"github.com/dustin/go-humanize"
"github.com/gosuri/uiprogress"
"github.com/anacrolix/torrent"
2015-04-28 13:24:17 +08:00
"github.com/anacrolix/torrent/metainfo"
2016-03-28 18:57:04 +08:00
"github.com/anacrolix/torrent/storage"
2013-09-26 17:49:15 +08:00
)
func resolvedPeerAddrs(ss []string) (ret []torrent.Peer, err error) {
for _, s := range ss {
var addr *net.TCPAddr
addr, err = net.ResolveTCPAddr("tcp", s)
if err != nil {
return
}
ret = append(ret, torrent.Peer{
IP: addr.IP,
Port: addr.Port,
})
}
return
2015-03-20 20:18:32 +08:00
}
2016-04-03 16:40:43 +08:00
func torrentBar(t *torrent.Torrent) {
bar := uiprogress.AddBar(1)
bar.AppendCompleted()
bar.AppendFunc(func(*uiprogress.Bar) (ret string) {
select {
case <-t.GotInfo():
default:
return "getting info"
2015-03-25 14:32:42 +08:00
}
if t.Seeding() {
return "seeding"
} else if t.BytesCompleted() == t.Info().TotalLength() {
return "completed"
} else {
return fmt.Sprintf("downloading (%s/%s)", humanize.Bytes(uint64(t.BytesCompleted())), humanize.Bytes(uint64(t.Info().TotalLength())))
2015-03-25 14:32:42 +08:00
}
})
bar.PrependFunc(func(*uiprogress.Bar) string {
return t.Name()
})
go func() {
<-t.GotInfo()
bar.Total = int(t.Info().TotalLength())
for {
bc := t.BytesCompleted()
bar.Set(int(bc))
time.Sleep(time.Second)
2015-10-23 09:42:19 +08:00
}
}()
}
func addTorrents(client *torrent.Client) {
for _, arg := range opts.Torrent {
2016-04-03 16:40:43 +08:00
t := func() *torrent.Torrent {
if strings.HasPrefix(arg, "magnet:") {
t, err := client.AddMagnet(arg)
if err != nil {
log.Fatalf("error adding magnet: %s", err)
}
return t
} else {
metaInfo, err := metainfo.LoadFromFile(arg)
if err != nil {
2015-11-05 20:21:39 +08:00
fmt.Fprintf(os.Stderr, "error loading torrent file %q: %s\n", arg, err)
os.Exit(1)
}
t, err := client.AddTorrent(metaInfo)
if err != nil {
log.Fatal(err)
}
return t
}
}()
torrentBar(t)
2015-11-05 20:21:39 +08:00
err := t.AddPeers(func() (ret []torrent.Peer) {
for _, ta := range opts.TestPeer {
ret = append(ret, torrent.Peer{
IP: ta.IP,
Port: ta.Port,
})
}
return
}())
if err != nil {
log.Fatal(err)
}
go func() {
<-t.GotInfo()
t.DownloadAll()
}()
2013-09-26 17:49:15 +08:00
}
}
var opts struct {
Mmap bool `help:"memory-map torrent data"`
TestPeer []*net.TCPAddr `short:"p" help:"addresses of some starting peers"`
Seed bool `help:"seed after download is complete"`
tagflag.StartPos
Torrent []string `type:"pos" arity:"+" help:"torrent file path or magnet uri"`
}
func main() {
log.SetFlags(log.LstdFlags | log.Lshortfile)
tagflag.Parse(&opts)
var clientConfig torrent.Config
if opts.Mmap {
2016-03-28 18:57:04 +08:00
clientConfig.DefaultStorage = storage.NewMMap("")
2015-03-25 14:32:42 +08:00
}
client, err := torrent.NewClient(&clientConfig)
if err != nil {
log.Fatalf("error creating client: %s", err)
}
defer client.Close()
http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
client.WriteStatus(w)
})
uiprogress.Start()
addTorrents(client)
if client.WaitAll() {
log.Print("downloaded ALL the torrents")
} else {
log.Fatal("y u no complete torrents?!")
}
2015-11-05 20:21:39 +08:00
if opts.Seed {
select {}
}
2013-09-26 17:49:15 +08:00
}