소스 검색

修复 supplier 多个实现

Signed-off-by: 716 <[email protected]>
716 3 년 전
부모
커밋
dbb9763ec4

+ 1 - 1
internal/ifaces/iSupplier.go

@@ -6,7 +6,7 @@ import (
 )
 
 type ISupplier interface {
-	CheckAlive() bool
+	CheckAlive() (bool, int64)
 
 	GetSupplierName() string
 

+ 8 - 5
internal/logic/sub_supplier/shooter/shooter.go

@@ -17,6 +17,7 @@ import (
 	"os"
 	"path/filepath"
 	"strings"
+	"time"
 )
 
 type Supplier struct {
@@ -39,14 +40,16 @@ func NewSupplier(_settings settings.Settings) *Supplier {
 	return &sup
 }
 
-func (s Supplier) CheckAlive() bool {
-
+func (s Supplier) CheckAlive() (bool, int64) {
+	// 计算当前时间
+	startT := time.Now()
 	_, err := s.getSubInfos(checkFileHash, checkFileName, qLan)
 	if err != nil {
-		return false
+		s.log.Errorln(s.GetSupplierName(), "CheckAlive", "Error", err)
+		return false, 0
 	}
-
-	return true
+	// 计算耗时
+	return true, time.Since(startT).Milliseconds()
 }
 
 func (s Supplier) GetSupplierName() string {

+ 2 - 1
internal/logic/sub_supplier/shooter/shooter_test.go

@@ -36,7 +36,8 @@ func TestNewSupplier(t *testing.T) {
 		println(i, sublist.Name, sublist.Ext, sublist.Language.String(), sublist.Score, sublist.FileUrl, len(sublist.Data))
 	}
 
-	if shooter.CheckAlive() == false {
+	alive, _ := shooter.CheckAlive()
+	if alive == false {
 		t.Fatal("CheckAlive == false")
 	}
 }

+ 16 - 0
internal/logic/sub_supplier/subhd/subhd.go

@@ -13,6 +13,7 @@ import (
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/rod_helper"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/settings"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/sub_parser_hub"
+	"github.com/allanpk716/ChineseSubFinder/internal/pkg/url_connectedness_helper"
 	"github.com/allanpk716/ChineseSubFinder/internal/types/language"
 	"github.com/allanpk716/ChineseSubFinder/internal/types/series"
 	"github.com/allanpk716/ChineseSubFinder/internal/types/supplier"
@@ -68,6 +69,21 @@ func NewSupplier(_settings settings.Settings) *Supplier {
 	return &sup
 }
 
+func (s Supplier) CheckAlive() (bool, int64) {
+
+	proxyStatus, proxySpeed, err := url_connectedness_helper.UrlConnectednessTest(common.SubSubHDRootUrl, s.settings.AdvancedSettings.ProxySettings.HttpProxyAddress)
+	if err != nil {
+		s.log.Errorln(s.GetSupplierName(), "CheckAlive", "Error", err)
+		return false, 0
+	}
+	if proxyStatus == false {
+		s.log.Errorln(s.GetSupplierName(), "CheckAlive", "Status != 200")
+		return false, proxySpeed
+	}
+
+	return true, proxySpeed
+}
+
 func (s Supplier) GetSupplierName() string {
 	return common.SubSiteSubHd
 }

+ 11 - 6
internal/logic/sub_supplier/subhd/subhd_test.go

@@ -3,10 +3,10 @@ package subhd
 import (
 	"fmt"
 	commonValue "github.com/allanpk716/ChineseSubFinder/internal/common"
-	series_helper2 "github.com/allanpk716/ChineseSubFinder/internal/logic/series_helper"
+	"github.com/allanpk716/ChineseSubFinder/internal/logic/series_helper"
+	"github.com/allanpk716/ChineseSubFinder/internal/pkg/settings"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/something_static"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/unit_test_helper"
-	"github.com/allanpk716/ChineseSubFinder/internal/types"
 	"path/filepath"
 	"testing"
 )
@@ -25,7 +25,7 @@ func TestSupplier_GetSubListFromFile(t *testing.T) {
 	rootDir := unit_test_helper.GetTestDataResourceRootPath([]string{"sub_spplier"}, 5, true)
 	movie1 := filepath.Join(rootDir, "zimuku", "movies", "消失爱人 (2016)", "消失爱人 (2016) 720p AAC.rmvb")
 
-	subhd := NewSupplier(types.ReqParam{DebugMode: true})
+	subhd := NewSupplier(*settings.NewSettings())
 	outList, err := subhd.getSubListFromFile4Movie(movie1)
 	if err != nil {
 		t.Error(err)
@@ -39,6 +39,11 @@ func TestSupplier_GetSubListFromFile(t *testing.T) {
 	for i, sublist := range outList {
 		println(i, sublist.Name, sublist.Ext, sublist.Language.String(), sublist.Score, sublist.FileUrl, len(sublist.Data))
 	}
+
+	alive, _ := subhd.CheckAlive()
+	if alive == false {
+		t.Fatal("CheckAlive == false")
+	}
 }
 
 // 无需关注这个测试用例,这个方案暂时弃用
@@ -53,11 +58,11 @@ func TestSupplier_GetSubListFromFile4Series(t *testing.T) {
 	rootDir := unit_test_helper.GetTestDataResourceRootPath([]string{"sub_spplier"}, 5, true)
 	ser := filepath.Join(rootDir, "zimuku", "series", "黄石 (2018)")
 	// 读取本地的视频和字幕信息
-	seriesInfo, err := series_helper2.ReadSeriesInfoFromDir(ser, nil, false)
+	seriesInfo, err := series_helper.ReadSeriesInfoFromDir(ser, nil, false)
 	if err != nil {
 		t.Fatal(err)
 	}
-	s := NewSupplier(types.ReqParam{DebugMode: true})
+	s := NewSupplier(*settings.NewSettings())
 	outList, err := s.GetSubListFromFile4Series(seriesInfo)
 	if err != nil {
 		t.Fatal(err)
@@ -79,7 +84,7 @@ func TestSupplier_getSubListFromKeyword4Movie(t *testing.T) {
 	//imdbID := "tt15299712" // 云南虫谷
 	//imdbID := "tt3626476" // Vacation Friends (2021)
 	getCode()
-	subhd := NewSupplier(types.ReqParam{DebugMode: true})
+	subhd := NewSupplier(*settings.NewSettings())
 	subInfos, err := subhd.getSubListFromKeyword4Movie(imdbID)
 	if err != nil {
 		t.Fatal(err)

+ 9 - 4
internal/logic/sub_supplier/xunlei/xunlei.go

@@ -17,6 +17,7 @@ import (
 	"math"
 	"os"
 	"path/filepath"
+	"time"
 )
 
 type Supplier struct {
@@ -39,18 +40,22 @@ func NewSupplier(_settings settings.Settings) *Supplier {
 	return &sup
 }
 
-func (s Supplier) CheckAlive() bool {
+func (s Supplier) CheckAlive() (bool, int64) {
 
+	// 计算当前时间
+	startT := time.Now()
 	jsonList, err := s.getSubInfos(checkFileName, checkCID)
 	if err != nil {
-		return false
+		s.log.Errorln(s.GetSupplierName(), "CheckAlive", "Error", err)
+		return false, 0
 	}
 
 	if len(jsonList.Sublist) < 1 {
-		return false
+		s.log.Errorln(s.GetSupplierName(), "CheckAlive", "Sublist < 1")
+		return false, 0
 	}
 
-	return true
+	return true, time.Since(startT).Milliseconds()
 }
 
 func (s Supplier) GetSupplierName() string {

+ 2 - 1
internal/logic/sub_supplier/xunlei/xunlei_test.go

@@ -38,7 +38,8 @@ func TestGetList(t *testing.T) {
 		println(i, sublist.Name, sublist.Ext, sublist.Language.String(), sublist.Score, len(sublist.Data))
 	}
 
-	if xunlie.CheckAlive() == false {
+	alive, _ := xunlie.CheckAlive()
+	if alive == false {
 		t.Fatal("CheckAlive == false")
 	}
 }

+ 16 - 0
internal/logic/sub_supplier/zimuku/zimuku.go

@@ -12,6 +12,7 @@ import (
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/notify_center"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/settings"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/sub_parser_hub"
+	"github.com/allanpk716/ChineseSubFinder/internal/pkg/url_connectedness_helper"
 	language2 "github.com/allanpk716/ChineseSubFinder/internal/types/language"
 	"github.com/allanpk716/ChineseSubFinder/internal/types/series"
 	"github.com/allanpk716/ChineseSubFinder/internal/types/supplier"
@@ -42,6 +43,21 @@ func NewSupplier(_settings settings.Settings) *Supplier {
 	return &sup
 }
 
+func (s Supplier) CheckAlive() (bool, int64) {
+
+	proxyStatus, proxySpeed, err := url_connectedness_helper.UrlConnectednessTest(common.SubZiMuKuRootUrl, s.settings.AdvancedSettings.ProxySettings.HttpProxyAddress)
+	if err != nil {
+		s.log.Errorln(s.GetSupplierName(), "CheckAlive", "Error", err)
+		return false, 0
+	}
+	if proxyStatus == false {
+		s.log.Errorln(s.GetSupplierName(), "CheckAlive", "Status != 200")
+		return false, proxySpeed
+	}
+
+	return true, proxySpeed
+}
+
 func (s Supplier) GetSupplierName() string {
 	return common.SubSiteZiMuKu
 }

+ 10 - 4
internal/logic/sub_supplier/zimuku/zimuku_test.go

@@ -2,6 +2,7 @@ package zimuku
 
 import (
 	series_helper2 "github.com/allanpk716/ChineseSubFinder/internal/logic/series_helper"
+	"github.com/allanpk716/ChineseSubFinder/internal/pkg/settings"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/unit_test_helper"
 	"path/filepath"
 	"testing"
@@ -11,7 +12,7 @@ func TestSupplier_GetSubListFromKeyword(t *testing.T) {
 
 	//imdbId1 := "tt3228774"
 	videoName := "黑白魔女库伊拉"
-	s := NewSupplier()
+	s := NewSupplier(*settings.NewSettings())
 	outList, err := s.getSubListFromKeyword(videoName)
 	if err != nil {
 		t.Error(err)
@@ -32,7 +33,7 @@ func TestSupplier_GetSubListFromFile(t *testing.T) {
 
 	rootDir := unit_test_helper.GetTestDataResourceRootPath([]string{"sub_spplier"}, 5, true)
 	movie1 := filepath.Join(rootDir, "zimuku", "movies", "The Devil All the Time (2020)", "The Devil All the Time (2020) WEBDL-1080p.mkv")
-	s := NewSupplier()
+	s := NewSupplier(*settings.NewSettings())
 	outList, err := s.getSubListFromMovie(movie1)
 	if err != nil {
 		t.Error(err)
@@ -41,6 +42,11 @@ func TestSupplier_GetSubListFromFile(t *testing.T) {
 	for i, sublist := range outList {
 		println(i, sublist.Name, sublist.Ext, sublist.Language.String(), sublist.Score, len(sublist.Data))
 	}
+
+	alive, _ := s.CheckAlive()
+	if alive == false {
+		t.Fatal("CheckAlive == false")
+	}
 }
 
 func TestSupplier_GetSubListFromFile4Series(t *testing.T) {
@@ -64,7 +70,7 @@ func TestSupplier_GetSubListFromFile4Series(t *testing.T) {
 	//epsMap[1] = 4
 	//series_helper2.SetTheSpecifiedEps2Download(seriesInfo, epsMap)
 
-	s := NewSupplier()
+	s := NewSupplier(*settings.NewSettings())
 	outList, err := s.GetSubListFromFile4Series(seriesInfo)
 	if err != nil {
 		t.Fatal(err)
@@ -85,7 +91,7 @@ func TestSupplier_getSubListFromKeyword(t *testing.T) {
 	//imdbID := "tt15299712" // 云南虫谷
 	//imdbID := "tt3626476"  // Vacation Friends (2021)
 	imdbID := "tt11192306" // Superman.and.Lois
-	zimuku := NewSupplier()
+	zimuku := NewSupplier(*settings.NewSettings())
 	subInfos, err := zimuku.getSubListFromKeyword(imdbID)
 	if err != nil {
 		t.Fatal(err)

+ 2 - 2
internal/pkg/url_connectedness_helper/url_connectedness_helper.go

@@ -9,7 +9,7 @@ import (
 )
 
 // UrlConnectednessTest 测试输入 url 的连通性
-func UrlConnectednessTest(testUrl, proxyAddr string) (bool, int, error) {
+func UrlConnectednessTest(testUrl, proxyAddr string) (bool, int64, error) {
 
 	var httpClient http.Client
 	if proxyAddr == "" {
@@ -51,7 +51,7 @@ func UrlConnectednessTest(testUrl, proxyAddr string) (bool, int, error) {
 	defer func() {
 		_ = res.Body.Close()
 	}()
-	speed := int(time.Now().Sub(begin).Nanoseconds() / 1000 / 1000) //ms
+	speed := time.Now().Sub(begin).Nanoseconds() / 1000 / 1000 //ms
 	// 判断是否成功访问,如果成功访问StatusCode应该为200
 	if res.StatusCode != http.StatusOK {
 		return false, 0, nil