Skip to content

Commit

Permalink
feat: follow jackett redirect to get real link
Browse files Browse the repository at this point in the history
  • Loading branch information
simon-ding committed Jul 24, 2024
1 parent 16ca00d commit 1359df5
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
5 changes: 1 addition & 4 deletions pkg/metadata/tv.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,10 @@ type Metadata struct {

func ParseTv(name string) *Metadata {
name = strings.ToLower(name)
if utils.IsASCII(name) { //english name
return parseEnglishName(name)
}
if utils.ContainsChineseChar(name) {
return parseChineseName(name)
}
return nil
return parseEnglishName(name)
}

func parseEnglishName(name string) *Metadata {
Expand Down
35 changes: 30 additions & 5 deletions pkg/transmission/transmission.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ package transmission
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"polaris/log"
"strings"

"github.com/hekmon/transmissionrpc/v3"
"github.com/pkg/errors"
Expand Down Expand Up @@ -34,20 +37,42 @@ type Config struct {
Password string `json:"password"`
}
type Client struct {
c *transmissionrpc.Client
c *transmissionrpc.Client
cfg Config
}

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{
Filename: &link,
DownloadDir: &dir,
})
log.Infof("get torrent info: %+v", t)
if t.ID == nil {
return nil, fmt.Errorf("download torrent error: %v", link)
}

return &Torrent{
ID: *t.ID,
c: c.c,
ID: *t.ID,
c: c.c,
Config: c.cfg,
}, err
}
Expand Down Expand Up @@ -95,7 +120,7 @@ func (t *Torrent) Progress() int {
if t.getTorrent().PercentComplete != nil && *t.getTorrent().PercentComplete >= 1 {
return 100
}

if t.getTorrent().PercentComplete != nil {
p := int(*t.getTorrent().PercentComplete * 100)
if p == 100 {
Expand Down Expand Up @@ -143,4 +168,4 @@ func ReloadTorrent(s string) (*Torrent, error) {
return nil, errors.Wrap(err, "reload client")
}
return &torrent, nil
}
}

0 comments on commit 1359df5

Please sign in to comment.