refactor: core client to engine

This commit is contained in:
Simon Ding
2025-03-31 11:06:03 +08:00
parent 6a2ab50d7d
commit eb2450e92b
13 changed files with 105 additions and 108 deletions

View File

@@ -1,4 +1,4 @@
package core package engine
import ( import (
"polaris/db" "polaris/db"
@@ -16,8 +16,8 @@ import (
"github.com/robfig/cron" "github.com/robfig/cron"
) )
func NewClient(db *db.Client, language string) *Client { func NewEngine(db *db.Client, language string) *Engine {
return &Client{ return &Engine{
db: db, db: db,
cron: cron.New(), cron: cron.New(),
tasks: make(map[int]*Task, 0), tasks: make(map[int]*Task, 0),
@@ -29,7 +29,7 @@ type scheduler struct {
cron string cron string
f func() error f func() error
} }
type Client struct { type Engine struct {
db *db.Client db *db.Client
cron *cron.Cron cron *cron.Cron
tasks map[int]*Task tasks map[int]*Task
@@ -37,20 +37,20 @@ type Client struct {
schedulers utils.Map[string, scheduler] schedulers utils.Map[string, scheduler]
} }
func (c *Client) registerCronJob(name string, cron string, f func() error) { func (c *Engine) registerCronJob(name string, cron string, f func() error) {
c.schedulers.Store(name, scheduler{ c.schedulers.Store(name, scheduler{
cron: cron, cron: cron,
f: f, f: f,
}) })
} }
func (c *Client) Init() { func (c *Engine) Init() {
go c.reloadTasks() go c.reloadTasks()
c.addSysCron() c.addSysCron()
go c.checkW500PosterOnStartup() go c.checkW500PosterOnStartup()
} }
func (c *Client) reloadTasks() { func (c *Engine) reloadTasks() {
allTasks := c.db.GetRunningHistories() allTasks := c.db.GetRunningHistories()
for _, t := range allTasks { for _, t := range allTasks {
dl, err := c.db.GetDownloadClient(t.DownloadClientID) dl, err := c.db.GetDownloadClient(t.DownloadClientID)
@@ -115,12 +115,12 @@ func (c *Client) reloadTasks() {
log.Infof("------ task reloading done ------") log.Infof("------ task reloading done ------")
} }
func (c *Client) buildInDownloader() (pkg.Downloader, error) { func (c *Engine) buildInDownloader() (pkg.Downloader, error) {
dir := c.db.GetDownloadDir() dir := c.db.GetDownloadDir()
return buildin.NewDownloader(dir) return buildin.NewDownloader(dir)
} }
func (c *Client) GetDownloadClient() (pkg.Downloader, *ent.DownloadClients, error) { func (c *Engine) GetDownloadClient() (pkg.Downloader, *ent.DownloadClients, error) {
downloaders := c.db.GetAllDonloadClients() downloaders := c.db.GetAllDonloadClients()
for _, d := range downloaders { for _, d := range downloaders {
if !d.Enable { if !d.Enable {
@@ -158,7 +158,7 @@ func (c *Client) GetDownloadClient() (pkg.Downloader, *ent.DownloadClients, erro
return nil, nil, errors.Errorf("no available download client") return nil, nil, errors.Errorf("no available download client")
} }
func (c *Client) TMDB() (*tmdb.Client, error) { func (c *Engine) TMDB() (*tmdb.Client, error) {
api := c.db.GetTmdbApiKey() api := c.db.GetTmdbApiKey()
if api == "" { if api == "" {
return nil, errors.New("TMDB apiKey not set") return nil, errors.New("TMDB apiKey not set")
@@ -168,7 +168,7 @@ func (c *Client) TMDB() (*tmdb.Client, error) {
return tmdb.NewClient(api, proxy, adult == "true") return tmdb.NewClient(api, proxy, adult == "true")
} }
func (c *Client) MustTMDB() *tmdb.Client { func (c *Engine) MustTMDB() *tmdb.Client {
t, err := c.TMDB() t, err := c.TMDB()
if err != nil { if err != nil {
log.Panicf("get tmdb: %v", err) log.Panicf("get tmdb: %v", err)
@@ -176,7 +176,7 @@ func (c *Client) MustTMDB() *tmdb.Client {
return t return t
} }
func (c *Client) RemoveTaskAndTorrent(id int) error { func (c *Engine) RemoveTaskAndTorrent(id int) error {
torrent := c.tasks[id] torrent := c.tasks[id]
if torrent != nil { if torrent != nil {
if err := torrent.Remove(); err != nil { if err := torrent.Remove(); err != nil {
@@ -187,6 +187,6 @@ func (c *Client) RemoveTaskAndTorrent(id int) error {
return nil return nil
} }
func (c *Client) GetTasks() map[int]*Task { func (c *Engine) GetTasks() map[int]*Task {
return c.tasks return c.tasks
} }

1
engine/fliters.go Normal file
View File

@@ -0,0 +1 @@
package engine

View File

@@ -1,4 +1,4 @@
package core package engine
import ( import (
"bytes" "bytes"
@@ -25,7 +25,7 @@ import (
"github.com/pkg/errors" "github.com/pkg/errors"
) )
func (c *Client) periodicallyUpdateImportlist() error { func (c *Engine) periodicallyUpdateImportlist() error {
log.Infof("begin check import list") log.Infof("begin check import list")
lists, err := c.db.GetAllImportLists() lists, err := c.db.GetAllImportLists()
if err != nil { if err != nil {
@@ -119,7 +119,7 @@ type AddWatchlistIn struct {
PreferSize int64 `json:"prefer_size"` PreferSize int64 `json:"prefer_size"`
} }
func (c *Client) AddTv2Watchlist(in AddWatchlistIn) (interface{}, error) { func (c *Engine) AddTv2Watchlist(in AddWatchlistIn) (interface{}, error) {
log.Debugf("add tv watchlist input %+v", in) log.Debugf("add tv watchlist input %+v", in)
if in.Folder == "" { if in.Folder == "" {
return nil, errors.New("folder should be provided") return nil, errors.New("folder should be provided")
@@ -247,7 +247,7 @@ func (c *Client) AddTv2Watchlist(in AddWatchlistIn) (interface{}, error) {
return nil, nil return nil, nil
} }
func (c *Client) getAlterTitles(tmdbId int, mediaType media.MediaType) ([]schema.AlternativeTilte, error){ func (c *Engine) getAlterTitles(tmdbId int, mediaType media.MediaType) ([]schema.AlternativeTilte, error) {
var titles []schema.AlternativeTilte var titles []schema.AlternativeTilte
if mediaType == media.MediaTypeTv { if mediaType == media.MediaTypeTv {
@@ -283,7 +283,7 @@ func (c *Client) getAlterTitles(tmdbId int, mediaType media.MediaType) ([]schema
return titles, nil return titles, nil
} }
func (c *Client) AddMovie2Watchlist(in AddWatchlistIn) (interface{}, error) { func (c *Engine) AddMovie2Watchlist(in AddWatchlistIn) (interface{}, error) {
log.Infof("add movie watchlist input: %+v", in) log.Infof("add movie watchlist input: %+v", in)
detailCn, err := c.MustTMDB().GetMovieDetails(in.TmdbID, db.LanguageCN) detailCn, err := c.MustTMDB().GetMovieDetails(in.TmdbID, db.LanguageCN)
if err != nil { if err != nil {
@@ -306,7 +306,6 @@ func (c *Client) AddMovie2Watchlist(in AddWatchlistIn) (interface{}, error) {
return nil, errors.Wrap(err, "get alter titles") return nil, errors.Wrap(err, "get alter titles")
} }
epid, err := c.db.SaveEposideDetail(&ent.Episode{ epid, err := c.db.SaveEposideDetail(&ent.Episode{
SeasonNumber: 1, SeasonNumber: 1,
EpisodeNumber: 1, EpisodeNumber: 1,
@@ -372,7 +371,7 @@ func (c *Client) AddMovie2Watchlist(in AddWatchlistIn) (interface{}, error) {
} }
func (c *Client) checkMovieFolder(m *ent.Media) error { func (c *Engine) checkMovieFolder(m *ent.Media) error {
var storageImpl, err = c.GetStorage(m.StorageID, media.MediaTypeMovie) var storageImpl, err = c.GetStorage(m.StorageID, media.MediaTypeMovie)
if err != nil { if err != nil {
return err return err
@@ -406,7 +405,7 @@ func IsJav(detail *tmdb.MovieDetails) bool {
return false return false
} }
func (c *Client) GetJavid(id int) string { func (c *Engine) GetJavid(id int) string {
alters, err := c.MustTMDB().GetMovieAlternativeTitles(id, c.language) alters, err := c.MustTMDB().GetMovieAlternativeTitles(id, c.language)
if err != nil { if err != nil {
return "" return ""
@@ -419,23 +418,23 @@ func (c *Client) GetJavid(id int) string {
return "" return ""
} }
func (c *Client) downloadBackdrop(path string, mediaID int) error { func (c *Engine) downloadBackdrop(path string, mediaID int) error {
url := "https://image.tmdb.org/t/p/original" + path url := "https://image.tmdb.org/t/p/original" + path
return c.downloadImage(url, mediaID, "backdrop.jpg") return c.downloadImage(url, mediaID, "backdrop.jpg")
} }
func (c *Client) downloadPoster(path string, mediaID int) error { func (c *Engine) downloadPoster(path string, mediaID int) error {
var url = "https://image.tmdb.org/t/p/original" + path var url = "https://image.tmdb.org/t/p/original" + path
return c.downloadImage(url, mediaID, "poster.jpg") return c.downloadImage(url, mediaID, "poster.jpg")
} }
func (c *Client) downloadW500Poster(path string, mediaID int) error { func (c *Engine) downloadW500Poster(path string, mediaID int) error {
url := "https://image.tmdb.org/t/p/w500" + path url := "https://image.tmdb.org/t/p/w500" + path
return c.downloadImage(url, mediaID, "poster_w500.jpg") return c.downloadImage(url, mediaID, "poster_w500.jpg")
} }
func (c *Client) downloadImage(url string, mediaID int, name string) error { func (c *Engine) downloadImage(url string, mediaID int, name string) error {
log.Infof("try to download image: %v", url) log.Infof("try to download image: %v", url)
var resp, err = http.Get(url) var resp, err = http.Get(url)
@@ -460,7 +459,7 @@ func (c *Client) downloadImage(url string, mediaID int, name string) error {
} }
func (c *Client) checkW500PosterOnStartup() { func (c *Engine) checkW500PosterOnStartup() {
log.Infof("check all w500 posters") log.Infof("check all w500 posters")
all := c.db.GetMediaWatchlist(media.MediaTypeTv) all := c.db.GetMediaWatchlist(media.MediaTypeTv)
movies := c.db.GetMediaWatchlist(media.MediaTypeMovie) movies := c.db.GetMediaWatchlist(media.MediaTypeMovie)
@@ -470,7 +469,7 @@ func (c *Client) checkW500PosterOnStartup() {
if _, err := os.Stat(targetFile); err != nil { if _, err := os.Stat(targetFile); err != nil {
log.Infof("poster_w500.jpg not exist for %s, will download it", e.NameEn) log.Infof("poster_w500.jpg not exist for %s, will download it", e.NameEn)
if e.MediaType ==media.MediaTypeTv { if e.MediaType == media.MediaTypeTv {
detail, err := c.MustTMDB().GetTvDetails(e.TmdbID, db.LanguageCN) detail, err := c.MustTMDB().GetTvDetails(e.TmdbID, db.LanguageCN)
if err != nil { if err != nil {
log.Warnf("get tmdb detail for %s error: %v", e.NameEn, err) log.Warnf("get tmdb detail for %s error: %v", e.NameEn, err)
@@ -500,7 +499,7 @@ func (c *Client) checkW500PosterOnStartup() {
} }
} }
func (c *Client) SuggestedMovieFolderName(tmdbId int) (string, error) { func (c *Engine) SuggestedMovieFolderName(tmdbId int) (string, error) {
d1, err := c.MustTMDB().GetMovieDetails(tmdbId, c.language) d1, err := c.MustTMDB().GetMovieDetails(tmdbId, c.language)
if err != nil { if err != nil {
@@ -545,7 +544,7 @@ func (c *Client) SuggestedMovieFolderName(tmdbId int) (string, error) {
return res, nil return res, nil
} }
func (c *Client) SuggestedSeriesFolderName(tmdbId int) (string, error) { func (c *Engine) SuggestedSeriesFolderName(tmdbId int) (string, error) {
d, err := c.MustTMDB().GetTvDetails(tmdbId, c.language) d, err := c.MustTMDB().GetTvDetails(tmdbId, c.language)
if err != nil { if err != nil {

View File

@@ -1,4 +1,4 @@
package core package engine
import ( import (
"polaris/ent" "polaris/ent"
@@ -11,7 +11,7 @@ import (
const prowlarrPrefix = "Prowlarr_" const prowlarrPrefix = "Prowlarr_"
func (c *Client) SyncProwlarrIndexers(apiKey, url string) error { func (c *Engine) SyncProwlarrIndexers(apiKey, url string) error {
client := prowlarr.New(apiKey, url) client := prowlarr.New(apiKey, url)
if ins, err := client.GetIndexers(); err != nil { if ins, err := client.GetIndexers(); err != nil {
return errors.Wrap(err, "connect to prowlarr error") return errors.Wrap(err, "connect to prowlarr error")
@@ -52,7 +52,7 @@ func (c *Client) SyncProwlarrIndexers(apiKey, url string) error {
return nil return nil
} }
func (c *Client) syncProwlarr() error { func (c *Engine) syncProwlarr() error {
p, err := c.db.GetProwlarrSetting() p, err := c.db.GetProwlarrSetting()
if err != nil { if err != nil {
return errors.Wrap(err, "db") return errors.Wrap(err, "db")
@@ -67,8 +67,7 @@ func (c *Client) syncProwlarr() error {
return nil return nil
} }
func (c *Engine) DeleteAllProwlarrIndexers() error {
func (c *Client) DeleteAllProwlarrIndexers() error {
all := c.db.GetAllIndexers() all := c.db.GetAllIndexers()
for _, index := range all { for _, index := range all {
if index.Synced { if index.Synced {

View File

@@ -1,4 +1,4 @@
package core package engine
import ( import (
"bytes" "bytes"
@@ -21,7 +21,7 @@ import (
"github.com/pkg/errors" "github.com/pkg/errors"
) )
func (c *Client) writeNfoFile(historyId int) error { func (c *Engine) writeNfoFile(historyId int) error {
if !c.nfoSupportEnabled() { if !c.nfoSupportEnabled() {
return nil return nil
} }
@@ -106,7 +106,7 @@ func (c *Client) writeNfoFile(historyId int) error {
return nil return nil
} }
func (c *Client) writePlexmatch(historyId int) error { func (c *Engine) writePlexmatch(historyId int) error {
if !c.plexmatchEnabled() { if !c.plexmatchEnabled() {
return nil return nil
@@ -169,15 +169,15 @@ func (c *Client) writePlexmatch(historyId int) error {
return st.WriteFile(seasonPlex, buff.Bytes()) return st.WriteFile(seasonPlex, buff.Bytes())
} }
func (c *Client) plexmatchEnabled() bool { func (c *Engine) plexmatchEnabled() bool {
return c.db.GetSetting(db.SettingPlexMatchEnabled) == "true" return c.db.GetSetting(db.SettingPlexMatchEnabled) == "true"
} }
func (c *Client) nfoSupportEnabled() bool { func (c *Engine) nfoSupportEnabled() bool {
return c.db.GetSetting(db.SettingNfoSupportEnabled) == "true" return c.db.GetSetting(db.SettingNfoSupportEnabled) == "true"
} }
func (c *Client) GetStorage(storageId int, mediaType media.MediaType) (storage.Storage, error) { func (c *Engine) GetStorage(storageId int, mediaType media.MediaType) (storage.Storage, error) {
st := c.db.GetStorage(storageId) st := c.db.GetStorage(storageId)
targetPath := st.TvPath targetPath := st.TvPath
if mediaType == media.MediaTypeMovie { if mediaType == media.MediaTypeMovie {
@@ -219,7 +219,7 @@ func (c *Client) GetStorage(storageId int, mediaType media.MediaType) (storage.S
return nil, errors.New("no storage found") return nil, errors.New("no storage found")
} }
func (c *Client) sendMsg(msg string) { func (c *Engine) sendMsg(msg string) {
clients, err := c.db.GetAllNotificationClients2() clients, err := c.db.GetAllNotificationClients2()
if err != nil { if err != nil {
log.Errorf("query notification clients: %v", err) log.Errorf("query notification clients: %v", err)
@@ -248,7 +248,7 @@ func (c *Client) sendMsg(msg string) {
} }
} }
func (c *Client) findEpisodeFilesPreMoving(historyId int) error { func (c *Engine) findEpisodeFilesPreMoving(historyId int) error {
his := c.db.GetHistory(historyId) his := c.db.GetHistory(historyId)
episodeIds := c.GetEpisodeIds(his) episodeIds := c.GetEpisodeIds(his)

View File

@@ -1,4 +1,4 @@
package core package engine
import "encoding/xml" import "encoding/xml"

View File

@@ -1,4 +1,4 @@
package core package engine
import ( import (
"bytes" "bytes"
@@ -16,7 +16,7 @@ import (
"github.com/pkg/errors" "github.com/pkg/errors"
) )
func (c *Client) DownloadEpisodeTorrent(r1 torznab.Result, seriesId, seasonNum int, episodeNums ...int) (*string, error) { func (c *Engine) DownloadEpisodeTorrent(r1 torznab.Result, seriesId, seasonNum int, episodeNums ...int) (*string, error) {
series, err := c.db.GetMedia(seriesId) series, err := c.db.GetMedia(seriesId)
if err != nil { if err != nil {
@@ -30,7 +30,7 @@ func (c *Client) DownloadEpisodeTorrent(r1 torznab.Result, seriesId, seasonNum i
tmdb 校验获取的资源名如果用资源名在tmdb搜索出来的结果能匹配上想要的资源则认为资源有效否则无效 tmdb 校验获取的资源名如果用资源名在tmdb搜索出来的结果能匹配上想要的资源则认为资源有效否则无效
解决名称过于简单的影视会匹配过多资源的问题 例如梦魇绝镇 FROM 解决名称过于简单的影视会匹配过多资源的问题 例如梦魇绝镇 FROM
*/ */
func (c *Client) checkBtReourceWithTmdb(r *torznab.Result, seriesId int) bool { func (c *Engine) checkBtReourceWithTmdb(r *torznab.Result, seriesId int) bool {
m := metadata.ParseTv(r.Name) m := metadata.ParseTv(r.Name)
se, err := c.MustTMDB().SearchMedia(m.NameEn, "", 1) se, err := c.MustTMDB().SearchMedia(m.NameEn, "", 1)
if err != nil { if err != nil {
@@ -57,7 +57,7 @@ func (c *Client) checkBtReourceWithTmdb(r *torznab.Result, seriesId int) bool {
} }
} }
func (c *Client) SearchAndDownload(seriesId, seasonNum int, episodeNums ...int) ([]string, error) { func (c *Engine) SearchAndDownload(seriesId, seasonNum int, episodeNums ...int) ([]string, error) {
res, err := SearchTvSeries(c.db, &SearchParam{ res, err := SearchTvSeries(c.db, &SearchParam{
MediaId: seriesId, MediaId: seriesId,
@@ -115,11 +115,11 @@ lo:
return torrentNames, nil return torrentNames, nil
} }
func (c *Client) DownloadMovie(m *ent.Media, r1 torznab.Result) (*string, error) { func (c *Engine) DownloadMovie(m *ent.Media, r1 torznab.Result) (*string, error) {
return c.downloadTorrent(m, r1, 0) return c.downloadTorrent(m, r1, 0)
} }
func (c *Client) downloadTorrent(m *ent.Media, r1 torznab.Result, seasonNum int, episodeNums ...int) (*string, error) { func (c *Engine) downloadTorrent(m *ent.Media, r1 torznab.Result, seasonNum int, episodeNums ...int) (*string, error) {
trc, dlc, err := c.GetDownloadClient() trc, dlc, err := c.GetDownloadClient()
if err != nil { if err != nil {
return nil, errors.Wrap(err, "connect transmission") return nil, errors.Wrap(err, "connect transmission")

View File

@@ -1,4 +1,4 @@
package core package engine
import ( import (
"fmt" "fmt"
@@ -18,7 +18,7 @@ import (
"github.com/pkg/errors" "github.com/pkg/errors"
) )
func (c *Client) addSysCron() { func (c *Engine) addSysCron() {
c.registerCronJob("check_running_tasks", "@every 1m", c.checkTasks) c.registerCronJob("check_running_tasks", "@every 1m", c.checkTasks)
c.registerCronJob("check_available_medias_to_download", "0 0 * * * *", func() error { c.registerCronJob("check_available_medias_to_download", "0 0 * * * *", func() error {
v := os.Getenv("POLARIS_NO_AUTO_DOWNLOAD") v := os.Getenv("POLARIS_NO_AUTO_DOWNLOAD")
@@ -48,14 +48,14 @@ func (c *Client) addSysCron() {
log.Infof("--------- add cron jobs done --------") log.Infof("--------- add cron jobs done --------")
} }
func (c *Client) mustAddCron(spec string, cmd func()) { func (c *Engine) mustAddCron(spec string, cmd func()) {
if err := c.cron.AddFunc(spec, cmd); err != nil { if err := c.cron.AddFunc(spec, cmd); err != nil {
log.Errorf("add func error: %v", err) log.Errorf("add func error: %v", err)
panic(err) panic(err)
} }
} }
func (c *Client) TriggerCronJob(name string) error { func (c *Engine) TriggerCronJob(name string) error {
job, ok := c.schedulers.Load(name) job, ok := c.schedulers.Load(name)
if !ok { if !ok {
return fmt.Errorf("job name not exists: %s", name) return fmt.Errorf("job name not exists: %s", name)
@@ -63,7 +63,7 @@ func (c *Client) TriggerCronJob(name string) error {
return job.f() return job.f()
} }
func (c *Client) checkTasks() error { func (c *Engine) checkTasks() error {
log.Debug("begin check tasks...") log.Debug("begin check tasks...")
for id, t := range c.tasks { for id, t := range c.tasks {
r := c.db.GetHistory(id) r := c.db.GetHistory(id)
@@ -124,7 +124,7 @@ seeding状态中会定时检查做种状态达到指定分享率会置
*/ */
func (c *Client) setHistoryStatus(id int, status history.Status) { func (c *Engine) setHistoryStatus(id int, status history.Status) {
r := c.db.GetHistory(id) r := c.db.GetHistory(id)
episodeIds := c.GetEpisodeIds(r) episodeIds := c.GetEpisodeIds(r)
@@ -152,7 +152,7 @@ func (c *Client) setHistoryStatus(id int, status history.Status) {
} }
} }
func (c *Client) setEpsideoStatus(episodeIds []int, status episode.Status) error { func (c *Engine) setEpsideoStatus(episodeIds []int, status episode.Status) error {
for _, id := range episodeIds { for _, id := range episodeIds {
ep, err := c.db.GetEpisodeByID(id) ep, err := c.db.GetEpisodeByID(id)
if err != nil { if err != nil {
@@ -171,7 +171,7 @@ func (c *Client) setEpsideoStatus(episodeIds []int, status episode.Status) error
return nil return nil
} }
func (c *Client) postTaskProcessing(id int) { func (c *Engine) postTaskProcessing(id int) {
if err := c.findEpisodeFilesPreMoving(id); err != nil { if err := c.findEpisodeFilesPreMoving(id); err != nil {
log.Errorf("finding all episode file error: %v", err) log.Errorf("finding all episode file error: %v", err)
} else { } else {
@@ -199,7 +199,7 @@ func getSeasonNum(h *ent.History) int {
return seasonNum return seasonNum
} }
func (c *Client) GetEpisodeIds(r *ent.History) []int { func (c *Engine) GetEpisodeIds(r *ent.History) []int {
var episodeIds []int var episodeIds []int
seasonNum := getSeasonNum(r) seasonNum := getSeasonNum(r)
@@ -231,7 +231,7 @@ func (c *Client) GetEpisodeIds(r *ent.History) []int {
return episodeIds return episodeIds
} }
func (c *Client) moveCompletedTask(id int) (err1 error) { func (c *Engine) moveCompletedTask(id int) (err1 error) {
torrent := c.tasks[id] torrent := c.tasks[id]
r := c.db.GetHistory(id) r := c.db.GetHistory(id)
// if r.Status == history.StatusUploading { // if r.Status == history.StatusUploading {
@@ -300,7 +300,7 @@ func (c *Client) moveCompletedTask(id int) (err1 error) {
return nil return nil
} }
func (c *Client) CheckDownloadedSeriesFiles(m *ent.Media) error { func (c *Engine) CheckDownloadedSeriesFiles(m *ent.Media) error {
if m.MediaType != media.MediaTypeTv { if m.MediaType != media.MediaTypeTv {
return nil return nil
} }
@@ -357,7 +357,7 @@ type Task struct {
UploadProgresser func() float64 UploadProgresser func() float64
} }
func (c *Client) DownloadSeriesAllEpisodes(id int) []string { func (c *Engine) DownloadSeriesAllEpisodes(id int) []string {
tvDetail, err := c.db.GetMediaDetails(id) tvDetail, err := c.db.GetMediaDetails(id)
if err != nil { if err != nil {
log.Errorf("get media details error: %v", err) log.Errorf("get media details error: %v", err)
@@ -429,7 +429,7 @@ func (c *Client) DownloadSeriesAllEpisodes(id int) []string {
return allNames return allNames
} }
func (c *Client) downloadAllTvSeries() { func (c *Engine) downloadAllTvSeries() {
log.Infof("begin check all tv series resources") log.Infof("begin check all tv series resources")
allSeries := c.db.GetMediaWatchlist(media.MediaTypeTv) allSeries := c.db.GetMediaWatchlist(media.MediaTypeTv)
for _, series := range allSeries { for _, series := range allSeries {
@@ -437,7 +437,7 @@ func (c *Client) downloadAllTvSeries() {
} }
} }
func (c *Client) downloadAllMovies() { func (c *Engine) downloadAllMovies() {
log.Infof("begin check all movie resources") log.Infof("begin check all movie resources")
allSeries := c.db.GetMediaWatchlist(media.MediaTypeMovie) allSeries := c.db.GetMediaWatchlist(media.MediaTypeMovie)
@@ -448,7 +448,7 @@ func (c *Client) downloadAllMovies() {
} }
} }
func (c *Client) DownloadMovieByID(id int) (string, error) { func (c *Engine) DownloadMovieByID(id int) (string, error) {
detail, err := c.db.GetMediaDetails(id) detail, err := c.db.GetMediaDetails(id)
if err != nil { if err != nil {
return "", errors.Wrap(err, "get media details") return "", errors.Wrap(err, "get media details")
@@ -468,7 +468,7 @@ func (c *Client) DownloadMovieByID(id int) (string, error) {
} }
} }
func (c *Client) downloadMovieSingleEpisode(m *ent.Media, ep *ent.Episode) (string, error) { func (c *Engine) downloadMovieSingleEpisode(m *ent.Media, ep *ent.Episode) (string, error) {
qiangban := c.db.GetSetting(db.SettingAllowQiangban) qiangban := c.db.GetSetting(db.SettingAllowQiangban)
allowQiangban := false allowQiangban := false
@@ -495,7 +495,7 @@ func (c *Client) downloadMovieSingleEpisode(m *ent.Media, ep *ent.Episode) (stri
return *s, nil return *s, nil
} }
func (c *Client) checkAllSeriesNewSeason() error { func (c *Engine) checkAllSeriesNewSeason() error {
log.Infof("begin checking series all new season") log.Infof("begin checking series all new season")
allSeries := c.db.GetMediaWatchlist(media.MediaTypeTv) allSeries := c.db.GetMediaWatchlist(media.MediaTypeTv)
for _, series := range allSeries { for _, series := range allSeries {
@@ -507,7 +507,7 @@ func (c *Client) checkAllSeriesNewSeason() error {
return nil return nil
} }
func (c *Client) checkSeiesNewSeason(media *ent.Media) error { func (c *Engine) checkSeiesNewSeason(media *ent.Media) error {
d, err := c.MustTMDB().GetTvDetails(media.TmdbID, c.language) d, err := c.MustTMDB().GetTvDetails(media.TmdbID, c.language)
if err != nil { if err != nil {
return errors.Wrap(err, "tmdb") return errors.Wrap(err, "tmdb")
@@ -545,7 +545,7 @@ func (c *Client) checkSeiesNewSeason(media *ent.Media) error {
return nil return nil
} }
func (c *Client) isSeedRatioLimitReached(indexId int, t pkg.Torrent) (float64, bool) { func (c *Engine) isSeedRatioLimitReached(indexId int, t pkg.Torrent) (float64, bool) {
indexer, err := c.db.GetIndexer(indexId) indexer, err := c.db.GetIndexer(indexId)
if err != nil { if err != nil {
return 0, true return 0, true

View File

@@ -1,4 +1,4 @@
package core package engine
import ( import (
"fmt" "fmt"

View File

@@ -1,2 +0,0 @@
package core

View File

@@ -3,10 +3,10 @@ package server
import ( import (
"fmt" "fmt"
"polaris/db" "polaris/db"
"polaris/engine"
"polaris/ent/media" "polaris/ent/media"
"polaris/log" "polaris/log"
"polaris/pkg/torznab" "polaris/pkg/torznab"
"polaris/server/core"
"strconv" "strconv"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
@@ -15,7 +15,7 @@ import (
func (s *Server) searchAndDownloadSeasonPackage(seriesId, seasonNum int) (*string, error) { func (s *Server) searchAndDownloadSeasonPackage(seriesId, seasonNum int) (*string, error) {
res, err := core.SearchTvSeries(s.db, &core.SearchParam{ res, err := engine.SearchTvSeries(s.db, &engine.SearchParam{
MediaId: seriesId, MediaId: seriesId,
SeasonNum: seasonNum, SeasonNum: seasonNum,
Episodes: nil, Episodes: nil,
@@ -54,7 +54,7 @@ func (s *Server) SearchAvailableTorrents(c *gin.Context) (interface{}, error) {
if in.Episode == 0 { if in.Episode == 0 {
//search season package //search season package
log.Infof("search series season package S%02d", in.Season) log.Infof("search series season package S%02d", in.Season)
res, err = core.SearchTvSeries(s.db, &core.SearchParam{ res, err = engine.SearchTvSeries(s.db, &engine.SearchParam{
MediaId: in.ID, MediaId: in.ID,
SeasonNum: in.Season, SeasonNum: in.Season,
Episodes: nil, Episodes: nil,
@@ -64,7 +64,7 @@ func (s *Server) SearchAvailableTorrents(c *gin.Context) (interface{}, error) {
} }
} else { } else {
log.Infof("search series episode S%02dE%02d", in.Season, in.Episode) log.Infof("search series episode S%02dE%02d", in.Season, in.Episode)
res, err = core.SearchTvSeries(s.db, &core.SearchParam{ res, err = engine.SearchTvSeries(s.db, &engine.SearchParam{
MediaId: in.ID, MediaId: in.ID,
SeasonNum: in.Season, SeasonNum: in.Season,
Episodes: []int{in.Episode}, Episodes: []int{in.Episode},
@@ -85,7 +85,7 @@ func (s *Server) SearchAvailableTorrents(c *gin.Context) (interface{}, error) {
allowQiangban = true allowQiangban = true
} }
res, err = core.SearchMovie(s.db, &core.SearchParam{ res, err = engine.SearchMovie(s.db, &engine.SearchParam{
MediaId: in.ID, MediaId: in.ID,
FilterQiangban: !allowQiangban, FilterQiangban: !allowQiangban,
}) })

View File

@@ -6,10 +6,10 @@ import (
"net/http/httputil" "net/http/httputil"
"net/url" "net/url"
"polaris/db" "polaris/db"
"polaris/engine"
"polaris/log" "polaris/log"
"polaris/pkg/cache" "polaris/pkg/cache"
"polaris/pkg/tmdb" "polaris/pkg/tmdb"
"polaris/server/core"
"polaris/ui" "polaris/ui"
"time" "time"
@@ -30,14 +30,14 @@ func NewServer(db *db.Client) *Server {
monitorNumCache: cache.NewCache[int, int](10 * time.Minute), monitorNumCache: cache.NewCache[int, int](10 * time.Minute),
downloadNumCache: cache.NewCache[int, int](10 * time.Minute), downloadNumCache: cache.NewCache[int, int](10 * time.Minute),
} }
s.core = core.NewClient(db, s.language) s.core = engine.NewEngine(db, s.language)
return s return s
} }
type Server struct { type Server struct {
r *gin.Engine r *gin.Engine
db *db.Client db *db.Client
core *core.Client core *engine.Engine
language string language string
jwtSerect string jwtSerect string
monitorNumCache *cache.Cache[int, int] monitorNumCache *cache.Cache[int, int]

View File

@@ -4,11 +4,11 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"polaris/db" "polaris/db"
"polaris/engine"
"polaris/ent" "polaris/ent"
"polaris/ent/episode" "polaris/ent/episode"
"polaris/ent/media" "polaris/ent/media"
"polaris/log" "polaris/log"
"polaris/server/core"
"strconv" "strconv"
"strings" "strings"
@@ -68,7 +68,7 @@ type addWatchlistIn struct {
} }
func (s *Server) AddTv2Watchlist(c *gin.Context) (interface{}, error) { func (s *Server) AddTv2Watchlist(c *gin.Context) (interface{}, error) {
var in core.AddWatchlistIn var in engine.AddWatchlistIn
if err := c.ShouldBindJSON(&in); err != nil { if err := c.ShouldBindJSON(&in); err != nil {
return nil, errors.Wrap(err, "bind query") return nil, errors.Wrap(err, "bind query")
} }
@@ -76,7 +76,7 @@ func (s *Server) AddTv2Watchlist(c *gin.Context) (interface{}, error) {
} }
func (s *Server) AddMovie2Watchlist(c *gin.Context) (interface{}, error) { func (s *Server) AddMovie2Watchlist(c *gin.Context) (interface{}, error) {
var in core.AddWatchlistIn var in engine.AddWatchlistIn
if err := c.ShouldBindJSON(&in); err != nil { if err := c.ShouldBindJSON(&in); err != nil {
return nil, errors.Wrap(err, "bind query") return nil, errors.Wrap(err, "bind query")
} }