|
@@ -4,16 +4,20 @@ import (
|
|
"encoding/json"
|
|
"encoding/json"
|
|
"github.com/ChineseSubFinder/ChineseSubFinder/pkg/types/common"
|
|
"github.com/ChineseSubFinder/ChineseSubFinder/pkg/types/common"
|
|
"github.com/go-resty/resty/v2"
|
|
"github.com/go-resty/resty/v2"
|
|
|
|
+ "strconv"
|
|
)
|
|
)
|
|
|
|
|
|
type Api struct {
|
|
type Api struct {
|
|
- client *resty.Client
|
|
|
|
- token string
|
|
|
|
- apiKey string
|
|
|
|
|
|
+ headerToken string
|
|
|
|
+ apiKey string
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func NewApi(headerToken string, apiKey string) *Api {
|
|
|
|
+ return &Api{headerToken: headerToken, apiKey: apiKey}
|
|
}
|
|
}
|
|
|
|
|
|
// QueryMovieSubtitle 查询电影的字幕
|
|
// QueryMovieSubtitle 查询电影的字幕
|
|
-func (a *Api) QueryMovieSubtitle(imdbID string) (*SubtitleResponse, error) {
|
|
|
|
|
|
+func (a *Api) QueryMovieSubtitle(client *resty.Client, imdbID string) (*SubtitleResponse, *LimitInfo, error) {
|
|
// 构建请求体
|
|
// 构建请求体
|
|
requestBody := SearchMovieSubtitleRequest{
|
|
requestBody := SearchMovieSubtitleRequest{
|
|
ImdbID: imdbID,
|
|
ImdbID: imdbID,
|
|
@@ -21,26 +25,26 @@ func (a *Api) QueryMovieSubtitle(imdbID string) (*SubtitleResponse, error) {
|
|
}
|
|
}
|
|
|
|
|
|
// 发送请求
|
|
// 发送请求
|
|
- resp, err := a.client.R().
|
|
|
|
- SetHeader("Authorization", "Bearer "+a.token).
|
|
|
|
|
|
+ resp, err := client.R().
|
|
|
|
+ SetHeader("Authorization", "Bearer "+a.headerToken).
|
|
SetBody(requestBody).
|
|
SetBody(requestBody).
|
|
Post(common.SubSubtitleBestRootUrlDef + common.SubSubtitleBestSearchMovieUrl)
|
|
Post(common.SubSubtitleBestRootUrlDef + common.SubSubtitleBestSearchMovieUrl)
|
|
if err != nil {
|
|
if err != nil {
|
|
- return nil, err
|
|
|
|
|
|
+ return nil, nil, err
|
|
}
|
|
}
|
|
|
|
|
|
// 解析响应
|
|
// 解析响应
|
|
var subtitleResponse SubtitleResponse
|
|
var subtitleResponse SubtitleResponse
|
|
err = json.Unmarshal(resp.Body(), &subtitleResponse)
|
|
err = json.Unmarshal(resp.Body(), &subtitleResponse)
|
|
if err != nil {
|
|
if err != nil {
|
|
- return nil, err
|
|
|
|
|
|
+ return nil, nil, err
|
|
}
|
|
}
|
|
|
|
|
|
- return &subtitleResponse, nil
|
|
|
|
|
|
+ return &subtitleResponse, NewHeaderInfo(resp), nil
|
|
}
|
|
}
|
|
|
|
|
|
// QueryTVEpsSubtitle 查询连续剧 一季 一集的字幕
|
|
// QueryTVEpsSubtitle 查询连续剧 一季 一集的字幕
|
|
-func (a *Api) QueryTVEpsSubtitle(imdbID string, season, episode int) (*SubtitleResponse, error) {
|
|
|
|
|
|
+func (a *Api) QueryTVEpsSubtitle(client *resty.Client, imdbID string, season, episode int) (*SubtitleResponse, *LimitInfo, error) {
|
|
// 构建请求体
|
|
// 构建请求体
|
|
requestBody := SearchTVEpsSubtitleRequest{
|
|
requestBody := SearchTVEpsSubtitleRequest{
|
|
ImdbID: imdbID,
|
|
ImdbID: imdbID,
|
|
@@ -50,26 +54,26 @@ func (a *Api) QueryTVEpsSubtitle(imdbID string, season, episode int) (*SubtitleR
|
|
}
|
|
}
|
|
|
|
|
|
// 发送请求
|
|
// 发送请求
|
|
- resp, err := a.client.R().
|
|
|
|
- SetHeader("Authorization", "Bearer "+a.token).
|
|
|
|
|
|
+ resp, err := client.R().
|
|
|
|
+ SetHeader("Authorization", "Bearer "+a.headerToken).
|
|
SetBody(requestBody).
|
|
SetBody(requestBody).
|
|
Post(common.SubSubtitleBestRootUrlDef + common.SubSubtitleBestSearchTVEpsUrl)
|
|
Post(common.SubSubtitleBestRootUrlDef + common.SubSubtitleBestSearchTVEpsUrl)
|
|
if err != nil {
|
|
if err != nil {
|
|
- return nil, err
|
|
|
|
|
|
+ return nil, nil, err
|
|
}
|
|
}
|
|
|
|
|
|
// 解析响应
|
|
// 解析响应
|
|
var subtitleResponse SubtitleResponse
|
|
var subtitleResponse SubtitleResponse
|
|
err = json.Unmarshal(resp.Body(), &subtitleResponse)
|
|
err = json.Unmarshal(resp.Body(), &subtitleResponse)
|
|
if err != nil {
|
|
if err != nil {
|
|
- return nil, err
|
|
|
|
|
|
+ return nil, nil, err
|
|
}
|
|
}
|
|
|
|
|
|
- return &subtitleResponse, nil
|
|
|
|
|
|
+ return &subtitleResponse, NewHeaderInfo(resp), nil
|
|
}
|
|
}
|
|
|
|
|
|
// QueryTVSeasonPackages 查询连续剧 一季的字幕包 ID 列表
|
|
// QueryTVSeasonPackages 查询连续剧 一季的字幕包 ID 列表
|
|
-func (a *Api) QueryTVSeasonPackages(imdbID string, season int) (*SeasonPackagesResponse, error) {
|
|
|
|
|
|
+func (a *Api) QueryTVSeasonPackages(client *resty.Client, imdbID string, season int) (*SeasonPackagesResponse, *LimitInfo, error) {
|
|
// 构建请求体
|
|
// 构建请求体
|
|
requestBody := SearchTVSeasonPackagesRequest{
|
|
requestBody := SearchTVSeasonPackagesRequest{
|
|
ImdbID: imdbID,
|
|
ImdbID: imdbID,
|
|
@@ -78,26 +82,26 @@ func (a *Api) QueryTVSeasonPackages(imdbID string, season int) (*SeasonPackagesR
|
|
}
|
|
}
|
|
|
|
|
|
// 发送请求
|
|
// 发送请求
|
|
- resp, err := a.client.R().
|
|
|
|
- SetHeader("Authorization", "Bearer "+a.token).
|
|
|
|
|
|
+ resp, err := client.R().
|
|
|
|
+ SetHeader("Authorization", "Bearer "+a.headerToken).
|
|
SetBody(requestBody).
|
|
SetBody(requestBody).
|
|
Post(common.SubSubtitleBestRootUrlDef + common.SubSubtitleBestSearchTVSeasonPackageUrl)
|
|
Post(common.SubSubtitleBestRootUrlDef + common.SubSubtitleBestSearchTVSeasonPackageUrl)
|
|
if err != nil {
|
|
if err != nil {
|
|
- return nil, err
|
|
|
|
|
|
+ return nil, nil, err
|
|
}
|
|
}
|
|
|
|
|
|
// 解析响应
|
|
// 解析响应
|
|
var seasonPackageResponse SeasonPackagesResponse
|
|
var seasonPackageResponse SeasonPackagesResponse
|
|
err = json.Unmarshal(resp.Body(), &seasonPackageResponse)
|
|
err = json.Unmarshal(resp.Body(), &seasonPackageResponse)
|
|
if err != nil {
|
|
if err != nil {
|
|
- return nil, err
|
|
|
|
|
|
+ return nil, nil, err
|
|
}
|
|
}
|
|
|
|
|
|
- return &seasonPackageResponse, nil
|
|
|
|
|
|
+ return &seasonPackageResponse, NewHeaderInfo(resp), nil
|
|
}
|
|
}
|
|
|
|
|
|
// QueryTVSeasonPackageByID 查询连续剧 一季 一字幕包的字幕
|
|
// QueryTVSeasonPackageByID 查询连续剧 一季 一字幕包的字幕
|
|
-func (a *Api) QueryTVSeasonPackageByID(imdbID string, seasonPackageId string) (*SubtitleResponse, error) {
|
|
|
|
|
|
+func (a *Api) QueryTVSeasonPackageByID(client *resty.Client, imdbID string, seasonPackageId string) (*SubtitleResponse, *LimitInfo, error) {
|
|
// 构建请求体
|
|
// 构建请求体
|
|
requestBody := SearchTVSeasonPackageByIDRequest{
|
|
requestBody := SearchTVSeasonPackageByIDRequest{
|
|
ImdbID: imdbID,
|
|
ImdbID: imdbID,
|
|
@@ -106,29 +110,29 @@ func (a *Api) QueryTVSeasonPackageByID(imdbID string, seasonPackageId string) (*
|
|
}
|
|
}
|
|
|
|
|
|
// 发送请求
|
|
// 发送请求
|
|
- resp, err := a.client.R().
|
|
|
|
- SetHeader("Authorization", "Bearer "+a.token).
|
|
|
|
|
|
+ resp, err := client.R().
|
|
|
|
+ SetHeader("Authorization", "Bearer "+a.headerToken).
|
|
SetBody(requestBody).
|
|
SetBody(requestBody).
|
|
Post(common.SubSubtitleBestRootUrlDef + common.SubSubtitleBestSearchTVSeasonPackageByIDUrl)
|
|
Post(common.SubSubtitleBestRootUrlDef + common.SubSubtitleBestSearchTVSeasonPackageByIDUrl)
|
|
if err != nil {
|
|
if err != nil {
|
|
- return nil, err
|
|
|
|
|
|
+ return nil, nil, err
|
|
}
|
|
}
|
|
|
|
|
|
// 解析响应
|
|
// 解析响应
|
|
var subtitleResponse SubtitleResponse
|
|
var subtitleResponse SubtitleResponse
|
|
err = json.Unmarshal(resp.Body(), &subtitleResponse)
|
|
err = json.Unmarshal(resp.Body(), &subtitleResponse)
|
|
if err != nil {
|
|
if err != nil {
|
|
- return nil, err
|
|
|
|
|
|
+ return nil, nil, err
|
|
}
|
|
}
|
|
|
|
|
|
- return &subtitleResponse, nil
|
|
|
|
|
|
+ return &subtitleResponse, NewHeaderInfo(resp), nil
|
|
}
|
|
}
|
|
|
|
|
|
// GetDownloadUrl 获取字幕下载地址
|
|
// GetDownloadUrl 获取字幕下载地址
|
|
-func (a *Api) GetDownloadUrl(subSha256, imdbID string,
|
|
|
|
|
|
+func (a *Api) GetDownloadUrl(client *resty.Client, subSha256, imdbID string,
|
|
isMovie bool, season, episode int,
|
|
isMovie bool, season, episode int,
|
|
seasonPackageId string, language int,
|
|
seasonPackageId string, language int,
|
|
- token string) (*GetUrlResponse, error) {
|
|
|
|
|
|
+ token string) (*GetUrlResponse, *LimitInfo, error) {
|
|
// 构建请求体
|
|
// 构建请求体
|
|
requestBody := DownloadUrlConvertRequest{
|
|
requestBody := DownloadUrlConvertRequest{
|
|
SubSha256: subSha256,
|
|
SubSha256: subSha256,
|
|
@@ -143,20 +147,63 @@ func (a *Api) GetDownloadUrl(subSha256, imdbID string,
|
|
}
|
|
}
|
|
|
|
|
|
// 发送请求
|
|
// 发送请求
|
|
- resp, err := a.client.R().
|
|
|
|
- SetHeader("Authorization", "Bearer "+a.token).
|
|
|
|
|
|
+ resp, err := client.R().
|
|
|
|
+ SetHeader("Authorization", "Bearer "+a.headerToken).
|
|
SetBody(requestBody).
|
|
SetBody(requestBody).
|
|
Post(common.SubSubtitleBestRootUrlDef + common.SubSubtitleBestGetDlURLUrl)
|
|
Post(common.SubSubtitleBestRootUrlDef + common.SubSubtitleBestGetDlURLUrl)
|
|
if err != nil {
|
|
if err != nil {
|
|
- return nil, err
|
|
|
|
|
|
+ return nil, nil, err
|
|
}
|
|
}
|
|
|
|
|
|
// 解析响应
|
|
// 解析响应
|
|
var getUrlResponse GetUrlResponse
|
|
var getUrlResponse GetUrlResponse
|
|
err = json.Unmarshal(resp.Body(), &getUrlResponse)
|
|
err = json.Unmarshal(resp.Body(), &getUrlResponse)
|
|
if err != nil {
|
|
if err != nil {
|
|
- return nil, err
|
|
|
|
|
|
+ return nil, nil, err
|
|
}
|
|
}
|
|
|
|
|
|
- return &getUrlResponse, nil
|
|
|
|
|
|
+ return &getUrlResponse, NewHeaderInfo(resp), nil
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+type LimitInfo struct {
|
|
|
|
+ dailyLimit string
|
|
|
|
+ dailyCount string
|
|
|
|
+ rateLimitLimit string
|
|
|
|
+ rateLimitRemaining string
|
|
|
|
+ rateLimitReset string
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func NewHeaderInfo(resp *resty.Response) *LimitInfo {
|
|
|
|
+ return &LimitInfo{
|
|
|
|
+ dailyLimit: resp.Header().Get("X-Daily-Limit"),
|
|
|
|
+ dailyCount: resp.Header().Get("X-Daily-Count"),
|
|
|
|
+ rateLimitLimit: resp.Header().Get("X-RateLimit-Limit"),
|
|
|
|
+ rateLimitRemaining: resp.Header().Get("X-RateLimit-Remaining"),
|
|
|
|
+ rateLimitReset: resp.Header().Get("X-RateLimit-Reset"),
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func (h LimitInfo) DailyLimit() int {
|
|
|
|
+ dailyLimit, _ := strconv.Atoi(h.dailyLimit)
|
|
|
|
+ return dailyLimit
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func (h LimitInfo) DailyCount() int {
|
|
|
|
+ dailyCount, _ := strconv.Atoi(h.dailyCount)
|
|
|
|
+ return dailyCount
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func (h LimitInfo) RateLimitLimit() int {
|
|
|
|
+ rateLimitLimit, _ := strconv.Atoi(h.rateLimitLimit)
|
|
|
|
+ return rateLimitLimit
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func (h LimitInfo) RateLimitRemaining() int {
|
|
|
|
+ rateLimitRemaining, _ := strconv.Atoi(h.rateLimitRemaining)
|
|
|
|
+ return rateLimitRemaining
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func (h LimitInfo) RateLimitReset() int {
|
|
|
|
+ rateLimitReset, _ := strconv.Atoi(h.rateLimitReset)
|
|
|
|
+ return rateLimitReset
|
|
}
|
|
}
|