| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445 | 
							- package pkg
 
- import (
 
- 	"fmt"
 
- 	"github.com/allanpk716/ChineseSubFinder/internal/common"
 
- 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/global_value"
 
- 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/log_helper"
 
- 	"github.com/allanpk716/ChineseSubFinder/internal/types"
 
- 	browser "github.com/allanpk716/fake-useragent"
 
- 	"github.com/go-resty/resty/v2"
 
- 	"io"
 
- 	"io/ioutil"
 
- 	"net/http"
 
- 	"os"
 
- 	"os/exec"
 
- 	"path/filepath"
 
- 	"regexp"
 
- 	"runtime"
 
- 	"strconv"
 
- 	"strings"
 
- )
 
- // NewHttpClient 新建一个 resty 的对象
 
- func NewHttpClient(_reqParam ...types.ReqParam) *resty.Client {
 
- 	//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"
 
- 	//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"
 
- 	// 随机的 Browser
 
- 	defUserAgent := browser.Random()
 
- 	var reqParam types.ReqParam
 
- 	var HttpProxy, UserAgent, Referer string
 
- 	if len(_reqParam) > 0 {
 
- 		reqParam = _reqParam[0]
 
- 	}
 
- 	if len(reqParam.HttpProxy) > 0 {
 
- 		HttpProxy = reqParam.HttpProxy
 
- 	}
 
- 	if len(reqParam.UserAgent) > 0 {
 
- 		UserAgent = reqParam.UserAgent
 
- 	} else {
 
- 		UserAgent = defUserAgent
 
- 	}
 
- 	if len(reqParam.Referer) > 0 {
 
- 		Referer = reqParam.Referer
 
- 	}
 
- 	httpClient := resty.New()
 
- 	httpClient.SetTimeout(common.HTMLTimeOut)
 
- 	if HttpProxy != "" {
 
- 		httpClient.SetProxy(HttpProxy)
 
- 	} else {
 
- 		httpClient.RemoveProxy()
 
- 	}
 
- 	httpClient.SetHeaders(map[string]string{
 
- 		"Content-Type": "application/json",
 
- 		"User-Agent":   UserAgent,
 
- 	})
 
- 	if len(Referer) > 0 {
 
- 		httpClient.SetHeader("Referer", Referer)
 
- 	}
 
- 	return httpClient
 
- }
 
- // DownFile 从指定的 url 下载文件
 
- func DownFile(urlStr string, _reqParam ...types.ReqParam) ([]byte, string, error) {
 
- 	var reqParam types.ReqParam
 
- 	if len(_reqParam) > 0 {
 
- 		reqParam = _reqParam[0]
 
- 	}
 
- 	httpClient := NewHttpClient(reqParam)
 
- 	resp, err := httpClient.R().Get(urlStr)
 
- 	if err != nil {
 
- 		return nil, "", err
 
- 	}
 
- 	filename := GetFileName(resp.RawResponse)
 
- 	return resp.Body(), filename, nil
 
- }
 
- // GetFileName 获取下载文件的文件名
 
- func GetFileName(resp *http.Response) string {
 
- 	contentDisposition := resp.Header.Get("Content-Disposition")
 
- 	if len(contentDisposition) == 0 {
 
- 		return ""
 
- 	}
 
- 	re := regexp.MustCompile(`filename=["]*([^"]+)["]*`)
 
- 	matched := re.FindStringSubmatch(contentDisposition)
 
- 	if matched == nil || len(matched) == 0 || len(matched[0]) == 0 {
 
- 		//fmt.Println("######")
 
- 		return ""
 
- 	}
 
- 	return matched[1]
 
- }
 
- // AddBaseUrl 判断传入的 url 是否需要拼接 baseUrl
 
- func AddBaseUrl(baseUrl, url string) string {
 
- 	if strings.Contains(url, "://") {
 
- 		return url
 
- 	}
 
- 	return fmt.Sprintf("%s%s", baseUrl, url)
 
- }
 
