Skip to content

Commit

Permalink
增加对分卷的兼容(暂定方案)
Browse files Browse the repository at this point in the history
需要手动指定分卷模式: tmdb/part.txt
取值范围: 0, 1, 2以上
0-缺省值,不使用分卷
1-自动根据剧集数自动算, 中间不能有缺失
2-以上为分卷的数量, 如s01e02.part1最终认为是s01e03
  • Loading branch information
fengqi committed Jun 9, 2023
1 parent b0ef2e7 commit 126f38c
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 5 deletions.
46 changes: 41 additions & 5 deletions shows/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -321,17 +322,51 @@ func (c *Collector) scanShowsFile(d *Dir) (map[string]*File, error) {
return nil, err
}

movieFiles := make(map[string]*File, 0)
movieFiles := make([]*File, 0)
for _, file := range fileInfo {
movieFile := c.parseShowsFile(d, file)
if movieFile == nil {
continue
if movieFile != nil {
if d.PartMode > 0 {
movieFile.Part = utils.MatchPart(file.Name())
}
movieFiles = append(movieFiles, movieFile)
}
}

// 处理分卷
// part=1会根据part出现的次数累加, 适合没有规律的, 比如E01只有上下, E03有上中下, 但是如果中间部分剧集缺失会导致算错
// part=2或者更大的数字会使用当前集数*2, 比如part=2的时候, E05.Part1会映射成E09, 可以缺失中间部分剧集
if d.PartMode == 1 {
// 使用season episode part多重排序
sort.Slice(movieFiles, func(i, j int) bool {
if movieFiles[i].Season == movieFiles[j].Season {
if movieFiles[i].Episode == movieFiles[j].Episode {
return movieFiles[i].Part < movieFiles[j].Part
}
return movieFiles[i].Episode < movieFiles[j].Episode
}
return movieFiles[i].Season < movieFiles[j].Season
})

// 重新计算episode
for i, item := range movieFiles {
item.Episode = i + 1
item.SeasonEpisode = fmt.Sprintf("s%02de%02d", item.Season, item.Episode)
}
} else {
for _, item := range movieFiles {
item.Episode = (item.Episode-1)*d.PartMode + item.Part
item.SeasonEpisode = fmt.Sprintf("s%02de%02d", item.Season, item.Episode)
}
}

movieFiles[movieFile.SeasonEpisode] = movieFile
// TODO 忘记这里为啥返回map,而不是slice了,先临时转成map,后续看看能不能改回来
movieFilesMap := make(map[string]*File)
for _, item := range movieFiles {
movieFilesMap[item.SeasonEpisode] = item
}

return movieFiles, nil
return movieFilesMap, nil
}

// 解析文件, 返回详情
Expand Down Expand Up @@ -482,6 +517,7 @@ func (c *Collector) parseShowsDir(baseDir string, file fs.FileInfo) *Dir {
showsDir.ReadTvId()
showsDir.ReadGroupId()
showsDir.checkCacheDir()
showsDir.ReadPart()

return showsDir
}
14 changes: 14 additions & 0 deletions shows/dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type Dir struct {
Source string `json:"source"` // 来源
Studio string `json:"studio"` // 媒体
IsCollection bool `json:"is_collection"` // 是否是合集目录
PartMode int `json:"part_mode"` // 分卷模式: 0不使用分卷, 1-自动, 2以上为手动指定分卷数量
}

// ReadTvId 从文件读取tvId
Expand Down Expand Up @@ -143,3 +144,16 @@ func (d *Dir) downloadImage(detail *tmdb.TvDetail) {
}
}
}

// ReadPart 读取分卷模式
func (d *Dir) ReadPart() {
partFile := d.GetCacheDir() + "/part.txt"
if _, err := os.Stat(partFile); err == nil {
bytes, err := os.ReadFile(partFile)
if err == nil {
d.PartMode, _ = strconv.Atoi(strings.Trim(string(bytes), "\r\n "))
} else {
utils.Logger.WarningF("read part specially file: %s err: %v", partFile, err)
}
}
}
1 change: 1 addition & 0 deletions shows/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type File struct {
SeasonEpisode string `json:"season_episode"`
Suffix string `json:"suffix"`
TvId int `json:"tv_id"`
Part int `json:"part"` // 分卷模式下,第几部分
//TvDetail *tmdb.TvDetail `json:"tv_detail"`
}

Expand Down
14 changes: 14 additions & 0 deletions utils/video.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ var (
optionsMatch *regexp.Regexp
resolutionMatch *regexp.Regexp
seasonRangeMatch *regexp.Regexp
partMatch *regexp.Regexp
)

func init() {
Expand Down Expand Up @@ -165,6 +166,7 @@ func init() {
chsEpisodeMatch, _ = regexp.Compile("(?:第|)([0-9]+)集")
resolutionMatch, _ = regexp.Compile("[0-9]{3,4}Xx*[0-9]{3,4}")
seasonRangeMatch, _ = regexp.Compile("[sS](0|)[0-9]+-[sS](0|)[0-9]+")
partMatch, _ = regexp.Compile("(:?.|-|_| |@)[pP]art([0-9])(:?.|-|_| |@)")
}

// IsCollection 是否是合集,如S01-S03季
Expand Down Expand Up @@ -434,3 +436,15 @@ func IsResolution(name string) string {
func Split(name string) []string {
return SplitWith(name, delimiter, delimiterExecute)
}

// MatchPart 匹配分卷
func MatchPart(name string) int {
find := partMatch.FindStringSubmatch(name)
if len(find) == 4 {
num, err := strconv.Atoi(find[2])
if err == nil {
return num
}
}
return 0
}

0 comments on commit 126f38c

Please sign in to comment.