This commit is contained in:
Simon Ding
2024-07-10 22:07:56 +08:00
parent a81b68b236
commit 171eef57cc
7 changed files with 80 additions and 39 deletions

View File

@@ -75,34 +75,29 @@ type searchAndDownloadIn struct {
Episode int `json:"episode"`
}
func (s *Server) SearchAndDownload(c *gin.Context) (interface{}, error) {
var in searchAndDownloadIn
if err := c.ShouldBindJSON(&in); err != nil {
return nil, errors.Wrap(err, "bind json")
}
func (s *Server) searchAndDownload(seriesId, seasonNum, episodeNum int) (*string, error) {
tr := s.db.GetTransmission()
trc, err := transmission.NewClient(tr.URL, tr.User, tr.Password)
if err != nil {
return nil, errors.Wrap(err, "connect transmission")
}
log.Infof("search episode resources link: %v", in)
series := s.db.GetSeriesDetails(in.ID)
series := s.db.GetSeriesDetails(seriesId)
if series == nil {
return nil, fmt.Errorf("no tv series of id %v", in.ID)
return nil, fmt.Errorf("no tv series of id %v", seriesId)
}
var ep *ent.Episode
for _, e := range series.Episodes {
if e.SeasonNumber == in.Season && e.EpisodeNumber == in.Episode {
if e.SeasonNumber == seasonNum && e.EpisodeNumber == episodeNum {
ep = e
}
}
if ep == nil {
return nil, errors.Errorf("no episode of season %d episode %d", in.Season, in.Episode)
return nil, errors.Errorf("no episode of season %d episode %d", seasonNum, episodeNum)
}
res := s.searchTvWithTorznab(series.OriginalName, in.Season, in.Episode)
res := s.searchTvWithTorznab(series.OriginalName, seasonNum, episodeNum)
if len(res) == 0 {
return "", fmt.Errorf("no resource found")
return nil, fmt.Errorf("no resource found")
}
r1 := res[0]
log.Infof("found resource to download: %v", r1)
@@ -138,8 +133,22 @@ func (s *Server) SearchAndDownload(c *gin.Context) (interface{}, error) {
// return nil, errors.Wrap(err, "download torrent")
// }
log.Errorf("success add %s to download task", r1.Name)
return &r1.Name, nil
}
func (s *Server) SearchAndDownload(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)
name, err := s.searchAndDownload(in.ID, in.Season, in.Episode)
if err != nil {
return nil, errors.Wrap(err, "download")
}
return gin.H{
"name": r1.Name,
"name": *name,
}, nil
}

View File

@@ -10,12 +10,21 @@ import (
)
func (s *Server) scheduler() {
s.cron.AddFunc("@every 1m", s.checkTasks)
s.mustAddCron("@every 1m", s.checkTasks)
s.cron.Start()
}
func (s *Server) mustAddCron(spec string, cmd func()) {
if err := s.cron.AddFunc(spec, cmd); err != nil {
log.Errorf("add func error: %v", err)
panic(err)
}
}
func (s *Server) checkTasks() {
log.Infof("begin check tasks...")
for id, t := range s.tasks {
log.Infof("task %s percentage done: %d%%", t.Name(), t.Progress())
log.Infof("task (%s) percentage done: %d%%", t.Name(), t.Progress())
if t.Progress() == 100 {
log.Infof("task is done: %v", t.Name())
s.moveCompletedTask(id)

View File

@@ -28,8 +28,8 @@ func (s *Server) SearchTvSeries(c *gin.Context) (interface{}, error) {
}
type addWatchlistIn struct {
TmdbID int `json:"id" binding:"required"`
StorageID int `json:"folder"`
TmdbID int `json:"id" binding:"required"`
StorageID int `json:"storage_id"`
}
func (s *Server) AddWatchlist(c *gin.Context) (interface{}, error) {
@@ -53,8 +53,6 @@ func (s *Server) AddWatchlist(c *gin.Context) (interface{}, error) {
}
}
var epIds []int
for _, season := range detail.Seasons {
seasonId := season.SeasonNumber
@@ -92,7 +90,6 @@ func (s *Server) GetWatchlist(c *gin.Context) (interface{}, error) {
return list, nil
}
func (s *Server) GetTvDetails(c *gin.Context) (interface{}, error) {
ids := c.Param("id")
id, err := strconv.Atoi(ids)
@@ -101,4 +98,4 @@ func (s *Server) GetTvDetails(c *gin.Context) (interface{}, error) {
}
detail := s.db.GetSeriesDetails(id)
return detail, nil
}
}