mirror of
https://github.com/simon-ding/polaris.git
synced 2026-06-10 03:57:39 +08:00
feat: follow jackett redirect to get real link
This commit is contained in:
@@ -19,13 +19,10 @@ type Metadata struct {
|
|||||||
|
|
||||||
func ParseTv(name string) *Metadata {
|
func ParseTv(name string) *Metadata {
|
||||||
name = strings.ToLower(name)
|
name = strings.ToLower(name)
|
||||||
if utils.IsASCII(name) { //english name
|
|
||||||
return parseEnglishName(name)
|
|
||||||
}
|
|
||||||
if utils.ContainsChineseChar(name) {
|
if utils.ContainsChineseChar(name) {
|
||||||
return parseChineseName(name)
|
return parseChineseName(name)
|
||||||
}
|
}
|
||||||
return nil
|
return parseEnglishName(name)
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseEnglishName(name string) *Metadata {
|
func parseEnglishName(name string) *Metadata {
|
||||||
|
|||||||
@@ -3,8 +3,11 @@ package transmission
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"polaris/log"
|
"polaris/log"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/hekmon/transmissionrpc/v3"
|
"github.com/hekmon/transmissionrpc/v3"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
@@ -34,20 +37,42 @@ type Config struct {
|
|||||||
Password string `json:"password"`
|
Password string `json:"password"`
|
||||||
}
|
}
|
||||||
type Client struct {
|
type Client struct {
|
||||||
c *transmissionrpc.Client
|
c *transmissionrpc.Client
|
||||||
cfg Config
|
cfg Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) Download(link, dir string) (*Torrent, error) {
|
func (c *Client) Download(link, dir string) (*Torrent, error) {
|
||||||
|
if strings.HasPrefix(link, "http") {
|
||||||
|
client := &http.Client{
|
||||||
|
CheckRedirect: func(req *http.Request, via []*http.Request) error {
|
||||||
|
return http.ErrUseLastResponse
|
||||||
|
},
|
||||||
|
}
|
||||||
|
resp, err:=client.Get(link)
|
||||||
|
if err == nil {
|
||||||
|
if resp.StatusCode == http.StatusFound {
|
||||||
|
loc, err := resp.Location()
|
||||||
|
if err == nil {
|
||||||
|
link = loc.String()
|
||||||
|
log.Warnf("transimision redirect to url: %v", link)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
t, err := c.c.TorrentAdd(context.TODO(), transmissionrpc.TorrentAddPayload{
|
t, err := c.c.TorrentAdd(context.TODO(), transmissionrpc.TorrentAddPayload{
|
||||||
Filename: &link,
|
Filename: &link,
|
||||||
DownloadDir: &dir,
|
DownloadDir: &dir,
|
||||||
})
|
})
|
||||||
log.Infof("get torrent info: %+v", t)
|
log.Infof("get torrent info: %+v", t)
|
||||||
|
if t.ID == nil {
|
||||||
|
return nil, fmt.Errorf("download torrent error: %v", link)
|
||||||
|
}
|
||||||
|
|
||||||
return &Torrent{
|
return &Torrent{
|
||||||
ID: *t.ID,
|
ID: *t.ID,
|
||||||
c: c.c,
|
c: c.c,
|
||||||
Config: c.cfg,
|
Config: c.cfg,
|
||||||
}, err
|
}, err
|
||||||
}
|
}
|
||||||
@@ -95,7 +120,7 @@ func (t *Torrent) Progress() int {
|
|||||||
if t.getTorrent().PercentComplete != nil && *t.getTorrent().PercentComplete >= 1 {
|
if t.getTorrent().PercentComplete != nil && *t.getTorrent().PercentComplete >= 1 {
|
||||||
return 100
|
return 100
|
||||||
}
|
}
|
||||||
|
|
||||||
if t.getTorrent().PercentComplete != nil {
|
if t.getTorrent().PercentComplete != nil {
|
||||||
p := int(*t.getTorrent().PercentComplete * 100)
|
p := int(*t.getTorrent().PercentComplete * 100)
|
||||||
if p == 100 {
|
if p == 100 {
|
||||||
@@ -143,4 +168,4 @@ func ReloadTorrent(s string) (*Torrent, error) {
|
|||||||
return nil, errors.Wrap(err, "reload client")
|
return nil, errors.Wrap(err, "reload client")
|
||||||
}
|
}
|
||||||
return &torrent, nil
|
return &torrent, nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user