- func GetDebugFolder() (string, error) {
 
- 	if global_value.DefDebugFolder == "" {
 
- 		nowProcessRoot, _ := os.Getwd()
 
- 		nowProcessRoot = filepath.Join(nowProcessRoot, common.DebugFolder)
 
- 		err := os.MkdirAll(nowProcessRoot, os.ModePerm)
 
- 		if err != nil {
 
- 			return "", err
 
- 		}
 
- 		global_value.DefDebugFolder = nowProcessRoot
 
- 		return nowProcessRoot, err
 
- 	}
 
- 	return global_value.DefDebugFolder, nil
 
- }
 
- // GetRootTmpFolder 获取缓存的根目录,每一个视频的缓存将在其中额外新建子集文件夹
 
- func GetRootTmpFolder() (string, error) {
 
- 	if global_value.DefTmpFolder == "" {
 
- 		nowProcessRoot, _ := os.Getwd()
 
- 		nowProcessRoot = filepath.Join(nowProcessRoot, common.TmpFolder)
 
- 		err := os.MkdirAll(nowProcessRoot, os.ModePerm)
 
- 		if err != nil {
 
- 			return "", err
 
- 		}
 
- 		global_value.DefTmpFolder = nowProcessRoot
 
- 		return nowProcessRoot, err
 
- 	}
 
- 	return global_value.DefTmpFolder, nil
 
- }
 
- // ClearRootTmpFolder 清理缓存的根目录,将里面的子文件夹一并清理
 
- func ClearRootTmpFolder() error {
 
- 	nowTmpFolder, err := GetRootTmpFolder()
 
- 	if err != nil {
 
- 		return err
 
- 	}
 
- 	pathSep := string(os.PathSeparator)
 
- 	files, err := ioutil.ReadDir(nowTmpFolder)
 
- 	if err != nil {
 
- 		return err
 
- 	}
 
- 	for _, curFile := range files {
 
- 		fullPath := nowTmpFolder + pathSep + curFile.Name()
 
- 		if curFile.IsDir() {
 
- 			err = os.RemoveAll(fullPath)
 
- 			if err != nil {
 
- 				return err
 
- 			}
 
- 		} else {
 
- 			// 这里就是文件了
 
- 			err = os.Remove(fullPath)
 
- 			if err != nil {
 
- 				return err
 
- 			}
 
- 		}
 
- 	}
 
- 	return nil
 
- }
 
- // GetTmpFolder 获取缓存的文件夹,没有则新建
 
- func GetTmpFolder(folderName string) (string, error) {
 
- 	rootPath, err := GetRootTmpFolder()
 
- 	if err != nil {
 
- 		return "", err
 
- 	}
 
- 	tmpFolderFullPath := filepath.Join(rootPath, folderName)
 
- 	err = os.MkdirAll(tmpFolderFullPath, os.ModePerm)
 
- 	if err != nil {
 
- 		return "", err
 
- 	}
 
- 	return tmpFolderFullPath, nil
 
- }
 
- // ClearFolder 清空文件夹
 
- func ClearFolder(folderName string) error {
 
- 	pathSep := string(os.PathSeparator)
 
- 	files, err := ioutil.ReadDir(folderName)
 
- 	if err != nil {
 
- 		return err
 
- 	}
 
- 	for _, curFile := range files {
 
- 		fullPath := folderName + pathSep + curFile.Name()
 
- 		if curFile.IsDir() {
 
- 			err = os.RemoveAll(fullPath)
 
- 			if err != nil {
 
- 				return err
 
- 			}
 
- 		} else {
 
- 			// 这里就是文件了
 
- 			err = os.Remove(fullPath)
 
- 			if err != nil {
 
- 				return err
 
- 			}
 
- 		}
 
- 	}
 
- 	return nil
 
- }
 
- // ClearTmpFolder 清理指定的缓存文件夹
 
- func ClearTmpFolder(folderName string) error {
 
- 	nowTmpFolder, err := GetTmpFolder(folderName)
 
- 	if err != nil {
 
- 		return err
 
- 	}
 
- 	return ClearFolder(nowTmpFolder)
 
- }
 
- // IsDir 存在且是文件夹
 
- func IsDir(path string) bool {
 
- 	s, err := os.Stat(path)
 
- 	if err != nil {
 
- 		return false
 
- 	}
 
- 	return s.IsDir()
 
- }
 
- // IsFile 存在且是文件
 
