Skip to content

Commit

Permalink
重构调整从视频文件获取 Season Eps 等信息的方式,由文件名改为优先从 nfo 文件
Browse files Browse the repository at this point in the history
Signed-off-by: allan716 <525223688@qq.com>
  • Loading branch information
allanpk716 committed Jun 10, 2022
1 parent 093f19d commit b5240c3
Show file tree
Hide file tree
Showing 19 changed files with 368 additions and 227 deletions.
12 changes: 7 additions & 5 deletions internal/backend/controllers/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package v1
import (
"errors"
"fmt"
"net/http"
"path/filepath"

"github.com/allanpk716/ChineseSubFinder/internal/dao"
"github.com/allanpk716/ChineseSubFinder/internal/models"
"github.com/allanpk716/ChineseSubFinder/internal/pkg/decode"
Expand All @@ -11,8 +14,6 @@ import (
"github.com/allanpk716/ChineseSubFinder/internal/types/common"
TTaskqueue "github.com/allanpk716/ChineseSubFinder/internal/types/task_queue"
"github.com/gin-gonic/gin"
"net/http"
"path/filepath"
)

// AddJobHandler 外部 API 接口添加任务的处理
Expand Down Expand Up @@ -50,8 +51,9 @@ func (cb *ControllerBase) AddJobHandler(c *gin.Context) {
)

if videoListAdd.VideoType == 1 {
// 连续剧
// 连续剧的时候需要额外提交信息
torrentInfo, err := decode.GetVideoInfoFromFileName(videoListAdd.PhysicalVideoFileFullPath)
epsVideoNfoInfo, err := decode.GetVideoNfoInfo4OneSeriesEpisode(videoListAdd.PhysicalVideoFileFullPath)
if err != nil {
return
}
Expand All @@ -60,8 +62,8 @@ func (cb *ControllerBase) AddJobHandler(c *gin.Context) {
err = errors.New(fmt.Sprintf("decode.GetSeriesDirRootFPath == Empty, %s", videoListAdd.PhysicalVideoFileFullPath))
return
}
nowJob.Season = torrentInfo.Season
nowJob.Episode = torrentInfo.Episode
nowJob.Season = epsVideoNfoInfo.Season
nowJob.Episode = epsVideoNfoInfo.Episode
nowJob.SeriesRootDirPath = seriesInfoDirPath
}

Expand Down
6 changes: 3 additions & 3 deletions internal/backend/controllers/v1/video_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func (cb *ControllerBase) VideoListAddHandler(c *gin.Context) {

if videoType == common.Series {
// 如果是连续剧,需要额外的读取这一个剧集的信息
torrentInfo, err := decode.GetVideoInfoFromFileName(videoListAdd.PhysicalVideoFileFullPath)
epsVideoNfoInfo, err := decode.GetVideoNfoInfo4OneSeriesEpisode(videoListAdd.PhysicalVideoFileFullPath)
if err != nil {
return
}
Expand All @@ -120,8 +120,8 @@ func (cb *ControllerBase) VideoListAddHandler(c *gin.Context) {
err = errors.New(fmt.Sprintf("decode.GetSeriesDirRootFPath == Empty, %s", videoListAdd.PhysicalVideoFileFullPath))
return
}
oneJob.Season = torrentInfo.Season
oneJob.Episode = torrentInfo.Episode
oneJob.Season = epsVideoNfoInfo.Season
oneJob.Episode = epsVideoNfoInfo.Episode
oneJob.SeriesRootDirPath = seriesInfoDirPath
}

Expand Down
2 changes: 1 addition & 1 deletion internal/logic/emby_helper/embyhelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ func (em *EmbyHelper) autoFindMappingPathWithMixInfoByIMDBId(mixInfo *emby.EmbyM
}

// 获取 IMDB 信息
localIMDBInfo, err := imdb_helper.GetVideoIMDBInfoFromLocal(em.log, types.VideoIMDBInfo{ImdbId: mixInfo.IMDBId}, true)
localIMDBInfo, err := imdb_helper.GetVideoIMDBInfoFromLocal(em.log, types.VideoNfoInfo{ImdbId: mixInfo.IMDBId}, true)
if err != nil {

if errors.Is(err, common2.SkipCreateInDB) == true {
Expand Down
24 changes: 10 additions & 14 deletions internal/logic/movie_helper/moviehelper.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package movie_helper

import (
"os"
"path/filepath"
"strings"
"time"

"github.com/allanpk716/ChineseSubFinder/internal/ifaces"
"github.com/allanpk716/ChineseSubFinder/internal/logic/sub_parser/ass"
"github.com/allanpk716/ChineseSubFinder/internal/logic/sub_parser/srt"
Expand All @@ -13,10 +18,6 @@ import (
"github.com/allanpk716/ChineseSubFinder/internal/types/supplier"
"github.com/jinzhu/now"
"github.com/sirupsen/logrus"
"os"
"path/filepath"
"strings"
"time"
)

// OneMovieDlSubInAllSite 一部电影在所有的网站下载相应的字幕
Expand Down Expand Up @@ -121,7 +122,7 @@ func MovieHasChineseSub(log *logrus.Logger, videoFilePath string) (bool, []strin
// SkipChineseMovie 跳过中文的电影
func SkipChineseMovie(log *logrus.Logger, videoFullPath string, _proxySettings ...*settings.ProxySettings) (bool, error) {

imdbInfo, err := decode.GetImdbInfo4Movie(videoFullPath)
imdbInfo, err := decode.GetVideoNfoInfo4Movie(videoFullPath)
if err != nil {
return false, err
}
Expand All @@ -145,12 +146,12 @@ func MovieNeedDlSub(log *logrus.Logger, videoFullPath string, ExpirationTime int
}
// 资源下载的时间后的多少天内都进行字幕的自动下载,替换原有的字幕
currentTime := time.Now()
mInfo, modifyTime, err := decode.GetVideoInfoFromFileFullPath(videoFullPath)
videoNfoInfo4Movie, modifyTime, err := decode.GetVideoInfoFromFileFullPath(videoFullPath, true)
if err != nil {
return false, err
}
// 如果这个视频发布的时间早于现在有两个年的间隔
if mInfo.Year > 0 && currentTime.Year()-2 > mInfo.Year {
if videoNfoInfo4Movie.GetYear() > 0 && currentTime.Year()-2 > videoNfoInfo4Movie.GetYear() {
if found == false {
// 需要下载的
return true, nil
Expand All @@ -160,16 +161,11 @@ func MovieNeedDlSub(log *logrus.Logger, videoFullPath string, ExpirationTime int
return false, nil
}
} else {
// 读取不到 IMDB 信息也能接受
videoIMDBInfo, err := decode.GetImdbInfo4Movie(videoFullPath)
if err != nil {
log.Errorln("MovieNeedDlSub.GetImdbInfo4Movie", err)
}
// 如果播出时间能够读取到,那么就以这个完后推算 3个月
// 如果读取不到 Aired Time 那么,下载后的 ModifyTime 3个月天内,都进行字幕的下载
var baseTime time.Time
if videoIMDBInfo.ReleaseDate != "" {
baseTime, err = now.Parse(videoIMDBInfo.ReleaseDate)
if videoNfoInfo4Movie.ReleaseDate != "" {
baseTime, err = now.Parse(videoNfoInfo4Movie.ReleaseDate)
if err != nil {
log.Errorln("Movie parse AiredTime", err)
baseTime = modifyTime
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,12 +362,12 @@ func (s *ScanPlayedVideoSubInfo) dealOneVideo(index int, videoFPath, orgSubFPath

// 通过视频的绝对路径,从本地的视频文件对应的 nfo 获取到这个视频的 IMDB ID,
var err error
var imdbInfo4Video types.VideoIMDBInfo
var imdbInfo4Video types.VideoNfoInfo

if isMovie == true {
imdbInfo4Video, err = decode.GetImdbInfo4Movie(videoFPath)
imdbInfo4Video, err = decode.GetVideoNfoInfo4Movie(videoFPath)
} else {
imdbInfo4Video, err = decode.GetSeriesSeasonImdbInfoFromEpisode(videoFPath)
imdbInfo4Video, err = decode.GetSeriesSeasonVideoNfoInfoFromEpisode(videoFPath)
}
if err != nil {
// 如果找不到当前电影的 IMDB Info 本地文件,那么就跳过
Expand Down Expand Up @@ -527,13 +527,13 @@ func (s *ScanPlayedVideoSubInfo) dealOneVideo(index int, videoFPath, orgSubFPath

if isMovie == false {
// 连续剧的时候,如果可能应该获取是 第几季 第几集
torrentInfo, _, err := decode.GetVideoInfoFromFileFullPath(videoFPath)
epsVideoNfoInfo, err := decode.GetVideoNfoInfo4OneSeriesEpisode(videoFPath)
if err != nil {
s.log.Warningln("ScanPlayedVideoSubInfo.Scan", videoTypes, ".GetVideoInfoFromFileFullPath", imdbInfo4Video.Title, err)
s.log.Warningln("ScanPlayedVideoSubInfo.Scan", videoTypes, ".GetVideoNfoInfo4OneSeriesEpisode", imdbInfo4Video.Title, err)
return
}
oneVideoSubInfo.Season = torrentInfo.Season
oneVideoSubInfo.Episode = torrentInfo.Episode
oneVideoSubInfo.Season = epsVideoNfoInfo.Season
oneVideoSubInfo.Episode = epsVideoNfoInfo.Episode
}

s.log.Debugln(10)
Expand Down
105 changes: 53 additions & 52 deletions internal/logic/series_helper/seriesHelper.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package series_helper

import (
"errors"
"path/filepath"
"strconv"
"time"

"github.com/allanpk716/ChineseSubFinder/internal/ifaces"
"github.com/allanpk716/ChineseSubFinder/internal/logic/sub_parser/ass"
"github.com/allanpk716/ChineseSubFinder/internal/logic/sub_parser/srt"
Expand All @@ -19,9 +22,6 @@ import (
"github.com/emirpasic/gods/maps/treemap"
"github.com/jinzhu/now"
"github.com/sirupsen/logrus"
"path/filepath"
"strconv"
"time"
)

func readSeriesInfo(log *logrus.Logger, seriesDir string, need2AnalyzeSub bool) (*series.SeriesInfo, map[string][]series.SubInfo, error) {
Expand All @@ -40,47 +40,56 @@ func readSeriesInfo(log *logrus.Logger, seriesDir string, need2AnalyzeSub bool)
return seriesInfo, SubDict, nil
}

// 搜索所有的字幕
subParserHub := sub_parser_hub.NewSubParserHub(log, ass.NewParser(log), srt.NewParser(log))
subFiles, err := sub_helper.SearchMatchedSubFileByDir(log, seriesDir)
// 先搜索这个目录下,所有符合条件的视频
matchedVideoFile, err := my_util.SearchMatchedVideoFile(log, seriesDir)
if err != nil {
return nil, nil, err
}
for _, subFile := range subFiles {
// 然后再从这个视频找到对用匹配的字幕
for _, oneVideoFPath := range matchedVideoFile {

info, _, err := decode.GetVideoInfoFromFileFullPath(subFile)
subFiles, err := sub_helper.SearchMatchedSubFileByOneVideo(log, oneVideoFPath)
if err != nil {
log.Errorln(err)
continue
return nil, nil, err
}
bFind, subParserFileInfo, err := subParserHub.DetermineFileTypeFromFile(subFile)
epsVideoNfoInfo, err := decode.GetVideoNfoInfo4OneSeriesEpisode(oneVideoFPath)
if err != nil {
log.Errorln("DetermineFileTypeFromFile", subFile, err)
continue
}
if bFind == false {
log.Warnln("DetermineFileTypeFromFile", subFile, "not support SubType")
continue
}
// 判断这个字幕是否包含中文
if subParserHub.IsSubHasChinese(subParserFileInfo) == false {
log.Errorln(err)
continue
}
epsKey := my_util.GetEpisodeKeyName(info.Season, info.Episode)
oneFileSubInfo := series.SubInfo{
Title: info.Title,
Season: info.Season,
Episode: info.Episode,
Language: subParserFileInfo.Lang,
Dir: filepath.Dir(subFile),
FileFullPath: subFile,
}
_, ok := SubDict[epsKey]
if ok == false {
// 初始化
SubDict[epsKey] = make([]series.SubInfo, 0)

for _, subFile := range subFiles {

bFind, subParserFileInfo, err := subParserHub.DetermineFileTypeFromFile(subFile)
if err != nil {
log.Errorln("DetermineFileTypeFromFile", subFile, err)
continue
}
if bFind == false {
log.Warnln("DetermineFileTypeFromFile", subFile, "not support SubType")
continue
}
// 判断这个字幕是否包含中文
if subParserHub.IsSubHasChinese(subParserFileInfo) == false {
continue
}
epsKey := my_util.GetEpisodeKeyName(epsVideoNfoInfo.Season, epsVideoNfoInfo.Episode)
oneFileSubInfo := series.SubInfo{
Title: epsVideoNfoInfo.Title,
Season: epsVideoNfoInfo.Season,
Episode: epsVideoNfoInfo.Episode,
Language: subParserFileInfo.Lang,
Dir: filepath.Dir(subFile),
FileFullPath: subFile,
}
_, ok := SubDict[epsKey]
if ok == false {
// 初始化
SubDict[epsKey] = make([]series.SubInfo, 0)
}
SubDict[epsKey] = append(SubDict[epsKey], oneFileSubInfo)
}
SubDict[epsKey] = append(SubDict[epsKey], oneFileSubInfo)
}

return seriesInfo, SubDict, nil
Expand Down Expand Up @@ -145,7 +154,7 @@ func ReadSeriesInfoFromEmby(log *logrus.Logger, seriesDir string, seriesVideoLis
// SkipChineseSeries 跳过中文连续剧
func SkipChineseSeries(log *logrus.Logger, seriesRootPath string, _proxySettings ...*settings.ProxySettings) (bool, *models.IMDBInfo, error) {

imdbInfo, err := decode.GetImdbInfo4SeriesDir(seriesRootPath)
imdbInfo, err := decode.GetVideoNfoInfo4SeriesDir(seriesRootPath)
if err != nil {
return false, nil, err
}
Expand Down Expand Up @@ -314,7 +323,7 @@ func whichSeasonEpsNeedDownloadSub(log *logrus.Logger, seriesInfo *series.Series
func GetSeriesInfoFromDir(log *logrus.Logger, seriesDir string) (*series.SeriesInfo, error) {
seriesInfo := series.SeriesInfo{}
// 只考虑 IMDB 去查询,文件名目前发现可能会跟电影重复,导致很麻烦,本来也有前置要求要削刮器处理的
videoInfo, err := decode.GetImdbInfo4SeriesDir(seriesDir)
videoInfo, err := decode.GetVideoNfoInfo4SeriesDir(seriesDir)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -348,7 +357,7 @@ func GetSeriesInfoFromDir(log *logrus.Logger, seriesDir string) (*series.SeriesI
if err != nil {
// 不是必须的
seriesInfo.Year = 0
log.Warnln("ReadSeriesInfoFromDir.GetImdbInfo4SeriesDir.strconv.Atoi", seriesDir, err)
log.Warnln("ReadSeriesInfoFromDir.GetVideoNfoInfo4SeriesDir.strconv.Atoi", seriesDir, err)
} else {
seriesInfo.Year = iYear
}
Expand All @@ -367,21 +376,21 @@ func getEpsInfoAndSubDic(log *logrus.Logger,
SubDict map[string][]series.SubInfo,
epsMap ...map[int][]int) {
// 正常来说,一集只有一个格式的视频,也就是 S01E01 只有一个,如果有多个则会只保存第一个
info, modifyTime, err := decode.GetVideoInfoFromFileFullPath(videoFile)
episodeInfo, modifyTime, err := decode.GetVideoInfoFromFileFullPath(videoFile, false)
if err != nil {
log.Errorln("model.GetVideoInfoFromFileFullPath", videoFile, err)
return
}

if len(epsMap) > 0 {
// 如果这个视频不在需要下载的 Eps 列表中,那么就跳过后续的逻辑
epsList, ok := epsMap[0][info.Season]
epsList, ok := epsMap[0][episodeInfo.Season]
if ok == false {
return
}
found := false
for _, oneEpsID := range epsList {
if oneEpsID == info.Episode {
if oneEpsID == episodeInfo.Episode {
// 在需要下载的 Eps 列表中
found = true
break
Expand All @@ -392,22 +401,14 @@ func getEpsInfoAndSubDic(log *logrus.Logger,
}
}

episodeInfo, err := decode.GetImdbInfo4OneSeriesEpisode(videoFile)
if err != nil {
if errors.Is(err, common.CanNotFindIMDBID) == false {
log.Errorln("model.GetImdbInfo4OneSeriesEpisode", videoFile, err)
return
}
}

epsKey := my_util.GetEpisodeKeyName(info.Season, info.Episode)
epsKey := my_util.GetEpisodeKeyName(episodeInfo.Season, episodeInfo.Episode)
_, ok := EpisodeDict[epsKey]
if ok == false {
// 初始化
oneFileEpInfo := series.EpisodeInfo{
Title: info.Title,
Season: info.Season,
Episode: info.Episode,
Title: episodeInfo.Title,
Season: episodeInfo.Season,
Episode: episodeInfo.Episode,
Dir: filepath.Dir(videoFile),
FileFullPath: videoFile,
ModifyTime: modifyTime,
Expand Down
7 changes: 4 additions & 3 deletions internal/logic/series_helper/seriesHelper_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package series_helper

import (
"testing"

"github.com/allanpk716/ChineseSubFinder/internal/pkg/log_helper"
"github.com/allanpk716/ChineseSubFinder/internal/pkg/unit_test_helper"
"testing"
)

func TestReadSeriesInfoFromDir(t *testing.T) {

series := unit_test_helper.GetTestDataResourceRootPath([]string{"series", "Loki"}, 4, false)
seriesInfo, err := ReadSeriesInfoFromDir(log_helper.GetLogger4Tester(), series, 90, false, false)
//series := unit_test_helper.GetTestDataResourceRootPath([]string{"series", "Loki"}, 4, false)
seriesInfo, err := ReadSeriesInfoFromDir(log_helper.GetLogger4Tester(), "X:\\连续剧\\黑袍纠察队 (2019)", 90, false, true)
if err != nil {
t.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/logic/sub_supplier/subhd/subhd.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ func (s *Supplier) getSubListFromFile4Movie(filePath string) ([]supplier.SubInfo
// 找到这个视频文件,尝试得到 IMDB ID
// 目前测试来看,加入 年 这个关键词去搜索,对 2020 年后的影片有利,因为网站有统一的详细页面了,而之前的,没有,会影响识别
// 所以,year >= 2020 年,则可以多加一个关键词(年)去搜索影片
imdbInfo, err := decode.GetImdbInfo4Movie(filePath)
imdbInfo, err := decode.GetVideoNfoInfo4Movie(filePath)
if err != nil {
// 允许的错误,跳过,继续进行文件名的搜索
s.log.Errorln("model.GetImdbInfo", err)
Expand Down

0 comments on commit b5240c3

Please sign in to comment.