feat: cron trigger api and fix import lists

This commit is contained in:
Simon Ding
2024-09-03 09:46:23 +08:00
parent 361556228b
commit ba1be8f279
7 changed files with 81 additions and 20 deletions

View File

@@ -13,8 +13,8 @@ import (
)
type Client struct {
apiKey string
tmdbClient *tmdb.Client
apiKey string
tmdbClient *tmdb.Client
enableAdultContent bool
}
@@ -43,8 +43,8 @@ func NewClient(apiKey, proxyUrl string, enableAdultContent bool) (*Client, error
}
return &Client{
apiKey: apiKey,
tmdbClient: tmdbClient,
apiKey: apiKey,
tmdbClient: tmdbClient,
enableAdultContent: enableAdultContent,
}, nil
}
@@ -59,7 +59,7 @@ func (c *Client) GetTvDetails(id int, language string) (*tmdb.TVDetails, error)
if !episodeNameUseful(d.LastEpisodeToAir.Name) {
log.Debug("should fetch english version")
var detailEN *tmdb.TVDetails
if language == "zh-CN" {
if language == "zh-CN" || language == "" {
detailEN, err = c.tmdbClient.GetTVDetails(id, withExternalIDs(withLangOption("en-US")))
if err != nil {
return d, nil
@@ -167,7 +167,7 @@ func (c *Client) GetEposideDetail(id, seasonNumber, eposideNumber int, language
}
if !episodeNameUseful(d.Name) {
var detailEN *tmdb.TVEpisodeDetails
if language == "zh-CN" {
if language == "zh-CN" || language == "" {
detailEN, err = c.tmdbClient.GetTVEpisodeDetails(id, seasonNumber, eposideNumber, withLangOption("en-US"))
if err != nil {
return d, nil
@@ -189,7 +189,7 @@ func (c *Client) GetSeasonDetails(id, seasonNumber int, language string) (*tmdb.
return nil, err
}
var detailEN *tmdb.TVSeasonDetails
if language == "zh-CN" {
if language == "zh-CN" || language == "" {
detailEN, err = c.tmdbClient.GetTVSeasonDetails(id, seasonNumber, withLangOption("en-US"))
if err != nil {
return detailCN, nil
@@ -213,20 +213,18 @@ func (c *Client) GetMovieAlternativeTitles(id int, language string) (*tmdb.Movie
return c.tmdbClient.GetMovieAlternativeTitles(id, withLangOption(language))
}
func (c *Client) GetByImdbId(imdbId string, lang string)(*tmdb.FindByID, error) {
func (c *Client) GetByImdbId(imdbId string, lang string) (*tmdb.FindByID, error) {
m := withLangOption(lang)
m["external_source"] = "imdb_id"
return c.tmdbClient.GetFindByID(imdbId, m)
}
func (c *Client) GetByTvdbId(imdbId string, lang string)(*tmdb.FindByID, error) {
func (c *Client) GetByTvdbId(imdbId string, lang string) (*tmdb.FindByID, error) {
m := withLangOption(lang)
m["external_source"] = "tvdb_id"
return c.tmdbClient.GetFindByID(imdbId, m)
}
func wrapLanguage(lang string) string {
if lang == "" {
lang = "zh-CN"

View File

@@ -6,6 +6,7 @@ import (
"polaris/log"
"polaris/pkg/tmdb"
"polaris/pkg/transmission"
"polaris/pkg/utils"
"github.com/pkg/errors"
"github.com/robfig/cron"
@@ -20,11 +21,23 @@ func NewClient(db *db.Client, language string) *Client {
}
}
type scheduler struct {
cron string
f func() error
}
type Client struct {
db *db.Client
cron *cron.Cron
tasks map[int]*Task
language string
schedulers utils.Map[string, scheduler]
}
func (c *Client) registerCronJob(name string, cron string, f func() error) {
c.schedulers.Store(name, scheduler{
cron: cron,
f: f,
})
}
func (c *Client) Init() {

View File

@@ -22,11 +22,13 @@ import (
)
func (c *Client) periodicallyUpdateImportlist() error {
log.Infof("begin check import list")
lists, err := c.db.GetAllImportLists()
if err != nil {
return errors.Wrap(err, "get from db")
}
for _, l := range lists {
log.Infof("check import list content for %v", l.Name)
if l.Type == importlist.TypePlex {
res, err := plexwatchlist.ParsePlexWatchlist(l.URL)
if err != nil {
@@ -61,12 +63,17 @@ func (c *Client) periodicallyUpdateImportlist() error {
log.Errorf("suggesting name error: %v", err)
continue
}
c.AddMovie2Watchlist(AddWatchlistIn{
_, err = c.AddMovie2Watchlist(AddWatchlistIn{
TmdbID: int(d.ID),
StorageID: l.StorageID,
Resolution: l.Qulity,
Folder: name,
})
if err != nil {
log.Errorf("[update_import_lists] add movie to watchlist error: %v", err)
} else {
log.Infof("[update_import_lists] add movie to watchlist success")
}
} else if len(tmdbRes.TvResults) > 0 {
d := tmdbRes.TvResults[0]
name, err := c.SuggestedSeriesFolderName(int(d.ID))
@@ -75,12 +82,18 @@ func (c *Client) periodicallyUpdateImportlist() error {
continue
}
c.AddTv2Watchlist(AddWatchlistIn{
_, err = c.AddTv2Watchlist(AddWatchlistIn{
TmdbID: int(d.ID),
StorageID: l.StorageID,
Resolution: l.Qulity,
Folder: name,
})
if err != nil {
log.Errorf("[update_import_lists] add tv to watchlist error: %v", err)
} else {
log.Infof("[update_import_lists] add tv to watchlist success")
}
}
}

View File

@@ -17,14 +17,22 @@ import (
)
func (c *Client) addSysCron() {
c.mustAddCron("@every 1m", c.checkTasks)
c.mustAddCron("0 0 * * * *", func() {
c.registerCronJob("check_running_tasks","@every 1m", c.checkTasks)
c.registerCronJob("check_available_medias_to_download","0 0 * * * *", func() error{
c.downloadAllTvSeries()
c.downloadAllMovies()
return nil
})
c.mustAddCron("0 0 */12 * * *", c.checkAllSeriesNewSeason)
c.mustAddCron("0 0/30 * * * *", func() {
c.periodicallyUpdateImportlist()
c.registerCronJob("check_series_new_release", "0 0 */12 * * *", c.checkAllSeriesNewSeason)
c.registerCronJob("update_import_lists","0 30 * * * *", c.periodicallyUpdateImportlist)
c.schedulers.Range(func(key string, value scheduler) bool {
c.mustAddCron(value.cron, func() {
if err := value.f(); err != nil {
log.Errorf("exexuting cron job %s error: %v", key, err)
}
})
return false
})
c.cron.Start()
}
@@ -36,7 +44,15 @@ func (c *Client) mustAddCron(spec string, cmd func()) {
}
}
func (c *Client) checkTasks() {
func (c *Client) TriggerCronJob(name string) error {
job, ok := c.schedulers.Load(name)
if !ok {
return fmt.Errorf("job name not exists: %s", name)
}
return job.f()
}
func (c *Client) checkTasks() error{
log.Debug("begin check tasks...")
for id, t := range c.tasks {
r := c.db.GetHistory(id)
@@ -68,6 +84,7 @@ func (c *Client) checkTasks() {
go c.postTaskProcessing(id)
}
}
return nil
}
func (c *Client) postTaskProcessing(id int) {
@@ -358,7 +375,7 @@ func (c *Client) downloadMovieSingleEpisode(ep *ent.Episode, targetDir string) (
return r1.Name, nil
}
func (c *Client) checkAllSeriesNewSeason() {
func (c *Client) checkAllSeriesNewSeason() error{
log.Infof("begin checking series all new season")
allSeries := c.db.GetMediaWatchlist(media.MediaTypeTv)
for _, series := range allSeries {
@@ -367,6 +384,7 @@ func (c *Client) checkAllSeriesNewSeason() {
log.Errorf("check series new season error: series name %v, error: %v", series.NameEn, err)
}
}
return nil
}
func (c *Client) checkSeiesNewSeason(media *ent.Media) error {

View File

@@ -68,6 +68,7 @@ func (s *Server) Serve() error {
setting.POST("/parse/tv", HttpHandler(s.ParseTv))
setting.POST("/parse/movie", HttpHandler(s.ParseMovie))
setting.POST("/monitoring", HttpHandler(s.ChangeEpisodeMonitoring))
setting.POST("/cron/trigger", HttpHandler(s.TriggerCronJob))
}
activity := api.Group("/activity")
{

View File

@@ -246,3 +246,20 @@ func (s *Server) EditMediaMetadata(c *gin.Context) (interface{}, error) {
}
return "success", nil
}
type triggerCronJobIn struct {
JobName string `json:"job_name"`
}
func (s *Server) TriggerCronJob(c *gin.Context) (interface{}, error) {
var in triggerCronJobIn
if err := c.ShouldBindJSON(&in); err != nil {
return nil, errors.Wrap(err, "bind")
}
err := s.core.TriggerCronJob(in.JobName)
if err != nil {
return nil, err
}
return "success", nil
}

View File

@@ -46,6 +46,7 @@ class _ImportlistState extends ConsumerState<Importlist> {
key: _formKey,
initialValue: {
"name": list.name,
"url": list.url,
"qulity": list.qulity,
"storage_id": list.storageId
},