- func IsFile(filePath string) bool {
 
- 	s, err := os.Stat(filePath)
 
- 	if err != nil {
 
- 		return false
 
- 	}
 
- 	return !s.IsDir()
 
- }
 
- // VideoNameSearchKeywordMaker 拼接视频搜索的 title 和 年份
 
- func VideoNameSearchKeywordMaker(title string, year string) string {
 
- 	iYear, err := strconv.Atoi(year)
 
- 	if err != nil {
 
- 		// 允许的错误
 
- 		log_helper.GetLogger().Errorln("VideoNameSearchKeywordMaker", "year to int", err)
 
- 		iYear = 0
 
- 	}
 
- 	searchKeyword := title
 
- 	if iYear >= 2020 {
 
- 		searchKeyword = searchKeyword + " " + year
 
- 	}
 
- 	return searchKeyword
 
- }
 
- // SearchMatchedVideoFile 搜索符合后缀名的视频文件
 
- func SearchMatchedVideoFile(dir string) ([]string, error) {
 
- 	var fileFullPathList = make([]string, 0)
 
- 	pathSep := string(os.PathSeparator)
 
- 	files, err := ioutil.ReadDir(dir)
 
- 	if err != nil {
 
- 		return nil, err
 
- 	}
 
- 	for _, curFile := range files {
 
- 		fullPath := dir + pathSep + curFile.Name()
 
- 		if curFile.IsDir() {
 
- 			// 内层的错误就无视了
 
- 			oneList, _ := SearchMatchedVideoFile(fullPath)
 
- 			if oneList != nil {
 
- 				fileFullPathList = append(fileFullPathList, oneList...)
 
- 			}
 
- 		} else {
 
- 			// 这里就是文件了
 
- 			if IsWantedVideoExtDef(curFile.Name()) == true {
 
- 				fileFullPathList = append(fileFullPathList, fullPath)
 
- 			}
 
- 		}
 
- 	}
 
- 	return fileFullPathList, nil
 
- }
 
- // IsWantedVideoExtDef 后缀名是否符合规则
 
- func IsWantedVideoExtDef(fileName string) bool {
 
- 	if len(global_value.WantedExtMap) < 1 {
 
- 		global_value.DefExtMap[common.VideoExtMp4] = common.VideoExtMp4
 
- 		global_value.DefExtMap[common.VideoExtMkv] = common.VideoExtMkv
 
- 		global_value.DefExtMap[common.VideoExtRmvb] = common.VideoExtRmvb
 
- 		global_value.DefExtMap[common.VideoExtIso] = common.VideoExtIso
 
- 		global_value.WantedExtMap[common.VideoExtMp4] = common.VideoExtMp4
 
- 		global_value.WantedExtMap[common.VideoExtMkv] = common.VideoExtMkv
 
- 		global_value.WantedExtMap[common.VideoExtRmvb] = common.VideoExtRmvb
 
- 		global_value.WantedExtMap[common.VideoExtIso] = common.VideoExtIso
 
- 		for _, videoExt := range global_value.CustomVideoExts {
 
- 			global_value.WantedExtMap[videoExt] = videoExt
 
- 		}
 
- 	}
 
- 	fileExt := strings.ToLower(filepath.Ext(fileName))
 
- 	_, bFound := global_value.WantedExtMap[fileExt]
 
- 	return bFound
 
- }
 
- func GetEpisodeKeyName(season, eps int) string {
 
- 	return "S" + strconv.Itoa(season) + "E" + strconv.Itoa(eps)
 
- }
 
- // CopyFile copies a single file from src to dst
 
- func CopyFile(src, dst string) error {
 
- 	var err error
 
- 	var srcfd *os.File
 
- 	var dstfd *os.File
 
- 	var srcinfo os.FileInfo
 
- 	if srcfd, err = os.Open(src); err != nil {
 
- 		return err
 
- 	}
 
- 	defer srcfd.Close()
 
- 	if dstfd, err = os.Create(dst); err != nil {
 
- 		return err
 
- 	}
 
- 	defer dstfd.Close()
 
- 	if _, err = io.Copy(dstfd, srcfd); err != nil {
 
- 		return err
 
- 	}
 
- 	if srcinfo, err = os.Stat(src); err != nil {
 
- 		return err
 
- 	}
 
- 	return os.Chmod(dst, srcinfo.Mode())
 
- }
 
