mirror of
https://github.com/simon-ding/polaris.git
synced 2026-06-07 10:37:39 +08:00
feat: block torrent in blacklist from addng to download client
This commit is contained in:
@@ -56,6 +56,7 @@ func (c *Engine) Init() {
|
|||||||
func (c *Engine) GetTask(id int) (*Task, bool) {
|
func (c *Engine) GetTask(id int) (*Task, bool) {
|
||||||
return c.tasks.Load(id)
|
return c.tasks.Load(id)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Engine) reloadUsingBuildinDownloader(h *ent.History) error{
|
func (c *Engine) reloadUsingBuildinDownloader(h *ent.History) error{
|
||||||
cl, err := buildin.NewDownloader(c.db.GetDownloadDir())
|
cl, err := buildin.NewDownloader(c.db.GetDownloadDir())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -16,14 +16,14 @@ import (
|
|||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (c *Engine) DownloadEpisodeTorrent(r1 torznab.Result, seriesId, seasonNum int, episodeNums ...int) (*string, error) {
|
func (c *Engine) DownloadEpisodeTorrent(r1 torznab.Result, op DownloadOptions) (*string, error) {
|
||||||
|
|
||||||
series, err := c.db.GetMedia(seriesId)
|
series, err := c.db.GetMedia(op.MediaId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("no tv series of id %v", seriesId)
|
return nil, fmt.Errorf("no tv series of id %v", op.MediaId)
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.downloadTorrent(series, r1, seasonNum, episodeNums...)
|
return c.downloadTorrent(series, r1, op)
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -83,9 +83,14 @@ lo:
|
|||||||
m.ParseExtraDescription(r.Description)
|
m.ParseExtraDescription(r.Description)
|
||||||
if len(episodeNums) == 0 { //want season pack
|
if len(episodeNums) == 0 { //want season pack
|
||||||
if m.IsSeasonPack {
|
if m.IsSeasonPack {
|
||||||
name, err := c.DownloadEpisodeTorrent(r, seriesId, seasonNum)
|
name, err := c.DownloadEpisodeTorrent(r, DownloadOptions{
|
||||||
|
SeasonNum: seasonNum,
|
||||||
|
MediaId: seriesId,
|
||||||
|
HashFilterFn: c.hashInBlacklist,
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
log.Warnf("download season pack error, continue next item: %v", err)
|
||||||
|
continue lo
|
||||||
}
|
}
|
||||||
torrentNames = append(torrentNames, *name)
|
torrentNames = append(torrentNames, *name)
|
||||||
break lo
|
break lo
|
||||||
@@ -98,9 +103,15 @@ lo:
|
|||||||
}
|
}
|
||||||
torrentEpisodes = append(torrentEpisodes, i)
|
torrentEpisodes = append(torrentEpisodes, i)
|
||||||
}
|
}
|
||||||
name, err := c.DownloadEpisodeTorrent(r, seriesId, seasonNum, torrentEpisodes...)
|
name, err := c.DownloadEpisodeTorrent(r, DownloadOptions{
|
||||||
|
SeasonNum: seasonNum,
|
||||||
|
MediaId: seriesId,
|
||||||
|
EpisodeNums: torrentEpisodes,
|
||||||
|
HashFilterFn: c.hashInBlacklist,
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
log.Warnf("download episode error, continue next item: %v", err)
|
||||||
|
continue lo
|
||||||
}
|
}
|
||||||
torrentNames = append(torrentNames, *name)
|
torrentNames = append(torrentNames, *name)
|
||||||
|
|
||||||
@@ -116,10 +127,27 @@ lo:
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Engine) DownloadMovie(m *ent.Media, r1 torznab.Result) (*string, error) {
|
func (c *Engine) DownloadMovie(m *ent.Media, r1 torznab.Result) (*string, error) {
|
||||||
return c.downloadTorrent(m, r1, 0)
|
return c.downloadTorrent(m, r1, DownloadOptions{
|
||||||
|
SeasonNum: 0,
|
||||||
|
MediaId: m.ID,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Engine) downloadTorrent(m *ent.Media, r1 torznab.Result, seasonNum int, episodeNums ...int) (*string, error) {
|
func (c *Engine) hashInBlacklist(hash string) bool {
|
||||||
|
blacklist, err := c.db.GetTorrentBlacklist()
|
||||||
|
if err!= nil {
|
||||||
|
log.Warnf("get torrent blacklist error: %v", err)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
for _, b := range blacklist {
|
||||||
|
if b.TorrentHash == hash {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Engine) downloadTorrent(m *ent.Media, r1 torznab.Result, op DownloadOptions) (*string, error) {
|
||||||
trc, dlc, err := c.GetDownloadClient()
|
trc, dlc, err := c.GetDownloadClient()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "get download client")
|
return nil, errors.Wrap(err, "get download client")
|
||||||
@@ -137,13 +165,13 @@ func (c *Engine) downloadTorrent(m *ent.Media, r1 torznab.Result, seasonNum int,
|
|||||||
var name = r1.Name
|
var name = r1.Name
|
||||||
var targetDir = m.TargetDir
|
var targetDir = m.TargetDir
|
||||||
if m.MediaType == media.MediaTypeTv { //tv download
|
if m.MediaType == media.MediaTypeTv { //tv download
|
||||||
targetDir = fmt.Sprintf("%s/Season %02d/", m.TargetDir, seasonNum)
|
targetDir = fmt.Sprintf("%s/Season %02d/", m.TargetDir, op.SeasonNum)
|
||||||
|
|
||||||
if len(episodeNums) > 0 {
|
if len(op.EpisodeNums) > 0 {
|
||||||
for _, epNum := range episodeNums {
|
for _, epNum := range op.EpisodeNums {
|
||||||
ep, err := c.db.GetEpisode(m.ID, seasonNum, epNum)
|
ep, err := c.db.GetEpisode(m.ID, op.SeasonNum, epNum)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Errorf("no episode of season %d episode %d", seasonNum, epNum)
|
return nil, errors.Errorf("no episode of season %d episode %d", op.SeasonNum, epNum)
|
||||||
|
|
||||||
}
|
}
|
||||||
if ep.Status == episode.StatusMissing {
|
if ep.Status == episode.StatusMissing {
|
||||||
@@ -151,7 +179,7 @@ func (c *Engine) downloadTorrent(m *ent.Media, r1 torznab.Result, seasonNum int,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
buff := &bytes.Buffer{}
|
buff := &bytes.Buffer{}
|
||||||
for i, ep := range episodeNums {
|
for i, ep := range op.EpisodeNums {
|
||||||
if i != 0 {
|
if i != 0 {
|
||||||
buff.WriteString(",")
|
buff.WriteString(",")
|
||||||
|
|
||||||
@@ -162,7 +190,7 @@ func (c *Engine) downloadTorrent(m *ent.Media, r1 torznab.Result, seasonNum int,
|
|||||||
|
|
||||||
} else { //season package download
|
} else { //season package download
|
||||||
name = fmt.Sprintf("全集 (%s)", name)
|
name = fmt.Sprintf("全集 (%s)", name)
|
||||||
c.db.SetSeasonAllEpisodeStatus(m.ID, seasonNum, episode.StatusDownloading)
|
c.db.SetSeasonAllEpisodeStatus(m.ID, op.SeasonNum, episode.StatusDownloading)
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {//movie download
|
} else {//movie download
|
||||||
@@ -177,12 +205,17 @@ func (c *Engine) downloadTorrent(m *ent.Media, r1 torznab.Result, seasonNum int,
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "get hash")
|
return nil, errors.Wrap(err, "get hash")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if op.HashFilterFn != nil && op.HashFilterFn(hash) {
|
||||||
|
return nil, errors.Errorf("hash is filtered: %s", hash)
|
||||||
|
}
|
||||||
|
|
||||||
r1.Link = link
|
r1.Link = link
|
||||||
|
|
||||||
history, err := c.db.SaveHistoryRecord(ent.History{
|
history, err := c.db.SaveHistoryRecord(ent.History{
|
||||||
MediaID: m.ID,
|
MediaID: m.ID,
|
||||||
EpisodeNums: episodeNums,
|
EpisodeNums: op.EpisodeNums,
|
||||||
SeasonNum: seasonNum,
|
SeasonNum: op.SeasonNum,
|
||||||
SourceTitle: r1.Name,
|
SourceTitle: r1.Name,
|
||||||
TargetDir: targetDir,
|
TargetDir: targetDir,
|
||||||
Status: history.StatusRunning,
|
Status: history.StatusRunning,
|
||||||
|
|||||||
@@ -18,6 +18,13 @@ import (
|
|||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type DownloadOptions struct {
|
||||||
|
HashFilterFn func(hash string) bool
|
||||||
|
SeasonNum int
|
||||||
|
EpisodeNums []int
|
||||||
|
MediaId int
|
||||||
|
}
|
||||||
|
|
||||||
func (c *Engine) addSysCron() {
|
func (c *Engine) addSysCron() {
|
||||||
c.registerCronJob("check_running_tasks", "@every 1m", c.checkTasks)
|
c.registerCronJob("check_running_tasks", "@every 1m", c.checkTasks)
|
||||||
c.registerCronJob("check_available_medias_to_download", "0 0 * * * *", func() error {
|
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
|
|||||||
r1 := res[0]
|
r1 := res[0]
|
||||||
log.Infof("begin download torrent resource: %v", r1.Name)
|
log.Infof("begin download torrent resource: %v", r1.Name)
|
||||||
|
|
||||||
s, err := c.downloadTorrent(m, r1, 0)
|
s, err := c.downloadTorrent(m, r1, DownloadOptions{MediaId: m.ID, SeasonNum: 0, HashFilterFn: c.hashInBlacklist})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,7 +28,10 @@ func (s *Server) searchAndDownloadSeasonPackage(seriesId, seasonNum int) (*strin
|
|||||||
|
|
||||||
r1 := res[0]
|
r1 := res[0]
|
||||||
log.Infof("found resource to download: %+v", r1)
|
log.Infof("found resource to download: %+v", r1)
|
||||||
return s.core.DownloadEpisodeTorrent(r1, seriesId, seasonNum)
|
return s.core.DownloadEpisodeTorrent(r1, engine.DownloadOptions{
|
||||||
|
SeasonNum: seasonNum,
|
||||||
|
MediaId: seriesId,
|
||||||
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -154,14 +157,21 @@ func (s *Server) DownloadTorrent(c *gin.Context) (interface{}, error) {
|
|||||||
name = fmt.Sprintf("%v S%02d", m.OriginalName, in.Season)
|
name = fmt.Sprintf("%v S%02d", m.OriginalName, in.Season)
|
||||||
}
|
}
|
||||||
res := torznab.Result{Name: name, Link: in.Link, Size: in.Size}
|
res := torznab.Result{Name: name, Link: in.Link, Size: in.Size}
|
||||||
return s.core.DownloadEpisodeTorrent(res, in.MediaID, in.Season)
|
return s.core.DownloadEpisodeTorrent(res, engine.DownloadOptions{
|
||||||
|
SeasonNum: in.Season,
|
||||||
|
MediaId: in.MediaID,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
name := in.Name
|
name := in.Name
|
||||||
if name == "" {
|
if name == "" {
|
||||||
name = fmt.Sprintf("%v S%02dE%02d", m.OriginalName, in.Season, in.Episode)
|
name = fmt.Sprintf("%v S%02dE%02d", m.OriginalName, in.Season, in.Episode)
|
||||||
}
|
}
|
||||||
res := torznab.Result{Name: name, Link: in.Link, Size: in.Size, IndexerId: in.IndexerId}
|
res := torznab.Result{Name: name, Link: in.Link, Size: in.Size, IndexerId: in.IndexerId}
|
||||||
return s.core.DownloadEpisodeTorrent(res, in.MediaID, in.Season, in.Episode)
|
return s.core.DownloadEpisodeTorrent(res, engine.DownloadOptions{
|
||||||
|
SeasonNum: in.Season,
|
||||||
|
MediaId: in.MediaID,
|
||||||
|
EpisodeNums: []int{in.Episode},
|
||||||
|
})
|
||||||
} else {
|
} else {
|
||||||
//movie
|
//movie
|
||||||
name := in.Name
|
name := in.Name
|
||||||
|
|||||||
Reference in New Issue
Block a user