moviehelper.go 6.8 KB

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