Browse Source

有待继续实现

Signed-off-by: allan716 <[email protected]>
allan716 4 years ago
parent
commit
eccb15e2d0

+ 2 - 0
common/util.go

@@ -88,6 +88,8 @@ func AddBaseUrl(baseUrl, url string) string {
 
 // ReqParam 可选择传入的参数
 type ReqParam struct {
+	UserExtList []string	// 用户确认的视频后缀名支持列表
+	DebugMode bool			// 调试标志位
 	HttpProxy string		// HttpClient 相关
 	UserAgent string		// HttpClient 相关
 	Referer   string		// HttpClient 相关

+ 171 - 0
downloader.go

@@ -0,0 +1,171 @@
+package ChineseSubFinder
+
+import (
+	"github.com/allanpk716/ChineseSubFinder/common"
+	"github.com/allanpk716/ChineseSubFinder/sub_supplier"
+	"github.com/allanpk716/ChineseSubFinder/sub_supplier/shooter"
+	"github.com/allanpk716/ChineseSubFinder/sub_supplier/subhd"
+	"github.com/allanpk716/ChineseSubFinder/sub_supplier/xunlei"
+	"github.com/allanpk716/ChineseSubFinder/sub_supplier/zimuku"
+	"github.com/go-rod/rod/lib/utils"
+	"io/ioutil"
+	"os"
+	"path"
+	"path/filepath"
+	"strconv"
+	"strings"
+	"sync"
+)
+
+type Downloader struct {
+	reqParam common.ReqParam
+	topic int					// 最多能够下载 Top 几的字幕,每一个网站
+	wantedExtList []string		// 人工确认的需要监控的视频后缀名
+	defExtList []string			// 内置支持的视频后缀名列表
+}
+
+func NewDownloader(_reqParam ... common.ReqParam) *Downloader {
+
+	var downloader Downloader
+	downloader.topic = common.DownloadSubsPerSite
+	if len(_reqParam) > 0 {
+		downloader.reqParam = _reqParam[0]
+		if downloader.reqParam.Topic > 0 && downloader.reqParam.Topic != downloader.topic {
+			downloader.topic = downloader.reqParam.Topic
+		}
+	}
+	downloader.defExtList = make([]string, 0)
+	downloader.defExtList = append(downloader.defExtList, VideoExtMp4)
+	downloader.defExtList = append(downloader.defExtList, VideoExtMkv)
+	downloader.defExtList = append(downloader.defExtList, VideoExtRmvb)
+	downloader.defExtList = append(downloader.defExtList, VideoExtIso)
+
+	if len(_reqParam) > 0 {
+		// 如果用户设置了关注的视频后缀名列表,则用ta的
+		if len(downloader.reqParam.UserExtList) > 0 {
+			downloader.wantedExtList = downloader.reqParam.UserExtList
+		} else {
+			// 不然就是内置默认的
+			downloader.wantedExtList = downloader.defExtList
+		}
+	} else {
+		// 不然就是内置默认的
+		downloader.wantedExtList = downloader.defExtList
+	}
+	return &downloader
+}
+
+func (d Downloader) GetNowSupportExtList() []string {
+	return d.wantedExtList
+}
+
+func (d Downloader) GetDefSupportExtList() []string {
+	return d.defExtList
+}
+
+func (d Downloader) DownloadSub(dir string) error {
+	nowVideoList, err := d.searchFile(dir)
+	if err != nil {
+		return err
+	}
+	// 构建每个字幕站点下载者的实例
+	var suppliers = make([]sub_supplier.ISupplier, 0)
+	suppliers = append(suppliers, shooter.NewSupplier(d.reqParam))
+	suppliers = append(suppliers, subhd.NewSupplier(d.reqParam))
+	suppliers = append(suppliers, xunlei.NewSupplier(d.reqParam))
+	suppliers = append(suppliers, zimuku.NewSupplier(d.reqParam))
+	// TODO 后续再改为每个视频以上的流程都是一个 channel 来做,并且需要控制在一个并发量之下(很可能没必要,毕竟要在弱鸡机器上挂机用的)
+	// 一个视频文件同时多个站点查询,阻塞完毕后,在进行下一个
+	for i, oneVideoFullPath := range nowVideoList {
+		ontVideoRootPath := filepath.Base(oneVideoFullPath)
+		// 同时进行查询
+		wg := sync.WaitGroup{}
+		wg.Add(len(suppliers))
+		println("DlSub Start", oneVideoFullPath)
+		for _, supplier := range suppliers {
+			println(i, supplier.GetSupplierName(), "Start...")
+			subInfos, err := supplier.GetSubListFromFile(oneVideoFullPath)
+			if err != nil {
+				println(supplier.GetSupplierName(), "GetSubListFromFile", err.Error())
+				wg.Done()
+				continue
+			}
+
+			if d.reqParam.DebugMode == true {
+				// 需要进行字幕文件的缓存
+				// 把缓存的文件夹新建出来
+				desFolderFullPath := path.Join(ontVideoRootPath, SubTmpFolderName)
+				err = os.MkdirAll(desFolderFullPath, os.ModePerm)
+				if err != nil{
+					println(supplier.GetSupplierName(), "MkdirAll", err.Error())
+					wg.Done()
+					continue
+				}
+				for x, info := range subInfos {
+					tmpSubFileName := info.Name
+					if strings.Contains(tmpSubFileName, info.Ext) == false {
+						tmpSubFileName = tmpSubFileName + info.Ext
+					}
+					desSubFileFullPath := path.Join(desFolderFullPath, strconv.Itoa(x) + "_" + tmpSubFileName)
+					err = utils.OutputFile(desSubFileFullPath, info.Data)
+					if err != nil {
+						println(supplier.GetSupplierName(), "WriteSubFile", info.Name, err.Error())
+						continue
+					}
+				}
+			}
+
+			println(supplier.GetSupplierName(), "End...")
+			wg.Done()
+		}
+		println(i, "DlSub End", oneVideoFullPath)
+		wg.Wait()
+	}
+
+	return nil
+}
+
+func (d Downloader)searchFile(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, _ := d.searchFile(fullPath)
+			if oneList != nil {
+				fileFullPathList = append(fileFullPathList, oneList...)
+			}
+		} else {
+			// 这里就是文件了
+			if d.isWantedExtDef(curFile.Name()) == true {
+				fileFullPathList = append(fileFullPathList, fullPath)
+			}
+		}
+	}
+	return fileFullPathList, nil
+}
+
+func (d Downloader) isWantedExtDef(fileName string) bool {
+	fileName = strings.ToLower(filepath.Ext(fileName))
+	for _, s := range d.wantedExtList {
+		if s == fileName {
+			return true
+		}
+	}
+	return false
+}
+
+const (
+	VideoExtMp4 = ".mp4"
+	VideoExtMkv = ".mkv"
+	VideoExtRmvb = ".rmvb"
+	VideoExtIso = ".iso"
+
+	SubTmpFolderName = "subTmp"
+)

