-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
64 lines (51 loc) · 1.29 KB
/
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package main
import (
"flag"
"fmt"
"log"
"os"
"github.com/peterkwesiansah/bitty/bencodeTorrent"
"github.com/peterkwesiansah/bitty/peers"
"github.com/peterkwesiansah/bitty/torrentfile"
)
func main() {
srcPath := flag.String("src", "", "Path to the source torrent file")
dstPath := flag.String("dst", "", "Destination path for the downloaded torrent content")
flag.Parse()
if *srcPath == "" || *dstPath == "" {
fmt.Println("run instead: go run main.go -src /path/to/source -dst /path/to/destination")
flag.Usage()
os.Exit(1)
}
bct, err := bencodeTorrent.Decode(*srcPath)
if err != nil {
log.Fatal(err)
}
p2p, err := peers.Peers(bct)
if err != nil {
log.Fatal(err)
}
tf := torrentfile.Torrent{
Peers: p2p.Peers,
PeerID: [20]byte(p2p.PeerId),
InfoHash: bct.InfoHash,
Name: bct.Info.Name,
PieceLength: bct.Info.PieceLength,
Length: bct.Info.Length,
PieceHashes: bct.PieceHashes,
}
buf, err := tf.Download()
if err != nil {
log.Fatal(err)
}
// Open the file for writing (create if it doesn't exist, truncate if it does)
file, err := os.Create(*dstPath)
if err != nil {
log.Fatalf("Error creating file: %v\n", err)
}
defer file.Close()
_, err = file.Write(buf)
if err != nil {
log.Fatalf("Error writing to file: %v\n", err)
}
}