moviehelper.go 6.6 KB

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