mirror of
https://github.com/simon-ding/polaris.git
synced 2026-04-20 10:47:30 +08:00
feat: show episode resource
This commit is contained in:
@@ -5,10 +5,11 @@ import (
|
||||
"polaris/ent"
|
||||
"polaris/ent/episode"
|
||||
"polaris/ent/history"
|
||||
"polaris/ent/media"
|
||||
"polaris/log"
|
||||
"polaris/pkg/torznab"
|
||||
"polaris/pkg/utils"
|
||||
"polaris/server/core"
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/pkg/errors"
|
||||
@@ -65,7 +66,7 @@ func (s *Server) searchAndDownloadSeasonPackage(seriesId, seasonNum int) (*strin
|
||||
return &r1.Name, nil
|
||||
}
|
||||
|
||||
func (s *Server) searchAndDownload(seriesId, seasonNum, episodeNum int) (*string, error) {
|
||||
func (s *Server) downloadEpisodeTorrent(r1 torznab.Result, seriesId, seasonNum, episodeNum int) (*string, error) {
|
||||
trc, err := s.getDownloadClient()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "connect transmission")
|
||||
@@ -83,13 +84,6 @@ func (s *Server) searchAndDownload(seriesId, seasonNum, episodeNum int) (*string
|
||||
if ep == nil {
|
||||
return nil, errors.Errorf("no episode of season %d episode %d", seasonNum, episodeNum)
|
||||
}
|
||||
|
||||
res, err := core.SearchEpisode(s.db, seriesId, seasonNum, episodeNum, true)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
r1 := res[0]
|
||||
log.Infof("found resource to download: %+v", r1)
|
||||
torrent, err := trc.Download(r1.Link, s.db.GetDownloadDir())
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "downloading")
|
||||
@@ -116,6 +110,17 @@ func (s *Server) searchAndDownload(seriesId, seasonNum, episodeNum int) (*string
|
||||
|
||||
log.Infof("success add %s to download task", r1.Name)
|
||||
return &r1.Name, nil
|
||||
|
||||
}
|
||||
func (s *Server) searchAndDownload(seriesId, seasonNum, episodeNum int) (*string, error) {
|
||||
|
||||
res, err := core.SearchEpisode(s.db, seriesId, seasonNum, episodeNum, true)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
r1 := res[0]
|
||||
log.Infof("found resource to download: %+v", r1)
|
||||
return s.downloadEpisodeTorrent(r1, seriesId, seasonNum, episodeNum)
|
||||
}
|
||||
|
||||
type searchAndDownloadIn struct {
|
||||
@@ -124,15 +129,34 @@ type searchAndDownloadIn struct {
|
||||
Episode int `json:"episode"`
|
||||
}
|
||||
|
||||
func (s *Server) SearchAvailableEpisodeResource(c *gin.Context) (interface{}, error) {
|
||||
func (s *Server) SearchAvailableTorrents(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, true)
|
||||
m, err := s.db.GetMedia(in.ID)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "search episode")
|
||||
return nil, errors.Wrap(err, "get media")
|
||||
}
|
||||
log.Infof("search torrents resources link: %+v", in)
|
||||
|
||||
var res []torznab.Result
|
||||
if m.MediaType == media.MediaTypeTv {
|
||||
res, err = core.SearchEpisode(s.db, in.ID, in.Season, in.Episode, false)
|
||||
if err != nil {
|
||||
if err.Error() == "no resource found" {
|
||||
return []TorznabSearchResult{}, nil
|
||||
}
|
||||
return nil, errors.Wrap(err, "search episode")
|
||||
}
|
||||
} else {
|
||||
res, err = core.SearchMovie(s.db, in.ID, false)
|
||||
if err != nil {
|
||||
if err.Error() == "no resource found" {
|
||||
return []TorznabSearchResult{}, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
var searchResults []TorznabSearchResult
|
||||
for _, r := range res {
|
||||
@@ -144,9 +168,6 @@ func (s *Server) SearchAvailableEpisodeResource(c *gin.Context) (interface{}, er
|
||||
Link: r.Link,
|
||||
})
|
||||
}
|
||||
if len(searchResults) == 0 {
|
||||
return nil, errors.New("no resource found")
|
||||
}
|
||||
return searchResults, nil
|
||||
}
|
||||
|
||||
@@ -187,59 +208,36 @@ type TorznabSearchResult struct {
|
||||
Peers int `json:"peers"`
|
||||
Source string `json:"source"`
|
||||
}
|
||||
|
||||
func (s *Server) SearchAvailableMovies(c *gin.Context) (interface{}, error) {
|
||||
ids := c.Param("id")
|
||||
id, err := strconv.Atoi(ids)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "convert")
|
||||
}
|
||||
|
||||
res, err := core.SearchMovie(s.db, id, false)
|
||||
if err != nil {
|
||||
if err.Error() == "no resource found" {
|
||||
return []TorznabSearchResult{}, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var searchResults []TorznabSearchResult
|
||||
for _, r := range res {
|
||||
searchResults = append(searchResults, TorznabSearchResult{
|
||||
Name: r.Name,
|
||||
Size: r.Size,
|
||||
Seeders: r.Seeders,
|
||||
Peers: r.Peers,
|
||||
Link: r.Link,
|
||||
Source: r.Source,
|
||||
})
|
||||
}
|
||||
if len(searchResults) == 0 {
|
||||
return []TorznabSearchResult{}, nil
|
||||
}
|
||||
return searchResults, nil
|
||||
}
|
||||
|
||||
type downloadTorrentIn struct {
|
||||
MediaID int `json:"media_id" binding:"required"`
|
||||
MediaID int `json:"id" binding:"required"`
|
||||
Season int `json:"season"`
|
||||
Episode int `json:"episode"`
|
||||
TorznabSearchResult
|
||||
}
|
||||
|
||||
func (s *Server) DownloadMovieTorrent(c *gin.Context) (interface{}, error) {
|
||||
func (s *Server) DownloadTorrent(c *gin.Context) (interface{}, error) {
|
||||
var in downloadTorrentIn
|
||||
if err := c.ShouldBindJSON(&in); err != nil {
|
||||
return nil, errors.Wrap(err, "bind json")
|
||||
}
|
||||
log.Infof("download torrent input: %+v", in)
|
||||
|
||||
m, err := s.db.GetMedia(in.MediaID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("no tv series of id %v", in.MediaID)
|
||||
}
|
||||
if m.MediaType == media.MediaTypeTv {
|
||||
name := in.Name
|
||||
if name == "" {
|
||||
name = fmt.Sprintf("%v S%02dE%02d", m.OriginalName, in.Season, in.Episode)
|
||||
}
|
||||
res := torznab.Result{Name: name, Link: in.Link, Size: in.Size}
|
||||
return s.downloadEpisodeTorrent(res, in.MediaID, in.Season, in.Episode)
|
||||
}
|
||||
trc, err := s.getDownloadClient()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "connect transmission")
|
||||
}
|
||||
media := s.db.GetMediaDetails(in.MediaID)
|
||||
if media == nil {
|
||||
return nil, fmt.Errorf("no tv series of id %v", in.MediaID)
|
||||
}
|
||||
|
||||
torrent, err := trc.Download(in.Link, s.db.GetDownloadDir())
|
||||
if err != nil {
|
||||
@@ -248,12 +246,12 @@ func (s *Server) DownloadMovieTorrent(c *gin.Context) (interface{}, error) {
|
||||
torrent.Start()
|
||||
name := in.Name
|
||||
if name == "" {
|
||||
name = media.OriginalName
|
||||
name = m.OriginalName
|
||||
}
|
||||
go func() {
|
||||
ep := media.Episodes[0]
|
||||
ep, _ := s.db.GetMovieDummyEpisode(m.ID)
|
||||
history, err := s.db.SaveHistoryRecord(ent.History{
|
||||
MediaID: media.ID,
|
||||
MediaID: m.ID,
|
||||
EpisodeID: ep.ID,
|
||||
SourceTitle: name,
|
||||
TargetDir: "./",
|
||||
|
||||
@@ -192,12 +192,18 @@ func (s *Server) checkDownloadedSeriesFiles(m *ent.Media) error {
|
||||
log.Errorf("find season episode num error: %v", err)
|
||||
continue
|
||||
}
|
||||
var dirname = filepath.Join(in.Name(), ep.Name())
|
||||
log.Infof("found match, season num %d, episode num %d", seNum, epNum)
|
||||
err = s.db.UpdateEpisodeFile(m.ID, seNum, epNum, dirname)
|
||||
ep, err := s.db.GetEpisode(m.ID, seNum, epNum)
|
||||
if err != nil {
|
||||
log.Error("update episode: %v", err)
|
||||
continue
|
||||
}
|
||||
err = s.db.SetEpisodeStatus(ep.ID, episode.StatusDownloaded)
|
||||
if err != nil {
|
||||
log.Error("update episode: %v", err)
|
||||
continue
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
return nil
|
||||
|
||||
@@ -83,11 +83,10 @@ func (s *Server) Serve() error {
|
||||
tv.GET("/search", HttpHandler(s.SearchMedia))
|
||||
tv.POST("/tv/watchlist", HttpHandler(s.AddTv2Watchlist))
|
||||
tv.GET("/tv/watchlist", HttpHandler(s.GetTvWatchlist))
|
||||
tv.POST("/tv/torrents", HttpHandler(s.SearchAvailableEpisodeResource))
|
||||
tv.POST("/torrents", HttpHandler(s.SearchAvailableTorrents))
|
||||
tv.POST("/torrents/download/", HttpHandler(s.DownloadTorrent))
|
||||
tv.POST("/movie/watchlist", HttpHandler(s.AddMovie2Watchlist))
|
||||
tv.GET("/movie/watchlist", HttpHandler(s.GetMovieWatchlist))
|
||||
tv.GET("/movie/resources/:id", HttpHandler(s.SearchAvailableMovies))
|
||||
tv.POST("/movie/resources/", HttpHandler(s.DownloadMovieTorrent))
|
||||
tv.GET("/record/:id", HttpHandler(s.GetMediaDetails))
|
||||
tv.DELETE("/record/:id", HttpHandler(s.DeleteFromWatchlist))
|
||||
tv.GET("/resolutions", HttpHandler(s.GetAvailableResolutions))
|
||||
|
||||
Reference in New Issue
Block a user