cmd/torrent-magnet: Converts a torrent from stdin to a magnet link

This commit is contained in:
Matt Joiner 2015-03-27 17:16:50 +11:00
parent 5049f5d375
commit 64f833f3f0
2 changed files with 35 additions and 2 deletions

View File

@ -2250,8 +2250,9 @@ func TorrentSpecFromMagnetURI(uri string) (spec *TorrentSpec, err error) {
func TorrentSpecFromMetaInfo(mi *metainfo.MetaInfo) (spec *TorrentSpec) {
spec = &TorrentSpec{
Trackers: mi.AnnounceList,
Info: &mi.Info,
Trackers: mi.AnnounceList,
Info: &mi.Info,
DisplayName: mi.Info.Name,
}
CopyExact(&spec.InfoHash, &mi.Info.Hash)
return

View File

@ -0,0 +1,32 @@
package main
import (
"fmt"
"os"
"github.com/anacrolix/libtorgo/metainfo"
"github.com/anacrolix/torrent"
)
func main() {
mi, err := metainfo.Load(os.Stdin)
if err != nil {
fmt.Fprintf(os.Stderr, "error reading metainfo from stdin: %s", err)
os.Exit(1)
}
ts := torrent.TorrentSpecFromMetaInfo(mi)
m := torrent.Magnet{
InfoHash: ts.InfoHash,
Trackers: func() (ret []string) {
for _, tier := range ts.Trackers {
for _, tr := range tier {
ret = append(ret, tr)
}
}
return
}(),
DisplayName: ts.DisplayName,
}
fmt.Fprintf(os.Stdout, "%s\n", m.String())
}