]> Sergey Matveev's repositories - btrtrc.git/blob - spec.go
Add webseeds from magnet links
[btrtrc.git] / spec.go
1 package torrent
2
3 import (
4         "github.com/anacrolix/torrent/metainfo"
5         "github.com/anacrolix/torrent/storage"
6 )
7
8 // Specifies a new torrent for adding to a client. There are helpers for magnet URIs and torrent
9 // metainfo files.
10 type TorrentSpec struct {
11         // The tiered tracker URIs.
12         Trackers  [][]string
13         InfoHash  metainfo.Hash
14         InfoBytes []byte
15         // The name to use if the Name field from the Info isn't available.
16         DisplayName string
17         Webseeds    []string
18         DhtNodes    []string
19
20         // The chunk size to use for outbound requests. Defaults to 16KiB if not set.
21         ChunkSize int
22         Storage   storage.ClientImpl
23 }
24
25 func TorrentSpecFromMagnetURI(uri string) (spec *TorrentSpec, err error) {
26         m, err := metainfo.ParseMagnetURI(uri)
27         if err != nil {
28                 return
29         }
30         spec = &TorrentSpec{
31                 Trackers:    [][]string{m.Trackers},
32                 DisplayName: m.DisplayName,
33                 InfoHash:    m.InfoHash,
34                 Webseeds:    m.Params["ws"],
35                 // TODO: What's the parameter for DHT nodes or bootstrap peers in a magnet link?
36         }
37         return
38 }
39
40 func TorrentSpecFromMetaInfo(mi *metainfo.MetaInfo) *TorrentSpec {
41         info, err := mi.UnmarshalInfo()
42         if err != nil {
43                 panic(err)
44         }
45         return &TorrentSpec{
46                 Trackers:    mi.UpvertedAnnounceList(),
47                 InfoHash:    mi.HashInfoBytes(),
48                 InfoBytes:   mi.InfoBytes,
49                 DisplayName: info.Name,
50                 Webseeds:    mi.UrlList,
51                 DhtNodes: func() (ret []string) {
52                         ret = make([]string, len(mi.Nodes))
53                         for _, node := range mi.Nodes {
54                                 ret = append(ret, string(node))
55                         }
56                         return
57                 }(),
58         }
59 }