From 19c6308a8140029b9fedbacf6cd2d09548bdaeab Mon Sep 17 00:00:00 2001 From: Simon Ding Date: Tue, 13 Aug 2024 10:05:38 +0800 Subject: [PATCH] feat: nfo support backend --- db/const.go | 19 ++++---- server/core/integration.go | 93 +++++++++++++++++++++++++++++++++++++- server/core/nfo.go | 62 ++++++++++++------------- server/core/scheduler.go | 3 ++ 4 files changed, 134 insertions(+), 43 deletions(-) diff --git a/db/const.go b/db/const.go index 3a1bb05..d704240 100644 --- a/db/const.go +++ b/db/const.go @@ -3,15 +3,16 @@ package db var Version = "undefined" const ( - SettingTmdbApiKey = "tmdb_api_key" - SettingLanguage = "language" - SettingJacketUrl = "jacket_url" - SettingJacketApiKey = "jacket_api_key" - SettingDownloadDir = "download_dir" - SettingLogLevel = "log_level" - SettingProxy = "proxy" - SettingPlexMatchEnabled = "plexmatch_enabled" - SettingAllowQiangban = "filter_qiangban" + SettingTmdbApiKey = "tmdb_api_key" + SettingLanguage = "language" + SettingJacketUrl = "jacket_url" + SettingJacketApiKey = "jacket_api_key" + SettingDownloadDir = "download_dir" + SettingLogLevel = "log_level" + SettingProxy = "proxy" + SettingPlexMatchEnabled = "plexmatch_enabled" + SettingNfoSupportEnabled = "nfo_support_enabled" + SettingAllowQiangban = "filter_qiangban" ) const ( diff --git a/server/core/integration.go b/server/core/integration.go index ae0777b..457092b 100644 --- a/server/core/integration.go +++ b/server/core/integration.go @@ -2,6 +2,7 @@ package core import ( "bytes" + "encoding/xml" "fmt" "github.com/pkg/errors" "os" @@ -15,9 +16,95 @@ import ( "polaris/pkg/storage" "polaris/pkg/utils" "slices" + "strconv" "strings" ) +func (c *Client) writeNfoFile(historyId int) error { + if !c.nfoSupportEnabled() { + return nil + } + + his := c.db.GetHistory(historyId) + + md, err := c.db.GetMedia(his.MediaID) + if err != nil { + return err + } + + if md.MediaType == media.MediaTypeTv { //tvshow.nfo + st, err := c.getStorage(md.StorageID, media.MediaTypeTv) + if err != nil { + return errors.Wrap(err, "get storage") + } + + nfoPath := filepath.Join(md.TargetDir, "tvshow.nfo") + _, err = st.ReadFile(nfoPath) + if err != nil { + log.Infof("tvshow.nfo file missing, create new one, tv series name: %s", md.NameEn) + show := Tvshow{ + Title: md.NameCn, + Originaltitle: md.OriginalName, + Showtitle: md.NameCn, + Plot: md.Overview, + ID: strconv.Itoa(md.TmdbID), + Uniqueid: []UniqueId{ + { + Text: strconv.Itoa(md.TmdbID), + Type: "tmdb", + Default: "true", + }, + { + Text: md.ImdbID, + Type: "imdb", + }, + }, + } + data, err := xml.Marshal(&show) + if err != nil { + return errors.Wrap(err, "xml marshal") + } + return st.WriteFile(nfoPath, data) + } + + } else if md.MediaType == media.MediaTypeMovie { //movie.nfo + st, err := c.getStorage(md.StorageID, media.MediaTypeMovie) + if err != nil { + return errors.Wrap(err, "get storage") + } + + nfoPath := filepath.Join(md.TargetDir, "movie.nfo") + _, err = st.ReadFile(nfoPath) + if err != nil { + log.Infof("movie.nfo file missing, create new one, tv series name: %s", md.NameEn) + nfoData := Movie{ + Title: md.NameCn, + Originaltitle: md.OriginalName, + Sorttitle: md.NameCn, + Plot: md.Overview, + ID: strconv.Itoa(md.TmdbID), + Uniqueid: []UniqueId{ + { + Text: strconv.Itoa(md.TmdbID), + Type: "tmdb", + Default: "true", + }, + { + Text: md.ImdbID, + Type: "imdb", + }, + }, + } + data, err := xml.Marshal(&nfoData) + if err != nil { + return errors.Wrap(err, "xml marshal") + } + return st.WriteFile(nfoPath, data) + } + } + return nil +} + func (c *Client) writePlexmatch(historyId int) error { if !c.plexmatchEnabled() { @@ -30,7 +117,7 @@ func (c *Client) writePlexmatch(historyId int) error { if err != nil { return err } - if series.MediaType != media.MediaTypeTv { + if series.MediaType != media.MediaTypeTv { //.plexmatch only support tv series return nil } st, err := c.getStorage(series.StorageID, media.MediaTypeTv) @@ -104,6 +191,10 @@ func (c *Client) plexmatchEnabled() bool { return c.db.GetSetting(db.SettingPlexMatchEnabled) == "true" } +func (c *Client) nfoSupportEnabled() bool { + return c.db.GetSetting(db.SettingNfoSupportEnabled) == "true" +} + func (c *Client) getStorage(storageId int, mediaType media.MediaType) (storage.Storage, error) { st := c.db.GetStorage(storageId) targetPath := st.TvPath diff --git a/server/core/nfo.go b/server/core/nfo.go index b9ae7bc..7c63b49 100644 --- a/server/core/nfo.go +++ b/server/core/nfo.go @@ -46,24 +46,20 @@ type Tvshow struct { Preview string `xml:"preview,attr"` } `xml:"thumb"` } `xml:"fanart"` - Mpaa string `xml:"mpaa"` - Playcount string `xml:"playcount"` - Lastplayed string `xml:"lastplayed"` - ID string `xml:"id"` - Uniqueid []struct { - Text string `xml:",chardata"` - Type string `xml:"type,attr"` - Default string `xml:"default,attr"` - } `xml:"uniqueid"` - Genre string `xml:"genre"` - Premiered string `xml:"premiered"` - Year string `xml:"year"` - Status string `xml:"status"` - Code string `xml:"code"` - Aired string `xml:"aired"` - Studio string `xml:"studio"` - Trailer string `xml:"trailer"` - Actor []struct { + Mpaa string `xml:"mpaa"` + Playcount string `xml:"playcount"` + Lastplayed string `xml:"lastplayed"` + ID string `xml:"id"` + Uniqueid []UniqueId `xml:"uniqueid"` + Genre string `xml:"genre"` + Premiered string `xml:"premiered"` + Year string `xml:"year"` + Status string `xml:"status"` + Code string `xml:"code"` + Aired string `xml:"aired"` + Studio string `xml:"studio"` + Trailer string `xml:"trailer"` + Actor []struct { Text string `xml:",chardata"` Name string `xml:"name"` Role string `xml:"role"` @@ -82,6 +78,11 @@ type Tvshow struct { Dateadded string `xml:"dateadded"` } +type UniqueId struct { + Text string `xml:",chardata"` + Type string `xml:"type,attr"` + Default string `xml:"default,attr"` +} type Episodedetails struct { XMLName xml.Name `xml:"episodedetails"` @@ -148,8 +149,7 @@ type Episodedetails struct { Total string `xml:"total"` } `xml:"resume"` Dateadded string `xml:"dateadded"` -} - +} type Movie struct { XMLName xml.Name `xml:"movie"` @@ -189,18 +189,14 @@ type Movie struct { Preview string `xml:"preview,attr"` } `xml:"thumb"` } `xml:"fanart"` - Mpaa string `xml:"mpaa"` - Playcount string `xml:"playcount"` - Lastplayed string `xml:"lastplayed"` - ID string `xml:"id"` - Uniqueid []struct { - Text string `xml:",chardata"` - Type string `xml:"type,attr"` - Default string `xml:"default,attr"` - } `xml:"uniqueid"` - Genre string `xml:"genre"` - Country []string `xml:"country"` - Set struct { + Mpaa string `xml:"mpaa"` + Playcount string `xml:"playcount"` + Lastplayed string `xml:"lastplayed"` + ID string `xml:"id"` + Uniqueid []UniqueId `xml:"uniqueid"` + Genre string `xml:"genre"` + Country []string `xml:"country"` + Set struct { Text string `xml:",chardata"` Name string `xml:"name"` Overview string `xml:"overview"` @@ -254,4 +250,4 @@ type Movie struct { Order string `xml:"order"` Thumb string `xml:"thumb"` } `xml:"actor"` -} +} diff --git a/server/core/scheduler.go b/server/core/scheduler.go index 9a0c473..92682e8 100644 --- a/server/core/scheduler.go +++ b/server/core/scheduler.go @@ -74,6 +74,9 @@ func (c *Client) postTaskProcessing(id int) { if err := c.writePlexmatch(id); err != nil { log.Errorf("write plexmatch file error: %v", err) } + if err := c.writeNfoFile(id); err != nil { + log.Errorf("write nfo file error: %v", err) + } } if err := c.moveCompletedTask(id); err != nil { log.Infof("post tasks for id %v fail: %v", id, err)