Add magnet-metainfo, tool to download magnet to torrent file
This commit is contained in:
parent
bcc814968b
commit
ecf2639856
22
client.go
22
client.go
|
@ -1083,6 +1083,21 @@ func (cl *Client) setMetaData(t *torrent, md metainfo.Info, bytes []byte) (err e
|
||||||
}
|
}
|
||||||
|
|
||||||
cl.downloadStrategy.TorrentStarted(t)
|
cl.downloadStrategy.TorrentStarted(t)
|
||||||
|
select {
|
||||||
|
case t.gotMetainfo <- &metainfo.MetaInfo{
|
||||||
|
Info: metainfo.InfoEx{
|
||||||
|
Info: md,
|
||||||
|
},
|
||||||
|
CreationDate: time.Now().Unix(),
|
||||||
|
Comment: "metadata set in client",
|
||||||
|
CreatedBy: "go.torrent",
|
||||||
|
// TODO(anacrolix): Expose trackers given when torrent added.
|
||||||
|
}:
|
||||||
|
default:
|
||||||
|
panic("shouldn't block")
|
||||||
|
}
|
||||||
|
close(t.gotMetainfo)
|
||||||
|
t.gotMetainfo = nil
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1096,7 +1111,10 @@ func newTorrent(ih InfoHash, announceList [][]string) (t *torrent, err error) {
|
||||||
|
|
||||||
closing: make(chan struct{}),
|
closing: make(chan struct{}),
|
||||||
ceasingNetworking: make(chan struct{}),
|
ceasingNetworking: make(chan struct{}),
|
||||||
|
|
||||||
|
gotMetainfo: make(chan *metainfo.MetaInfo, 1),
|
||||||
}
|
}
|
||||||
|
t.GotMetainfo = t.gotMetainfo
|
||||||
t.Trackers = make([][]tracker.Client, len(announceList))
|
t.Trackers = make([][]tracker.Client, len(announceList))
|
||||||
for tierIndex := range announceList {
|
for tierIndex := range announceList {
|
||||||
tier := t.Trackers[tierIndex]
|
tier := t.Trackers[tierIndex]
|
||||||
|
@ -1120,12 +1138,12 @@ func newTorrent(ih InfoHash, announceList [][]string) (t *torrent, err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cl *Client) AddMagnet(uri string) (err error) {
|
func (cl *Client) AddMagnet(uri string) (t *torrent, err error) {
|
||||||
m, err := ParseMagnetURI(uri)
|
m, err := ParseMagnetURI(uri)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
t, err := newTorrent(m.InfoHash, [][]string{m.Trackers})
|
t, err = newTorrent(m.InfoHash, [][]string{m.Trackers})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,48 @@
|
||||||
|
// Converts magnet URIs and info hashes into torrent metainfo files.
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"flag"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
"github.com/anacrolix/libtorgo/bencode"
|
||||||
|
|
||||||
|
"bitbucket.org/anacrolix/go.torrent"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
flag.Parse()
|
||||||
|
cl, err := torrent.NewClient(nil)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("error creating client: %s", err)
|
||||||
|
}
|
||||||
|
wg := sync.WaitGroup{}
|
||||||
|
for _, arg := range flag.Args() {
|
||||||
|
t, err := cl.AddMagnet(arg)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("error adding magnet to client: %s", err)
|
||||||
|
}
|
||||||
|
wg.Add(1)
|
||||||
|
go func() {
|
||||||
|
defer wg.Done()
|
||||||
|
mi, ok := <-t.GotMetainfo
|
||||||
|
t.Close()
|
||||||
|
if !ok {
|
||||||
|
log.Printf("no metainfo for %q", t.Name())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
f, err := os.Create(mi.Info.Name + ".torrent")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("error creating torrent metainfo file: %s", err)
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
err = bencode.NewEncoder(f).Encode(mi)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("error writing torrent metainfo file: %s", err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
wg.Wait()
|
||||||
|
}
|
|
@ -56,7 +56,7 @@ func main() {
|
||||||
log.Fatalf("error parsing magnet uri: %s", err)
|
log.Fatalf("error parsing magnet uri: %s", err)
|
||||||
}
|
}
|
||||||
ih = m.InfoHash
|
ih = m.InfoHash
|
||||||
err = client.AddMagnet(arg)
|
_, err = client.AddMagnet(arg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("error adding magnet: %s", err)
|
log.Fatalf("error adding magnet: %s", err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -130,7 +130,7 @@ func main() {
|
||||||
log.Printf("error adding torrent to client: %s", err)
|
log.Printf("error adding torrent to client: %s", err)
|
||||||
}
|
}
|
||||||
} else if ev.MagnetURI != "" {
|
} else if ev.MagnetURI != "" {
|
||||||
err := client.AddMagnet(ev.MagnetURI)
|
_, err := client.AddMagnet(ev.MagnetURI)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("error adding magnet: %s", err)
|
log.Printf("error adding magnet: %s", err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -142,7 +142,7 @@ func TestDownloadOnDemand(t *testing.T) {
|
||||||
seeder.WriteStatus(w)
|
seeder.WriteStatus(w)
|
||||||
})
|
})
|
||||||
defer seeder.Stop()
|
defer seeder.Stop()
|
||||||
err = seeder.AddMagnet(fmt.Sprintf("magnet:?xt=urn:btih:%x", layout.Metainfo.Info.Hash))
|
_, err = seeder.AddMagnet(fmt.Sprintf("magnet:?xt=urn:btih:%x", layout.Metainfo.Info.Hash))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -67,6 +67,9 @@ type torrent struct {
|
||||||
DisplayName string
|
DisplayName string
|
||||||
MetaData []byte
|
MetaData []byte
|
||||||
metadataHave []bool
|
metadataHave []bool
|
||||||
|
|
||||||
|
gotMetainfo chan *metainfo.MetaInfo
|
||||||
|
GotMetainfo <-chan *metainfo.MetaInfo
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *torrent) worstConnsHeap() (wcs *worstConns) {
|
func (t *torrent) worstConnsHeap() (wcs *worstConns) {
|
||||||
|
|
Loading…
Reference in New Issue