+ 37 - 0
downloader_test.go

@@ -0,0 +1,37 @@
+package ChineseSubFinder
+
+import (
+	"github.com/allanpk716/ChineseSubFinder/common"
+	"github.com/allanpk716/ChineseSubFinder/sub_supplier/shooter"
+	"testing"
+)
+
+func TestDownloader_searchFile(t *testing.T) {
+
+	//dirRoot := "X:\\动漫\\EVA"
+	dirRoot := "X:\\电影\\Spiral From the Book of Saw (2021)"
+
+	dl := NewDownloader()
+	files, err := dl.searchFile(dirRoot)
+	if err != nil {
+		t.Fatal(err)
+	}
+	sp := shooter.NewSupplier()
+	for i, file := range files {
+		println(i, file)
+		_, err := sp.ComputeFileHash(file)
+		if err != nil {
+			t.Fatal(err)
+		}
+	}
+}
+
+func TestDownloader_DownloadSub(t *testing.T) {
+	dirRoot := "X:\\电影\\Spiral From the Book of Saw (2021)"
+
+	dl := NewDownloader(common.ReqParam{DebugMode: true})
+	err := dl.DownloadSub(dirRoot)
+	if err != nil {
+		t.Fatal(err)
+	}
+}

+ 3 - 1
sub_supplier/iSupplier.go

@@ -1,6 +1,8 @@
 package sub_supplier
 
