1
0

util.go 14 KB

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