feat: proxy only affects tmdb

This commit is contained in:
Simon Ding
2024-08-03 09:54:23 +08:00
parent f4da80c845
commit 578b6a9d78
4 changed files with 30 additions and 32 deletions

View File

@@ -1,9 +1,12 @@
package tmdb package tmdb
import ( import (
"net/http"
"net/url"
"polaris/log" "polaris/log"
"strconv" "strconv"
"strings" "strings"
"time"
tmdb "github.com/cyruzin/golang-tmdb" tmdb "github.com/cyruzin/golang-tmdb"
"github.com/pkg/errors" "github.com/pkg/errors"
@@ -14,11 +17,29 @@ type Client struct {
tmdbClient *tmdb.Client tmdbClient *tmdb.Client
} }
func NewClient(apiKey string) (*Client, error) { func NewClient(apiKey, proxyUrl string) (*Client, error) {
tmdbClient, err := tmdb.Init(apiKey) tmdbClient, err := tmdb.Init(apiKey)
if err != nil { if err != nil {
return nil, errors.Wrap(err, "new tmdb client") return nil, errors.Wrap(err, "new tmdb client")
} }
if proxyUrl != "" {
//set proxy
u, err := url.Parse(proxyUrl)
if err != nil {
log.Errorf("parse proxy %v error, skip: %v", proxyUrl, err)
} else {
tmdbClient.SetClientConfig(http.Client{
Timeout: time.Second * 10,
Transport: &http.Transport{
Proxy: http.ProxyURL(u),
MaxIdleConns: 10,
IdleConnTimeout: 15 * time.Second,
},
})
}
}
return &Client{ return &Client{
apiKey: apiKey, apiKey: apiKey,
@@ -50,7 +71,6 @@ func (c *Client) GetTvDetails(id int, language string) (*tmdb.TVDetails, error)
} }
} }
return d, err return d, err
} }
@@ -172,7 +192,7 @@ func (c *Client) GetSeasonDetails(id, seasonNumber int, language string) (*tmdb.
} }
for i, ep := range detailCN.Episodes { for i, ep := range detailCN.Episodes {
if !episodeNameUseful(ep.Name) && episodeNameUseful(detailEN.Episodes[i].Name){ if !episodeNameUseful(ep.Name) && episodeNameUseful(detailEN.Episodes[i].Name) {
detailCN.Episodes[i].Name = detailEN.Episodes[i].Name detailCN.Episodes[i].Name = detailEN.Episodes[i].Name
detailCN.Episodes[i].Overview = detailEN.Episodes[i].Overview detailCN.Episodes[i].Overview = detailEN.Episodes[i].Overview
} }
@@ -198,7 +218,6 @@ func withLangOption(language string) map[string]string {
} }
} }
func episodeNameUseful(name string) bool { func episodeNameUseful(name string) bool {
return !strings.HasSuffix(name, "集") && !strings.HasPrefix(strings.ToLower(name), "episode") return !strings.HasSuffix(name, "集") && !strings.HasPrefix(strings.ToLower(name), "episode")
} }

View File

@@ -66,7 +66,8 @@ func (c *Client) TMDB() (*tmdb.Client, error) {
if api == "" { if api == "" {
return nil, errors.New("TMDB apiKey not set") return nil, errors.New("TMDB apiKey not set")
} }
return tmdb.NewClient(api) proxy := c.db.GetSetting(db.SettingProxy)
return tmdb.NewClient(api, proxy)
} }
func (c *Client) MustTMDB() *tmdb.Client { func (c *Client) MustTMDB() *tmdb.Client {

View File

@@ -40,8 +40,6 @@ type Server struct {
func (s *Server) Serve() error { func (s *Server) Serve() error {
s.core.Init() s.core.Init()
s.restoreProxy()
s.jwtSerect = s.db.GetSetting(db.JwtSerectKey) s.jwtSerect = s.db.GetSetting(db.JwtSerectKey)
//st, _ := fs.Sub(ui.Web, "build/web") //st, _ := fs.Sub(ui.Web, "build/web")
s.r.Use(static.Serve("/", static.EmbedFolder(ui.Web, "build/web"))) s.r.Use(static.Serve("/", static.EmbedFolder(ui.Web, "build/web")))
@@ -129,7 +127,8 @@ func (s *Server) TMDB() (*tmdb.Client, error) {
if api == "" { if api == "" {
return nil, errors.New("TMDB apiKey not set") return nil, errors.New("TMDB apiKey not set")
} }
return tmdb.NewClient(api) proxy := s.db.GetSetting(db.SettingProxy)
return tmdb.NewClient(api, proxy)
} }
func (s *Server) MustTMDB() *tmdb.Client { func (s *Server) MustTMDB() *tmdb.Client {

View File

@@ -3,8 +3,6 @@ package server
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"net/http"
"net/url"
"polaris/db" "polaris/db"
"polaris/ent" "polaris/ent"
"polaris/log" "polaris/log"
@@ -55,29 +53,10 @@ func (s *Server) SetSetting(c *gin.Context) (interface{}, error) {
s.db.SetSetting(db.SettingPlexMatchEnabled, "false") s.db.SetSetting(db.SettingPlexMatchEnabled, "false")
} }
s.setProxy(in.Proxy) s.db.SetSetting(db.SettingProxy, in.Proxy)
return nil, nil return nil, nil
} }
func (s *Server) setProxy(proxy string) {
proxyUrl, err := url.Parse(proxy)
tp := http.DefaultTransport.(*http.Transport)
if proxy == "" || err != nil {
log.Warnf("proxy url not valid, disabling: %v", proxy)
tp.Proxy = nil
s.db.SetSetting(db.SettingProxy, "")
} else {
log.Infof("set proxy to %v", proxy)
tp.Proxy = http.ProxyURL(proxyUrl)
s.db.SetSetting(db.SettingProxy, proxy)
}
}
func (s *Server) restoreProxy() {
p := s.db.GetSetting(db.SettingProxy)
s.setProxy(p)
}
func (s *Server) GetSetting(c *gin.Context) (interface{}, error) { func (s *Server) GetSetting(c *gin.Context) (interface{}, error) {
tmdb := s.db.GetSetting(db.SettingTmdbApiKey) tmdb := s.db.GetSetting(db.SettingTmdbApiKey)
downloadDir := s.db.GetSetting(db.SettingDownloadDir) downloadDir := s.db.GetSetting(db.SettingDownloadDir)