Skip to content

Commit 9e41a15

Browse files
committed
feat: block torrent in blacklist from addng to download client
1 parent 0d527c2 commit 9e41a15

File tree

4 files changed

+74
-23
lines changed

4 files changed

+74
-23
lines changed

engine/client.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ func (c *Engine) Init() {
5656
func (c *Engine) GetTask(id int) (*Task, bool) {
5757
return c.tasks.Load(id)
5858
}
59+
5960
func (c *Engine) reloadUsingBuildinDownloader(h *ent.History) error{
6061
cl, err := buildin.NewDownloader(c.db.GetDownloadDir())
6162
if err != nil {

engine/resources.go

Lines changed: 52 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ import (
1616
"github.com/pkg/errors"
1717
)
1818

19-
func (c *Engine) DownloadEpisodeTorrent(r1 torznab.Result, seriesId, seasonNum int, episodeNums ...int) (*string, error) {
19+
func (c *Engine) DownloadEpisodeTorrent(r1 torznab.Result, op DownloadOptions) (*string, error) {
2020

21-
series, err := c.db.GetMedia(seriesId)
21+
series, err := c.db.GetMedia(op.MediaId)
2222
if err != nil {
23-
return nil, fmt.Errorf("no tv series of id %v", seriesId)
23+
return nil, fmt.Errorf("no tv series of id %v", op.MediaId)
2424
}
2525

26-
return c.downloadTorrent(series, r1, seasonNum, episodeNums...)
26+
return c.downloadTorrent(series, r1, op)
2727
}
2828

2929
/*
@@ -83,9 +83,14 @@ lo:
8383
m.ParseExtraDescription(r.Description)
8484
if len(episodeNums) == 0 { //want season pack
8585
if m.IsSeasonPack {
86-
name, err := c.DownloadEpisodeTorrent(r, seriesId, seasonNum)
86+
name, err := c.DownloadEpisodeTorrent(r, DownloadOptions{
87+
SeasonNum: seasonNum,
88+
MediaId: seriesId,
89+
HashFilterFn: c.hashInBlacklist,
90+
})
8791
if err != nil {
88-
return nil, err
92+
log.Warnf("download season pack error, continue next item: %v", err)
93+
continue lo
8994
}
9095
torrentNames = append(torrentNames, *name)
9196
break lo
@@ -98,9 +103,15 @@ lo:
98103
}
99104
torrentEpisodes = append(torrentEpisodes, i)
100105
}
101-
name, err := c.DownloadEpisodeTorrent(r, seriesId, seasonNum, torrentEpisodes...)
106+
name, err := c.DownloadEpisodeTorrent(r, DownloadOptions{
107+
SeasonNum: seasonNum,
108+
MediaId: seriesId,
109+
EpisodeNums: torrentEpisodes,
110+
HashFilterFn: c.hashInBlacklist,
111+
})
102112
if err != nil {
103-
return nil, err
113+
log.Warnf("download episode error, continue next item: %v", err)
114+
continue lo
104115
}
105116
torrentNames = append(torrentNames, *name)
106117

@@ -116,10 +127,27 @@ lo:
116127
}
117128

118129
func (c *Engine) DownloadMovie(m *ent.Media, r1 torznab.Result) (*string, error) {
119-
return c.downloadTorrent(m, r1, 0)
130+
return c.downloadTorrent(m, r1, DownloadOptions{
131+
SeasonNum: 0,
132+
MediaId: m.ID,
133+
})
134+
}
135+
136+
func (c *Engine) hashInBlacklist(hash string) bool {
137+
blacklist, err := c.db.GetTorrentBlacklist()
138+
if err!= nil {
139+
log.Warnf("get torrent blacklist error: %v", err)
140+
return false
141+
}
142+
for _, b := range blacklist {
143+
if b.TorrentHash == hash {
144+
return true
145+
}
146+
}
147+
return false
120148
}
121149

122-
func (c *Engine) downloadTorrent(m *ent.Media, r1 torznab.Result, seasonNum int, episodeNums ...int) (*string, error) {
150+
func (c *Engine) downloadTorrent(m *ent.Media, r1 torznab.Result, op DownloadOptions) (*string, error) {
123151
trc, dlc, err := c.GetDownloadClient()
124152
if err != nil {
125153
return nil, errors.Wrap(err, "get download client")
@@ -137,21 +165,21 @@ func (c *Engine) downloadTorrent(m *ent.Media, r1 torznab.Result, seasonNum int,
137165
var name = r1.Name
138166
var targetDir = m.TargetDir
139167
if m.MediaType == media.MediaTypeTv { //tv download
140-
targetDir = fmt.Sprintf("%s/Season %02d/", m.TargetDir, seasonNum)
168+
targetDir = fmt.Sprintf("%s/Season %02d/", m.TargetDir, op.SeasonNum)
141169

142-
if len(episodeNums) > 0 {
143-
for _, epNum := range episodeNums {
144-
ep, err := c.db.GetEpisode(m.ID, seasonNum, epNum)
170+
if len(op.EpisodeNums) > 0 {
171+
for _, epNum := range op.EpisodeNums {
172+
ep, err := c.db.GetEpisode(m.ID, op.SeasonNum, epNum)
145173
if err != nil {
146-
return nil, errors.Errorf("no episode of season %d episode %d", seasonNum, epNum)
174+
return nil, errors.Errorf("no episode of season %d episode %d", op.SeasonNum, epNum)
147175

148176
}
149177
if ep.Status == episode.StatusMissing {
150178
c.db.SetEpisodeStatus(ep.ID, episode.StatusDownloading)
151179
}
152180
}
153181
buff := &bytes.Buffer{}
154-
for i, ep := range episodeNums {
182+
for i, ep := range op.EpisodeNums {
155183
if i != 0 {
156184
buff.WriteString(",")
157185

@@ -162,7 +190,7 @@ func (c *Engine) downloadTorrent(m *ent.Media, r1 torznab.Result, seasonNum int,
162190

163191
} else { //season package download
164192
name = fmt.Sprintf("全集 (%s)", name)
165-
c.db.SetSeasonAllEpisodeStatus(m.ID, seasonNum, episode.StatusDownloading)
193+
c.db.SetSeasonAllEpisodeStatus(m.ID, op.SeasonNum, episode.StatusDownloading)
166194
}
167195

168196
} else {//movie download
@@ -177,12 +205,17 @@ func (c *Engine) downloadTorrent(m *ent.Media, r1 torznab.Result, seasonNum int,
177205
if err != nil {
178206
return nil, errors.Wrap(err, "get hash")
179207
}
208+
209+
if op.HashFilterFn != nil && op.HashFilterFn(hash) {
210+
return nil, errors.Errorf("hash is filtered: %s", hash)
211+
}
212+
180213
r1.Link = link
181214

182215
history, err := c.db.SaveHistoryRecord(ent.History{
183216
MediaID: m.ID,
184-
EpisodeNums: episodeNums,
185-
SeasonNum: seasonNum,
217+
EpisodeNums: op.EpisodeNums,
218+
SeasonNum: op.SeasonNum,
186219
SourceTitle: r1.Name,
187220
TargetDir: targetDir,
188221
Status: history.StatusRunning,

engine/scheduler.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ import (
1818
"github.com/pkg/errors"
1919
)
2020

21+
type DownloadOptions struct {
22+
HashFilterFn func(hash string) bool
23+
SeasonNum int
24+
EpisodeNums []int
25+
MediaId int
26+
}
27+
2128
func (c *Engine) addSysCron() {
2229
c.registerCronJob("check_running_tasks", "@every 1m", c.checkTasks)
2330
c.registerCronJob("check_available_medias_to_download", "0 0 * * * *", func() error {
@@ -494,7 +501,7 @@ func (c *Engine) downloadMovieSingleEpisode(m *ent.Media, ep *ent.Episode) (stri
494501
r1 := res[0]
495502
log.Infof("begin download torrent resource: %v", r1.Name)
496503

497-
s, err := c.downloadTorrent(m, r1, 0)
504+
s, err := c.downloadTorrent(m, r1, DownloadOptions{MediaId: m.ID, SeasonNum: 0, HashFilterFn: c.hashInBlacklist})
498505
if err != nil {
499506
return "", err
500507
}

server/resources.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ func (s *Server) searchAndDownloadSeasonPackage(seriesId, seasonNum int) (*strin
2828

2929
r1 := res[0]
3030
log.Infof("found resource to download: %+v", r1)
31-
return s.core.DownloadEpisodeTorrent(r1, seriesId, seasonNum)
31+
return s.core.DownloadEpisodeTorrent(r1, engine.DownloadOptions{
32+
SeasonNum: seasonNum,
33+
MediaId: seriesId,
34+
})
3235

3336
}
3437

@@ -154,14 +157,21 @@ func (s *Server) DownloadTorrent(c *gin.Context) (interface{}, error) {
154157
name = fmt.Sprintf("%v S%02d", m.OriginalName, in.Season)
155158
}
156159
res := torznab.Result{Name: name, Link: in.Link, Size: in.Size}
157-
return s.core.DownloadEpisodeTorrent(res, in.MediaID, in.Season)
160+
return s.core.DownloadEpisodeTorrent(res, engine.DownloadOptions{
161+
SeasonNum: in.Season,
162+
MediaId: in.MediaID,
163+
})
158164
}
159165
name := in.Name
160166
if name == "" {
161167
name = fmt.Sprintf("%v S%02dE%02d", m.OriginalName, in.Season, in.Episode)
162168
}
163169
res := torznab.Result{Name: name, Link: in.Link, Size: in.Size, IndexerId: in.IndexerId}
164-
return s.core.DownloadEpisodeTorrent(res, in.MediaID, in.Season, in.Episode)
170+
return s.core.DownloadEpisodeTorrent(res, engine.DownloadOptions{
171+
SeasonNum: in.Season,
172+
MediaId: in.MediaID,
173+
EpisodeNums: []int{in.Episode},
174+
})
165175
} else {
166176
//movie
167177
name := in.Name

0 commit comments

Comments
 (0)