aboutsummaryrefslogtreecommitdiff
path: root/meta/torrent.go
blob: 09d91edc498584a2802a1cf7cac208f334c9e412 (plain)
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
package meta

// see also: https://en.wikipedia.org/wiki/Torrent_file

import (
	"fmt"
	"io/ioutil"
	"time"

	"dim13.org/btget/bencode"
)

type Nodes map[string]int // Host:Port

type Torrent struct {
	Announce     string     `bencode:"announce"`
	AnnounceList [][]string `bencode:"announce-list,optional"` // BEP-0012
	Comment      string     `bencode:"comment,optional"`
	CreatedBy    string     `bencode:"created by,optional"`
	CreationDate time.Time  `bencode:"creation date,optional"`
	Encoding     string     `bencode:"encoding,optional"`
	HTTPSeeds    []string   `bencode:"httpseeds,optional"` // BEP-0017
	Info         Info       `bencode:"info"`
	//Nodes        Nodes      `bencode:"nodes"` // BEP-0005
	//URLList      []string   `bencode:"url-list,optional"`
}

func (t Torrent) String() string {
	s := fmt.Sprintf("announce: %s\n", t.Announce)
	s += fmt.Sprintf("comment: %s\n", t.Comment)
	s += fmt.Sprintf("date: %s\n", t.CreationDate)
	s += fmt.Sprintf("%v", t.Info)
	s += fmt.Sprintf("info hash: %x", t.Info.Hash())
	return s
}

func New(fname string) (Torrent, error) {
	var tor Torrent
	body, err := ioutil.ReadFile(fname)
	if err != nil {
		return Torrent{}, err
	}
	_, err = bencode.Unmarshal(body, &tor)
	return tor, err
}