moviehelper.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. package movie_helper
  2. import (
  3. "github.com/allanpk716/ChineseSubFinder/internal/ifaces"
  4. "github.com/allanpk716/ChineseSubFinder/internal/logic/sub_parser/ass"
  5. "github.com/allanpk716/ChineseSubFinder/internal/logic/sub_parser/srt"
  6. "github.com/allanpk716/ChineseSubFinder/internal/pkg/decode"
  7. "github.com/allanpk716/ChineseSubFinder/internal/pkg/imdb_helper"
  8. "github.com/allanpk716/ChineseSubFinder/internal/pkg/log_helper"
  9. "github.com/allanpk716/ChineseSubFinder/internal/pkg/settings"
  10. "github.com/allanpk716/ChineseSubFinder/internal/pkg/sub_helper"
  11. "github.com/allanpk716/ChineseSubFinder/internal/pkg/sub_parser_hub"
  12. "github.com/allanpk716/ChineseSubFinder/internal/types/common"
  13. "github.com/allanpk716/ChineseSubFinder/internal/types/supplier"
  14. "github.com/jinzhu/now"
  15. "os"
  16. "path/filepath"
  17. "strings"
  18. "time"
  19. )
  20. // OneMovieDlSubInAllSite 一部电影在所有的网站下载相应的字幕
  21. func OneMovieDlSubInAllSite(Suppliers []ifaces.ISupplier, oneVideoFullPath string, i int64) []supplier.SubInfo {
  22. defer func() {
  23. log_helper.GetLogger().Infoln(common.QueueName, i, "DlSub End", oneVideoFullPath)
  24. }()
  25. var outSUbInfos = make([]supplier.SubInfo, 0)
  26. log_helper.GetLogger().Infoln(common.QueueName, i, "DlSub Start", oneVideoFullPath)
  27. for _, oneSupplier := range Suppliers {
  28. log_helper.GetLogger().Infoln(common.QueueName, i, oneSupplier.GetSupplierName(), oneVideoFullPath)
  29. subInfos, err := OneMovieDlSubInOneSite(oneVideoFullPath, i, oneSupplier)
  30. if err != nil {
  31. log_helper.GetLogger().Errorln(common.QueueName, i, oneSupplier.GetSupplierName(), "oneMovieDlSubInOneSite", err)
  32. continue
  33. }
  34. outSUbInfos = append(outSUbInfos, subInfos...)
  35. }
  36. for index, info := range outSUbInfos {
  37. log_helper.GetLogger().Debugln(common.QueueName, i, "OneMovieDlSubInAllSite get sub", index, "Name:", info.Name, "FileUrl:", info.FileUrl)
  38. }
  39. return outSUbInfos
  40. }
  41. // OneMovieDlSubInOneSite 一部电影在一个站点下载字幕
  42. func OneMovieDlSubInOneSite(oneVideoFullPath string, i int64, supplier ifaces.ISupplier) ([]supplier.SubInfo, error) {
  43. defer func() {
  44. log_helper.GetLogger().Infoln(common.QueueName, i, supplier.GetSupplierName(), "End...")
  45. }()
  46. log_helper.GetLogger().Infoln(common.QueueName, i, supplier.GetSupplierName(), "Start...")
  47. subInfos, err := supplier.GetSubListFromFile4Movie(oneVideoFullPath)
  48. if err != nil {
  49. return nil, err
  50. }
  51. // 把后缀名给改好
  52. sub_helper.ChangeVideoExt2SubExt(subInfos)
  53. return subInfos, nil
  54. }
  55. // MovieHasChineseSub 这个视频文件的目录下面有字幕文件了没有
  56. func MovieHasChineseSub(videoFilePath string) (bool, []string, []string, error) {
  57. dir := filepath.Dir(videoFilePath)
  58. videoFileName := filepath.Base(videoFilePath)
  59. videoFileName = strings.ReplaceAll(videoFileName, filepath.Ext(videoFileName), "")
  60. files, err := os.ReadDir(dir)
  61. if err != nil {
  62. return false, nil, nil, err
  63. }
  64. // 所有的中文字幕列表
  65. var chineseSubFullPathList = make([]string, 0)
  66. // 所有的中文字幕列表,需要文件名与视频名称一样,也就是 Sub 文件半酣 Video name 即可
  67. var chineseSubFitVideoNameFullPathList = make([]string, 0)
  68. bFoundChineseSub := false
  69. for _, curFile := range files {
  70. if curFile.IsDir() {
  71. continue
  72. } else {
  73. // 文件
  74. if sub_parser_hub.IsSubExtWanted(curFile.Name()) == false {
  75. continue
  76. }
  77. // 字幕文件是否包含中文
  78. subFileFullPath := filepath.Join(dir, curFile.Name())
  79. if sub_parser_hub.NewSubParserHub(ass.NewParser(), srt.NewParser()).IsSubHasChinese(subFileFullPath) == true {
  80. if bFoundChineseSub == false {
  81. bFoundChineseSub = true
  82. }
  83. chineseSubFullPathList = append(chineseSubFullPathList, subFileFullPath)
  84. if strings.Contains(curFile.Name(), videoFileName) == true {
  85. chineseSubFitVideoNameFullPathList = append(chineseSubFitVideoNameFullPathList, subFileFullPath)
  86. }
  87. }
  88. }
  89. }
  90. return bFoundChineseSub, chineseSubFullPathList, chineseSubFitVideoNameFullPathList, nil
  91. }
  92. // SkipChineseMovie 跳过中文的电影
  93. func SkipChineseMovie(videoFullPath string, _proxySettings ...*settings.ProxySettings) (bool, error) {
  94. imdbInfo, err := decode.GetImdbInfo4Movie(videoFullPath)
  95. if err != nil {
  96. return false, err
  97. }
  98. isChineseVideo, _, err := imdb_helper.IsChineseVideo(imdbInfo, _proxySettings...)
  99. if err != nil {
  100. return false, err
  101. }
  102. if isChineseVideo == true {
  103. log_helper.GetLogger().Infoln("Skip", videoFullPath, "Sub Download, because movie is Chinese")
  104. return true, nil
  105. } else {
  106. return false, nil
  107. }
  108. }
  109. func MovieNeedDlSub(videoFullPath string, ExpirationTime int) (bool, error) {
  110. // 视频下面有不有字幕
  111. found, _, _, err := MovieHasChineseSub(videoFullPath)
  112. if err != nil {
  113. return false, err
  114. }
  115. // 资源下载的时间后的多少天内都进行字幕的自动下载,替换原有的字幕
  116. currentTime := time.Now()
  117. mInfo, modifyTime, err := decode.GetVideoInfoFromFileFullPath(videoFullPath)
  118. if err != nil {
  119. return false, err
  120. }
  121. // 如果这个视频发布的时间早于现在有两个年的间隔
  122. if mInfo.Year > 0 && currentTime.Year()-2 > mInfo.Year {
  123. if found == false {
  124. // 需要下载的
  125. return true, nil
  126. } else {
  127. // 有字幕了,没必要每次都刷新,跳过
  128. log_helper.GetLogger().Infoln("Skip", filepath.Base(videoFullPath), "Sub Download, because movie has sub and published more than 2 years")
  129. return false, nil
  130. }
  131. } else {
  132. // 读取不到 IMDB 信息也能接受
  133. videoIMDBInfo, err := decode.GetImdbInfo4Movie(videoFullPath)
  134. if err != nil {
  135. log_helper.GetLogger().Errorln("MovieNeedDlSub.GetImdbInfo4Movie", err)
  136. }
  137. // 如果播出时间能够读取到,那么就以这个完后推算 3个月
  138. // 如果读取不到 Aired Time 那么,下载后的 ModifyTime 3个月天内,都进行字幕的下载
  139. var baseTime time.Time
  140. if videoIMDBInfo.ReleaseDate != "" {
  141. baseTime, err = now.Parse(videoIMDBInfo.ReleaseDate)
  142. if err != nil {
  143. log_helper.GetLogger().Errorln("Movie parse AiredTime", err)
  144. baseTime = modifyTime
  145. }
  146. } else {
  147. baseTime = modifyTime
  148. }
  149. // 3个月内,或者没有字幕都要进行下载
  150. if baseTime.AddDate(0, 0, ExpirationTime).After(currentTime) == true || found == false {
  151. // 需要下载的
  152. return true, nil
  153. } else {
  154. if baseTime.AddDate(0, 0, ExpirationTime).After(currentTime) == false {
  155. log_helper.GetLogger().Infoln("Skip", filepath.Base(videoFullPath), "Sub Download, because movie has sub and downloaded or aired more than 3 months")
  156. return false, nil
  157. }
  158. if found == true {
  159. log_helper.GetLogger().Infoln("Skip", filepath.Base(videoFullPath), "Sub Download, because sub file found")
  160. return false, nil
  161. }
  162. return false, nil
  163. }
  164. }
  165. }