Browse Source

正在添加 zimuku

Signed-off-by: 716 <[email protected]>
716 4 years ago
parent
commit
08e19b7b36

+ 29 - 5
common/decode.go

@@ -2,12 +2,15 @@ package common
 
 import (
 	"github.com/beevik/etree"
+	PTN "github.com/middelink/go-parse-torrent-name"
 	"io/ioutil"
 	"os"
+	"path/filepath"
+	"regexp"
 	"strings"
 )
 
-func get_IMDB_movie_xml(movieFilePath string) (string, error) {
+func getImdbMovieXml(movieFilePath string) (string, error) {
 	doc := etree.NewDocument()
 	if err := doc.ReadFromFile(movieFilePath); err != nil {
 		return "", err
@@ -19,7 +22,7 @@ func get_IMDB_movie_xml(movieFilePath string) (string, error) {
 	return "", CanNotFindIMDBID
 }
 
-func get_IMDB_nfo(nfoFilePath string) (string, error) {
+func getImdbNfo(nfoFilePath string) (string, error) {
 	doc := etree.NewDocument()
 	if err := doc.ReadFromFile(nfoFilePath); err != nil {
 		return "", err
@@ -31,7 +34,7 @@ func get_IMDB_nfo(nfoFilePath string) (string, error) {
 	return "", CanNotFindIMDBID
 }
 
-func Get_IMDB_Id(dirPth string) (string ,error) {
+func GetImdbId(dirPth string) (string ,error) {
 	dir, err := ioutil.ReadDir(dirPth)
 	if err != nil {
 		return "", err
@@ -63,7 +66,7 @@ func Get_IMDB_Id(dirPth string) (string ,error) {
 	}
 
 	if movieFilePath != "" {
-		outId, err := get_IMDB_movie_xml(movieFilePath)
+		outId, err := getImdbMovieXml(movieFilePath)
 		if err != nil {
 			println(err)
 		} else {
@@ -72,7 +75,7 @@ func Get_IMDB_Id(dirPth string) (string ,error) {
 	}
 
 	if nfoFilePath != "" {
-		outId, err := get_IMDB_nfo(nfoFilePath)
+		outId, err := getImdbNfo(nfoFilePath)
 		if err != nil {
 			return "", err
 		} else {
@@ -83,8 +86,29 @@ func Get_IMDB_Id(dirPth string) (string ,error) {
 	return "", CanNotFindIMDBID
 }
 
+//GetVideoInfo 从文件名推断视频文件的信息
+func GetVideoInfo(videoFileName string) (*PTN.TorrentInfo, error) {
+
+	parse, err := PTN.Parse(filepath.Base(videoFileName))
+	if err != nil {
+		return nil, err
+	}
+	compile, err := regexp.Compile(regFixTitle2)
+	if err != nil {
+		return nil, err
+	}
+	match := compile.ReplaceAllString(parse.Title, "")
+	match = strings.TrimRight(match, "")
+	parse.Title = match
+	return parse, nil
+}
+
 const (
 	metadataFileEmby = "movie.xml"
 	suffixNameXml    = ".xml"
 	suffixNameNfo    = ".nfo"
+	// 去除特殊字符,仅仅之有中文
+	regFixTitle = "[^\u4e00-\u9fa5a-zA-Z0-9\\s]"
+	// 去除特殊字符,把特殊字符都写进去
+	regFixTitle2 = "[`~!@#$%^&*()+-=|{}';'\\[\\].<>/?~!@#¥%……&*()——+|{}【】';”“’。、?]"
 )

+ 53 - 10
common/decode_test.go

@@ -1,6 +1,8 @@
 package common
 
-import "testing"
+import (
+	"testing"
+)
 
 func TestGet_IMDB_Id(t *testing.T) {
 	type args struct {
@@ -12,18 +14,18 @@ func TestGet_IMDB_Id(t *testing.T) {
 		want    string
 		wantErr bool
 	}{
-		{name: "have", args: args{dirPth: "Y:\\电影\\Army of the Dead (2021)"}, want: "tt0993840", wantErr: false},
-		{name: "want error", args: args{dirPth: "Y:\\电影\\"}, want: "", wantErr: true},
+		{name: "have", args: args{dirPth: "x:\\电影\\Army of the Dead (2021)"}, want: "tt0993840", wantErr: false},
+		{name: "want error", args: args{dirPth: "x:\\电影\\"}, want: "", wantErr: true},
 	}
 	for _, tt := range tests {
 		t.Run(tt.name, func(t *testing.T) {
-			got, err := Get_IMDB_Id(tt.args.dirPth)
+			got, err := GetImdbId(tt.args.dirPth)
 			if (err != nil) != tt.wantErr {
-				t.Errorf("Get_IMDB_Id() error = %v, wantErr %v", err, tt.wantErr)
+				t.Errorf("GetImdbId() error = %v, wantErr %v", err, tt.wantErr)
 				return
 			}
 			if got != tt.want {
-				t.Errorf("Get_IMDB_Id() got = %v, want %v", got, tt.want)
+				t.Errorf("GetImdbId() got = %v, want %v", got, tt.want)
 			}
 		})
 	}
@@ -31,8 +33,8 @@ func TestGet_IMDB_Id(t *testing.T) {
 
 func Test_get_IMDB_movie_xml(t *testing.T) {
     want := "tt0993840"
-	dirPth := "Y:\\电影\\Army of the Dead (2021)\\movie.xml"
-	got, err := get_IMDB_movie_xml(dirPth)
+	dirPth := "x:\\电影\\Army of the Dead (2021)\\movie.xml"
+	got, err := getImdbMovieXml(dirPth)
 	if err != nil {
 		t.Error(err)
 	}
@@ -43,12 +45,53 @@ func Test_get_IMDB_movie_xml(t *testing.T) {
 
 func Test_get_IMDB_nfo(t *testing.T) {
 	want := "tt0993840"
-	dirPth := "Y:\\电影\\Army of the Dead (2021)\\Army of the Dead (2021) WEBDL-1080p.nfo"
-	got, err := get_IMDB_nfo(dirPth)
+	dirPth := "X:\\电影\\Army of the Dead (2021)\\Army of the Dead (2021) WEBDL-1080p.nfo"
+	got, err := getImdbNfo(dirPth)
 	if err != nil {
 		t.Error(err)
 	}
 	if got != want {
 		t.Errorf("Test_get_IMDB_movie_xml() got = %v, want %v", got, want)
 	}
+}
+
+func Test_VideoInfo(t *testing.T) {
+
+	movieFile1 := "X:\\电影\\Spiral From the Book of Saw (2021)\\Spiral From the Book of Saw (2021) WEBDL-1080p.mkv"
+	movieFile2 := "人之怒 WEBDL-1080p.mkv"
+	movieFile3 := "機動戦士Zガンダム WEBDL-1080p.mkv"
+	movieFile4 := "机动战士Z高达:星之继承者 (2005) 1080p TrueHD.mkv"
+	standard1 := "X:\\连续剧\\The Bad Batch\\Season 1\\The Bad Batch - S01E01 - Aftermath WEBDL-1080p.mkv"
+
+	m1, err := GetVideoInfo(movieFile1)
+	if err != nil {
+		t.Error(err)
+	}
+	println(m1.Title, m1.Year, m1.Quality, m1.Codec, m1.Hardcoded)
+
+	m2, err := GetVideoInfo(movieFile2)
+	if err != nil {
+		t.Error(err)
+	}
+	println(m2.Title, m2.Quality, m2.Codec, m2.Hardcoded)
+
+	m3, err := GetVideoInfo(movieFile3)
+	if err != nil {
+		t.Error(err)
+	}
+	println(m3.Title, m3.Quality, m3.Codec, m3.Hardcoded)
+
+	m4, err := GetVideoInfo(movieFile4)
+	if err != nil {
+		t.Error(err)
+	}
+	println(m4.Title, m4.Quality, m4.Codec, m4.Hardcoded)
+
+	s1, err := GetVideoInfo(standard1)
+	if err != nil {
+		t.Error(err)
+	}
+	println(s1.Title, s1.Season, s1.Episode, s1.Quality, s1.Codec, s1.Hardcoded)
+
+
 }

+ 1 - 0
common/urls.go

@@ -3,4 +3,5 @@ package common
 const (
 	SubXunLeiRootUrl = "http://sub.xmp.sandai.net:8000/subxl/%s.json"
 	SubShooterRootUrl = "https://www.shooter.cn/api/subapi.php"
+	SubZimukuRootUrl = "http://zmk.pw/search"
 )

+ 2 - 1
go.mod

@@ -3,7 +3,8 @@ module github.com/allanpk716/ChineseSubFinder
 go 1.15
 
 require (
-	github.com/PuerkitoBio/goquery v1.6.1 // indirect
+	github.com/PuerkitoBio/goquery v1.6.1
 	github.com/beevik/etree v1.1.0
 	github.com/go-resty/resty/v2 v2.6.0
+	github.com/middelink/go-parse-torrent-name v0.0.0-20190301154245-3ff4efacd4c4
 )

+ 2 - 0
go.sum

@@ -8,6 +8,8 @@ github.com/beevik/etree v1.1.0 h1:T0xke/WvNtMoCqgzPhkX2r4rjY3GDZFi+FjpRZY2Jbs=
 github.com/beevik/etree v1.1.0/go.mod h1:r8Aw8JqVegEf0w2fDnATrX9VpkMcyFeM0FhwO62wh+A=
 github.com/go-resty/resty/v2 v2.6.0 h1:joIR5PNLM2EFqqESUjCMGXrWmXNHEU9CEiK813oKYS4=
 github.com/go-resty/resty/v2 v2.6.0/go.mod h1:PwvJS6hvaPkjtjNg9ph+VrSD92bi5Zq73w/BIH7cC3Q=
+github.com/middelink/go-parse-torrent-name v0.0.0-20190301154245-3ff4efacd4c4 h1:C/VViMMbR/4Ti2aXrWpKy34S05cRaVd6EvV9BFR3qJ8=
+github.com/middelink/go-parse-torrent-name v0.0.0-20190301154245-3ff4efacd4c4/go.mod h1:H66QhXPJpUSdWschhL6u//v3ge96/qMnQ9mWp3efbxA=
 github.com/thedevsaddam/gojsonq v1.9.1 h1:zQulEP43nwmq5EKrNWyIgJVbqDeMdC1qzXM/f5O15a0=
 github.com/thedevsaddam/gojsonq v2.3.0+incompatible h1:i2lFTvGY4LvoZ2VUzedsFlRiyaWcJm3Uh6cQ9+HyQA8=
 github.com/thedevsaddam/gojsonq/v2 v2.5.2 h1:CoMVaYyKFsVj6TjU6APqAhAvC07hTI6IQen8PHzHYY0=

+ 3 - 0
sub_supplier/iSupplier.go

@@ -1,5 +1,8 @@
 package sub_supplier
 
 type iSupplier interface {
+
 	GetSubListFromFile(filePath string, httpProxy string) ([]SubInfo, error)
+
+	GetSubListFromKeyword(keyword string, httpProxy string) ([]SubInfo, error)
 }

+ 4 - 0
sub_supplier/shooter/shooter.go

@@ -69,6 +69,10 @@ func (s Supplier) GetSubListFromFile(filePath string, httpProxy string) ([]sub_s
 	return outSubInfoList, nil
 }
 
+func (s Supplier) GetSubListFromKeyword(keyword string, httpProxy string) ([]sub_supplier.SubInfo, error) {
+	panic("not implemented")
+}
+
 func (s Supplier) computeFileHash(filePath string) (string, error) {
 	hash := ""
 	fp, err := os.Open(filePath)

+ 4 - 2
sub_supplier/shooter/shooter_test.go

@@ -5,9 +5,11 @@ import (
 )
 
 func TestNewSupplier(t *testing.T) {
-
+	//movie1 := "X:\\电影\\消失爱人 (2016)\\消失爱人 (2016) 720p AAC.rmvb"
+	//movie2 := "X:\\电影\\机动战士Z高达:星之继承者 (2005)\\机动战士Z高达:星之继承者 (2005) 1080p TrueHD.mkv"
+	standard1 := "X:\\连续剧\\The Bad Batch\\Season 1\\The Bad Batch - S01E01 - Aftermath WEBDL-1080p.mkv"
 	shooter := NewSupplier()
-	outList, err := shooter.GetSubListFromFile("X:\\电影\\消失爱人 (2016)\\消失爱人 (2016) 720p AAC.rmvb", "")
+	outList, err := shooter.GetSubListFromFile(standard1, "")
 	if err != nil {
 		t.Error(err)
 	}

+ 4 - 0
sub_supplier/xunlei/xunlei.go

@@ -56,6 +56,10 @@ func (s Supplier) GetSubListFromFile(filePath string, httpProxy string) ([]sub_s
 	return outSubList, nil
 }
 
+func (s Supplier) GetSubListFromKeyword(keyword string, httpProxy string) ([]sub_supplier.SubInfo, error) {
+	panic("not implemented")
+}
+
 //getCid 获取指定文件的唯一 cid
 func (s Supplier) getCid(filePath string) (string, error) {
 	hash := ""

+ 4 - 2
sub_supplier/xunlei/xunlei_test.go

@@ -5,9 +5,11 @@ import (
 )
 
 func TestGetList(t *testing.T) {
-
+	//movie1:= "X:\\电影\\Spiral From the Book of Saw (2021)\\Spiral From the Book of Saw (2021) WEBDL-1080p.mkv"
+	//movie2 := "X:\\电影\\机动战士Z高达:星之继承者 (2005)\\机动战士Z高达:星之继承者 (2005) 1080p TrueHD.mkv"
+	standard1 := "X:\\连续剧\\The Bad Batch\\Season 1\\The Bad Batch - S01E01 - Aftermath WEBDL-1080p.mkv"
 	xunlie := NewSupplier()
-	outList, err := xunlie.GetSubListFromFile("X:\\电影\\Spiral From the Book of Saw (2021)\\Spiral From the Book of Saw (2021) WEBDL-1080p.mkv", "")
+	outList, err := xunlie.GetSubListFromFile(standard1, "")
 	if err != nil {
 		t.Error(err)
 	}

+ 110 - 0
sub_supplier/zimuku/zimuku.go

@@ -0,0 +1,110 @@
+package zimuku
+
+import (
+	"github.com/PuerkitoBio/goquery"
+	"github.com/allanpk716/ChineseSubFinder/common"
+	"github.com/allanpk716/ChineseSubFinder/sub_supplier"
+	"github.com/go-resty/resty/v2"
+	"path/filepath"
+	"strings"
+)
+
+type Supplier struct {
+
+}
+
+func NewSupplier() *Supplier {
+	return &Supplier{}
+}
+
+func (s Supplier) GetSubListFromFile(filePath string, httpProxy string) ([]sub_supplier.SubInfo, error) {
+
+	/*
+		虽然是传入视频文件路径,但是其实需要读取对应的视频文件目录下的
+		movie.xml 以及 *.nfo,找到 IMDB id
+		优先通过 IMDB id 去查找字幕
+		如果找不到,再靠文件名提取影片名称去查找
+	*/
+	// 得到这个视频文件名中的信息
+	info, err := common.GetVideoInfo(filePath)
+	if err != nil {
+		return nil, err
+	}
+	// 找到这个视频文件,然后读取它目录下的文件,尝试得到 IMDB ID
+	fileRootDirPath := filepath.Dir(filePath)
+	imdbId, err := common.GetImdbId(fileRootDirPath)
+	if err != nil {
+		return nil, err
+	}
+
+	// 先用 imdb id 找
+	subInfoList, err := s.GetSubListFromKeyword(imdbId, httpProxy)
+	if err != nil {
+		return nil, err
+	}
+	// 如果有就优先返回
+	if len(subInfoList) >0 {
+		return subInfoList, nil
+	}
+	// 如果没有,那么就用文件名查找
+	subInfoList, err = s.GetSubListFromKeyword(info.Title, httpProxy)
+	if err != nil {
+		return nil, err
+	}
+
+	return subInfoList, nil
+}
+
+func (s Supplier) GetSubListFromKeyword(keyword string, httpProxy string) ([]sub_supplier.SubInfo, error) {
+
+	httpClient := resty.New()
+	httpClient.SetTimeout(common.HTMLTimeOut)
+	if httpProxy != "" {
+		httpClient.SetProxy(httpProxy)
+	}
+	httpClient.SetHeaders(map[string]string{
+		"Content-Type": "application/json",
+		"User-Agent": "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",
+	})
+	// 第一级界面,有多少个字幕
+	resp, err := httpClient.R().
+		SetQueryParams(map[string]string{
+			"q": keyword,
+		}).
+		Get(common.SubZimukuRootUrl)
+	if err != nil {
+		return nil, err
+	}
+	//println(resp.String())
+	// 解析 html
+	doc, err := goquery.NewDocumentFromReader(strings.NewReader(resp.String()))
+	if err != nil {
+		return nil, err
+	}
+	doc.Find("div div table tbody tr").Each(func(i int, s *goquery.Selection) {
+		// For each item found, get the band and title
+		aa := s.Find("a[href]")
+		//comicPicEpisode := aa.Text()
+		println(aa.Text())
+	})
+
+	// 第二级界面,单个字幕详情
+
+	// 第三级界面,具体字幕下载
+
+
+	return nil, nil
+}
+
+type SubResult struct {
+	Title string
+	OtherName string
+	SubList []SubInfo
+}
+
+type SubInfo struct {
+	Score			float32
+	DownloadTimes 	int
+	Url				string
+
+}

+ 20 - 0
sub_supplier/zimuku/zimuku_test.go

@@ -0,0 +1,20 @@
+package zimuku
+
+import (
+	"testing"
+)
+
+func TestSupplier_GetSubListFromKeyword(t *testing.T) {
+
+	//imdbId1 := "tt3228774"
+	videoName := "黑白魔女库伊拉"
+	s := Supplier{}
+	subList, err := s.GetSubListFromKeyword(videoName, "")
+	if err != nil {
+		t.Error(err)
+	}
+
+	for _, info := range subList {
+		println(info.Name)
+	}
+}