Browse Source

完成中文语言字幕、字幕格式的过滤逻辑

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

+ 2 - 1
README.md

@@ -25,9 +25,10 @@
 
 * 完成初版自动下载
   * ~~多个字幕网站的下载支持~~
-  * 解析下载到的字幕是什么语言的
+  * 解析下载到的字幕是什么语言的(文件名分析?文件内容分析?)
   * 搜索视频文件
   * 配置文件支持
+* 字幕的风评(有些字幕太差了,需要进行过滤,考虑排除,字幕组,关键词,机翻,以及评分等条件
 * 加入 Web 设置界面
 * docker 打包
 * docker-compose 文件

+ 1 - 1
common/common.go

@@ -2,7 +2,7 @@ package common
 
 import "time"
 
-const HTMLTimeOut = 20 * time.Second	// HttpClient 超时时间
+const HTMLTimeOut = 60 * time.Second	// HttpClient 超时时间
 
 const DownloadSubsPerSite = 1 // 默认,每个网站下载一个字幕,允许额外传参调整
 

+ 15 - 0
common/lang.go

@@ -50,6 +50,21 @@ func LangConverter(subLang string) Language {
 	}
 }
 
+func HasChineseLang(lan Language) bool {
+	switch lan {
+	case ChineseSimple:
+	case ChineseTraditional:
+	case ChineseSimpleEnglish:
+	case ChineseTraditionalEnglish:
+	case ChineseSimpleJapanese:
+	case ChineseTraditionalJapanese:
+	case ChineseSimpleKorean:
+	case ChineseTraditionalKorean:
+		return true
+	}
+	return false
+}
+
 // Language 语言类型,注意,这里默认还是查找的是中文字幕,只不过下载的时候可能附带了其他的
 type Language int
 const (

+ 13 - 0
common/util.go

@@ -98,6 +98,19 @@ func GetDebugFolder() (string, error) {
 	return nowProcessRoot, err
 }
 
+func IsSubTypeWanted(subName string) bool {
+	const subTypeASS = "ass"
+	const subTypeSSA = "ssa"
+	const subTypeSRT = "srt"
+	if strings.Contains(strings.ToLower(subName), subTypeASS) ||
+		strings.Contains(strings.ToLower(subName), subTypeSSA) ||
+		strings.Contains(strings.ToLower(subName), subTypeSRT) {
+		return true
+	}
+
+	return false
+}
+
 // ReqParam 可选择传入的参数
 type ReqParam struct {
 	UserExtList []string	// 用户确认的视频后缀名支持列表

+ 2 - 1
sub_supplier/shooter/shooter_test.go

@@ -6,7 +6,8 @@ import (
 )
 
 func TestNewSupplier(t *testing.T) {
-	movie1 := "X:\\电影\\消失爱人 (2016)\\消失爱人 (2016) 720p AAC.rmvb"
+	movie1 := "X:\\电影\\龙猫 (1988)\\龙猫 (1988) 1080p DTS.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})

+ 9 - 1
sub_supplier/subhd/subhd.go

@@ -169,17 +169,25 @@ func (s Supplier) Step1(detailPageUrl string) ([]HdListItem, error) {
 	const subTableKeyword = ".table-sm tr"
 	const oneSubTrTitleKeyword = "a.text-dark"
 	const oneSubTrDownloadCountKeyword = "td.p-3"
+	const oneSubLangAndTypeKetword = ".text-secondary"
 
 	doc.Find(subTableKeyword).EachWithBreak(func(i int, tr *goquery.Selection) bool {
 		if tr.Find(oneSubTrTitleKeyword).Size() == 0 {
 			return true
 		}
+		// 文件的下载页面,还需要分析
 		downUrl, exists := tr.Find(oneSubTrTitleKeyword).Eq(0).Attr("href")
 		if !exists {
 			return true
 		}
+		// 文件名
 		title := strings.TrimSpace(tr.Find(oneSubTrTitleKeyword).Text())
-
+		// 字幕类型
+		insideSubType := tr.Find(oneSubLangAndTypeKetword).Text()
+		if common.IsSubTypeWanted(insideSubType) == false {
+			return true
+		}
+		// 下载的次数
 		downCount, err := common.GetNumber2int(tr.Find(oneSubTrDownloadCountKeyword).Eq(1).Text())
 		if err != nil {
 			return true

+ 34 - 14
sub_supplier/xunlei/xunlei.go

@@ -36,6 +36,7 @@ func (s Supplier) GetSubListFromFile(filePath string) ([]sub_supplier.SubInfo, e
 
 	cid, err := s.getCid(filePath)
 	var jsonList SublistSliceXunLei
+	var tmpXunLeiSubListChinese = make([]SublistXunLei, 0)
 	var outSubList []sub_supplier.SubInfo
 	if len(cid) == 0 {
 		return outSubList, common.XunLeiCIdIsEmpty
@@ -50,26 +51,45 @@ func (s Supplier) GetSubListFromFile(filePath string) ([]sub_supplier.SubInfo, e
 	// 剔除空的
 	for _, v := range jsonList.Sublist {
 		if len(v.Scid) > 0 {
-
-			data, filename, err := common.DownFile(v.Surl)
-			if err != nil {
-				println(err.Error())
-				continue
+			// 符合中文语言的先加入列表
+			tmpLang := common.LangConverter(v.Language)
+			if common.HasChineseLang(tmpLang) == true && common.IsSubTypeWanted(v.Sname) == true {
+				tmpXunLeiSubListChinese = append(tmpXunLeiSubListChinese, v)
 			}
-			ext := ""
-			if filename == "" {
-				ext = filepath.Ext(v.Surl)
-			} else {
-				ext = filepath.Ext(filename)
+		}
+	}
+	// TODO 这里需要考虑,可以设置为高级选项,不够就用 unknow 来补充
+	// 如果不够,再补 unknow
+	if len(tmpXunLeiSubListChinese) < s.topic {
+		for _, v := range jsonList.Sublist {
+			if len(tmpXunLeiSubListChinese) >= s.topic {
+				break
 			}
 			tmpLang := common.LangConverter(v.Language)
-			outSubList = append(outSubList, *sub_supplier.NewSubInfo(v.Sname, tmpLang, v.Surl, v.Svote, v.Roffset, ext, data))
-			// 如果够了那么多个字幕就返回
-			if len(outSubList) >= s.topic {
-				return outSubList, nil
+			if common.HasChineseLang(tmpLang) == false {
+				tmpXunLeiSubListChinese = append(tmpXunLeiSubListChinese, v)
 			}
 		}
 	}
+	// 再开始下载字幕
+	for _, v := range tmpXunLeiSubListChinese {
+		tmpLang := common.LangConverter(v.Language)
+		data, filename, err := common.DownFile(v.Surl)
+		if err != nil {
+			println(err.Error())
+			continue
+		}
+		ext := ""
+		if filename == "" {
+			ext = filepath.Ext(v.Surl)
+		} else {
+			ext = filepath.Ext(filename)
+		}
+
+		outSubList = append(outSubList, *sub_supplier.NewSubInfo(v.Sname, tmpLang, v.Surl, v.Svote, v.Roffset, ext, data))
+	}
+
+
 	return outSubList, nil
 }
 

+ 2 - 1
sub_supplier/xunlei/xunlei_test.go

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

+ 31 - 17
sub_supplier/zimuku/zimuku.go

@@ -97,10 +97,6 @@ func (s Supplier) GetSubListFromKeyword(keyword string) ([]sub_supplier.SubInfo,
 	// 第三级界面,单个字幕详情
 	// 找到最大的优先级的字幕下载
 	sort.Sort(SortByPriority{subResult.SubInfos})
-	// 移除多出来的字幕
-	if len(subResult.SubInfos) >= s.topic {
-		subResult.SubInfos = subResult.SubInfos[:s.topic]
-	}
 
 	for i := range subResult.SubInfos {
 		err = s.Step2(&subResult.SubInfos[i])
@@ -109,8 +105,31 @@ func (s Supplier) GetSubListFromKeyword(keyword string) ([]sub_supplier.SubInfo,
 			continue
 		}
 	}
-	// 第四级界面,具体字幕下载
+
+	// TODO 这里需要考虑,可以设置为高级选项,不够就用 unknow 来补充
+	// 首先过滤出中文的字幕,同时需要满足是支持的字幕
+	var tmpSubInfo = make([]SubInfo, 0)
 	for _, subInfo := range subResult.SubInfos {
+		tmpLang := common.LangConverter(subInfo.Lang)
+		if common.HasChineseLang(tmpLang) == true && common.IsSubTypeWanted(subInfo.Ext) == true {
+			tmpSubInfo = append(tmpSubInfo, subInfo)
+		}
+	}
+	// 看字幕够不够
+	if len(tmpSubInfo) < s.topic {
+		for _, subInfo := range subResult.SubInfos {
+			if len(tmpSubInfo) >= s.topic {
+				break
+			}
+			tmpLang := common.LangConverter(subInfo.Lang)
+			if common.HasChineseLang(tmpLang) == false {
+				tmpSubInfo = append(tmpSubInfo, subInfo)
+			}
+		}
+	}
+
+	// 第四级界面,具体字幕下载
+	for _, subInfo := range tmpSubInfo {
 		fileName, data, err := s.Step3(subInfo.SubDownloadPageUrl)
 		if err != nil {
 			println(err.Error())
@@ -139,16 +158,6 @@ func (s Supplier) Step0(keyword string) (string, error) {
 	// 找到对应影片的详情界面
 	re := regexp.MustCompile(`<p\s+class="tt\s+clearfix"><a\s+href="(/subs/[\w]+\.html)"\s+target="_blank"><b>(.*?)</b></a></p>`)
 	matched := re.FindAllStringSubmatch(resp.String(), -1)
-	//lists := make([]string, 0)
-	//for _, match := range matched {
-	//	// 去重
-	//	for _, list := range lists {
-	//		if list != match[1] {
-	//			lists = append(lists, match[1])
-	//		}
-	//	}
-	//	lists = append(lists, match[1])
-	//}
 	if len(matched) < 1 {
 		return "", common.ZiMuKuSearchKeyWordStep0DetailPageUrlNotFound
 	}
@@ -173,15 +182,19 @@ func (s Supplier) Step1(filmDetailPageUrl string) (SubResult, error) {
 	var subResult SubResult
 	subResult.SubInfos = SubInfos{}
 	doc.Find("#subtb tbody tr").Each(func(i int, tr *goquery.Selection) {
+		// 字幕下载页面地址
 		href, exists := tr.Find("a").Attr("href")
 		if !exists {
 			return
 		}
+		// 标题
 		title, exists := tr.Find("a").Attr("title")
 		if !exists {
 			return
 		}
+		// 扩展名
 		ext := tr.Find(".label-info").Text()
+		// 作者信息
 		authorInfos := tr.Find(".gray")
 		authorInfo := ""
 		authorInfos.Each(func(a_i int, a_lb *goquery.Selection) {
@@ -191,11 +204,12 @@ func (s Supplier) Step1(filmDetailPageUrl string) (SubResult, error) {
 		if authorInfoLen > 0 {
 			authorInfo = authorInfo[0 : authorInfoLen-3]
 		}
-
+		// 语言
 		lang, exists := tr.Find("img").First().Attr("alt")
 		if !exists {
 			lang = ""
 		}
+		// 投票
 		rate, exists := tr.Find(".rating-star").First().Attr("title")
 		if !exists {
 			rate = ""
@@ -204,7 +218,7 @@ func (s Supplier) Step1(filmDetailPageUrl string) (SubResult, error) {
 		if err != nil {
 			return
 		}
-
+		// 下载次数统计
 		downCountNub := 0
 		downCount := tr.Find("td").Eq(3).Text()
 		if strings.Contains(downCount, "万") {