- // CopyDir copies a whole directory recursively
 
- func CopyDir(src string, dst string) error {
 
- 	var err error
 
- 	var fds []os.FileInfo
 
- 	var srcinfo os.FileInfo
 
- 	if srcinfo, err = os.Stat(src); err != nil {
 
- 		return err
 
- 	}
 
- 	if err = os.MkdirAll(dst, srcinfo.Mode()); err != nil {
 
- 		return err
 
- 	}
 
- 	if fds, err = ioutil.ReadDir(src); err != nil {
 
- 		return err
 
- 	}
 
- 	for _, fd := range fds {
 
- 		srcfp := filepath.Join(src, fd.Name())
 
- 		dstfp := filepath.Join(dst, fd.Name())
 
- 		if fd.IsDir() {
 
- 			if err = CopyDir(srcfp, dstfp); err != nil {
 
- 				fmt.Println(err)
 
- 			}
 
- 		} else {
 
- 			if err = CopyFile(srcfp, dstfp); err != nil {
 
- 				fmt.Println(err)
 
- 			}
 
- 		}
 
- 	}
 
- 	return nil
 
- }
 
- // CopyTestData 单元测试前把测试的数据 copy 一份出来操作,src 目录中默认应该有一个 org 原始数据文件夹,然后需要复制一份 test 文件夹出来
 
- func CopyTestData(srcDir string) (string, error) {
 
- 	// 测试数据的文件夹
 
- 	orgDir := filepath.Join(srcDir, "org")
 
- 	testDir := filepath.Join(srcDir, "test")
 
- 	if IsDir(testDir) == true {
 
- 		err := ClearFolder(testDir)
 
- 		if err != nil {
 
- 			return "", err
 
- 		}
 
- 	}
 
- 	err := CopyDir(orgDir, testDir)
 
- 	if err != nil {
 
- 		return "", err
 
- 	}
 
- 	return testDir, nil
 
- }
 
- // CloseChrome 强行结束没有关闭的 Chrome 进程
 
- func CloseChrome() {
 
- 	cmdString := ""
 
- 	var command *exec.Cmd
 
- 	sysType := runtime.GOOS
 
- 	if sysType == "linux" {
 
- 		// LINUX系统
 
- 		cmdString = "pkill chrome"
 
- 		command = exec.Command("/bin/sh", "-c", cmdString)
 
- 	}
 
- 	if sysType == "windows" {
 
- 		// windows系统
 
- 		cmdString = "taskkill /F /im notepad.exe"
 
- 		command = exec.Command("cmd.exe", "/c", cmdString)
 
- 	}
 
- 	if cmdString == "" || command == nil {
 
- 		log_helper.GetLogger().Errorln("CloseChrome OS:", sysType)
 
- 		return
 
- 	}
 
- 	err := command.Run()
 
- 	if err != nil {
 
- 		log_helper.GetLogger().Errorln("CloseChrome", err)
 
- 	}
 
- }
 
- // OSCheck 强制的系统支持检查
 
- func OSCheck() bool {
 
- 	sysType := runtime.GOOS
 
- 	if sysType == "linux" {
 
- 		return true
 
- 	}
 
- 	if sysType == "windows" {
 
- 		return true
 
- 	}
 
- 	return false
 
- }
 
- // FixWindowPathBackSlash 修复 Windows 反斜杠的梗
 
- func FixWindowPathBackSlash(path string) string {
 
- 	return strings.Replace(path, string(filepath.Separator), "/", -1)
 
- }
 
- func WriteStrings2File(desfilePath string, strings []string) error {
 
- 	dstFile, err := os.Create(desfilePath)
 
- 	if err != nil {
 
- 		return err
 
- 	}
 
- 	defer func() {
 
- 		_ = dstFile.Close()
 
- 	}()
 
- 	allString := ""
 
- 	for _, s := range strings {
 
- 		allString += s + "\r\n"
 
- 	}
 
- 	_, err = dstFile.WriteString(allString)
 
- 	if err != nil {
 
- 		return err
 
- 	}
 
- 	return nil
 
- }
 
 
  |