Skip to content

Commit

Permalink
feat: apply global size limiter
Browse files Browse the repository at this point in the history
  • Loading branch information
simon-ding committed Nov 19, 2024
1 parent b2a9f1f commit b4c2002
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions server/core/torrent.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ func SearchTvSeries(db1 *db.Client, param *SearchParam) ([]torznab.Result, error
if series == nil {
return nil, fmt.Errorf("no tv series of id %v", param.MediaId)
}
limiter, err := db1.GetSizeLimiter("tv")
if err != nil {
log.Warnf("get tv size limiter: %v", err)
limiter = &db.MediaSizeLimiter{}
}
log.Debugf("check tv series %s, season %d, episode %v", series.NameEn, param.SeasonNum, param.Episodes)

res := searchWithTorznab(db1, prowlarr.TV, series.NameEn, series.NameCn, series.OriginalName)
Expand Down Expand Up @@ -80,7 +85,7 @@ lo:
continue
}

if !torrentSizeOk(series, r.Size, meta.EndEpisode+1-meta.StartEpisode, param) {
if !torrentSizeOk(series, limiter, r.Size, meta.EndEpisode+1-meta.StartEpisode, param) {
continue
}

Expand Down Expand Up @@ -114,11 +119,12 @@ func imdbIDMatchExact(id1, id2 string) bool {
return id1 == id2
}

func torrentSizeOk(detail *db.MediaDetails, torrentSize int64, torrentEpisodeNum int, param *SearchParam) bool {
func torrentSizeOk(detail *db.MediaDetails, globalLimiter *db.MediaSizeLimiter, torrentSize int64, torrentEpisodeNum int, param *SearchParam) bool {
defaultMinSize := int64(80 * 1000 * 1000) //tv, 80M min
if detail.MediaType == media.MediaTypeMovie {
defaultMinSize = 200 * 1000 * 1000 // movie, 200M min
}

if detail.Limiter.SizeMin > 0 { //if size limiter set, use configured min size
defaultMinSize = detail.Limiter.SizeMin
}
Expand All @@ -133,18 +139,31 @@ func torrentSizeOk(detail *db.MediaDetails, torrentSize int64, torrentEpisodeNum
}

if param.CheckFileSize { //check file size when trigger automatic download

if detail.Limiter.SizeMin > 0 { //min size
sizeMin := detail.Limiter.SizeMin * int64(multiplier)
if torrentSize < sizeMin { //比最小要求的大小还要小, min size not qualify
return false
}
} else if globalLimiter != nil {
resLimiter := globalLimiter.GetLimiter(detail.Resolution)
sizeMin := resLimiter.MinSize * int64(multiplier)
if torrentSize < sizeMin { //比最小要求的大小还要小, min size not qualify
return false
}
}

if detail.Limiter.SizeMax > 0 { //max size
sizeMax := detail.Limiter.SizeMax * int64(multiplier)
if torrentSize > sizeMax { //larger than max size wanted, max size not qualify
return false
}
} else if globalLimiter != nil {
resLimiter := globalLimiter.GetLimiter(detail.Resolution)
sizeMax := resLimiter.MaxSIze * int64(multiplier)
if torrentSize > sizeMax { //larger than max size wanted, max size not qualify
return false
}
}
}
return torrentSize > defaultMinSize*int64(multiplier)
Expand Down Expand Up @@ -181,6 +200,12 @@ func SearchMovie(db1 *db.Client, param *SearchParam) ([]torznab.Result, error) {
return nil, errors.New("no media found of id")
}

limiter, err := db1.GetSizeLimiter("movie")
if err != nil {
log.Warnf("get tv size limiter: %v", err)
limiter = &db.MediaSizeLimiter{}
}

res := searchWithTorznab(db1, prowlarr.Movie, movieDetail.NameEn, movieDetail.NameCn, movieDetail.OriginalName)
if movieDetail.Extras.IsJav() {
res1 := searchWithTorznab(db1, prowlarr.Movie, movieDetail.Extras.JavId)
Expand Down Expand Up @@ -221,7 +246,7 @@ func SearchMovie(db1 *db.Client, param *SearchParam) ([]torznab.Result, error) {
continue
}

if !torrentSizeOk(movieDetail, r.Size, 1, param) {
if !torrentSizeOk(movieDetail, limiter, r.Size, 1, param) {
continue
}

Expand Down

0 comments on commit b4c2002

Please sign in to comment.