mirror of
https://github.com/simon-ding/polaris.git
synced 2026-03-08 10:40:45 +08:00
feat: self calculate required torrent hash
This commit is contained in:
@@ -5,7 +5,7 @@ import (
|
||||
"fmt"
|
||||
"polaris/pkg"
|
||||
"polaris/pkg/go-qbittorrent/qbt"
|
||||
"time"
|
||||
"polaris/pkg/utils"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
@@ -39,14 +39,14 @@ func NewClient(url, user, pass string) (*Client, error) {
|
||||
}
|
||||
|
||||
func (c *Client) GetAll() ([]pkg.Torrent, error) {
|
||||
tt, err :=c.c.Torrents(qbt.TorrentsOptions{})
|
||||
tt, err := c.c.Torrents(qbt.TorrentsOptions{})
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "get torrents")
|
||||
}
|
||||
var res []pkg.Torrent
|
||||
for _, t := range tt {
|
||||
t1 := &Torrent{
|
||||
c: c.c,
|
||||
c: c.c,
|
||||
Hash: t.Hash,
|
||||
Info: c.Info,
|
||||
}
|
||||
@@ -56,40 +56,15 @@ func (c *Client) GetAll() ([]pkg.Torrent, error) {
|
||||
}
|
||||
|
||||
func (c *Client) Download(link, dir string) (pkg.Torrent, error) {
|
||||
all, err := c.c.Torrents(qbt.TorrentsOptions{})
|
||||
hash, err := utils.MagnetHash(link)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "get old torrents")
|
||||
}
|
||||
allHash := make(map[string]bool, len(all))
|
||||
for _, t := range all {
|
||||
allHash[t.Hash] = true
|
||||
return nil, errors.Wrap(err, "get hash")
|
||||
}
|
||||
err = c.c.DownloadLinks([]string{link}, qbt.DownloadOptions{Savepath: &dir})
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "qbt download")
|
||||
}
|
||||
var newHash string
|
||||
|
||||
loop:
|
||||
for i := 0; i < 10; i++ {
|
||||
time.Sleep(1 * time.Second)
|
||||
all, err = c.c.Torrents(qbt.TorrentsOptions{})
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "get new torrents")
|
||||
}
|
||||
|
||||
for _, t := range all {
|
||||
if !allHash[t.Hash] {
|
||||
newHash = t.Hash
|
||||
break loop
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if newHash == "" {
|
||||
return nil, fmt.Errorf("download torrent fail: timeout")
|
||||
}
|
||||
return &Torrent{Hash: newHash, c: c.c, Info: c.Info}, nil
|
||||
return &Torrent{Hash: hash, c: c.c, Info: c.Info}, nil
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"net/url"
|
||||
"polaris/db"
|
||||
"polaris/log"
|
||||
"polaris/pkg/utils"
|
||||
"slices"
|
||||
"strconv"
|
||||
"time"
|
||||
@@ -80,9 +81,14 @@ func (r *Response) ToResults(indexer *db.TorznabInfo) []Result {
|
||||
if slices.Contains(item.Category, "3000") { //exclude audio files
|
||||
continue
|
||||
}
|
||||
link, err := utils.Link2Magnet(item.Link)
|
||||
if err != nil {
|
||||
log.Warnf("converting link to magnet error, error: %v, link: %v", err, item.Link)
|
||||
continue
|
||||
}
|
||||
r := Result{
|
||||
Name: item.Title,
|
||||
Link: item.Link,
|
||||
Link: link,
|
||||
Size: mustAtoI(item.Size),
|
||||
Seeders: mustAtoI(item.GetAttr("seeders")),
|
||||
Peers: mustAtoI(item.GetAttr("peers")),
|
||||
|
||||
@@ -4,11 +4,10 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"polaris/log"
|
||||
"polaris/pkg"
|
||||
"strings"
|
||||
"polaris/pkg/utils"
|
||||
|
||||
"github.com/hekmon/transmissionrpc/v3"
|
||||
"github.com/pkg/errors"
|
||||
@@ -54,45 +53,28 @@ func (c *Client) GetAll() ([]pkg.Torrent, error) {
|
||||
var torrents []pkg.Torrent
|
||||
for _, t := range all {
|
||||
torrents = append(torrents, &Torrent{
|
||||
Hash: *t.HashString,
|
||||
Hash: *t.HashString,
|
||||
c: c.c,
|
||||
Config: c.cfg,
|
||||
Config: c.cfg,
|
||||
})
|
||||
}
|
||||
return torrents, nil
|
||||
}
|
||||
|
||||
func (c *Client) Download(link, dir string) (pkg.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)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
hash, err := utils.MagnetHash(link)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "get hash")
|
||||
}
|
||||
|
||||
t, err := c.c.TorrentAdd(context.TODO(), transmissionrpc.TorrentAddPayload{
|
||||
Filename: &link,
|
||||
DownloadDir: &dir,
|
||||
})
|
||||
log.Infof("get torrent info: %+v", t)
|
||||
if t.HashString == nil {
|
||||
return nil, fmt.Errorf("download torrent error: %v", link)
|
||||
}
|
||||
log.Debugf("get torrent info: %+v", t)
|
||||
|
||||
return &Torrent{
|
||||
Hash: *t.HashString,
|
||||
Hash: hash,
|
||||
c: c.c,
|
||||
Config: c.cfg,
|
||||
}, err
|
||||
@@ -100,7 +82,7 @@ func (c *Client) Download(link, dir string) (pkg.Torrent, error) {
|
||||
|
||||
type Torrent struct {
|
||||
//t *transmissionrpc.Torrent
|
||||
c *transmissionrpc.Client
|
||||
c *transmissionrpc.Client
|
||||
Hash string `json:"hash"`
|
||||
Config
|
||||
}
|
||||
@@ -136,7 +118,6 @@ func (t *Torrent) Exists() bool {
|
||||
return len(r) > 0
|
||||
}
|
||||
|
||||
|
||||
func (t *Torrent) Name() (string, error) {
|
||||
tt, err := t.getTorrent()
|
||||
if err != nil {
|
||||
|
||||
@@ -251,3 +251,24 @@ func Link2Magnet(link string) (string, error) {
|
||||
}
|
||||
return mg.String(), nil
|
||||
}
|
||||
|
||||
|
||||
func MagnetHash(link string) (string, error) {
|
||||
if mi, err := metainfo.ParseMagnetV2Uri(link); err != nil {
|
||||
return "", errors.Errorf("magnet link is not valid: %v", err)
|
||||
} else {
|
||||
hash := ""
|
||||
if mi.InfoHash.Unwrap().HexString() != "" {
|
||||
hash = mi.InfoHash.Unwrap().HexString()
|
||||
} else {
|
||||
btmh := mi.V2InfoHash.Unwrap()
|
||||
if btmh.HexString() != "" {
|
||||
hash = btmh.HexString()
|
||||
}
|
||||
}
|
||||
if hash == "" {
|
||||
return "", errors.Errorf("magnet has no info hash: %v", link)
|
||||
}
|
||||
return hash, nil
|
||||
}
|
||||
}
|
||||
@@ -11,7 +11,6 @@ import (
|
||||
"polaris/pkg/utils"
|
||||
"strconv"
|
||||
|
||||
"github.com/anacrolix/torrent/metainfo"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
@@ -123,21 +122,9 @@ func (s *Server) addTorrent2Blacklist(link string) error {
|
||||
if link == "" {
|
||||
return nil
|
||||
}
|
||||
if mi, err := metainfo.ParseMagnetV2Uri(link); err != nil {
|
||||
return errors.Errorf("magnet link is not valid: %v", err)
|
||||
if hash, err := utils.MagnetHash(link); err != nil {
|
||||
return err
|
||||
} else {
|
||||
hash := ""
|
||||
if mi.InfoHash.Unwrap().HexString() != "" {
|
||||
hash = mi.InfoHash.Unwrap().HexString()
|
||||
} else {
|
||||
btmh := mi.V2InfoHash.Unwrap()
|
||||
if btmh.HexString() != "" {
|
||||
hash = btmh.HexString()
|
||||
}
|
||||
}
|
||||
if hash == "" {
|
||||
return errors.Errorf("magnet has no info hash: %v", link)
|
||||
}
|
||||
item := ent.Blacklist{
|
||||
Type: blacklist.TypeTorrent,
|
||||
Value: schema.BlacklistValue{
|
||||
|
||||
Reference in New Issue
Block a user