forked from 1290799223/trafficConsume
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
68 lines (55 loc) · 1.34 KB
/
main.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
package main
import (
"sync/atomic"
aslog "github.com/anacrolix/log"
"github.com/anacrolix/torrent"
"github.com/robfig/cron/v3"
log "github.com/sirupsen/logrus"
)
func main() {
getVersion()
cfg := torrent.NewDefaultClientConfig()
cfg.DefaultStorage = new(storage.Client)
cfg.ExtendedHandshakeClientVersion = string([]byte{0xde, 0xad, 0xbe, 0xef})
cfg.Logger = aslog.Default.WithFilterLevel(aslog.Error)
cli, err := client.New(cfg)
if err != nil {
log.Fatal(err)
}
defer cli.Close()
// Monitor stats
go cli.Monitor()
// add fake file
cli.AddFakeTorrent()
// // default torrents
cli.AddTorrents(metahash.GetDefaultMetaHashes())
// nyaa torrents
nyaaHs := metahash.GetNyaaMetaHashes()
cli.AddTorrents(nyaaHs)
// cron jobs
var isRunning atomic.Bool
job := cron.New()
_, err = job.AddFunc("@every 1h", func() {
if isRunning.Load() {
return
}
isRunning.Store(true)
defer isRunning.Store(false)
newNyaaHs := metahash.GetNyaaMetaHashes()
cli.AddTorrents(newNyaaHs)
dropHs := metahash.NeedDropTorrents(nyaaHs, newNyaaHs)
for _, h := range dropHs {
if t, ok := cli.Torrent(h); ok {
t.Drop()
}
}
log.Infof("Add %d torrents, Drop %d torrents", len(newNyaaHs)+len(dropHs)-len(nyaaHs), len(dropHs))
nyaaHs = newNyaaHs
})
if err != nil {
log.Fatal(err)
}
job.Start()
// block wait completed
cli.WaitAll()
}