skip_scan_info.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package models
  2. import (
  3. "crypto/sha256"
  4. "fmt"
  5. "github.com/ChineseSubFinder/ChineseSubFinder/pkg/decode"
  6. "github.com/WQGroup/logger"
  7. PTN "github.com/middelink/go-parse-torrent-name"
  8. "path/filepath"
  9. )
  10. type SkipScanInfo struct {
  11. /*
  12. 这里的 UID 计算方式有两种:
  13. 1. 电影,由电影的文件夹路径计算 sha256 得到,X:\电影\Three Thousand Years of Longing (2022)
  14. 2. 连续剧,由连续剧的文件夹路径计算 sha256 得到,只能具体到一集(S01E01 这里是拼接出来的不是真正的文件名)
  15. X:\连续剧\绝命毒师S01E01
  16. */
  17. UID string `gorm:"type:varchar(64);primarykey"`
  18. Skip bool `gorm:"type:bool;default:false"`
  19. season int `gorm:"-"`
  20. eps int `gorm:"-"`
  21. }
  22. func (s *SkipScanInfo) Season() int {
  23. return s.season
  24. }
  25. func (s *SkipScanInfo) Eps() int {
  26. return s.eps
  27. }
  28. func NewSkipScanInfoByUID(uid string, skip bool) *SkipScanInfo {
  29. var skipScanInfo SkipScanInfo
  30. skipScanInfo.UID = uid
  31. skipScanInfo.Skip = skip
  32. return &skipScanInfo
  33. }
  34. func GenerateUID4Movie(movieFPath string) string {
  35. movieDirPath := filepath.Dir(movieFPath)
  36. fileUID := fmt.Sprintf("%x", sha256.Sum256([]byte(movieDirPath)))
  37. return fileUID
  38. }
  39. func GenerateUID4Series(seriesDirFPath string, season, eps int) string {
  40. mixInfo := fmt.Sprintf("%sS%02dE%02d", seriesDirFPath, season, eps)
  41. fileUID := fmt.Sprintf("%x", sha256.Sum256([]byte(mixInfo)))
  42. return fileUID
  43. }
  44. func NewSkipScanInfoByMovie(movieFPath string, skip bool) *SkipScanInfo {
  45. var skipScanInfo SkipScanInfo
  46. skipScanInfo.UID = GenerateUID4Movie(movieFPath)
  47. skipScanInfo.Skip = skip
  48. return &skipScanInfo
  49. }
  50. func NewSkipScanInfoBySeries(seriesDirFPath string, season, eps int, skip bool) *SkipScanInfo {
  51. var skipScanInfo SkipScanInfo
  52. skipScanInfo.UID = GenerateUID4Series(seriesDirFPath, season, eps)
  53. skipScanInfo.Skip = skip
  54. skipScanInfo.season = season
  55. skipScanInfo.eps = eps
  56. return &skipScanInfo
  57. }
  58. func NewSkipScanInfoBySeriesEx(oneEpsFPath string, skip bool) *SkipScanInfo {
  59. seriesDirFPath := filepath.Dir(filepath.Dir(oneEpsFPath))
  60. var season, eps int
  61. oneSeriesEpisode, err := decode.GetVideoNfoInfo4OneSeriesEpisode(oneEpsFPath)
  62. if err != nil {
  63. // 换一种方式获取 Season 和 Eps 信息
  64. var parse *PTN.TorrentInfo
  65. parse, err = PTN.Parse(oneEpsFPath)
  66. if err != nil {
  67. season = -1
  68. eps = -1
  69. logger.GetLogger().Errorln("NewSkipScanInfoBySeriesEx Parse Error: ", err)
  70. }
  71. season = parse.Season
  72. eps = parse.Episode
  73. } else {
  74. season = oneSeriesEpisode.Season
  75. eps = oneSeriesEpisode.Episode
  76. }
  77. var skipScanInfo SkipScanInfo
  78. skipScanInfo.UID = GenerateUID4Series(seriesDirFPath, season, eps)
  79. skipScanInfo.Skip = skip
  80. skipScanInfo.season = season
  81. skipScanInfo.eps = eps
  82. return &skipScanInfo
  83. }