/
grabber.go
105 lines (96 loc) · 2.15 KB
/
grabber.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package torrent
import (
"errors"
alog "github.com/anacrolix/log"
"github.com/anacrolix/torrent"
"github.com/anacrolix/torrent/metainfo"
"github.com/majestrate/bitchan/gossip"
"github.com/majestrate/bitchan/storage"
"github.com/sirupsen/logrus"
"net/http"
"net/url"
"os"
"path/filepath"
)
var log = logrus.New()
var ErrNotAdded = errors.New("torrent not addded")
type Grabber struct {
Client *torrent.Client
gossiper gossip.Gossiper
store storage.Store
}
func (g *Grabber) ForEachSeed(visit func(*torrent.Torrent)) {
for _, t := range g.Client.Torrents() {
if t.Seeding() {
visit(t)
}
}
}
func (g *Grabber) Grab(metainfoURL string) error {
log.WithFields(logrus.Fields{
"url": metainfoURL,
}).Info("grabbing metainfo")
resp, err := http.Get(metainfoURL)
if err != nil {
log.WithFields(logrus.Fields{
"url": metainfoURL,
"err": err,
}).Error("grabbing metainfo failed")
return err
}
defer resp.Body.Close()
mi, err := metainfo.Load(resp.Body)
if err != nil {
log.WithFields(logrus.Fields{
"url": metainfoURL,
"err": err,
}).Error("reading metainfo failed")
return err
}
t, err := g.Client.AddTorrent(mi)
if err != nil {
log.WithFields(logrus.Fields{
"url": metainfoURL,
"err": err,
}).Error("failed to add torrent")
return err
}
log.WithFields(logrus.Fields{
"url": metainfoURL,
}).Info("download starting")
u, _ := url.Parse(metainfoURL)
f, err := os.Create(filepath.Join(g.store.GetRoot(), filepath.Base(u.Path)))
if err == nil {
defer f.Close()
mi.Write(f)
}
if t.Seeding() {
log.WithFields(logrus.Fields{
"url": metainfoURL,
"infohash": t.InfoHash().HexString(),
}).Info("seeding")
}else {
log.WithFields(logrus.Fields{
"url": metainfoURL,
"infohash": t.InfoHash().HexString(),
}).Info("downloading")
t.DownloadAll()
}
return nil
}
func (g *Grabber) Stop() {
g.Client.Close()
}
func NewGrabber(st storage.Store, g gossip.Gossiper) *Grabber {
cfg := torrent.NewDefaultClientConfig()
cfg.DataDir = st.GetRoot()
cfg.Seed = true
cfg.Debug = false
cfg.Logger = alog.Discard
t, _ := torrent.NewClient(cfg)
return &Grabber{
Client: t,
gossiper: g,
store: st,
}
}