feat: download movie every 1h

This commit is contained in:
Simon Ding
2024-07-20 17:26:09 +08:00
parent 9adc1f418b
commit 2fe0a4187a
15 changed files with 240 additions and 128 deletions

View File

@@ -15,7 +15,7 @@ import (
"github.com/pkg/errors"
)
func SearchSeasonPackage(db1 *db.Client, seriesId, seasonNum int) ([]torznab.Result, error) {
func SearchSeasonPackage(db1 *db.Client, seriesId, seasonNum int, checkResolution bool) ([]torznab.Result, error) {
series := db1.GetMediaDetails(seriesId)
if series == nil {
return nil, fmt.Errorf("no tv series of id %v", seriesId)
@@ -31,8 +31,12 @@ func SearchSeasonPackage(db1 *db.Client, seriesId, seasonNum int) ([]torznab.Res
if !isNameAcceptable(r.Name, series.Media, seasonNum, -1) {
continue
}
if checkResolution && IsWantedResolution(r.Name, series.Resolution) {
continue
}
filtered = append(filtered, r)
}
if len(filtered) == 0 {
@@ -41,7 +45,7 @@ func SearchSeasonPackage(db1 *db.Client, seriesId, seasonNum int) ([]torznab.Res
return filtered, nil
}
func SearchEpisode(db1 *db.Client, seriesId, seasonNum, episodeNum int) ([]torznab.Result, error) {
func SearchEpisode(db1 *db.Client, seriesId, seasonNum, episodeNum int, checkResolution bool) ([]torznab.Result, error) {
series := db1.GetMediaDetails(seriesId)
if series == nil {
return nil, fmt.Errorf("no tv series of id %v", seriesId)
@@ -58,6 +62,10 @@ func SearchEpisode(db1 *db.Client, seriesId, seasonNum, episodeNum int) ([]torzn
if !isNameAcceptable(r.Name, series.Media, seasonNum, episodeNum) {
continue
}
if checkResolution && IsWantedResolution(r.Name, series.Resolution) {
continue
}
filtered = append(filtered, r)
}
@@ -65,7 +73,7 @@ func SearchEpisode(db1 *db.Client, seriesId, seasonNum, episodeNum int) ([]torzn
}
func SearchMovie(db1 *db.Client, movieId int) ([]torznab.Result, error) {
func SearchMovie(db1 *db.Client, movieId int, checkResolution bool) ([]torznab.Result, error) {
movieDetail := db1.GetMediaDetails(movieId)
if movieDetail == nil {
return nil, errors.New("no media found of id")
@@ -84,6 +92,10 @@ func SearchMovie(db1 *db.Client, movieId int) ([]torznab.Result, error) {
if !isNameAcceptable(r.Name, movieDetail.Media, -1, -1) {
continue
}
if checkResolution && IsWantedResolution(r.Name, movieDetail.Resolution) {
continue
}
filtered = append(filtered, r)
}
@@ -117,10 +129,6 @@ func searchWithTorznab(db *db.Client, q string) []torznab.Result {
}
func isNameAcceptable(torrentName string, m *ent.Media, seasonNum, episodeNum int) bool {
if !utils.ContainsIgnoreCase(torrentName,m.NameCn) && !utils.ContainsIgnoreCase(torrentName,m.NameEn) &&
!utils.ContainsIgnoreCase(torrentName,m.OriginalName) {
return false
}
if !utils.IsNameAcceptable(torrentName, m.NameCn) && !utils.IsNameAcceptable(torrentName, m.NameEn) && !utils.IsNameAcceptable(torrentName, m.OriginalName){
return false //name not match
}
@@ -156,4 +164,16 @@ func isNameAcceptable(torrentName string, m *ent.Media, seasonNum, episodeNum in
}
}
return true
}
func IsWantedResolution(name string, res media.Resolution) bool {
switch res {
case media.Resolution720p:
return utils.ContainsIgnoreCase(name, "720p")
case media.Resolution1080p:
return utils.ContainsIgnoreCase(name, "1080p")
case media.Resolution4k:
return utils.ContainsIgnoreCase(name, "4k") || utils.ContainsIgnoreCase(name, "2160p")
}
return false
}

View File

@@ -74,7 +74,7 @@ func (s *Server) searchAndDownloadSeasonPackage(seriesId, seasonNum int) (*strin
return nil, errors.Wrap(err, "connect transmission")
}
res, err := core.SearchSeasonPackage(s.db, seriesId, seasonNum)
res, err := core.SearchSeasonPackage(s.db, seriesId, seasonNum, true)
if err != nil {
return nil, err
}
@@ -89,7 +89,6 @@ func (s *Server) searchAndDownloadSeasonPackage(seriesId, seasonNum int) (*strin
return nil, errors.New("no enough space")
}
torrent, err := trc.Download(r1.Magnet, s.db.GetDownloadDir())
if err != nil {
return nil, errors.Wrap(err, "downloading")
@@ -139,7 +138,7 @@ func (s *Server) searchAndDownload(seriesId, seasonNum, episodeNum int) (*string
return nil, errors.Errorf("no episode of season %d episode %d", seasonNum, episodeNum)
}
res, err := core.SearchEpisode(s.db, seriesId, seasonNum, episodeNum)
res, err := core.SearchEpisode(s.db, seriesId, seasonNum, episodeNum, true)
if err != nil {
return nil, err
}
@@ -179,13 +178,13 @@ type searchAndDownloadIn struct {
Episode int `json:"episode"`
}
func (s *Server) SearchAvailableEpisodeResource(c *gin.Context) (interface{}, error) {
func (s *Server) SearchAvailableEpisodeResource(c *gin.Context) (interface{}, error) {
var in searchAndDownloadIn
if err := c.ShouldBindJSON(&in); err != nil {
return nil, errors.Wrap(err, "bind json")
}
log.Infof("search episode resources link: %v", in)
res, err := core.SearchEpisode(s.db, in.ID, in.Season, in.Episode)
res, err := core.SearchEpisode(s.db, in.ID, in.Season, in.Episode, true)
if err != nil {
return nil, errors.Wrap(err, "search episode")
}
@@ -254,7 +253,7 @@ func (s *Server) SearchAvailableMovies(c *gin.Context) (interface{}, error) {
return nil, errors.New("no media found of id " + ids)
}
res, err := core.SearchMovie(s.db, id)
res, err := core.SearchMovie(s.db, id, false)
if err != nil {
return nil, err
}
@@ -277,7 +276,7 @@ func (s *Server) SearchAvailableMovies(c *gin.Context) (interface{}, error) {
type downloadTorrentIn struct {
MediaID int `json:"media_id" binding:"required"`
Link string `json:"link" binding:"required"`
TorznabSearchResult
}
func (s *Server) DownloadMovieTorrent(c *gin.Context) (interface{}, error) {
@@ -303,26 +302,23 @@ func (s *Server) DownloadMovieTorrent(c *gin.Context) (interface{}, error) {
torrent.Start()
go func() {
for {
if !torrent.Exists() {
continue
}
history, err := s.db.SaveHistoryRecord(ent.History{
MediaID: media.ID,
SourceTitle: torrent.Name(),
TargetDir: "./",
Status: history.StatusRunning,
Size: torrent.Size(),
Saved: torrent.Save(),
})
if err != nil {
log.Errorf("save history error: %v", err)
}
s.tasks[history.ID] = &Task{Torrent: torrent}
break
ep := media.Episodes[0]
history, err := s.db.SaveHistoryRecord(ent.History{
MediaID: media.ID,
EpisodeID: ep.ID,
SourceTitle: media.NameCn,
TargetDir: "./",
Status: history.StatusRunning,
Size: in.Size,
Saved: torrent.Save(),
})
if err != nil {
log.Errorf("save history error: %v", err)
}
s.tasks[history.ID] = &Task{Torrent: torrent}
s.db.SetEpisodeStatus(ep.ID, episode.StatusDownloading)
}()
log.Infof("success add %s to download task", media.NameEn)

View File

@@ -11,6 +11,7 @@ import (
"polaris/pkg"
"polaris/pkg/storage"
"polaris/pkg/utils"
"polaris/server/core"
"github.com/pkg/errors"
)
@@ -19,6 +20,7 @@ func (s *Server) scheduler() {
s.mustAddCron("@every 1m", s.checkTasks)
//s.mustAddCron("@every 1h", s.checkAllFiles)
s.mustAddCron("@every 1h", s.downloadTvSeries)
s.mustAddCron("@every 1h", s.downloadMovie)
s.cron.Start()
}
@@ -241,3 +243,58 @@ func (s *Server) downloadTvSeries() {
}
}
func (s *Server) downloadMovie() {
log.Infof("begin check all movie resources")
allSeries := s.db.GetMediaWatchlist(media.MediaTypeMovie)
for _, series := range allSeries {
detail := s.db.GetMediaDetails(series.ID)
if len(detail.Episodes) == 0 {
log.Errorf("no related dummy episode: %v", detail.NameEn)
continue
}
if err := s.downloadMovieSingleEpisode(detail.Episodes[0]); err != nil {
log.Errorf("download movie error: %v", err)
}
}
}
func (s *Server) downloadMovieSingleEpisode(ep *ent.Episode) error {
trc, err := s.getDownloadClient()
if err != nil {
return errors.Wrap(err, "connect transmission")
}
res, err := core.SearchMovie(s.db, ep.MediaID, true)
if err != nil {
return errors.Wrap(err, "search movie")
}
r1 := res[0]
log.Infof("begin download torrent resource: %v", r1.Name)
torrent, err := trc.Download(r1.Magnet, s.db.GetDownloadDir())
if err != nil {
return errors.Wrap(err, "downloading")
}
torrent.Start()
history, err := s.db.SaveHistoryRecord(ent.History{
MediaID: ep.MediaID,
EpisodeID: ep.ID,
SourceTitle: r1.Name,
TargetDir: "./",
Status: history.StatusRunning,
Size: r1.Size,
Saved: torrent.Save(),
})
if err != nil {
log.Errorf("save history error: %v", err)
}
s.tasks[history.ID] = &Task{Torrent: torrent}
s.db.SetEpisodeStatus(ep.ID, episode.StatusDownloading)
return nil
}

View File

@@ -116,7 +116,7 @@ func (s *Server) AddTv2Watchlist(c *gin.Context) (interface{}, error) {
OriginalName: detail.OriginalName,
Overview: detail.Overview,
AirDate: detail.FirstAirDate,
Resolution: string(in.Resolution),
Resolution: media.Resolution(in.Resolution),
StorageID: in.StorageID,
TargetDir: in.Folder,
}, epIds)
@@ -181,7 +181,7 @@ func (s *Server) AddMovie2Watchlist(c *gin.Context) (interface{}, error) {
OriginalName: detail.OriginalTitle,
Overview: detail.Overview,
AirDate: detail.ReleaseDate,
Resolution: string(in.Resolution),
Resolution: media.Resolution(in.Resolution),
StorageID: in.StorageID,
TargetDir: "./",
}, []int{epid})