util.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. package pkg
  2. import (
  3. "fmt"
  4. browser "github.com/EDDYCJY/fake-useragent"
  5. "github.com/allanpk716/ChineseSubFinder/internal/common"
  6. "github.com/allanpk716/ChineseSubFinder/internal/pkg/log_helper"
  7. "github.com/allanpk716/ChineseSubFinder/internal/pkg/rod_helper"
  8. "github.com/allanpk716/ChineseSubFinder/internal/types"
  9. "github.com/go-resty/resty/v2"
  10. "io"
  11. "io/ioutil"
  12. "net/http"
  13. "os"
  14. "path"
  15. "path/filepath"
  16. "regexp"
  17. "strconv"
  18. "strings"
  19. "time"
  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 defDebugFolder == "" {
  96. nowProcessRoot, _ := os.Getwd()
  97. nowProcessRoot = path.Join(nowProcessRoot, common.DebugFolder)
  98. err := os.MkdirAll(nowProcessRoot, os.ModePerm)
  99. if err != nil {
  100. return "", err
  101. }
  102. defDebugFolder = nowProcessRoot
  103. return nowProcessRoot, err
  104. }
  105. return defDebugFolder, nil
  106. }
  107. // GetRootTmpFolder 获取缓存的根目录,每一个视频的缓存将在其中额外新建子集文件夹
  108. func GetRootTmpFolder() (string, error) {
  109. if defTmpFolder == "" {
  110. nowProcessRoot, _ := os.Getwd()
  111. nowProcessRoot = path.Join(nowProcessRoot, common.TmpFolder)
  112. err := os.MkdirAll(nowProcessRoot, os.ModePerm)
  113. if err != nil {
  114. return "", err
  115. }
  116. defTmpFolder = nowProcessRoot
  117. return nowProcessRoot, err
  118. }
  119. return 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 := path.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. // TODO 强制使用固定的视频后缀名匹配列表,后续有需求再考虑额实现外部可配置的列表
  252. if len(wantedExtList) < 1 {
  253. defExtList = append(defExtList, common.VideoExtMp4)
  254. defExtList = append(defExtList, common.VideoExtMkv)
  255. defExtList = append(defExtList, common.VideoExtRmvb)
  256. defExtList = append(defExtList, common.VideoExtIso)
  257. wantedExtList = append(defExtList, common.VideoExtMp4)
  258. wantedExtList = append(defExtList, common.VideoExtMkv)
  259. wantedExtList = append(defExtList, common.VideoExtRmvb)
  260. wantedExtList = append(defExtList, common.VideoExtIso)
  261. }
  262. fileName = strings.ToLower(filepath.Ext(fileName))
  263. for _, s := range wantedExtList {
  264. if s == fileName {
  265. return true
  266. }
  267. }
  268. return false
  269. }
  270. func GetEpisodeKeyName(season, eps int) string {
  271. return "S" + strconv.Itoa(season) + "E" + strconv.Itoa(eps)
  272. }
  273. // ReloadBrowser 提前把浏览器下载好
  274. func ReloadBrowser() {
  275. page, err := rod_helper.NewBrowserLoadPage("https://www.baidu.com", "", 300*time.Second, 2)
  276. if err != nil {
  277. return
  278. }
  279. defer page.Close()
  280. }
  281. // CopyFile copies a single file from src to dst
  282. func CopyFile(src, dst string) error {
  283. var err error
  284. var srcfd *os.File
  285. var dstfd *os.File
  286. var srcinfo os.FileInfo
  287. if srcfd, err = os.Open(src); err != nil {
  288. return err
  289. }
  290. defer srcfd.Close()
  291. if dstfd, err = os.Create(dst); err != nil {
  292. return err
  293. }
  294. defer dstfd.Close()
  295. if _, err = io.Copy(dstfd, srcfd); err != nil {
  296. return err
  297. }
  298. if srcinfo, err = os.Stat(src); err != nil {
  299. return err
  300. }
  301. return os.Chmod(dst, srcinfo.Mode())
  302. }
  303. // CopyDir copies a whole directory recursively
  304. func CopyDir(src string, dst string) error {
  305. var err error
  306. var fds []os.FileInfo
  307. var srcinfo os.FileInfo
  308. if srcinfo, err = os.Stat(src); err != nil {
  309. return err
  310. }
  311. if err = os.MkdirAll(dst, srcinfo.Mode()); err != nil {
  312. return err
  313. }
  314. if fds, err = ioutil.ReadDir(src); err != nil {
  315. return err
  316. }
  317. for _, fd := range fds {
  318. srcfp := path.Join(src, fd.Name())
  319. dstfp := path.Join(dst, fd.Name())
  320. if fd.IsDir() {
  321. if err = CopyDir(srcfp, dstfp); err != nil {
  322. fmt.Println(err)
  323. }
  324. } else {
  325. if err = CopyFile(srcfp, dstfp); err != nil {
  326. fmt.Println(err)
  327. }
  328. }
  329. }
  330. return nil
  331. }
  332. // CopyTestData 单元测试前把测试的数据 copy 一份出来操作,src 目录中默认应该有一个 org 原始数据文件夹,然后需要复制一份 test 文件夹出来
  333. func CopyTestData(srcDir string) (string, error) {
  334. // 测试数据的文件夹
  335. orgDir := path.Join(srcDir, "org")
  336. testDir := path.Join(srcDir, "test")
  337. if IsDir(testDir) == true {
  338. err := ClearFolder(testDir)
  339. if err != nil {
  340. return "", err
  341. }
  342. }
  343. err := CopyDir(orgDir, testDir)
  344. if err != nil {
  345. return "", err
  346. }
  347. return testDir, nil
  348. }