-type iSupplier interface {
+type ISupplier interface {
+
+	GetSupplierName() string
 
 	GetSubListFromFile(filePath string) ([]SubInfo, error)
 

+ 6 - 2
sub_supplier/shooter/shooter.go

@@ -29,6 +29,10 @@ func NewSupplier(_reqParam ... common.ReqParam) *Supplier {
 	return &sup
 }
 
+func (s Supplier) GetSupplierName() string {
+	return "shooter"
+}
+
 func (s Supplier) GetSubListFromFile(filePath string) ([]sub_supplier.SubInfo, error) {
 
 	// 可以提供的字幕查询 eng或者chn
@@ -36,7 +40,7 @@ func (s Supplier) GetSubListFromFile(filePath string) ([]sub_supplier.SubInfo, e
 	var outSubInfoList []sub_supplier.SubInfo
 	var jsonList []SublistShooter
 
-	hash, err := s.computeFileHash(filePath)
+	hash, err := s.ComputeFileHash(filePath)
 	if err != nil {
 		return nil, err
 	}
@@ -86,7 +90,7 @@ func (s Supplier) GetSubListFromKeyword(keyword string) ([]sub_supplier.SubInfo,
 	panic("not implemented")
 }
 
-func (s Supplier) computeFileHash(filePath string) (string, error) {
+func (s Supplier) ComputeFileHash(filePath string) (string, error) {
 	hash := ""
 	fp, err := os.Open(filePath)
 	if err != nil {

+ 8 - 0
sub_supplier/subhd/subhd.go

@@ -39,6 +39,10 @@ func NewSupplier(_reqParam ... common.ReqParam) *Supplier {
 	return &sup
 }
 
+func (s Supplier) GetSupplierName() string {
+	return "subhd"
+}
+
 func (s Supplier) GetSubListFromFile(filePath string) ([]sub_supplier.SubInfo, error) {
 	/*
 		虽然是传入视频文件路径,但是其实需要读取对应的视频文件目录下的
@@ -122,7 +126,11 @@ func (s Supplier) Step0(keyword string) (string, error) {
 	if err != nil {
 		return "", err
 	}
+	// 是否有查找到的结果,至少要有结果。根据这里这样下面才能判断是分析失效了,还是就是没有结果而已
+	// TODO 1 更新这个理,注意,调试模式缓存字幕的输出位置错了,变成当前项目了,需要修复
 	re := regexp.MustCompile(`<a\shref="(/d/[\w]+)">\s?<img`)
+	// 这里是确认能继续分析的详细连接
+	re := regexp.MustCompile(`”总共\shref="(/d/[\w]+)">\s?<img`)
 	matched := re.FindAllStringSubmatch(result, -1)
 	if len(matched) < 1 || len(matched[0]) < 2{
 		return "",  common.SubHDStep0HrefIsNull

+ 2 - 1
sub_supplier/subhd/subhd_test.go

@@ -7,7 +7,8 @@ import (
 
 func TestSupplier_GetSubListFromFile(t *testing.T) {
 
-	movie1 := "X:\\电影\\消失爱人 (2016)\\消失爱人 (2016) 720p AAC.rmvb"
+	movie1 := "X:\\电影\\Spiral From the Book of Saw (2021)\\Spiral From the Book of Saw (2021) WEBDL-1080p.mkv"
+	//movie1 := "X:\\电影\\消失爱人 (2016)\\消失爱人 (2016) 720p AAC.rmvb"
 	//movie1 := "X:\\电影\\机动战士Z高达:星之继承者 (2005)\\机动战士Z高达:星之继承者 (2005) 1080p TrueHD.mkv"
 	//movie1 := "X:\\连续剧\\The Bad Batch\\Season 1\\The Bad Batch - S01E01 - Aftermath WEBDL-1080p.mkv"
 	shooter := NewSupplier(common.ReqParam{Topic: 3})

+ 4 - 0
sub_supplier/xunlei/xunlei.go

@@ -28,6 +28,10 @@ func NewSupplier(_reqParam ... common.ReqParam) *Supplier {
 	return &sup
 }
 
+func (s Supplier) GetSupplierName() string {
+	return "xunlei"
+}
+
 func (s Supplier) GetSubListFromFile(filePath string) ([]sub_supplier.SubInfo, error) {
 
 	cid, err := s.getCid(filePath)

+ 4 - 0
sub_supplier/zimuku/zimuku.go

@@ -29,6 +29,10 @@ func NewSupplier(_reqParam ... common.ReqParam) *Supplier {
 	return &sup
 }
 
+func (s Supplier) GetSupplierName() string {
+	return "zimuku"
+}
+
 func (s Supplier) GetSubListFromFile(filePath string) ([]sub_supplier.SubInfo, error) {
 
 	/*