Selaa lähdekoodia

基础的 subtitle_best api

Signed-off-by: allan716 <[email protected]>
allan716 2 vuotta sitten
vanhempi
sitoutus
6f4b986040

+ 158 - 0
pkg/logic/sub_supplier/subtitle_best/api.go

@@ -1,4 +1,162 @@
 package subtitle_best
 
+import (
+	"encoding/json"
+	"github.com/ChineseSubFinder/ChineseSubFinder/pkg/types/common"
+	"github.com/go-resty/resty/v2"
+)
+
 type Api struct {
+	client *resty.Client
+	token  string
+	apiKey string
+}
+
+// QueryMovieSubtitle 查询电影的字幕
+func (a *Api) QueryMovieSubtitle(imdbID string) (*SubtitleResponse, error) {
+	// 构建请求体
+	requestBody := SearchMovieSubtitleRequest{
+		ImdbID: imdbID,
+		ApiKey: a.apiKey,
+	}
+
+	// 发送请求
+	resp, err := a.client.R().
+		SetHeader("Authorization", "Bearer "+a.token).
+		SetBody(requestBody).
+		Post(common.SubSubtitleBestRootUrlDef + common.SubSubtitleBestSearchMovieUrl)
+	if err != nil {
+		return nil, err
+	}
+
+	// 解析响应
+	var subtitleResponse SubtitleResponse
+	err = json.Unmarshal(resp.Body(), &subtitleResponse)
+	if err != nil {
+		return nil, err
+	}
+
+	return &subtitleResponse, nil
+}
+
+// QueryTVEpsSubtitle 查询连续剧 一季 一集的字幕
+func (a *Api) QueryTVEpsSubtitle(imdbID string, season, episode int) (*SubtitleResponse, error) {
+	// 构建请求体
+	requestBody := SearchTVEpsSubtitleRequest{
+		ImdbID:  imdbID,
+		ApiKey:  a.apiKey,
+		Season:  season,
+		Episode: episode,
+	}
+
+	// 发送请求
+	resp, err := a.client.R().
+		SetHeader("Authorization", "Bearer "+a.token).
+		SetBody(requestBody).
+		Post(common.SubSubtitleBestRootUrlDef + common.SubSubtitleBestSearchTVEpsUrl)
+	if err != nil {
+		return nil, err
+	}
+
+	// 解析响应
+	var subtitleResponse SubtitleResponse
+	err = json.Unmarshal(resp.Body(), &subtitleResponse)
+	if err != nil {
+		return nil, err
+	}
+
+	return &subtitleResponse, nil
+}
+
+// QueryTVSeasonPackages 查询连续剧 一季的字幕包 ID 列表
+func (a *Api) QueryTVSeasonPackages(imdbID string, season int) (*SeasonPackagesResponse, error) {
+	// 构建请求体
+	requestBody := SearchTVSeasonPackagesRequest{
+		ImdbID: imdbID,
+		ApiKey: a.apiKey,
+		Season: season,
+	}
+
+	// 发送请求
+	resp, err := a.client.R().
+		SetHeader("Authorization", "Bearer "+a.token).
+		SetBody(requestBody).
+		Post(common.SubSubtitleBestRootUrlDef + common.SubSubtitleBestSearchTVSeasonPackageUrl)
+	if err != nil {
+		return nil, err
+	}
+
+	// 解析响应
+	var seasonPackageResponse SeasonPackagesResponse
+	err = json.Unmarshal(resp.Body(), &seasonPackageResponse)
+	if err != nil {
+		return nil, err
+	}
+
+	return &seasonPackageResponse, nil
+}
+
+// QueryTVSeasonPackageByID 查询连续剧 一季 一字幕包的字幕
+func (a *Api) QueryTVSeasonPackageByID(imdbID string, seasonPackageId string) (*SubtitleResponse, error) {
+	// 构建请求体
+	requestBody := SearchTVSeasonPackageByIDRequest{
+		ImdbID:          imdbID,
+		ApiKey:          a.apiKey,
+		SeasonPackageId: seasonPackageId,
+	}
+
+	// 发送请求
+	resp, err := a.client.R().
+		SetHeader("Authorization", "Bearer "+a.token).
+		SetBody(requestBody).
+		Post(common.SubSubtitleBestRootUrlDef + common.SubSubtitleBestSearchTVSeasonPackageByIDUrl)
+	if err != nil {
+		return nil, err
+	}
+
+	// 解析响应
+	var subtitleResponse SubtitleResponse
+	err = json.Unmarshal(resp.Body(), &subtitleResponse)
+	if err != nil {
+		return nil, err
+	}
+
+	return &subtitleResponse, nil
+}
+
+// GetDownloadUrl 获取字幕下载地址
+func (a *Api) GetDownloadUrl(subSha256, imdbID string,
+	isMovie bool, season, episode int,
+	seasonPackageId string, language int,
+	token string) (*GetUrlResponse, error) {
+	// 构建请求体
+	requestBody := DownloadUrlConvertRequest{
+		SubSha256:       subSha256,
+		ImdbID:          imdbID,
+		IsMovie:         isMovie,
+		Season:          season,
+		Episode:         episode,
+		SeasonPackageId: seasonPackageId,
+		Language:        language,
+		ApiKey:          a.apiKey,
+		Token:           token,
+	}
+
+	// 发送请求
+	resp, err := a.client.R().
+		SetHeader("Authorization", "Bearer "+a.token).
+		SetBody(requestBody).
+		Post(common.SubSubtitleBestRootUrlDef + common.SubSubtitleBestGetDlURLUrl)
+	if err != nil {
+		return nil, err
+	}
+
+	// 解析响应
+	var getUrlResponse GetUrlResponse
+	err = json.Unmarshal(resp.Body(), &getUrlResponse)
+	if err != nil {
+		return nil, err
+	}
+
+	return &getUrlResponse, nil
 }

