forked from jpillora/cloud-torrent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
44 lines (41 loc) · 898 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Converts magnet URIs and info hashes into torrent metainfo files.
package main
import (
"flag"
"log"
"os"
"sync"
"github.com/anacrolix/torrent"
"github.com/anacrolix/torrent/bencode"
)
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()
<-t.GotInfo()
mi := t.MetaInfo()
t.Drop()
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()
}