moviehelper.go 6.4 KB

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