+ 66 - 0
pkg/logic/sub_supplier/subtitle_best/common.go

@@ -0,0 +1,66 @@
+package subtitle_best
+
+type Subtitle struct {
+	SubSha256 string `json:"sub_sha256"`
+	Title     string `json:"title"`
+	Language  int    `json:"language"`
+	Ext       string `json:"ext"`
+	IsMovie   bool   `json:"is_movie"`
+	Season    int    `json:"season"`
+	Episode   int    `json:"episode"`
+	Token     string `json:"token"`
+}
+
+type SubtitleResponse struct {
+	Status    int        `json:"status"`
+	Message   string     `json:"message"`
+	Subtitles []Subtitle `json:"subtitles"`
+}
+
+type SeasonPackagesResponse struct {
+	Status           int      `json:"status"`
+	Message          string   `json:"message"`
+	SeasonPackageIds []string `json:"season_package_ids"`
+}
+
+type GetUrlResponse struct {
+	Status       int    `json:"status"`
+	Message      string `json:"message"`
+	DownloadLink string `json:"download_link"`
+}
+
+type SearchMovieSubtitleRequest struct {
+	ImdbID string `json:"imdb_id"`
+	ApiKey string `json:"api_key"`
+}
+
+type SearchTVEpsSubtitleRequest struct {
+	ImdbID  string `json:"imdb_id"`
+	Season  int    `json:"season"`
+	Episode int    `json:"episode"`
+	ApiKey  string `json:"api_key"`
+}
+
+type SearchTVSeasonPackagesRequest struct {
+	ImdbID string `json:"imdb_id"`
+	Season int    `json:"season"`
+	ApiKey string `json:"api_key"`
+}
+
+type SearchTVSeasonPackageByIDRequest struct {
+	ImdbID          string `json:"imdb_id"`
+	SeasonPackageId string `json:"season_package_id"`
+	ApiKey          string `json:"api_key"`
+}
+
+type DownloadUrlConvertRequest struct {
+	SubSha256       string `json:"sub_sha256"`
+	ImdbID          string `json:"imdb_id"`
+	IsMovie         bool   `json:"is_movie"`
+	Season          int    `json:"season"`
+	Episode         int    `json:"episode"`
+	SeasonPackageId string `json:"season_package_id"`
+	Language        int    `json:"language"`
+	ApiKey          string `json:"api_key"`
+	Token           string `json:"token"`
+}

+ 1 - 1
pkg/settings/supplier_settings.go

@@ -22,7 +22,7 @@ func NewSuppliersSettings() *SuppliersSettings {
 		A4k:          NewOneSupplierSettings(common.SubSiteA4K, common.SubA4kRootUrlDef, common.SubA4kSearchUrl, -1),
 		SubHD:        NewOneSupplierSettings(common.SubSiteSubHd, common.SubSubHDRootUrlDef, common.SubSubHDSearchUrl, 20),
 		Zimuku:       NewOneSupplierSettings(common.SubSiteZiMuKu, common.SubZiMuKuRootUrlDef, common.SubZiMuKuSearchFormatUrl, 20),
-		SubtitleBest: NewOneSupplierSettings(common.SubSiteSubtitleBest, common.SubSubtitleBestRootUrlDef, common.SubSubtitleBestSearchFormatUrl, 20),
+		SubtitleBest: NewOneSupplierSettings(common.SubSiteSubtitleBest, common.SubSubtitleBestRootUrlDef, common.SubSubtitleBestSearchMovieUrl, 20),
 	}
 }
 

+ 6 - 2
pkg/types/common/urls.go

@@ -15,6 +15,10 @@ const (
 	SubA4kRootUrlDef = "https://www.a4k.net"
 	SubA4kSearchUrl  = "/search?term="
 
-	SubSubtitleBestRootUrlDef      = "https://api.subtitle.best"
-	SubSubtitleBestSearchFormatUrl = "/share-sub/v1/search-movie"
+	SubSubtitleBestRootUrlDef                   = "https://api.subtitle.best/share-sub/v1"
+	SubSubtitleBestSearchMovieUrl               = "/search-movie"
+	SubSubtitleBestSearchTVEpsUrl               = "/search-tv-eps"
+	SubSubtitleBestSearchTVSeasonPackageUrl     = "/search-tv-season-package"
+	SubSubtitleBestSearchTVSeasonPackageByIDUrl = "/search-tv-season-package-id"
+	SubSubtitleBestGetDlURLUrl                  = "/get-dl-url"
 )