|
|
@@ -1,8 +1,14 @@
|
|
|
package subtitle_best_api
|
|
|
|
|
|
import (
|
|
|
+ "bytes"
|
|
|
"errors"
|
|
|
"fmt"
|
|
|
+ "github.com/allanpk716/ChineseSubFinder/internal/models"
|
|
|
+ "io/ioutil"
|
|
|
+ "os"
|
|
|
+ "path/filepath"
|
|
|
+ "strconv"
|
|
|
|
|
|
"github.com/allanpk716/ChineseSubFinder/internal/pkg/my_util"
|
|
|
"github.com/allanpk716/ChineseSubFinder/internal/pkg/random_auth_key"
|
|
|
@@ -61,7 +67,8 @@ func (s *SubtitleBestApi) GetMediaInfo(id, source, videoType string, _proxySetti
|
|
|
return &mediaInfoReply, nil
|
|
|
}
|
|
|
|
|
|
-func (s SubtitleBestApi) AskFroUpload(subSha256 string, _proxySettings ...*settings.ProxySettings) (*AskForUploadReply, error) {
|
|
|
+// AskFroUpload 在使用这个接口前,需要从 IMDB ID 获取到 TMDB ID
|
|
|
+func (s *SubtitleBestApi) AskFroUpload(subSha256 string, _proxySettings ...*settings.ProxySettings) (*AskForUploadReply, error) {
|
|
|
|
|
|
const postUrl = webUrlBase + "/v1/media-info"
|
|
|
httpClient, err := my_util.NewHttpClient(_proxySettings...)
|
|
|
@@ -89,6 +96,69 @@ func (s SubtitleBestApi) AskFroUpload(subSha256 string, _proxySettings ...*setti
|
|
|
return &askForUploadReply, nil
|
|
|
}
|
|
|
|
|
|
+// UploadSub 在使用这个接口前,需要从 IMDB ID 获取到 TMDB ID,其实在这一步应该默认就拿到了 TMDB ID,需要提前在 AskFroUpload 接口调用前就搞定这个
|
|
|
+// year 这个也是从之前的接口拿到, 2019 or 2022
|
|
|
+func (s *SubtitleBestApi) UploadSub(videoSubInfo *models.VideoSubInfo, subSaveRootDirPath string, tmdbId, year string, _proxySettings ...*settings.ProxySettings) (*UploadSubReply, error) {
|
|
|
+
|
|
|
+ const postUrl = webUrlBase + "/v1/upload-sub"
|
|
|
+ httpClient, err := my_util.NewHttpClient(_proxySettings...)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+ authKey, err := s.randomAuthKey.GetAuthKey()
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+ // 从相对路径转换为绝对路径
|
|
|
+ subFileFPath := filepath.Join(subSaveRootDirPath, videoSubInfo.StoreRPath)
|
|
|
+ if my_util.IsFile(subFileFPath) == false {
|
|
|
+ return nil, errors.New(fmt.Sprintf("sub file not exist, %s", subFileFPath))
|
|
|
+ }
|
|
|
+ file, err := os.Open(subFileFPath)
|
|
|
+ if err != nil {
|
|
|
+ return nil, errors.New(fmt.Sprintf("open sub file failed, %s", subFileFPath))
|
|
|
+ }
|
|
|
+ defer func() {
|
|
|
+ _ = file.Close()
|
|
|
+ }()
|
|
|
+ fd, err := ioutil.ReadAll(file)
|
|
|
+ if err != nil {
|
|
|
+ return nil, errors.New(fmt.Sprintf("read sub file failed, %s", subFileFPath))
|
|
|
+ }
|
|
|
+
|
|
|
+ isDouble := "false"
|
|
|
+ if videoSubInfo.IsDouble == true {
|
|
|
+ isDouble = "true"
|
|
|
+ }
|
|
|
+
|
|
|
+ var uploadSubReply UploadSubReply
|
|
|
+ _, err = httpClient.R().
|
|
|
+ SetHeader("Authorization", "beer "+authKey).
|
|
|
+ SetFileReader("sub_file_context", videoSubInfo.SubName, bytes.NewReader(fd)).
|
|
|
+ SetFormData(map[string]string{
|
|
|
+ "sub_sha256": videoSubInfo.SHA256,
|
|
|
+ "season": strconv.Itoa(videoSubInfo.Season),
|
|
|
+ "episode": strconv.Itoa(videoSubInfo.Episode),
|
|
|
+ "is_double": isDouble,
|
|
|
+ "language_iso": videoSubInfo.LanguageISO,
|
|
|
+ "my_language": videoSubInfo.MyLanguage,
|
|
|
+ "extra_pre_name": videoSubInfo.ExtraPreName,
|
|
|
+ "imdb_id": videoSubInfo.IMDBInfoID,
|
|
|
+ "tmdb_id": tmdbId,
|
|
|
+ "video_feature": videoSubInfo.Feature,
|
|
|
+ "year": year,
|
|
|
+ }).
|
|
|
+ SetResult(&uploadSubReply).
|
|
|
+ Post(postUrl)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+ return &uploadSubReply, nil
|
|
|
+}
|
|
|
+
|
|
|
const (
|
|
|
webUrlBase = "https://api.subtitle.best"
|
|
|
)
|