moviehelper.go 7.3 KB

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