util.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. package pkg
  2. import (
  3. "fmt"
  4. "github.com/allanpk716/ChineseSubFinder/internal/common"
  5. "github.com/allanpk716/ChineseSubFinder/internal/pkg/global_value"
  6. "github.com/allanpk716/ChineseSubFinder/internal/pkg/log_helper"
  7. "github.com/allanpk716/ChineseSubFinder/internal/types"
  8. browser "github.com/allanpk716/fake-useragent"
  9. "github.com/go-resty/resty/v2"
  10. "io"
  11. "io/ioutil"
  12. "net/http"
  13. "os"
  14. "os/exec"
  15. "path/filepath"
  16. "regexp"
  17. "runtime"
  18. "strconv"
  19. "strings"
  20. )
  21. // NewHttpClient 新建一个 resty 的对象
  22. func NewHttpClient(_reqParam ...types.ReqParam) *resty.Client {
  23. //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"
  24. //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"
  25. // 随机的 Browser
  26. defUserAgent := browser.Random()
  27. var reqParam types.ReqParam
  28. var HttpProxy, UserAgent, Referer string
  29. if len(_reqParam) > 0 {
  30. reqParam = _reqParam[0]
  31. }
  32. if len(reqParam.HttpProxy) > 0 {
  33. HttpProxy = reqParam.HttpProxy
  34. }
  35. if len(reqParam.UserAgent) > 0 {
  36. UserAgent = reqParam.UserAgent
  37. } else {
  38. UserAgent = defUserAgent
  39. }
  40. if len(reqParam.Referer) > 0 {
  41. Referer = reqParam.Referer
  42. }
  43. httpClient := resty.New()
  44. httpClient.SetTimeout(common.HTMLTimeOut)
  45. if HttpProxy != "" {
  46. httpClient.SetProxy(HttpProxy)
  47. } else {
  48. httpClient.RemoveProxy()
  49. }
  50. httpClient.SetHeaders(map[string]string{
  51. "Content-Type": "application/json",
  52. "User-Agent": UserAgent,
  53. })
  54. if len(Referer) > 0 {
  55. httpClient.SetHeader("Referer", Referer)
  56. }
  57. return httpClient
  58. }
  59. // DownFile 从指定的 url 下载文件
  60. func DownFile(urlStr string, _reqParam ...types.ReqParam) ([]byte, string, error) {
  61. var reqParam types.ReqParam
  62. if len(_reqParam) > 0 {
  63. reqParam = _reqParam[0]
  64. }
  65. httpClient := NewHttpClient(reqParam)
  66. resp, err := httpClient.R().Get(urlStr)
  67. if err != nil {
  68. return nil, "", err
  69. }
  70. filename := GetFileName(resp.RawResponse)
  71. return resp.Body(), filename, nil
  72. }
  73. // GetFileName 获取下载文件的文件名
  74. func GetFileName(resp *http.Response) string {
  75. contentDisposition := resp.Header.Get("Content-Disposition")
  76. if len(contentDisposition) == 0 {
  77. return ""
  78. }
  79. re := regexp.MustCompile(`filename=["]*([^"]+)["]*`)
  80. matched := re.FindStringSubmatch(contentDisposition)
  81. if matched == nil || len(matched) == 0 || len(matched[0]) == 0 {
  82. //fmt.Println("######")
  83. return ""
  84. }
  85. return matched[1]
  86. }
  87. // AddBaseUrl 判断传入的 url 是否需要拼接 baseUrl
  88. func AddBaseUrl(baseUrl, url string) string {
  89. if strings.Contains(url, "://") {
  90. return url
  91. }
  92. return fmt.Sprintf("%s%s", baseUrl, url)
  93. }
  94. func GetDebugFolder() (string, error) {
  95. if global_value.DefDebugFolder == "" {
  96. nowProcessRoot, _ := os.Getwd()
  97. nowProcessRoot = filepath.Join(nowProcessRoot, common.DebugFolder)
  98. err := os.MkdirAll(nowProcessRoot, os.ModePerm)
  99. if err != nil {
  100. return "", err
  101. }
  102. global_value.DefDebugFolder = nowProcessRoot
  103. return nowProcessRoot, err
  104. }
  105. return global_value.DefDebugFolder, nil
  106. }
  107. // GetRootTmpFolder 获取缓存的根目录,每一个视频的缓存将在其中额外新建子集文件夹
  108. func GetRootTmpFolder() (string, error) {
  109. if global_value.DefTmpFolder == "" {
  110. nowProcessRoot, _ := os.Getwd()
  111. nowProcessRoot = filepath.Join(nowProcessRoot, common.TmpFolder)
  112. err := os.MkdirAll(nowProcessRoot, os.ModePerm)
  113. if err != nil {
  114. return "", err
  115. }
  116. global_value.DefTmpFolder = nowProcessRoot
  117. return nowProcessRoot, err
  118. }
  119. return global_value.DefTmpFolder, nil
  120. }
  121. // ClearRootTmpFolder 清理缓存的根目录,将里面的子文件夹一并清理
  122. func ClearRootTmpFolder() error {
  123. nowTmpFolder, err := GetRootTmpFolder()
  124. if err != nil {
  125. return err
  126. }
  127. pathSep := string(os.PathSeparator)
  128. files, err := ioutil.ReadDir(nowTmpFolder)
  129. if err != nil {
  130. return err
  131. }
  132. for _, curFile := range files {
  133. fullPath := nowTmpFolder + pathSep + curFile.Name()
  134. if curFile.IsDir() {
  135. err = os.RemoveAll(fullPath)
  136. if err != nil {
  137. return err
  138. }
  139. } else {
  140. // 这里就是文件了
  141. err = os.Remove(fullPath)
  142. if err != nil {
  143. return err
  144. }
  145. }
  146. }
  147. return nil
  148. }
  149. // GetTmpFolder 获取缓存的文件夹,没有则新建
  150. func GetTmpFolder(folderName string) (string, error) {
  151. rootPath, err := GetRootTmpFolder()
  152. if err != nil {
  153. return "", err
  154. }
  155. tmpFolderFullPath := filepath.Join(rootPath, folderName)
  156. err = os.MkdirAll(tmpFolderFullPath, os.ModePerm)
  157. if err != nil {
  158. return "", err
  159. }
  160. return tmpFolderFullPath, nil
  161. }
  162. // ClearFolder 清空文件夹
  163. func ClearFolder(folderName string) error {
  164. pathSep := string(os.PathSeparator)
  165. files, err := ioutil.ReadDir(folderName)
  166. if err != nil {
  167. return err
  168. }
  169. for _, curFile := range files {
  170. fullPath := folderName + pathSep + curFile.Name()
  171. if curFile.IsDir() {
  172. err = os.RemoveAll(fullPath)
  173. if err != nil {
  174. return err
  175. }
  176. } else {
  177. // 这里就是文件了
  178. err = os.Remove(fullPath)
  179. if err != nil {
  180. return err
  181. }
  182. }
  183. }
  184. return nil
  185. }
  186. // ClearTmpFolder 清理指定的缓存文件夹
  187. func ClearTmpFolder(folderName string) error {
  188. nowTmpFolder, err := GetTmpFolder(folderName)
  189. if err != nil {
  190. return err
  191. }
  192. return ClearFolder(nowTmpFolder)
  193. }
  194. // IsDir 存在且是文件夹
  195. func IsDir(path string) bool {
  196. s, err := os.Stat(path)
  197. if err != nil {
  198. return false
  199. }
  200. return s.IsDir()
  201. }
  202. // IsFile 存在且是文件
  203. func IsFile(filePath string) bool {
  204. s, err := os.Stat(filePath)
  205. if err != nil {
  206. return false
  207. }
  208. return !s.IsDir()
  209. }
  210. // VideoNameSearchKeywordMaker 拼接视频搜索的 title 和 年份
  211. func VideoNameSearchKeywordMaker(title string, year string) string {
  212. iYear, err := strconv.Atoi(year)
  213. if err != nil {
  214. // 允许的错误
  215. log_helper.GetLogger().Errorln("VideoNameSearchKeywordMaker", "year to int", err)
  216. iYear = 0
  217. }
  218. searchKeyword := title
  219. if iYear >= 2020 {
  220. searchKeyword = searchKeyword + " " + year
  221. }
  222. return searchKeyword
  223. }
  224. // SearchMatchedVideoFile 搜索符合后缀名的视频文件
  225. func SearchMatchedVideoFile(dir string) ([]string, error) {
  226. var fileFullPathList = make([]string, 0)
  227. pathSep := string(os.PathSeparator)
  228. files, err := ioutil.ReadDir(dir)
  229. if err != nil {
  230. return nil, err
  231. }
  232. for _, curFile := range files {
  233. fullPath := dir + pathSep + curFile.Name()
  234. if curFile.IsDir() {
  235. // 内层的错误就无视了
  236. oneList, _ := SearchMatchedVideoFile(fullPath)
  237. if oneList != nil {
  238. fileFullPathList = append(fileFullPathList, oneList...)
  239. }
  240. } else {
  241. // 这里就是文件了
  242. if IsWantedVideoExtDef(curFile.Name()) == true {
  243. fileFullPathList = append(fileFullPathList, fullPath)
  244. }
  245. }
  246. }
  247. return fileFullPathList, nil
  248. }
  249. // IsWantedVideoExtDef 后缀名是否符合规则
  250. func IsWantedVideoExtDef(fileName string) bool {
  251. if len(global_value.WantedExtMap) < 1 {
  252. global_value.DefExtMap[common.VideoExtMp4] = common.VideoExtMp4
  253. global_value.DefExtMap[common.VideoExtMkv] = common.VideoExtMkv
  254. global_value.DefExtMap[common.VideoExtRmvb] = common.VideoExtRmvb
  255. global_value.DefExtMap[common.VideoExtIso] = common.VideoExtIso
  256. global_value.WantedExtMap[common.VideoExtMp4] = common.VideoExtMp4
  257. global_value.WantedExtMap[common.VideoExtMkv] = common.VideoExtMkv
  258. global_value.WantedExtMap[common.VideoExtRmvb] = common.VideoExtRmvb
  259. global_value.WantedExtMap[common.VideoExtIso] = common.VideoExtIso
  260. for _, videoExt := range global_value.CustomVideoExts {
  261. global_value.WantedExtMap[videoExt] = videoExt
  262. }
  263. }
  264. fileExt := strings.ToLower(filepath.Ext(fileName))
  265. _, bFound := global_value.WantedExtMap[fileExt]
  266. return bFound
  267. }
  268. func GetEpisodeKeyName(season, eps int) string {
  269. return "S" + strconv.Itoa(season) + "E" + strconv.Itoa(eps)
  270. }
  271. // CopyFile copies a single file from src to dst
  272. func CopyFile(src, dst string) error {
  273. var err error
  274. var srcfd *os.File
  275. var dstfd *os.File
  276. var srcinfo os.FileInfo
  277. if srcfd, err = os.Open(src); err != nil {
  278. return err
  279. }
  280. defer srcfd.Close()
  281. if dstfd, err = os.Create(dst); err != nil {
  282. return err
  283. }
  284. defer dstfd.Close()
  285. if _, err = io.Copy(dstfd, srcfd); err != nil {
  286. return err
  287. }
  288. if srcinfo, err = os.Stat(src); err != nil {
  289. return err
  290. }
  291. return os.Chmod(dst, srcinfo.Mode())
  292. }
  293. // CopyDir copies a whole directory recursively
  294. func CopyDir(src string, dst string) error {
  295. var err error
  296. var fds []os.FileInfo
  297. var srcinfo os.FileInfo
  298. if srcinfo, err = os.Stat(src); err != nil {
  299. return err
  300. }
  301. if err = os.MkdirAll(dst, srcinfo.Mode()); err != nil {
  302. return err
  303. }
  304. if fds, err = ioutil.ReadDir(src); err != nil {
  305. return err
  306. }
  307. for _, fd := range fds {
  308. srcfp := filepath.Join(src, fd.Name())
  309. dstfp := filepath.Join(dst, fd.Name())
  310. if fd.IsDir() {
  311. if err = CopyDir(srcfp, dstfp); err != nil {
  312. fmt.Println(err)
  313. }
  314. } else {
  315. if err = CopyFile(srcfp, dstfp); err != nil {
  316. fmt.Println(err)
  317. }
  318. }
  319. }
  320. return nil
  321. }
  322. // CopyTestData 单元测试前把测试的数据 copy 一份出来操作,src 目录中默认应该有一个 org 原始数据文件夹,然后需要复制一份 test 文件夹出来
  323. func CopyTestData(srcDir string) (string, error) {
  324. // 测试数据的文件夹
  325. orgDir := filepath.Join(srcDir, "org")
  326. testDir := filepath.Join(srcDir, "test")
  327. if IsDir(testDir) == true {
  328. err := ClearFolder(testDir)
  329. if err != nil {
  330. return "", err
  331. }
  332. }
  333. err := CopyDir(orgDir, testDir)
  334. if err != nil {
  335. return "", err
  336. }
  337. return testDir, nil
  338. }
  339. // CloseChrome 强行结束没有关闭的 Chrome 进程
  340. func CloseChrome() {
  341. cmdString := ""
  342. var command *exec.Cmd
  343. sysType := runtime.GOOS
  344. if sysType == "linux" {
  345. // LINUX系统
  346. cmdString = "pkill chrome"
  347. command = exec.Command("/bin/sh", "-c", cmdString)
  348. }
  349. if sysType == "windows" {
  350. // windows系统
  351. cmdString = "taskkill /F /im notepad.exe"
  352. command = exec.Command("cmd.exe", "/c", cmdString)
  353. }
  354. if cmdString == "" || command == nil {
  355. log_helper.GetLogger().Errorln("CloseChrome OS:", sysType)
  356. return
  357. }
  358. err := command.Run()
  359. if err != nil {
  360. log_helper.GetLogger().Errorln("CloseChrome", err)
  361. }
  362. }
  363. // OSCheck 强制的系统支持检查
  364. func OSCheck() bool {
  365. sysType := runtime.GOOS
  366. if sysType == "linux" {
  367. return true
  368. }
  369. if sysType == "windows" {
  370. return true
  371. }
  372. return false
  373. }
  374. // FixWindowPathBackSlash 修复 Windows 反斜杠的梗
  375. func FixWindowPathBackSlash(path string) string {
  376. return strings.Replace(path, string(filepath.Separator), "/", -1)
  377. }
  378. func WriteStrings2File(desfilePath string, strings []string) error {
  379. dstFile, err := os.Create(desfilePath)
  380. if err != nil {
  381. return err
  382. }
  383. defer func() {
  384. _ = dstFile.Close()
  385. }()
  386. allString := ""
  387. for _, s := range strings {
  388. allString += s + "\r\n"
  389. }
  390. _, err = dstFile.WriteString(allString)
  391. if err != nil {
  392. return err
  393. }
  394. return nil
  395. }