浏览代码

新增额外获取 code 的方法

Signed-off-by: allan716 <[email protected]>
allan716 3 年之前
父节点
当前提交
8e265f8b0b

+ 1 - 1
internal/logic/pre_download_process/pre_download_proces.go

@@ -61,7 +61,7 @@ func (p *PreDownloadProcess) Init() *PreDownloadProcess {
 	// 获取验证码
 	nowTT := time.Now()
 	nowTimeFileNamePrix := fmt.Sprintf("%d%d%d", nowTT.Year(), nowTT.Month(), nowTT.Day())
-	updateTimeString, code, err := something_static.GetCodeFromWeb(p.log, nowTimeFileNamePrix)
+	updateTimeString, code, err := something_static.GetCodeFromWeb(p.log, nowTimeFileNamePrix, p.fileDownloader)
 	if err != nil {
 		notify_center.Notify.Add("GetSubhdCode", "GetCodeFromWeb,"+err.Error())
 		p.log.Errorln("something_static.GetCodeFromWeb", err)

+ 13 - 1
internal/logic/sub_supplier/subhd/subhd_test.go

@@ -121,9 +121,21 @@ func TestSupplier_getSubListFromKeyword4Movie(t *testing.T) {
 
 func getCode() {
 
+	if my_util.ReadCustomAuthFile(log_helper.GetLogger4Tester()) == false {
+		println("read auth file failed")
+		return
+	}
+	fileDownloader := file_downloader.NewFileDownloader(
+		cache_center.NewCacheCenter("local_task_queue", settings.GetSettings(), log_helper.GetLogger4Tester()),
+		random_auth_key.AuthKey{
+			BaseKey:  global_value.BaseKey(),
+			AESKey16: global_value.AESKey16(),
+			AESIv16:  global_value.AESIv16(),
+		})
+
 	nowTT := time.Now()
 	nowTimeFileNamePrix := fmt.Sprintf("%d%d%d", nowTT.Year(), nowTT.Month(), nowTT.Day())
-	updateTimeString, code, err := something_static.GetCodeFromWeb(log_helper.GetLogger4Tester(), nowTimeFileNamePrix)
+	updateTimeString, code, err := something_static.GetCodeFromWeb(log_helper.GetLogger4Tester(), nowTimeFileNamePrix, fileDownloader)
 	if err != nil {
 		commonValue.SubhdCode = ""
 	} else {

+ 11 - 0
internal/pkg/subtitle_best_api/code_data.go

@@ -0,0 +1,11 @@
+package subtitle_best_api
+
+type CodeReqData struct {
+	NowTime string `json:"now_time"`
+}
+
+type CodeReplyData struct {
+	Status  int    `json:"status"`
+	Message string `json:"message"`
+	Code    string `json:"code"`
+}

+ 47 - 1
internal/pkg/subtitle_best_api/subtitle_best_api.go

@@ -2,12 +2,14 @@ package subtitle_best_api
 
 import (
 	"bytes"
+	"encoding/base64"
 	"errors"
 	"fmt"
 	"io/ioutil"
 	"os"
 	"path/filepath"
 	"strconv"
+	"time"
 
 	"github.com/allanpk716/ChineseSubFinder/internal/models"
 
@@ -28,6 +30,50 @@ func NewSubtitleBestApi(inAuthKey random_auth_key.AuthKey) *SubtitleBestApi {
 	}
 }
 
+func (s *SubtitleBestApi) GetCode(_proxySettings ...*settings.ProxySettings) (string, error) {
+
+	if s.authKey.BaseKey == random_auth_key.BaseKey || s.authKey.AESKey16 == random_auth_key.AESKey16 || s.authKey.AESIv16 == random_auth_key.AESIv16 {
+		return "", errors.New("auth key is not set")
+	}
+
+	postUrl := webUrlBase + "/v1/subhd-code"
+	httpClient, err := my_util.NewHttpClient(_proxySettings...)
+	if err != nil {
+		return "", err
+	}
+
+	authKey, err := s.randomAuthKey.GetAuthKey()
+	if err != nil {
+		return "", err
+	}
+
+	var codeReplyData CodeReplyData
+	_, err = httpClient.R().
+		SetHeader("Authorization", "beer "+authKey).
+		SetQueryParams(map[string]string{
+			"now_time": time.Now().Format("2006-01-02"),
+		}).
+		SetHeader("Accept", "application/json").
+		SetResult(&codeReplyData).
+		Get(postUrl)
+	if err != nil {
+		return "", err
+	}
+
+	//println(resp.StatusCode())
+
+	if codeReplyData.Status == 0 {
+		return "", errors.New(codeReplyData.Message)
+	}
+
+	decodeBytes, err := base64.StdEncoding.DecodeString(codeReplyData.Code)
+	if err != nil {
+		return "", err
+	}
+
+	return string(decodeBytes), nil
+}
+
 func (s *SubtitleBestApi) GetMediaInfo(id, source, videoType string, _proxySettings ...*settings.ProxySettings) (*MediaInfoReply, error) {
 
 	if s.authKey.BaseKey == random_auth_key.BaseKey || s.authKey.AESKey16 == random_auth_key.AESKey16 || s.authKey.AESIv16 == random_auth_key.AESIv16 {
@@ -310,5 +356,5 @@ func (s *SubtitleBestApi) AskFindSub(VideoFeature, ImdbId, TmdbId, Season, Episo
 
 const (
 	webUrlBase = "https://api.subtitle.best"
-	//webUrlBase = "http://127.0.0.1:8889"
+	//webUrlBase = "http://127.0.0.1:8890"
 )