util.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. package my_util
  2. import (
  3. "crypto/sha1"
  4. "encoding/hex"
  5. "fmt"
  6. "github.com/allanpk716/ChineseSubFinder/internal/common"
  7. "github.com/allanpk716/ChineseSubFinder/internal/pkg/decode"
  8. "github.com/allanpk716/ChineseSubFinder/internal/pkg/global_value"
  9. "github.com/allanpk716/ChineseSubFinder/internal/pkg/log_helper"
  10. "github.com/allanpk716/ChineseSubFinder/internal/pkg/regex_things"
  11. "github.com/allanpk716/ChineseSubFinder/internal/pkg/settings"
  12. browser "github.com/allanpk716/fake-useragent"
  13. "github.com/go-resty/resty/v2"
  14. "github.com/google/uuid"
  15. "io"
  16. "math"
  17. "net/http"
  18. "os"
  19. "os/exec"
  20. "path/filepath"
  21. "regexp"
  22. "runtime"
  23. "strconv"
  24. "strings"
  25. "time"
  26. )
  27. // NewHttpClient 新建一个 resty 的对象
  28. func NewHttpClient(_proxySettings ...settings.ProxySettings) *resty.Client {
  29. //const defUserAgent = "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50"
  30. //const defUserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36 Edg/91.0.864.41"
  31. var proxySettings settings.ProxySettings
  32. var HttpProxy, UserAgent, Referer string
  33. if len(_proxySettings) > 0 {
  34. proxySettings = _proxySettings[0]
  35. }
  36. if proxySettings.UseHttpProxy == true && len(proxySettings.HttpProxyAddress) > 0 {
  37. HttpProxy = proxySettings.HttpProxyAddress
  38. }
  39. // 随机的 Browser
  40. UserAgent = browser.Random()
  41. httpClient := resty.New()
  42. httpClient.SetTimeout(common.HTMLTimeOut)
  43. httpClient.SetRetryCount(2)
  44. if HttpProxy != "" {
  45. httpClient.SetProxy(HttpProxy)
  46. } else {
  47. httpClient.RemoveProxy()
  48. }
  49. if len(proxySettings.Referer) > 0 {
  50. Referer = proxySettings.Referer
  51. }
  52. httpClient.SetHeaders(map[string]string{
  53. "Content-Type": "application/json",
  54. "User-Agent": UserAgent,
  55. })
  56. if len(Referer) > 0 {
  57. httpClient.SetHeader("Referer", Referer)
  58. }
  59. return httpClient
  60. }
  61. // DownFile 从指定的 url 下载文件
  62. func DownFile(urlStr string, _proxySettings ...settings.ProxySettings) ([]byte, string, error) {
  63. var proxySettings settings.ProxySettings
  64. if len(_proxySettings) > 0 {
  65. proxySettings = _proxySettings[0]
  66. }
  67. httpClient := NewHttpClient(proxySettings)
  68. resp, err := httpClient.R().Get(urlStr)
  69. if err != nil {
  70. return nil, "", err
  71. }
  72. filename := GetFileName(resp.RawResponse)
  73. if filename == "" {
  74. log_helper.GetLogger().Warningln("DownFile.GetFileName is string.empty", urlStr)
  75. }
  76. return resp.Body(), filename, nil
  77. }
  78. // GetFileName 获取下载文件的文件名
  79. func GetFileName(resp *http.Response) string {
  80. contentDisposition := resp.Header.Get("Content-Disposition")
  81. if len(contentDisposition) == 0 {
  82. return ""
  83. }
  84. re := regexp.MustCompile(`filename=["]*([^"]+)["]*`)
  85. matched := re.FindStringSubmatch(contentDisposition)
  86. if matched == nil || len(matched) == 0 || len(matched[0]) == 0 {
  87. log_helper.GetLogger().Errorln("GetFileName.Content-Disposition", contentDisposition)
  88. return ""
  89. }
  90. return matched[1]
  91. }
  92. // AddBaseUrl 判断传入的 url 是否需要拼接 baseUrl
  93. func AddBaseUrl(baseUrl, url string) string {
  94. if strings.Contains(url, "://") {
  95. return url
  96. }
  97. return fmt.Sprintf("%s%s", baseUrl, url)
  98. }
  99. // IsDir 存在且是文件夹
  100. func IsDir(path string) bool {
  101. s, err := os.Stat(path)
  102. if err != nil {
  103. return false
  104. }
  105. return s.IsDir()
  106. }
  107. // IsFile 存在且是文件
  108. func IsFile(filePath string) bool {
  109. s, err := os.Stat(filePath)
  110. if err != nil {
  111. return false
  112. }
  113. return !s.IsDir()
  114. }
  115. // VideoNameSearchKeywordMaker 拼接视频搜索的 title 和 年份
  116. func VideoNameSearchKeywordMaker(title string, year string) string {
  117. iYear, err := strconv.Atoi(year)
  118. if err != nil {
  119. // 允许的错误
  120. log_helper.GetLogger().Errorln("VideoNameSearchKeywordMaker", "year to int", err)
  121. iYear = 0
  122. }
  123. searchKeyword := title
  124. if iYear >= 2020 {
  125. searchKeyword = searchKeyword + " " + year
  126. }
  127. return searchKeyword
  128. }
  129. // SearchMatchedVideoFileFromDirs 搜索符合后缀名的视频文件
  130. func SearchMatchedVideoFileFromDirs(dirs []string) ([]string, error) {
  131. var fileFullPathList = make([]string, 0)
  132. for _, dir := range dirs {
  133. matchedVideoFile, err := SearchMatchedVideoFile(dir)
  134. if err != nil {
  135. return nil, err
  136. }
  137. fileFullPathList = append(fileFullPathList, matchedVideoFile...)
  138. }
  139. return fileFullPathList, nil
  140. }
  141. // SearchMatchedVideoFile 搜索符合后缀名的视频文件
  142. func SearchMatchedVideoFile(dir string) ([]string, error) {
  143. var fileFullPathList = make([]string, 0)
  144. pathSep := string(os.PathSeparator)
  145. files, err := os.ReadDir(dir)
  146. if err != nil {
  147. return nil, err
  148. }
  149. for _, curFile := range files {
  150. fullPath := dir + pathSep + curFile.Name()
  151. if curFile.IsDir() {
  152. // 内层的错误就无视了
  153. oneList, _ := SearchMatchedVideoFile(fullPath)
  154. if oneList != nil {
  155. fileFullPathList = append(fileFullPathList, oneList...)
  156. }
  157. } else {
  158. // 这里就是文件了
  159. if IsWantedVideoExtDef(curFile.Name()) == false {
  160. continue
  161. } else {
  162. // 跳过不符合的文件,比如 MAC OS 下可能有缓存文件,见 #138
  163. fi, err := curFile.Info()
  164. if err != nil {
  165. log_helper.GetLogger().Debugln("SearchMatchedVideoFile, file.Info:", fullPath, err)
  166. continue
  167. }
  168. if fi.Size() == 4096 && strings.HasPrefix(curFile.Name(), "._") == true {
  169. log_helper.GetLogger().Debugln("SearchMatchedVideoFile file.Size() == 4096 && Prefix Name == ._*", fullPath)
  170. continue
  171. }
  172. fileFullPathList = append(fileFullPathList, fullPath)
  173. }
  174. }
  175. }
  176. return fileFullPathList, nil
  177. }
  178. func SearchTVNfo(dir string) ([]string, error) {
  179. var fileFullPathList = make([]string, 0)
  180. pathSep := string(os.PathSeparator)
  181. files, err := os.ReadDir(dir)
  182. if err != nil {
  183. return nil, err
  184. }
  185. for _, curFile := range files {
  186. fullPath := dir + pathSep + curFile.Name()
  187. if curFile.IsDir() {
  188. // 内层的错误就无视了
  189. oneList, _ := SearchTVNfo(fullPath)
  190. if oneList != nil {
  191. fileFullPathList = append(fileFullPathList, oneList...)
  192. }
  193. } else {
  194. // 这里就是文件了
  195. if strings.ToLower(curFile.Name()) != decode.MetadateTVNfo {
  196. continue
  197. } else {
  198. // 跳过不符合的文件,比如 MAC OS 下可能有缓存文件,见 #138
  199. fi, err := curFile.Info()
  200. if err != nil {
  201. log_helper.GetLogger().Debugln("SearchTVNfo, file.Info:", fullPath, err)
  202. continue
  203. }
  204. if fi.Size() == 4096 && strings.HasPrefix(curFile.Name(), "._") == true {
  205. log_helper.GetLogger().Debugln("SearchTVNfo file.Size() == 4096 && Prefix Name == ._*", fullPath)
  206. continue
  207. }
  208. fileFullPathList = append(fileFullPathList, fullPath)
  209. }
  210. }
  211. }
  212. return fileFullPathList, nil
  213. }
  214. // IsWantedVideoExtDef 后缀名是否符合规则
  215. func IsWantedVideoExtDef(fileName string) bool {
  216. if len(global_value.WantedExtMap) < 1 {
  217. global_value.DefExtMap[common.VideoExtMp4] = common.VideoExtMp4
  218. global_value.DefExtMap[common.VideoExtMkv] = common.VideoExtMkv
  219. global_value.DefExtMap[common.VideoExtRmvb] = common.VideoExtRmvb
  220. global_value.DefExtMap[common.VideoExtIso] = common.VideoExtIso
  221. global_value.WantedExtMap[common.VideoExtMp4] = common.VideoExtMp4
  222. global_value.WantedExtMap[common.VideoExtMkv] = common.VideoExtMkv
  223. global_value.WantedExtMap[common.VideoExtRmvb] = common.VideoExtRmvb
  224. global_value.WantedExtMap[common.VideoExtIso] = common.VideoExtIso
  225. for _, videoExt := range global_value.CustomVideoExts {
  226. global_value.WantedExtMap[videoExt] = videoExt
  227. }
  228. }
  229. fileExt := strings.ToLower(filepath.Ext(fileName))
  230. _, bFound := global_value.WantedExtMap[fileExt]
  231. return bFound
  232. }
  233. func GetEpisodeKeyName(season, eps int) string {
  234. return "S" + strconv.Itoa(season) + "E" + strconv.Itoa(eps)
  235. }
  236. // CopyFile copies a single file from src to dst
  237. func CopyFile(src, dst string) error {
  238. var err error
  239. var srcFd *os.File
  240. var dstFd *os.File
  241. var srcInfo os.FileInfo
  242. if srcFd, err = os.Open(src); err != nil {
  243. return err
  244. }
  245. defer func() {
  246. _ = srcFd.Close()
  247. }()
  248. if dstFd, err = os.Create(dst); err != nil {
  249. return err
  250. }
  251. defer func() {
  252. _ = dstFd.Close()
  253. }()
  254. if _, err = io.Copy(dstFd, srcFd); err != nil {
  255. return err
  256. }
  257. if srcInfo, err = os.Stat(src); err != nil {
  258. return err
  259. }
  260. return os.Chmod(dst, srcInfo.Mode())
  261. }
  262. // CopyDir copies a whole directory recursively
  263. func CopyDir(src string, dst string) error {
  264. var err error
  265. var fds []os.DirEntry
  266. var srcInfo os.FileInfo
  267. if srcInfo, err = os.Stat(src); err != nil {
  268. return err
  269. }
  270. if err = os.MkdirAll(dst, srcInfo.Mode()); err != nil {
  271. return err
  272. }
  273. if fds, err = os.ReadDir(src); err != nil {
  274. return err
  275. }
  276. for _, fd := range fds {
  277. srcfp := filepath.Join(src, fd.Name())
  278. dstfp := filepath.Join(dst, fd.Name())
  279. if fd.IsDir() {
  280. if err = CopyDir(srcfp, dstfp); err != nil {
  281. fmt.Println(err)
  282. }
  283. } else {
  284. if err = CopyFile(srcfp, dstfp); err != nil {
  285. fmt.Println(err)
  286. }
  287. }
  288. }
  289. return nil
  290. }
  291. // CloseChrome 强行结束没有关闭的 Chrome 进程
  292. func CloseChrome() {
  293. cmdString := ""
  294. var command *exec.Cmd
  295. sysType := runtime.GOOS
  296. if sysType == "linux" {
  297. // LINUX系统
  298. cmdString = "pkill chrome"
  299. command = exec.Command("/bin/sh", "-c", cmdString)
  300. }
  301. if sysType == "windows" {
  302. // windows系统
  303. cmdString = "taskkill /F /im chrome.exe"
  304. command = exec.Command("cmd.exe", "/c", cmdString)
  305. }
  306. if sysType == "darwin" {
  307. // macOS
  308. // https://stackoverflow.com/questions/57079120/using-exec-command-in-golang-how-do-i-open-a-new-terminal-and-execute-a-command
  309. cmdString = `tell application "/Applications/Google Chrome.app" to quit`
  310. command = exec.Command("osascript", "-s", "h", "-e", cmdString)
  311. }
  312. if cmdString == "" || command == nil {
  313. log_helper.GetLogger().Errorln("CloseChrome OS:", sysType)
  314. return
  315. }
  316. err := command.Run()
  317. if err != nil {
  318. log_helper.GetLogger().Warningln("CloseChrome", err)
  319. }
  320. }
  321. // OSCheck 强制的系统支持检查
  322. func OSCheck() bool {
  323. sysType := runtime.GOOS
  324. if sysType == "linux" {
  325. return true
  326. }
  327. if sysType == "windows" {
  328. return true
  329. }
  330. if sysType == "darwin" {
  331. return true
  332. }
  333. return false
  334. }
  335. // FixWindowPathBackSlash 修复 Windows 反斜杠的梗
  336. func FixWindowPathBackSlash(path string) string {
  337. return strings.Replace(path, string(filepath.Separator), "/", -1)
  338. }
  339. func WriteStrings2File(desfilePath string, strings []string) error {
  340. dstFile, err := os.Create(desfilePath)
  341. if err != nil {
  342. return err
  343. }
  344. defer func() {
  345. _ = dstFile.Close()
  346. }()
  347. allString := ""
  348. for _, s := range strings {
  349. allString += s + "\r\n"
  350. }
  351. _, err = dstFile.WriteString(allString)
  352. if err != nil {
  353. return err
  354. }
  355. return nil
  356. }
  357. func TimeNumber2Time(inputTimeNumber float64) time.Time {
  358. newTime := time.Time{}.Add(time.Duration(inputTimeNumber * math.Pow10(9)))
  359. return newTime
  360. }
  361. func Time2SecondNumber(inTime time.Time) float64 {
  362. outSecond := 0.0
  363. outSecond += float64(inTime.Hour() * 60 * 60)
  364. outSecond += float64(inTime.Minute() * 60)
  365. outSecond += float64(inTime.Second())
  366. outSecond += float64(inTime.Nanosecond()) / 1000 / 1000 / 1000
  367. return outSecond
  368. }
  369. func Time2Duration(inTime time.Time) time.Duration {
  370. return time.Duration(Time2SecondNumber(inTime) * math.Pow10(9))
  371. }
  372. func Second2Time(sec int64) time.Time {
  373. return time.Unix(sec, 0)
  374. }
  375. // ReplaceSpecString 替换特殊的字符
  376. func ReplaceSpecString(inString string, rep string) string {
  377. return regex_things.RegMatchSpString.ReplaceAllString(inString, rep)
  378. }
  379. func Bool2Int(inBool bool) int {
  380. if inBool == true {
  381. return 1
  382. } else {
  383. return 0
  384. }
  385. }
  386. // Round 取整
  387. func Round(x float64) int64 {
  388. if x-float64(int64(x)) > 0 {
  389. return int64(x) + 1
  390. } else {
  391. return int64(x)
  392. }
  393. //return int64(math.Floor(x + 0.5))
  394. }
  395. // MakePowerOfTwo 2的整次幂数 buffer length is not a power of two
  396. func MakePowerOfTwo(x int64) int64 {
  397. power := math.Log2(float64(x))
  398. tmpRound := Round(power)
  399. return int64(math.Pow(2, float64(tmpRound)))
  400. }
  401. // MakeCeil10msMultipleFromFloat 将传入的秒,规整到 10ms 的倍数,返回依然是 秒,向上取整
  402. func MakeCeil10msMultipleFromFloat(input float64) float64 {
  403. const bb = 100
  404. // 先转到 10 ms 单位,比如传入是 1.912 - > 191.2
  405. t10ms := input * bb
  406. // 191.2 - > 192.0
  407. newT10ms := math.Ceil(t10ms)
  408. // 转换回来
  409. return newT10ms / bb
  410. }
  411. // MakeFloor10msMultipleFromFloat 将传入的秒,规整到 10ms 的倍数,返回依然是 秒,向下取整
  412. func MakeFloor10msMultipleFromFloat(input float64) float64 {
  413. const bb = 100
  414. // 先转到 10 ms 单位,比如传入是 1.912 - > 191.2
  415. t10ms := input * bb
  416. // 191.2 - > 191.0
  417. newT10ms := math.Floor(t10ms)
  418. // 转换回来
  419. return newT10ms / bb
  420. }
  421. // MakeCeil10msMultipleFromTime 向上取整,规整到 10ms 的倍数
  422. func MakeCeil10msMultipleFromTime(input time.Time) time.Time {
  423. nowTime := MakeCeil10msMultipleFromFloat(Time2SecondNumber(input))
  424. newTime := time.Time{}.Add(time.Duration(nowTime * math.Pow10(9)))
  425. return newTime
  426. }
  427. // MakeFloor10msMultipleFromTime 向下取整,规整到 10ms 的倍数
  428. func MakeFloor10msMultipleFromTime(input time.Time) time.Time {
  429. nowTime := MakeFloor10msMultipleFromFloat(Time2SecondNumber(input))
  430. newTime := time.Time{}.Add(time.Duration(nowTime * math.Pow10(9)))
  431. return newTime
  432. }
  433. // Time2SubTimeString 时间转字幕格式的时间字符串
  434. func Time2SubTimeString(inTime time.Time, timeFormat string) string {
  435. /*
  436. 这里进行时间转字符串的时候有一点比较特殊
  437. 正常来说输出的格式是类似 15:04:05.00
  438. 那么有个问题,字幕的时间格式是 0:00:12.00, 小时,是个数,除非有跨度到 20 小时的视频,不然小时就应该是个数
  439. 这就需要一个额外的函数去处理这些情况
  440. */
  441. outTimeString := inTime.Format(timeFormat)
  442. if inTime.Hour() > 9 {
  443. // 小时,两位数
  444. return outTimeString
  445. } else {
  446. // 小时,一位数
  447. items := strings.SplitN(outTimeString, ":", -1)
  448. if len(items) == 3 {
  449. outTimeString = strings.Replace(outTimeString, items[0], fmt.Sprintf("%d", inTime.Hour()), 1)
  450. return outTimeString
  451. }
  452. return outTimeString
  453. }
  454. }
  455. // IsEqual 比较 float64
  456. func IsEqual(f1, f2 float64) bool {
  457. const MIN = 0.000001
  458. if f1 > f2 {
  459. return math.Dim(f1, f2) < MIN
  460. } else {
  461. return math.Dim(f2, f1) < MIN
  462. }
  463. }
  464. // ParseTime 解析字幕时间字符串,这里可能小数点后面有 2-4 位
  465. func ParseTime(inTime string) (time.Time, error) {
  466. parseTime, err := time.Parse(common.TimeFormatPoint2, inTime)
  467. if err != nil {
  468. parseTime, err = time.Parse(common.TimeFormatPoint3, inTime)
  469. if err != nil {
  470. parseTime, err = time.Parse(common.TimeFormatPoint4, inTime)
  471. }
  472. }
  473. return parseTime, err
  474. }
  475. // GetFileSHA1 获取文件的 SHA1 值
  476. func GetFileSHA1(srcFileFPath string) (string, error) {
  477. infile, err := os.Open(srcFileFPath)
  478. if err != nil {
  479. return "", err
  480. }
  481. defer func() {
  482. _ = infile.Close()
  483. }()
  484. h := sha1.New()
  485. _, err = io.Copy(h, infile)
  486. if err != nil {
  487. return "", err
  488. }
  489. return hex.EncodeToString(h.Sum(nil)), nil
  490. }
  491. // WriteFile 写文件
  492. func WriteFile(desFileFPath string, bytes []byte) error {
  493. var err error
  494. nowDesPath := desFileFPath
  495. if filepath.IsAbs(nowDesPath) == false {
  496. nowDesPath, err = filepath.Abs(nowDesPath)
  497. if err != nil {
  498. return err
  499. }
  500. }
  501. // 创建对应的目录
  502. nowDirPath := filepath.Dir(nowDesPath)
  503. err = os.MkdirAll(nowDirPath, os.ModePerm)
  504. if err != nil {
  505. return err
  506. }
  507. file, err := os.Create(nowDesPath)
  508. if err != nil {
  509. return err
  510. }
  511. defer func() {
  512. _ = file.Close()
  513. }()
  514. _, err = file.Write(bytes)
  515. if err != nil {
  516. return err
  517. }
  518. return nil
  519. }
  520. // GetNowTimeString 获取当前的时间,没有秒
  521. func GetNowTimeString() (string, int, int, int) {
  522. nowTime := time.Now()
  523. addString := fmt.Sprintf("%d-%d-%d", nowTime.Hour(), nowTime.Minute(), nowTime.Nanosecond())
  524. return addString, nowTime.Hour(), nowTime.Minute(), nowTime.Nanosecond()
  525. }
  526. // GenerateAccessToken 生成随机的 AccessToken
  527. func GenerateAccessToken() string {
  528. u4 := uuid.New()
  529. return u4.String()
  530. }