1
0

util.go 14 KB

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