]> Sergey Matveev's repositories - btrtrc.git/blob - cmd/btrtrc/add.go
Fixed lame speed calculation
[btrtrc.git] / cmd / btrtrc / add.go
1 package main
2
3 import (
4         "bufio"
5         "encoding/hex"
6         "log"
7         "os"
8         "path"
9         "strings"
10         "time"
11
12         "github.com/anacrolix/torrent"
13         "github.com/anacrolix/torrent/metainfo"
14         "github.com/anacrolix/torrent/types/infohash"
15 )
16
17 type stringAddr string
18
19 func (stringAddr) Network() string   { return "" }
20 func (me stringAddr) String() string { return string(me) }
21
22 func resolveTestPeers(addrs []string) (ret []torrent.PeerInfo) {
23         for _, ta := range addrs {
24                 ret = append(ret, torrent.PeerInfo{Addr: stringAddr(ta)})
25         }
26         return
27 }
28
29 func readLinesFromFIFO(pth string) []string {
30         fd, err := os.OpenFile(pth, os.O_RDONLY, os.FileMode(0666))
31         if err != nil {
32                 log.Println("OpenFile:", pth, err)
33                 time.Sleep(time.Second)
34                 return nil
35         }
36         var lines []string
37         scanner := bufio.NewScanner(fd)
38         for scanner.Scan() {
39                 t := scanner.Text()
40                 if len(t) > 0 {
41                         lines = append(lines, t)
42                 }
43         }
44         fd.Close()
45         return lines
46 }
47
48 func fifoAdd(c *torrent.Client) {
49         pth := path.Join(FIFOsDir, "add")
50         recreateFIFO(pth)
51         for {
52                 for _, what := range readLinesFromFIFO(pth) {
53                         cols := strings.Fields(what)
54                         what = cols[0]
55                         var t *torrent.Torrent
56                         var err error
57                         if strings.HasPrefix(what, "magnet:") {
58                                 t, err = c.AddMagnet(what)
59                                 if err != nil {
60                                         log.Println("AddMagnet:", what, err)
61                                         continue
62                                 }
63                         } else {
64                                 metaInfo, err := metainfo.LoadFromFile(what)
65                                 if err != nil {
66                                         log.Println("LoadFromFile:", what, err)
67                                         continue
68                                 }
69                                 t, err = c.AddTorrent(metaInfo)
70                                 if err != nil {
71                                         log.Println("AddTorrent:", what, err)
72                                         continue
73                                 }
74                         }
75                         if len(cols) > 1 {
76                                 t.AddPeers(resolveTestPeers(cols[1:]))
77                         }
78                         go fifoPeerList(t)
79                         go fifoFileList(t)
80                         log.Println("added:", t.InfoHash().HexString(), t.Name())
81                         go func() {
82                                 <-t.GotInfo()
83                                 if err = saveTorrent(t); err != nil {
84                                         log.Println("saveTorrent:", err)
85                                 }
86                                 txStatsLoad(t.InfoHash())
87                                 t.DownloadAll()
88                         }()
89                 }
90                 time.Sleep(time.Second)
91         }
92 }
93
94 func fifoDel(c *torrent.Client) {
95         pth := path.Join(FIFOsDir, "del")
96         recreateFIFO(pth)
97         for {
98                 for _, what := range readLinesFromFIFO(pth) {
99                         raw, err := hex.DecodeString(what)
100                         if err != nil {
101                                 log.Println(err)
102                                 continue
103                         }
104                         if len(raw) != infohash.Size {
105                                 log.Println("bad length")
106                                 continue
107                         }
108                         var i infohash.T
109                         copy(i[:], raw)
110                         t, ok := c.Torrent(i)
111                         if !ok {
112                                 log.Println("no suck torrent", what)
113                                 continue
114                         }
115                         txStatsDump(t)
116                         txStatsDel(t.InfoHash())
117                         t.Drop()
118                         for _, where := range []string{"files", "peers"} {
119                                 pth := path.Join(where, t.InfoHash().HexString())
120                                 os.Remove(pth)
121                                 fd, err := os.Open(pth)
122                                 if err == nil {
123                                         fd.Close()
124                                 }
125                         }
126                         log.Println("deleted:", what, t.Name())
127                 }
128                 time.Sleep(time.Second)
129         }
130 }