cmd/torrent-magnet: Converts a torrent from stdin to a magnet link
This commit is contained in:
parent
5049f5d375
commit
64f833f3f0
|
@ -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
|
||||
|
|
|
@ -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())
|
||||
}
|
Loading…
Reference in New Issue