Files
polaris/server/watchlist.go
2024-09-02 23:47:19 +08:00

171 lines
4.3 KiB
Go

package server
import (
"os"
"path/filepath"
"polaris/db"
"polaris/ent"
"polaris/ent/episode"
"polaris/ent/media"
"polaris/log"
"polaris/server/core"
"strconv"
"github.com/gin-gonic/gin"
"github.com/pkg/errors"
)
type searchTvParam struct {
Query string `form:"query"`
Page int `form:"page"`
}
func (s *Server) SearchTvSeries(c *gin.Context) (interface{}, error) {
var q searchTvParam
if err := c.ShouldBindQuery(&q); err != nil {
return nil, errors.Wrap(err, "bind query")
}
log.Infof("search tv series with keyword: %v", q.Query)
r, err := s.MustTMDB().SearchTvShow(q.Query, "")
if err != nil {
return nil, errors.Wrap(err, "search tv")
}
return r, nil
}
func (s *Server) SearchMedia(c *gin.Context) (interface{}, error) {
var q searchTvParam
if err := c.ShouldBindQuery(&q); err != nil {
return nil, errors.Wrap(err, "bind query")
}
log.Infof("search media with keyword: %v", q.Query)
tmdb, err := s.TMDB()
if err != nil {
return nil, err
}
r, err := tmdb.SearchMedia(q.Query, s.language, q.Page)
if err != nil {
return nil, errors.Wrap(err, "search tv")
}
for i, res := range r.Results {
if s.db.TmdbIdInWatchlist(int(res.ID)) {
r.Results[i].InWatchlist = true
}
}
return r, nil
}
type addWatchlistIn struct {
TmdbID int `json:"tmdb_id" binding:"required"`
StorageID int `json:"storage_id" `
Resolution string `json:"resolution" binding:"required"`
Folder string `json:"folder" binding:"required"`
DownloadHistoryEpisodes bool `json:"download_history_episodes"` //for tv
SizeMin int `json:"size_min"`
SizeMax int `json:"size_max"`
}
func (s *Server) AddTv2Watchlist(c *gin.Context) (interface{}, error) {
var in core.AddWatchlistIn
if err := c.ShouldBindJSON(&in); err != nil {
return nil, errors.Wrap(err, "bind query")
}
return s.core.AddTv2Watchlist(in)
}
func (s *Server) AddMovie2Watchlist(c *gin.Context) (interface{}, error) {
var in core.AddWatchlistIn
if err := c.ShouldBindJSON(&in); err != nil {
return nil, errors.Wrap(err, "bind query")
}
return s.core.AddMovie2Watchlist(in)
}
type MediaWithStatus struct {
*ent.Media
MonitoredNum int `json:"monitored_num"`
DownloadedNum int `json:"downloaded_num"`
}
//missing: episode aired missing
//downloaded: all monitored episode downloaded
//monitoring: episode aired downloaded, but still has not aired episode
//for movie, only monitoring/downloaded
func (s *Server) GetTvWatchlist(c *gin.Context) (interface{}, error) {
list := s.db.GetMediaWatchlist(media.MediaTypeTv)
res := make([]MediaWithStatus, len(list))
for i, item := range list {
var ms = MediaWithStatus{
Media: item,
MonitoredNum: 0,
DownloadedNum: 0,
}
details := s.db.GetMediaDetails(item.ID)
for _, ep := range details.Episodes {
if ep.Monitored {
ms.MonitoredNum++
if ep.Status == episode.StatusDownloaded {
ms.DownloadedNum++
}
}
}
res[i] = ms
}
return res, nil
}
func (s *Server) GetMovieWatchlist(c *gin.Context) (interface{}, error) {
list := s.db.GetMediaWatchlist(media.MediaTypeMovie)
res := make([]MediaWithStatus, len(list))
for i, item := range list {
var ms = MediaWithStatus{
Media: item,
MonitoredNum: 1,
DownloadedNum: 0,
}
dummyEp, err := s.db.GetMovieDummyEpisode(item.ID)
if err != nil {
log.Errorf("get dummy episode: %v", err)
} else {
if dummyEp.Status == episode.StatusDownloaded {
ms.DownloadedNum++
}
}
res[i] = ms
}
return res, nil
}
type MediaDetails struct {
*db.MediaDetails
Storage *ent.Storage `json:"storage"`
}
func (s *Server) GetMediaDetails(c *gin.Context) (interface{}, error) {
ids := c.Param("id")
id, err := strconv.Atoi(ids)
if err != nil {
return nil, errors.Wrap(err, "convert")
}
detail := s.db.GetMediaDetails(id)
st := s.db.GetStorage(detail.StorageID)
return MediaDetails{MediaDetails: detail, Storage: &st.Storage}, nil
}
func (s *Server) DeleteFromWatchlist(c *gin.Context) (interface{}, error) {
ids := c.Param("id")
id, err := strconv.Atoi(ids)
if err != nil {
return nil, errors.Wrap(err, "convert")
}
if err := s.db.DeleteMedia(id); err != nil {
return nil, errors.Wrap(err, "delete db")
}
os.RemoveAll(filepath.Join(db.ImgPath, ids)) //delete image related
return "success", nil
}