-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtorrent.go
More file actions
74 lines (61 loc) · 1.67 KB
/
torrent.go
File metadata and controls
74 lines (61 loc) · 1.67 KB
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
65
66
67
68
69
70
71
72
73
74
// torrent.go
package main
import (
"encoding/hex"
"fmt"
"net/url"
"strings"
"github.com/anacrolix/torrent/metainfo"
)
func extractNameFromMetaInfo(metaInfo *metainfo.MetaInfo) (string, error) {
info, err := metaInfo.UnmarshalInfo()
if err != nil {
return "", err
}
return info.Name, nil
}
func getInfoHash(metaInfo *metainfo.MetaInfo) string {
infoHash := metaInfo.HashInfoBytes()
return hex.EncodeToString(infoHash[:])
}
func getTotalSize(metaInfo *metainfo.MetaInfo) (int64, error) {
info, err := metaInfo.UnmarshalInfo()
if err != nil {
return 0, err
}
var totalSize int64
for _, file := range info.Files {
totalSize += file.Length
}
return totalSize, nil
}
func generateMagnetLink(metaInfo *metainfo.MetaInfo, fileName string, totalSize int64) string {
var builder strings.Builder
infoHash := metaInfo.HashInfoBytes()
builder.WriteString(fmt.Sprintf("magnet:?xt=urn:btih:%s", hex.EncodeToString(infoHash.Bytes())))
if fileName != "" {
builder.WriteString("&dn=")
builder.WriteString(url.QueryEscape(fileName))
}
builder.WriteString(fmt.Sprintf("&xl=%d", totalSize))
return builder.String()
}
func modifyMetadata(metaInfo *metainfo.MetaInfo, createdBy, comment string) {
if *verboseFlag {
if len(metaInfo.AnnounceList) > 0 {
fmt.Println("Modifying torrent metadata...")
fmt.Println("Removing the following trackers:")
for _, trackers := range metaInfo.AnnounceList {
for _, tracker := range trackers {
fmt.Println(" -", tracker)
}
}
} else {
fmt.Println("Modifying torrent metadata... (no trackers found)")
}
}
metaInfo.Announce = ""
metaInfo.AnnounceList = nil
metaInfo.CreatedBy = createdBy
metaInfo.Comment = comment
}